home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 14 / MA_Cover_14.iso / source / c / pegase_src / lists.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-13  |  7.2 KB  |  296 lines

  1. #ifndef CLASSES_LISTS_HPP
  2. #define CLASSES_LISTS_HPP
  3.  
  4. /*
  5. **
  6. ** lists.hpp
  7. **
  8. ** (c) 1999 Didier Levet
  9. **
  10. ** Lists/Nodes
  11. **
  12. **
  13. */
  14.  
  15.  
  16. #ifndef  CLIB_EXEC_PROTOS_H
  17. #include <clib/exec_protos.h>
  18. #endif
  19.  
  20. #ifndef  EXEC_TYPES_H
  21. #include <exec/types.h>
  22. #endif
  23.  
  24. #ifndef  EXEC_NODES_H
  25. #include <exec/nodes.h>
  26. #endif
  27.  
  28.  
  29. //----------------------------------------------------------------------------------------------------
  30. //========================================== Class MinNode ===========================================
  31. //----------------------------------------------------------------------------------------------------
  32.  
  33. class CMinNode
  34. {
  35.     public:
  36.  
  37.         friend class CMinList;
  38.  
  39.         // Constructor and destructor.
  40.  
  41.         CMinNode() : pSucc(NULL), pPred(NULL)    {}
  42.         ~CMinNode() {
  43.             if (IsLinked() != FALSE)
  44.             {
  45.                 pPred->pSucc = pSucc;           // Unlink the node before deleting it.
  46.                 pSucc->pPred = pPred;
  47.             }
  48.         }
  49.  
  50.         // Accessors
  51.  
  52.         CMinNode *Next() const
  53.         {
  54.             return ((pSucc->pSucc != NULL) ? pSucc : NULL);
  55.         }
  56.  
  57.         CMinNode *Pred() const
  58.         {
  59.             return ((pPred->pPred != NULL) ? pPred : NULL);
  60.         }
  61.  
  62.         // Control.
  63.  
  64.         int IsLinked() const {return ((int) pSucc | (int) pPred);}
  65.         int IsLast()   const {return (pSucc->pSucc == NULL);}
  66.         int IsFirst()  const {return (pPred->pPred == NULL);}
  67.  
  68.         operator int()      const   {return ((int) pSucc | (int) pPred);}
  69.         int operator !()    const   {return !((int) pSucc | (int) pPred);}
  70.  
  71.         // Nodes management.
  72.  
  73.         void Remove();
  74.         void Insert(CMinNode *node);
  75.  
  76.     protected:
  77.  
  78.         CMinNode & operator = (const CMinNode &);     // Not implemented.
  79.         CMinNode(const CMinNode &);                   // Not implemented.
  80.  
  81.         void ResetLinks()   {pSucc = pPred = NULL;}
  82.  
  83.     private:
  84.  
  85.         CMinNode *pSucc;
  86.         CMinNode *pPred;
  87. };
  88.  
  89.  
  90. /*----------------------------------------------------------------------------------------------------*/
  91.  
  92. // inline CMinNode::~CMinNode()
  93. // {
  94. //     if (IsLinked() != FALSE)
  95. //     {
  96. //         pPred->pSucc = pSucc;           // Unlink the node before deleting it.
  97. //         pSucc->pPred = pPred;
  98. //     }
  99. // }
  100.  
  101. inline void CMinNode::Insert(CMinNode *node)
  102. {
  103.     pPred = node;                       // Insert the current node after the node given.
  104.     pSucc = node->pSucc;
  105.     pSucc->pPred = node->pSucc = this;
  106. }
  107.  
  108. inline void CMinNode::Remove()
  109. {
  110.     pPred->pSucc = pSucc;
  111.     pSucc->pPred = pPred;
  112.     ResetLinks();
  113. }
  114.  
  115.  
  116. //----------------------------------------------------------------------------------------------------
  117. //=========================================== Class Node =============================================
  118. //----------------------------------------------------------------------------------------------------
  119.  
  120. #ifndef STRING
  121. typedef const char *STRING;
  122. #endif
  123.  
  124.  
  125. class CNode : public CMinNode
  126. {
  127.     public:
  128.  
  129.         // Constructor and destructor.
  130.  
  131.         CNode(STRING name = NULL, UBYTE type = NT_USER, BYTE pri = 0)
  132.             : pcName(name), bPri(pri), bType(type) {}
  133.  
  134.         // Accessors
  135.  
  136.         STRING NodeName() const         {return pcName;}
  137.         void   NodeName(STRING name)    {pcName = name;}
  138.  
  139.         UBYTE NodeType() const          {return bType;}
  140.         void  NodeType(UBYTE type)      {bType = type;}
  141.  
  142.         BYTE  NodePri() const           {return bPri;}
  143.         void  NodePri(BYTE pri)         {bPri = pri;}
  144.  
  145.     protected:
  146.  
  147.         CNode & operator = (const CNode &);       // Not implemented.
  148.         CNode(const CNode &);                     // Not implemented.
  149.  
  150.     private:
  151.  
  152.         UBYTE       bType;
  153.         BYTE        bPri;
  154.         STRING      pcName;
  155. };
  156.  
  157.  
  158. //----------------------------------------------------------------------------------------------------
  159. //========================================= Class MinList ============================================
  160. //----------------------------------------------------------------------------------------------------
  161.  
  162. class CMinList
  163. {
  164.     public:
  165.  
  166.         // Constructor and destructor.
  167.  
  168.         CMinList() : pTail(NULL)         {ResetList();}
  169.  
  170.         ~CMinList()
  171.         {
  172.             CMinNode *p2;
  173.             CMinNode *p1 = (CMinNode *) this;
  174.  
  175.             while ((p2 = p1->Next()) != NULL)
  176.             {
  177.                 p1->ResetLinks();       // Unlink all nodes before freeing the list.
  178.                 p1 = p2;
  179.             }
  180.         }
  181.  
  182.         // Control
  183.  
  184.         int IsEmpty() const         {return (poTailPred == (CMinNode *) this);}
  185.  
  186.         operator int ()  const      {return (poTailPred == (CMinNode *) this);}
  187.         int operator !() const      {return (poTailPred != (CMinNode *) this);}
  188.  
  189.         // Manipulators.
  190.  
  191.         CMinNode *RemHead();
  192.         CMinNode *RemTail();
  193.  
  194.         void Enqueue(CNode *node);
  195.         void AddHead(CMinNode *node)     {node->Insert(poHead);}
  196.         void AddTail(CMinNode *node)     {node->Insert(poTailPred);}
  197.  
  198.         CNode *FindName(STRING name) const;
  199.  
  200.         static CNode *FindName(const CNode *node, STRING name) const;
  201.  
  202.         // Misc.
  203.  
  204.         CMinNode *First() const  {return ( (IsEmpty() != FALSE) ? NULL : poHead);}
  205.         CMinNode *Last()  const  {return ( (IsEmpty() != FALSE) ? NULL : poTailPred);}
  206.  
  207.     protected:
  208.  
  209.         CMinList(const CMinList &);                   // Not implemented.
  210.         CMinList & operator = (const CMinList &);     // Not implemented.
  211.  
  212.     private:
  213.  
  214.         CMinNode *poHead;
  215.         CMinNode *const pTail;
  216.         CMinNode *poTailPred;
  217.  
  218.         void ResetList() {poHead = (CMinNode *) &pTail; poTailPred = (CMinNode *) &poHead;}
  219. };
  220.  
  221. /*----------------------------------------------------------------------------------------------------*/
  222.  
  223. inline CMinNode * CMinList::RemHead()
  224. {
  225.     CMinNode *p = NULL;
  226.  
  227.     if (IsEmpty() == FALSE)
  228.     {
  229.         p = poHead;
  230.         p->Remove();
  231.     }
  232.     return (p);
  233. }
  234.  
  235.  
  236. inline CMinNode * CMinList::RemTail()
  237. {
  238.     CMinNode *p = NULL;
  239.  
  240.     if (IsEmpty() == FALSE)
  241.     {
  242.         p = poTailPred,
  243.         p->Remove();
  244.     }
  245.     return (p);
  246. }
  247.  
  248.  
  249. inline void CMinList::Enqueue(CNode *node)
  250. {
  251.     ::Enqueue((List *) this, (Node *) node);
  252. }
  253.  
  254.  
  255. inline CNode *CMinList::FindName(STRING name) const
  256. {
  257.     return (CNode *) ::FindName((List *) this, (UBYTE *) name);
  258. }
  259.  
  260.  
  261. inline CNode *CMinList::FindName(const CNode *node, STRING name) const
  262. {
  263.     return (CNode *) ::FindName((List *) node, (UBYTE *) name);
  264. }
  265.  
  266.  
  267. /*----------------------------------------------------------------------------------------------------*/
  268. /*=========================================== Classe List ============================================*/
  269. /*----------------------------------------------------------------------------------------------------*/
  270.  
  271. class CList : public CMinList
  272. {
  273.     public:
  274.  
  275.         CList(UBYTE type = NT_USER) : bType(type)    {}
  276.  
  277.         UBYTE   Type() const        {return bType;}
  278.         void    Type(UBYTE type)    {bType = type;}
  279.  
  280.     protected:
  281.  
  282.         CList &operator = (const CList &);    // Not implemented.
  283.         CList(const CList &);                 // Not implemented.
  284.  
  285.     private:
  286.  
  287.         UBYTE   bType;
  288.         UBYTE   bPad;
  289. };
  290.  
  291.  
  292. /*----------------------------------------------------------------------------------------------------*/
  293.  
  294. #endif
  295.  
  296.