home *** CD-ROM | disk | FTP | other *** search
- {
-
- ****** The MaxArray and GenericHeap Objects (and Descendants) *****
-
-
- Design and Implementation --- Eric C. Wentz
-
- 1-703-860-3807
-
- 2374 Antiqua Court
- Reston, Va. 22091
-
-
- Last Modification Date : 7-22-89
-
-
- ********************************************************************
-
-
- Design Objective(s):
-
- (1) {MaxArray}
-
- Implementation of a fully Generic Array-like Object,
- Capable of all Array functions, but not limited by the
- Segment-size limitation imposed by Turbo Pascal (65521 bytes).
- Additionally, the Object should be capable of Dynamic
- allocation and de-allocation to enhance RAM utilization efficiency.
-
- (2) {GenericHeap}
-
- Implementation of a fully Generic Heap Object, capable
- of all Heap functions for any Data type (including a
- user-defineable collating sequence), including Records and Objects,
- to facilatate sorting, creation of Priority-Queues and other
- uses for which a Heap is a useful Data structure.
-
-
- Limitations:
-
- As the MaxArray and the GenericHeap are fundamentally defined
- by use of Dynamically allocated arrays of un-typed bytes, it is
- only possible to define them for similar-sized constituents. In
- particular it will not be possible to declare a MaxArray of
- Ancestor-Objects and then use it to contain different-sized
- Descendant Objects. The user should be able to get around this by
- declaring a MaxArray of pointers to such objects, but I have not
- made any attempts to test this.
-
- As (I believe) Turbo allocates the same number of bytes for each
- variant of a Variant record, there should be no problems with
- variant records.
-
- ***************************************************************************
- Implementation {MaxArray} :
- ***************************************************************************
-
- Unit HugeAray; { Huge Generic Dynamic Arrays }
-
- { NOTE: MaxArrays are Indexed 0..MaxSize-1 }
-
- { All similarities to the FlexArray Object are entirely deliberate. With }
- { the notable exception of the lack of Re-Sizing capability, MaxArrays }
- { should be entirely trivial substitutions for FlexArrays. }
-
- INTERFACE {NOT the complete Interface of Unit HugeAray, but all needed
- definitions are reproduced here.}
- Type
- MaxArray = Object
-
- Procedure Create; {All MaxArrays *MUST* be CREATEd exactly ONCE,
- and BEFORE ANY other operations are performed!!!!}
-
- Procedure Init (MaxElements : LongInt; ElementSize : Word);
-
- {Once CREATEd, or DESTROYed, a MaxArray may be
- INITed to any desired number of elements}
-
- Procedure Destroy; {Return all memory for given MaxArray to the Heap.}
-
- Procedure Accept (Var El; Index : LongInt; Size : Word);
- {Assign the Indexth El}
-
- Procedure Retrieve (Var El; Index : LongInt; Size : Word);
- {Get the Indexth El}
-
- Procedure Copy (From : MaxArray);
- {Target *MUST* already be initialized}
- {to the EXACT same parameters as From}
- {this will save checking for sufficent}
- {available Memory!}
-
- Procedure Swap (I,J : LongInt); {Swap the Ith and Jth Elements}
-
- Function MaxSize : LongInt; {Report Array Length}
- Function ElemSize : Word; {Report Element Size}
-
- End;
-
- IMPLEMENTATION { Not Reproduced }
-
-
- ***************************************************************************
- Implementation {GenericHeap} :
- ***************************************************************************
-
- Unit MGenHeap; { MaxArray-Based Generic Heaps }
-
- { GenericHeaps are indexed 1..MaxElements, rather then 0..MaxElements-1 }
-
-
- INTERFACE {NOT the complete Interface of the MGenHeap unit,
- but all needed definitions are reproduced.}
- Type
- GenericHeap = Object (MaxArray)
-
- Procedure Init (MaxElements : LongInt; ElementSize : Word;
- GreaterFunc : SortFunc);
-
- { Accept, Retrieve, and Swap are only redefined to }
- { implement the 1..MaxElement indexing needed for Heaps }
-
- Procedure Accept (Var El; Index : LongInt; Size : Word);
-
- Procedure Retrieve (Var El; Index : LongInt; Size : Word);
-
- Procedure Swap (I,J : LongInt);
-
- Procedure SiftDown (I,J : LongInt);
- { While I can think of No reason to }
- { Use SiftDown externally, there may }
- { be a reason, so I have exported it }
-
- Procedure SiftUp (Var El; Index : LongInt; Size : Word);
- { SiftUp can be used in place of Accept }
- { In order to Create/Maintain a Heap as }
- { a Heap while adding elements, thus }
- { allowing the use of Sort instead of }
- { HeapSort which structures a Heap by }
- { using BuildHeap. }
-
- Procedure BuildHeap;
- { Creates the Heap structure from }
- { the ground up. }
- Procedure Sort;
- { Sorts a Heap into Ascending order }
- { Assumes HEAP is built or maintained. }
-
- Procedure ChangeSort (NewSort : SortFunc);
-
- { Permits the changing of sorting methods }
- { such as might be required for sorting }
- { records by a different field, for example }
-
- { NOTE: This will require use of HeapSort to re-sort. }
-
- Procedure HeapSort;
- { Sorts a Heap into Ascending order }
- { Assumes nothing about Heap structure. }
-
- Procedure Copy (From : GenericHeap);
- { Target Heap *MUST* be initialized }
- { to EXACTLY same parameters as From }
- End;
-
- IMPLEMENTATION { Not Reproduced }
-
- **************************************************************************
- { UNIT DEPENDENCIES }
- **************************************************************************
-
- FlexPntr SrtFuncs
- | \ / |
- | \ / |
- HugeAray \ / |
- / \ \ / |
- / \ \ / |
- / \ \/ |
- H_Arrays MGenHeap |
- \ |
- \ |
- \ |
- \ |
- \|
- Heaps
-
- ****************************************************************************
- { ERROR MESSAGES }
- ****************************************************************************
-
- I have made considerable effort to anticipate all possible errors when
- manipulating MaxArrays, and have incorporated a simple error procedure into
- the implementation section of unit HugeAray. It prints a simple diagnostic
- message to the screen and HALTs the program. This "bonehead" strategy seems
- to be sufficient for three reasons: (1) I BELIEVE that ALL conceivable errors
- will be due to developmental errors. (2) I dislike the necessity of passing
- boolean "fail" variables. (3) Enough statistical information methods are
- provided to enable the user to define more sophisticated error checking and
- recovery methods, if such is desired or needed by some situation which I have
- not anticipated.
-
- POSSIBLE ERROR MESSAGES:
-
- Attempted Init on Un-Created or Initialized MaxArray.
-
- Insufficient Memory for Requested MaxArray.
-
- Attempted Accept with Incorrect Size Variable.
-
- Attempted Accept on Un-Initialized MaxArray.
-
- Attempted Retrieve with Incorrect Size Variable.
-
- Attempted Retrieve with Un-Initialized MaxArray.
-
- ***** INDEX OUT OF BOUNDS *****
-
- Attempted Copy on Un-Created or Initialized MaxArray.
-
- Attempted Illegal Copy operation. { PROBABLY wrong data size... }
-
- Insufficient Memory for requested MaxArray.
-
-
- { To be honest, I must add that I may very easily have overlooked something.
- If you find such an error, please write and let me know!!! }
-
-
- ******************************************************************************
- { PROPRIETARY CODE }
- ******************************************************************************
-
- Since the only significantly original work in these units is in MGenHeap and
- HugeAray, I have elected not to distribute these in source form. In fact, the
- source for these should be completely unnecessary. However, if you really
- want it, you can convince me by sending me a check for $20.00 at the above
- address.
-
- As to the FlexPntr unit. ALL the code in it was either directly taken from
- an article in PROGRAMMERS JOURNAL by Tom Swann or inspired by the same article
- (Programming on the Variant Express PJ V6.5 Sept/Oct 1988), and is of such a
- fundamental nature that no changes are really possible or desireable in ANY
- context. I have distributed it as a TPU simply because Turbo has no other
- method available for PRIVATE declarations (ala ADA). Therefore, since I did
- not really originate the code, and since there is no reason to ever change
- any of it, I will NOT distribute the source code for any reason. Those
- incureably interested individuals should look up the above article, paying
- particular attention to Mr. Swann's definitions of IntPtr and IntRec, which
- I have renamed "FlexPtr" and "FlxArray" respectively. NOTE: the array which
- Mr. Swann defines within the IntRec is referenced with field name "Flex" in
- my version of the construct, and is an array of Bytes, not Integers.
-
- ******************************************************************************
- ******************************************************************************
- ******************************************************************************