home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file19 / readme < prev    next >
Encoding:
Text File  |  1995-05-19  |  14.0 KB  |  354 lines

  1.                            M A T R I X  P A C K A G E
  2.                                  from the book
  3.                        "Practical Data Structures in C++"
  4.                                 by Bryan Flamig
  5.  
  6.                                   README FILE
  7.                                 August 23, 1993
  8.  
  9.                       Copyright(c) 1993 Azarona Software
  10.                              All rights reserved.
  11.  
  12. INTRODUCTION
  13. ============
  14.  
  15. This is source code in C++ for implementing user-defined vectors and
  16. matrices, as given in Chapter 7 of the book "Practical Data Structures 
  17. in C++", by Bryan Flamig, (John Wiley & Sons, ISBN 0-471-55863-X). 
  18.  
  19. The classes as given here illustrate several nifty techniques for
  20. handling vectors and matrices:
  21.  
  22.    1. Both vector and matrix objects are reference counted. This allows
  23.       efficient assignment and returns from functions.
  24.  
  25.    2. "Vector strides" are implemented. A vector stride is how many
  26.       physical elements of a vector make up one logical element. For
  27.       example, in a 4x4 row-major matrix, a row vector has a stride
  28.       of 1, a column vector has a stride of 4, and a diagonal vector
  29.       has a stride of 5.
  30.  
  31.    3. "Smart pointers" known as vector pointers are introduced, as
  32.       invented by the author. These are pointers that know about 
  33.       vector strides. Very efficient as the vector pointer class is 
  34.       small and can be completely inlined.
  35.  
  36.    3. Sub-vectors and sub-matrices are supported. These vectors can
  37.       matrices share their data with the original vector/matrix.
  38.  
  39.    4. An efficient form of matrix transposition is shown, which does
  40.       not have to actually move the matrix elements around to 
  41.       form the transpose. Smoke and mirrors are used instead :) 
  42.       You can even transpose shared sub-matrices.
  43.  
  44.    5. The matrix class is actually derived from the vector class.
  45.       Among other things, this allows the elements of the matrix
  46.       to be treated as a giant vector.
  47.  
  48.    6. Vectors are dynamically allocated using overloaded new and
  49.       delete operators, to compact the elements and housekeeping
  50.       data needed for the vectors as tight as possible. The elements
  51.       are constructed using in-place construction via an overloaded 
  52.       placement new operator.
  53.       
  54.  
  55. There are tricks in this code that, as far as I know, have 
  56. not been published anywhere else, such as a clean and elegant 
  57. way of implementing "smart" pointers that know about vector
  58. strides.
  59.  
  60. If you like what you see, you may be interested in picking up a
  61. copy of the "Practical Data Structures in C++" book, since it is
  62. chock-full of examples like this. 
  63.  
  64.  
  65. COPYRIGHTED SOFTWARE
  66. ====================
  67.  
  68. The source code is copyrighted by Azarona Software, but I'm
  69. providing it to you free of charge. You are free to experiment
  70. with the code, include it in your own applications, personal
  71. or commercial. HOWEVER, YOU MAY NOT DISTRIBUTE THE SOURCE CODE
  72. FOR A PROFIT OR FEE OF ANY KIND WITHOUT THE EXPRESS WRITTEN 
  73. PERMISSION OF AZARONA SOFTWARE. YOU MAY ONLY DISTRIBUTE THE
  74. CODE IN OBJECT (COMPILED) FORM, AND ONLY IF MADE A PART OF
  75. YOUR PACKAGE, AND NOT BY ITSELF.
  76.  
  77. You are granted permission to give away the source code to
  78. friends, upload the files to a BBS, etc., AS LONG AS NO FEE 
  79. IS CHARGED FOR THE SOFTWARE, AND AS LONG AS THE FILES ARE 
  80. DISTRIBUTED EXACTLY AS GIVEN HERE, WITH THE SAME FILE NAMES, 
  81. THE SAME NUMBER OF FILES, AND THE COPYRIGHT NOTICES INTACT. 
  82. YOU MAY NOT INCLUDE THE SOURCE AS PART OF A COMMERCIAL PACKAGE,
  83. OR ANY PACKAGE SOLD AND DISTRIBUTED FOR ANY FEE OR PROFIT OF
  84. ANY KIND. You may "zip" the files into a single-file archive, 
  85. if you wish.
  86.  
  87.  
  88. SOURCE CODE PROVIDED AS-IS
  89. ==========================
  90.  
  91. Azarona Software provides this source code as-is. USE THE
  92. SOFTWARE AT YOUR OWN RISK. While the software has been tested
  93. extensively, we make no representations about its suitability 
  94. or robustness, and do not take any liability for the use of
  95. the software.
  96.  
  97.  
  98. FEATURES OF THIS SOURCE CODE
  99. ============================
  100.  
  101. The source code as provided implements the following classes:
  102.  
  103.   SimpleMatrix          A simple matrix class that illustrates
  104.                         how to construct dynamic, user-defined
  105.                         matrices, and how to use vector pointers
  106.                         in conjunction with matrices.
  107.  
  108.   VecPtr                A "smart" pointer class that knows about
  109.                         vector strides. Very slick.
  110.  
  111.   Vector                A reference-counted vector class that
  112.                         forms the backbone of the Matrix class
  113.  
  114.   Matrix                A reference-counted matrix class that
  115.                         supports sub-matrices and transposition
  116.                         that does not require elements to be
  117.                         shuffled around.
  118.  
  119.   Shape                 This is just a test class used to verify
  120.                         that the constructors/destructors for
  121.                         vector and matrix elements are called
  122.                         the right number of times.
  123.  
  124.  
  125. WHAT COMPILERS YOU MAY USE
  126. ==========================
  127.  
  128. The code was developed using BC++ 3.1, and was tested with the
  129. latest Cfront compiler in Unix as well. The code uses many 
  130. sophisticated C++ features, all of which are legal C++
  131. as defined in the C++ Annotated Reference Manual, (the "ARM", 
  132. currently the de facto standard reference for C++). Unfortunately,
  133. some current compilers won't be able to handle what's given
  134. here. Most of the problems are due to the template code. (I use
  135. templates in some "strange" ways, that many compilers complain
  136. about.) This will become less of a problem as time goes on, and
  137. the compilers "catch up" to the standard.
  138.  
  139.  
  140. DESIGN OF THE MATRIX CLASS
  141. ==========================
  142.  
  143. The Matrix class implements dynamic, two-dimensional matrices.
  144. These matrices are reference-counted, which allows more convenient
  145. methods of performing assignments and returning matrices from
  146. functions. Integral to the Matrix class is the Vector class. 
  147. A Vector object is basically an array whose elements can be 
  148. non-contiguous. That is, the stride of a Vector may be greater
  149. than one. Using vector objects, you can conveniently manipulate
  150. the rows, columns, and diagonals of a matrix as though they
  151. were arrays. 
  152.  
  153. With both the vector and matrix class, you can turn range-checking 
  154. on for the subscripts, at compile time. With range-checking turned 
  155. off, no overhead accrues at run-time. The range-checking uses a
  156. user-defined error handling routine. Obviously, exception handling
  157. would be nice here.
  158.  
  159. Several forms of subscripting are provided to illustrate
  160. different ways of accessing matrix elements. First, there is
  161. the standard "double-subscripting" method, eg: m[42][17]. Then,
  162. there is the "2-d" method, eg: m(42, 17). Also, you can return
  163. row, column, or diagonal vectors and work with those. Also,
  164. you can return "vector pointers", which are low-level forms
  165. of vectors that do not do range-checking. Using vector pointers,
  166. you can implement operations such as matrix multiply very
  167. efficiently.
  168.  
  169. Rigorous but slightly obtuse design: The source code was designed
  170. rigorously to handle constant vectors, matrices, etc. Because of 
  171. this, and the fact that the classes are all defined as templates,
  172. the source may look a little obtuse at first. 
  173.  
  174.  
  175. INTENT OF THE MATRIX PACKAGE
  176. ============================
  177.  
  178. This package is provided as an illustration of some important
  179. C++ techniques that can be used for working with vectors and
  180. matrices. While the fundamental design is sound, it isn't
  181. necessarily complete. There are many matrix operations that
  182. obviously aren't supported, (such as taking determinants, etc.)
  183. but ought to be. And vice versa, there are some operations
  184. supported that maybe don't need to be, (such as rigorous handling
  185. of constant objects, sub-matrices, etc.)
  186.  
  187.  
  188. SOURCE CODE DOCUMENTATION
  189. =========================
  190.  
  191. The source code as given is fully described in the "Practical Data
  192. Structures in C++" book. Here we provide documentation only in the
  193. form of comments in the code. (The code is heavily commented, 
  194. though.)
  195.  
  196.  
  197. SAMPLE TEST PROGRAMS
  198. ====================
  199.  
  200. There are numerous test programs provided that may not do
  201. a whole lot of useful things, but they do exercise the
  202. features of the matrix package. Some of the test programs
  203. use "Shape" objects as the matrix elements. The Shape class
  204. was designed to test how constructors and destructors are
  205. called, and is useful in ensuring that the matrix and
  206. vectors clean up after themselves properly.
  207.  
  208. Some of the test programs check how efficiently matrix 
  209. multiplies are handled, which should give you some idea
  210. on the overhead accrued by these classes. (Any time you
  211. make something general, performance is bound to suffer.)
  212. Using low-level vector pointers, it's possible to make 
  213. matrix operations fairly efficient, with roughly a 30%
  214. drop in performance over using hard-coded, statically
  215. sized matrices. It would appear that most of the
  216. performance drop is due to dynamically sizing the matrices,
  217. and not due to that fact that sophisticated operations
  218. such as "smoke and mirrors" transpositions are supported.
  219.  
  220.  
  221. PACKING LIST
  222. ============
  223.  
  224. This distribution should contain the following files:
  225.  
  226.  
  227. MATRIX.H        The matrix template header and "methods" file
  228. MATRIX.MTH
  229.  
  230. PLACENEW.H      Contains a definition for the placement new
  231.                 operator. With some compilers, you may not 
  232.                 need this.
  233.  
  234. RANGE.H         Header and source files for handling subscript
  235. RANGEERR.CPP    range errors.
  236.  
  237. README          This readme file
  238.  
  239. SHAPE.CPP       Source and header file for a "Shape" class used
  240. SHAPE.H         for testing proper construction/destruction of
  241.                 matrix and vector elements.
  242.  
  243. SIMPMAT.H       A simplified matrix class used to illustrate
  244. SIMPMAT.MTH     how to create dynamically sized matrices, and
  245.                 how to use vector pointers.
  246.  
  247. TSTMAIN.CPP     An include file of source used by some of the
  248.                 test programs that tests proper construction
  249.                 and destruction of elements.
  250.  
  251. TSTMAT.CPP      First test program for the Matrix class. Tests      
  252. TSTMAT.MAK      the creation of matrices, and basic operations
  253. TSTMAT.PRJ      such as copying, sub-matrices, transpositions, etc.
  254.  
  255. TSTMAT2.CPP     Like tstmat.cpp, except Shape elements are used,
  256. TSTMAT2.MAK     to test proper construction/destruction of elements 
  257. TSTMAT2.PRJ
  258.  
  259. TSTMAT3.CPP     Tests performance of matrix multiplies, using
  260. TSTMAT3.MAK     various forms of accessing the matrix elements.
  261. TSTMAT3.PRJ
  262.  
  263. TSTMAT4.CPP     Like tstmat3, but also includes a full comparison
  264. TSTMAT4.MAK     of using hard-coded static matrices versus the
  265. TSTMAT4.PRJ     general user-defined matrices.
  266.  
  267. TSTSMAT.CPP     Test program for the SimpleMatrix class.
  268. TSTSMAT.MAK
  269. TSTSMAT.PRJ
  270.  
  271. TSTSMAT2.CPP    Like tstsmat.cpp, except shape elements are used
  272. TSTSMAT2.MAK    to test for proper construction/destruction of
  273. TSTSMAT2.PRJ    matrix elements.
  274.  
  275. TSTVEC.CPP      Test program for the Vector class. Tests features
  276. TSTVEC.MAK      such as sharing slices of other vectors, etc.
  277. TSTVEC.PRJ
  278.  
  279. TSTVEC2.CPP     Like tstvec.cpp, except Shape elements are used to
  280. TSTVEC2.MAK     test proper construction/destruction of elements.
  281. TSTVEC2.PRJ
  282.  
  283. VECPTR.H        The vector pointer class template header file
  284.  
  285. VECTOR.H        The vector class header and methods file
  286. VECTOR.MTH
  287.  
  288.  
  289. ABOUT THE PRACTICAL DATA STRUCTURES BOOK
  290. ========================================
  291.  
  292. Practical Data Structures in C++
  293.        by Bryan Flamig
  294.       John Wiley & Sons
  295.       ISBN 0-471-55863-X
  296.  
  297. If you're looking for an eminently readable data structures book
  298. written for C++, you've come to the right place. The book covers
  299. data structures from a practical point of view, with clearly
  300. explained examples. For the topics covered, there are no mysteries
  301. left to the reader to unravel: you won't have to spend days trying
  302. to figure out how to accomplish something described in this book. 
  303. For example, some books talk about red-black trees, splay trees, 
  304. and B-trees, but neglect to show you how to delete entries from 
  305. these trees! In this book, each operation of these data structures
  306. is fully explained and laid out. And since complete code is provided
  307. on the accompanying disk, you can put the code to work immediately. 
  308. This book:
  309.  
  310.   * Presents high-quality code, very carefully designed and
  311.     crafted specifically for C++, with efficiency and elegance
  312.     a top priority.
  313.  
  314.   * Makes extensive use of C++ templates, the first book of its
  315.     kind to do so.
  316.  
  317.   * Covers concrete data types, abstract data types, resizable
  318.     arrays, smart pointers, strings, vectors, matrices, linked
  319.     lists, stacks, queues, binary trees, red-black trees, splay
  320.     trees, file-based objects, caching and B-trees.
  321.  
  322.   * Includes several techniques never before published, such as
  323.     template-based vector pointers and cached-object pointers, 
  324.     and in-place construction of array elements.
  325.  
  326. The programming techniques shown in this book were crafted spec-
  327. ifically for C++, and the code given is not simply a rewrite of
  328. code written in C, Pascal, or some other language. Each data
  329. structure discussed in the text and demonstrated on disk has
  330. been designed to take advantage of the special features of C++.
  331.  
  332. If C++ is you language of choice, Practical Data Structures in C++
  333. will show you the most effective and practical ways to store data.
  334.  
  335.  
  336. ABOUT THE AUTHOR
  337. ================
  338.  
  339. Bryan Flamig is a software developer and consultant specializing in
  340. C++ programming. He is the author of numerous books, including the 
  341. popular "Turbo C++: Step by Step", and coauthor of "Object-Oriented
  342. Programming with Turbo C++", and of course, the author of "Practical
  343. Data Structures in C++", all published by John Wiley & Sons.
  344.  
  345. Besides consulting and writing books, Bryan also gives training 
  346. seminars focusing on C++. You may reach Bryan at:
  347.  
  348. Azarona Software
  349. P.O. Box 768
  350. Conifer, CO  80433
  351.  
  352. (303) 697-1728 Fax
  353. 73057,3172     CompuServe
  354.