home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / text_cla / cmemlib.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-12  |  8.1 KB  |  332 lines

  1.  
  2. #if defined (CMEMLIB_Sym)
  3. #else
  4. #define CMEMLIB_Sym
  5.  
  6.  
  7. ////////////////////////////////////////////////////////////////////////
  8. // 
  9. //  Module Name:  cmemlib.h 
  10. //
  11. //  Description:  Cmemlib include for class definitions
  12. //
  13. //  Written by:   John Tal
  14. //
  15. //
  16. //  Modification history:
  17. //
  18. //  Date         Engineer     Mod #          Modification Description
  19. //
  20. //  23-May-1991  Tal          v 1.0-001      Initial conversion to C++
  21. //  01-Oct-1991   Tal         v 1.0-002      Added comments on container 
  22. //                                           class
  23. //
  24. ////////////////////////////////////////////////////////////////////////
  25.  
  26. #include <stdlib.h>
  27. #include "memincs.h"
  28.  
  29.  
  30. //
  31. //  Tree Class
  32. //
  33.  
  34. class TREE_C
  35. {
  36.  
  37. private:
  38.  
  39.     //  Available to class
  40.     //  Unavailable to derived classes
  41.     //  Unavailable to application
  42.  
  43.     //
  44.     // TRE structs
  45.     //
  46.  
  47.     struct TNODE_S
  48.     {
  49.       PVOID          pvData;
  50.       struct TNODE_S *pstLeft;
  51.       struct TNODE_S *pstRight;
  52.     };
  53.     typedef struct TNODE_S TNODE_T;
  54.     typedef TNODE_T *TNODE_P;
  55.     typedef TNODE_T **TNODE_PP;
  56.  
  57.     //   The TNODE_x structure CLASS appears within the TREE_C class.
  58.     //   This was done to resolve the problem in needing a head pointer
  59.     //   to the entire tree.  If the TREE_C class pointed to itself, then
  60.     //   every node on the tree would have had to be an instance of the
  61.     //   TREE_C class (the head accessed by being declared static to
  62.     //   each instance).
  63.     //
  64.     //   The solution was to have TREE_C contain TNODE_T, TREE_C is then
  65.     //   'officially' known as a 'container' class.  
  66.     //
  67.     //   Another clue leading to the use of container class is that it
  68.     //   would not have made sense to create multiple instances of the
  69.     //   TNODE_ class each having the same references to the manipulation
  70.     //   routines (Ins, Del, Leaf, etc).   The use of a container class
  71.     //   also promotes better data hiding, the user/application does not 
  72.     //   know about the 'real' class utilized at the lowest layer.
  73.  
  74.  
  75.     TNODE_P  pstHead;
  76.     TNODE_P  pstNode;
  77.     SHORT    Leaf(TNODE_P);
  78.     VOID     RotateLeft(TNODE_P,TNODE_PP);
  79.     VOID     RotateRight(TNODE_P,TNODE_PP);
  80.     VOID     Ins(TNODE_PP,TNODE_P);
  81.     SHORT    Del(PVOID, TNODE_P, TNODE_PP);
  82.  
  83. protected:
  84.  
  85.     //  Available to class
  86.     //  Available to derived classes
  87.     //  Unavailable to application
  88.  
  89.     SHORT (*pCompareFunc)(PVOID,PVOID); 
  90.  
  91.           //  Note:  Use virtual functions VERY sparingly.
  92.           //         Here we got around it by storing a ptr to func
  93.           //         Do not recommend using friend functions at all
  94.           //         (possibly unless trying to overload operators)
  95.  
  96. public:
  97.  
  98.     //  Available to class
  99.     //  Available to derived classes
  100.     //  Available to application
  101.  
  102.     TREE_C(VOID);
  103.     ~TREE_C(VOID);
  104.     SHORT    Delete(PVOID);          // Remove entry
  105.     SHORT    Find(PVOID,PPVOID);     // Find entry
  106.     SHORT    Insert(PVOID);          // Insert entry
  107.     SHORT    Vacate(BOOL);           // Remove all entries
  108.     SHORT    SetCompareFunc(SHORT (*)(PVOID,PVOID));
  109. };
  110.  
  111.  
  112. typedef TREE_C * TREE_CP;
  113. typedef TREE_C ** TREE_CPP;
  114.  
  115.  
  116.  
  117.  
  118. //
  119. //  Link List Class
  120. //
  121.  
  122. class LLIST_C
  123. {
  124.  
  125. private:
  126.  
  127.     //  Available to class
  128.     //  Unavailable to derived classes
  129.     //  Unavailable to application 
  130.  
  131.  
  132. protected:
  133.  
  134.     //  Available to class
  135.     //  Available to derived classes
  136.     //  Unavailable to application 
  137.  
  138.     //
  139.     // LST structs
  140.     //
  141.  
  142.     struct LLIST_S
  143.     {
  144.        struct LLIST_S  *pstPrev;
  145.        struct LLIST_S  *pstNext;
  146.        PVOID   pvData;
  147.     };
  148.     typedef struct LLIST_S LLIST_T;
  149.     typedef LLIST_T *LLIST_P;
  150.     typedef LLIST_T **LLIST_PP;
  151.  
  152.  
  153.     LLIST_P   pstHead;
  154.     LLIST_P   pstCurNode;
  155.  
  156.     //  Since we are keeping application away from internals, we will provide
  157.     //  a way for application to set multiple 'markers' in the list to jump
  158.     //  around
  159.  
  160.     struct TRACK_S
  161.     {
  162.        LLIST_P  pstNode;
  163.        SHORT    sState;
  164.     };
  165.     typedef struct TRACK_S TRACK_T;
  166.     typedef TRACK_S * TRACK_P;
  167.  
  168.  
  169.     TRACK_P  pstTrack;
  170.     SHORT    sTrackSize;
  171.  
  172.     #define TRACK_AVAIL 0
  173.     #define TRACK_USED  1
  174.  
  175.     //
  176.     //  Internal maint routines
  177.     //
  178.  
  179.     SHORT  AddAfterNode(LLIST_P, LLIST_P);   
  180.     SHORT  AddBeforeNode(LLIST_P, LLIST_P);
  181.     SHORT  DeleteNode(LLIST_P);
  182.     SHORT  FindNode(PVOID,LLIST_PP);
  183.     SHORT  FindLastNode(LLIST_PP);
  184.     SHORT  Alloc(PVOID,LLIST_PP);
  185.     SHORT  UnlinkNode(LLIST_P);
  186.  
  187.     SHORT (*pCompareFunc) (PVOID,PVOID);  //  Used for most funcs below
  188.  
  189. public:
  190.  
  191.     //  Available to class
  192.     //  Available to derived classes
  193.     //  Available to application 
  194.  
  195.  
  196.     LLIST_C(SHORT = 10);          // initializes sTrackSize
  197.     ~LLIST_C(VOID);                // Vacate
  198.  
  199.     //  Following are all standard application functions
  200.     //  Application never sees the LLIST_x types
  201.     
  202.     SHORT  AddAfterCur(PVOID);     // Used after Find or GoMark 
  203.     SHORT  AddBeforeCur(PVOID);    //  ""
  204.     SHORT  DeleteCur(VOID);        // Deletes Current node
  205.                                          // current node undefined after 
  206.     SHORT  Find(PVOID,PPVOID);     // Return address PPVOID based on key
  207.                                          // At address PVOID, cf pCompareFunc 
  208.     SHORT  FindLast(PPVOID);       // Return last address
  209.     SHORT  FindFirst(PPVOID);      // Return 1st address
  210.     SHORT  GetCur(PPVOID);         // Read based on pstCurNode
  211.     SHORT  GetPrev(PPVOID);        // Read prev based on pstCurNode
  212.     SHORT  GetNext(PPVOID);        // Read next based on pstCurNode
  213.     SHORT  Insert(PVOID);          // Insert Address
  214.     SHORT  Vacate(BOOL);           // Removes all data from list 
  215.     SHORT  SetMark(PSHORT);        // Remember pstCurNode
  216.     SHORT  GoMark(SHORT);          // Restore pstCurNode
  217.     SHORT  ReleaseMark(SHORT);     // Release/free mark
  218.     SHORT  SetCompareFunc(SHORT (*)(PVOID,PVOID));
  219. };
  220.  
  221. typedef LLIST_C * LLIST_CP;
  222. typedef LLIST_C ** LLIST_CPP;
  223.  
  224.  
  225. //
  226. //  Queue Class
  227. //
  228.  
  229. class QUEUE_C : LLIST_C    // derived from LLIST_C class
  230. {
  231. private:
  232.  
  233.    LLIST_P  pstTail; 
  234.    long   lEntries;
  235.  
  236. public:
  237.  
  238.    QUEUE_C(VOID);
  239.    ~QUEUE_C(VOID) { Vacate(C_FALSE); };
  240.    SHORT   Vacate(BOOL);           // Vacate all entries
  241.    SHORT   Enq(PVOID);             // Add entry
  242.    SHORT   Deq(PPVOID);            // Remove entry
  243.    SHORT   GetEntries(PLONG);      // Return entries in queue
  244.    SHORT   Find(PVOID,PPVOID);     // Return address PPVOID based on key
  245.    SHORT   SetCompareFunc(SHORT (*)(PVOID,PVOID));
  246. };
  247.  
  248. typedef QUEUE_C * QUEUE_CP;
  249. typedef QUEUE_C ** QUEUE_CPP;
  250.  
  251.  
  252.  
  253. //
  254. //  Stack Class
  255. //
  256.  
  257. class STACK_C : LLIST_C
  258. {
  259.  
  260.  
  261. public:
  262.    STACK_C(VOID) { pCompareFunc = NULL; };
  263.    ~STACK_C(VOID) { Vacate(C_FALSE); };
  264.    SHORT  Vacate(BOOL);
  265.    SHORT  IsEmpty(PBOOL);
  266.    SHORT  Pop(PPVOID);
  267.    SHORT  Push(PVOID);
  268.    SHORT  Find(PVOID,PPVOID);     // Return address PPVOID based on key
  269.    SHORT  SetCompareFunc(SHORT (*)(PVOID,PVOID));
  270.  
  271. };
  272.  
  273. typedef STACK_C * STACK_CP;
  274. typedef STACK_C ** STACK_CPP;
  275.  
  276.  
  277. //
  278. //  Heap Class
  279. //
  280.  
  281.  
  282. class HEAP_C
  283. {
  284. private:
  285.  
  286.     //
  287.     //  HEP structs
  288.     //  
  289.  
  290.     struct HEAP_DATA_S
  291.     {
  292.        SHORT  sPriority;
  293.        PVOID  pvData;
  294.     };
  295.  
  296.     typedef struct HEAP_DATA_S HEAP_DATA_T;
  297.     typedef HEAP_DATA_T * HEAP_DATA_P;
  298.     typedef HEAP_DATA_T ** HEAP_DATA_PP;
  299.  
  300.     HEAP_DATA_P  pstHeapData;
  301.     SHORT sMaxElms;
  302.     SHORT sBottom;
  303.  
  304.     SHORT  Down(VOID);
  305.     SHORT  Up(VOID);
  306.     VOID   Swap(HEAP_DATA_P,HEAP_DATA_P);
  307.  
  308. protected:
  309.  
  310.     SHORT (*pCompareFunc) (PVOID,PVOID);  //  Used for most funcs below
  311.  
  312. public:
  313.  
  314.     HEAP_C(SHORT = 100);
  315.     ~HEAP_C(VOID) { Vacate(); free(pstHeapData); };
  316.     SHORT  Enq(SHORT, PVOID);
  317.     SHORT  Deq(PSHORT, PPVOID);
  318.     SHORT  Find(PVOID, PPVOID);
  319.     SHORT  GetEntries(PSHORT);
  320.     SHORT  SetCompareFunc(SHORT (*)(PVOID,PVOID));
  321.     SHORT  Vacate(BOOL = C_FALSE);
  322. };
  323.  
  324. typedef HEAP_C * HEAP_CP;
  325. typedef HEAP_C ** HEAP_CPP;
  326.  
  327.  
  328.  
  329. #endif
  330.  
  331.  
  332.