home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / OTL-MC7.DMS / in.adf / classsource.lha / ClassSource / Exec / Lists / Lists.c
Encoding:
C/C++ Source or Header  |  1995-02-12  |  4.9 KB  |  346 lines

  1.  
  2. #include <classes/Exec/Lists.h>
  3.  
  4. #include <pragma/exec_lib.h>
  5.  
  6. NodeC::NodeC()
  7. {
  8.     mln_Succ = NULL;
  9.     mln_Pred = NULL;
  10. }
  11.  
  12. NodeC::NodeC(const NodeC &)
  13. {
  14.     mln_Succ = NULL;
  15.     mln_Pred = NULL;
  16. }
  17.  
  18. NodeC::~NodeC()
  19. {
  20.     remove();
  21. }
  22.  
  23. NodeC &NodeC::operator = (const NodeC &s)
  24. {
  25.     if (this != &s)
  26.     {
  27.         mln_Succ = NULL;
  28.         mln_Pred = NULL;
  29.     };
  30.     return *this;
  31. }
  32.  
  33. VOID NodeC::remove()
  34. {
  35.     if (isInList())
  36.     {
  37.         Remove((struct Node *) this);
  38.         mln_Succ = NULL;
  39.         mln_Pred = NULL;
  40.     };
  41. }
  42.  
  43. ULONG NodeC::index() const
  44. {
  45.     if (isInList())
  46.     {
  47.         ULONG i = 0;
  48.         struct MinNode *n = (struct MinNode *) this;
  49.         while (n = n->mln_Pred)
  50.             i++;
  51.         return i;
  52.     };
  53.     return 0;
  54. }
  55.  
  56. NodeC *NodeC::next() const
  57. {
  58.     if (mln_Succ->mln_Succ == NULL)
  59.         return NULL;
  60.     return (NodeC *) mln_Succ;
  61. }
  62.  
  63. NodeC *NodeC::prev() const
  64. {
  65.     if (mln_Pred->mln_Pred == NULL)
  66.         return NULL;
  67.     return (NodeC *) mln_Pred;
  68. }
  69.  
  70. // **********************************************************
  71.  
  72. ListC::ListC()
  73. {
  74.     newList();
  75. }
  76.  
  77. ListC::ListC(const ListC &s)
  78. {
  79.     newList();
  80. }
  81.  
  82. ListC::~ListC()
  83. {
  84.     NodeC *n;
  85.     while (n = remHead()) {
  86.         n->mln_Succ = NULL;
  87.         n->mln_Pred = NULL;
  88.     };
  89. }
  90.  
  91. ListC &ListC::operator = (const ListC &s)
  92. {
  93.     if (this != &s)
  94.     {
  95.         NodeC *n;
  96.         while (n = (NodeC *) remHead()) {
  97.             n->mln_Succ = NULL;
  98.             n->mln_Pred = NULL;
  99.         };
  100.     };
  101.     return *this;
  102. }
  103.  
  104. VOID ListC::insert(NodeC &node, NodeC *after)
  105. {
  106.     if (!node.isInList())
  107.         Insert((struct List *) this,
  108.             (struct Node *) &node,(struct Node *) after);
  109. }
  110.  
  111. VOID ListC::addHead(NodeC &node)
  112. {
  113.     if (!node.isInList())
  114.         AddHead((struct List *) this,(struct Node *) &node);
  115. }
  116.  
  117. VOID ListC::addTail(NodeC &node)
  118. {
  119.     if (!node.isInList())
  120.         AddTail((struct List *) this,(struct Node *) &node);
  121. }
  122.  
  123. NodeC *ListC::remHead()
  124. {
  125.     NodeC *n = (NodeC *) RemHead((struct List *) this);
  126.     if (n)
  127.     {
  128.         n->mln_Succ = NULL;
  129.         n->mln_Pred = NULL;
  130.     };
  131.     return n;
  132. }
  133.  
  134. NodeC *ListC::remTail()
  135. {
  136.     NodeC *n = (NodeC *) RemTail((struct List *) this);
  137.     if (n)
  138.     {
  139.         n->mln_Succ = NULL;
  140.         n->mln_Pred = NULL;
  141.     };
  142.     return n;
  143. }
  144.  
  145. ULONG ListC::indexOf(const NodeC &node) const
  146. {
  147.     NodeC *n = (NodeC *) mlh_Head;
  148.     ULONG i = 1;
  149.     while (n->mln_Succ)
  150.     {
  151.         if (n == &node)
  152.             return i;
  153.         i++;
  154.         n = (NodeC *) n->mln_Succ;
  155.     };
  156.     return 0;
  157. }
  158.  
  159. ULONG ListC::length() const
  160. {
  161.     ULONG i = 0;
  162.     ListCursorC lc((ListC &) *this);
  163.     while (!lc.isDone())
  164.     {
  165.         i++;
  166.         lc.next();
  167.     };
  168.     return i;
  169. }
  170.  
  171. NodeC *ListC::find(ULONG index) const
  172. {
  173.     ListCursorC lc((ListC &) *this);
  174.     while (!lc.isDone())
  175.     {
  176.         if (index == 0)
  177.             return lc.item();
  178.         index--;
  179.         lc.next();
  180.     };
  181.     return NULL;
  182. }
  183.  
  184. NodeC *ListC::first() const
  185. {
  186.     if (mlh_Head->mln_Succ == NULL)
  187.         return NULL;
  188.     return (NodeC *) mlh_Head;
  189. }
  190.  
  191. NodeC *ListC::last() const
  192. {
  193.     if (mlh_TailPred->mln_Pred == NULL)
  194.         return NULL;
  195.     return (NodeC *) mlh_TailPred;
  196. }
  197.  
  198. // **************************************************************
  199.  
  200. ListCursorC::ListCursorC(const ListC &l)
  201. {
  202.     list = (ListC *) &l;
  203.     first();
  204. }
  205.  
  206. ListCursorC::ListCursorC(const ListCursorC &s)
  207. {
  208.     list = s.list;
  209.     first();
  210. }
  211.  
  212. ListCursorC &ListCursorC::operator = (const ListCursorC &s)
  213. {
  214.     if (this != &s)
  215.     {
  216.         list = s.list;
  217.         pos = s.pos;
  218.     };
  219.     return *this;
  220. }
  221.  
  222. VOID ListCursorC::first()
  223. {
  224.     pos = list->isEmpty() ? NULL : (NodeC *) list->mlh_Head;
  225. }
  226.  
  227. VOID ListCursorC::last()
  228. {
  229.     pos = list->isEmpty() ? NULL : (NodeC *) list->mlh_TailPred;
  230. }
  231.  
  232. VOID ListCursorC::next()
  233. {
  234.     if (pos)
  235.         if (pos->mln_Succ)
  236.             pos = (NodeC *) pos->mln_Succ;
  237. }
  238.  
  239. VOID ListCursorC::prev()
  240. {
  241.     if (pos)
  242.         if (pos->mln_Pred)
  243.             pos = (NodeC *) pos->mln_Pred;
  244. }
  245.  
  246. NodeC *ListCursorC::item() const
  247. {
  248.     return isDone() ? NULL : pos;
  249. }
  250.  
  251. BOOL ListCursorC::isDone() const
  252. {
  253.     if (pos)
  254.         if ((pos->mln_Succ != NULL) && (pos->mln_Pred != NULL))
  255.             return FALSE;
  256.     return TRUE;
  257. }
  258.  
  259. // *************************************************************
  260.  
  261. ENodeC::ENodeC(STRPTR name, UBYTE type, BYTE pri)
  262.     : NodeC()
  263. {
  264.     ln_Type = type;
  265.     ln_Pri = pri;
  266.     ln_Name = name;
  267. }
  268.  
  269. ENodeC::ENodeC(const ENodeC &s)
  270.     : NodeC()
  271. {
  272.     ln_Type = s.ln_Type;
  273.     ln_Pri = s.ln_Pri;
  274.     ln_Name = s.ln_Name;
  275. }
  276.  
  277. ENodeC::~ENodeC()
  278. {
  279. }
  280.  
  281. ENodeC &ENodeC::operator= (const ENodeC &s)
  282. {
  283.     if (this != &s)
  284.     {
  285.         NodeC::operator=(s);
  286.         ln_Type = s.ln_Type;
  287.         ln_Pri = s.ln_Pri;
  288.         ln_Name = s.ln_Name;
  289.     };
  290.     return *this;
  291. }
  292.  
  293. ENodeC *ENodeC::find(STRPTR name) const
  294. {
  295.     return (ENodeC *) FindName((struct List *) this,name);
  296. }
  297.  
  298. // **********************************************************
  299.  
  300. EListC::EListC(UBYTE type)
  301.     : ListC()
  302. {
  303.     lh_Type = type;
  304. }
  305.  
  306. EListC::EListC(const EListC &s)
  307.     : ListC()
  308. {
  309.     lh_Type = s.lh_Type;
  310. }
  311.  
  312. EListC::~EListC()
  313. {
  314. }
  315.  
  316. EListC &EListC::operator= (const EListC &s)
  317. {
  318.     if (this != &s)
  319.     {
  320.         ListC::operator=(s);
  321.         lh_Type = s.lh_Type;
  322.     };
  323.     return *this;
  324. }
  325.  
  326. VOID EListC::enqueue(ENodeC &node)
  327. {
  328.     Enqueue((struct List *) this,(struct Node *) &node);
  329. }
  330.  
  331. ENodeC *EListC::find(STRPTR name) const
  332. {
  333.     return (ENodeC *) FindName((struct List *) this,name);
  334. }
  335.  
  336. // **************************************************************
  337.  
  338. EListCursorC &EListCursorC::operator = (const EListCursorC &s)
  339. {
  340.     if (this != &s)
  341.     {
  342.         ListCursorC::operator=(s);
  343.     };
  344.     return *this;
  345. }
  346.