home *** CD-ROM | disk | FTP | other *** search
/ Computer Panoráma / computer_panorama_1997-12-hibas.iso / SHARE / GRAPH / PTC051.ZIP / SRC / ITERATOR.H < prev    next >
C/C++ Source or Header  |  1997-08-27  |  5KB  |  283 lines

  1. //////////////////////////
  2. // linked list iterator //
  3. //////////////////////////
  4.  
  5. #ifndef __ITERATOR_H
  6. #define __ITERATOR_H
  7.  
  8. #include "list.h"
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. template <class T> class Iterator
  17. {
  18.     // friends
  19.     friend class List<T>;
  20.  
  21.     public:
  22.  
  23.         // setup
  24.         Iterator();
  25.         Iterator(List<T> &list);
  26.         ~Iterator();
  27.  
  28.         // navigation
  29.         inline T* move(int position);
  30.         inline T* first();
  31.         inline T* last();
  32.         inline T* next();                           
  33.         inline T* prev();
  34.         
  35.         // current object
  36.         inline T* current() const;
  37.  
  38.         // operations on current object
  39.         inline int replace(T const &object);
  40.         inline int replace(T* object);
  41.         inline int insert(T const & object);
  42.         inline int insert(T* object);
  43.         inline int remove();
  44.         inline int free();
  45.  
  46.         // iterator status
  47.         inline int ok() const;
  48.  
  49.     private:
  50.  
  51.         // current node
  52.         List<T>::NODE *CurrentNode;
  53.  
  54.         // local list
  55.         List<T> *LocalList;
  56.  
  57.         // null objects
  58.         List<T> NullList;                // set to static?
  59.         List<T>::NODE NullNode;
  60. };
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. template <class T> inline Iterator<T>::Iterator()
  71. {
  72.     // setup null node
  73.     NullNode.next=&NullNode;
  74.     NullNode.prev=&NullNode;
  75.     NullNode.object=NULL;
  76.     
  77.     // defaults
  78.     LocalList=&NullList;
  79.     CurrentNode=&NullNode;
  80. }
  81.  
  82.  
  83. template <class T> inline Iterator<T>::Iterator(List<T> &list)
  84. {
  85.     // setup null node
  86.     NullNode.next=&NullNode;
  87.     NullNode.prev=&NullNode;
  88.     NullNode.object=NULL;
  89.     
  90.     // initialize
  91.     LocalList=&list;
  92.     CurrentNode=LocalList->Root.next;
  93.  
  94.     // register iterator
  95.     //LocalList->RegisterIterator(this);
  96. }
  97.  
  98.  
  99. template <class T> inline Iterator<T>::~Iterator()
  100. {
  101.     // release iterator
  102.     //LocalList->ReleaseIterator(this);
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. template <class T> inline T* Iterator<T>::move(int position)
  111. {
  112.     // move current node to position
  113.     int p=0;
  114.     List<T>::NODE *node=LocalList->Root.next;
  115.     while (node->object)
  116.     {
  117.         if (p==position)
  118.         {
  119.             // found position
  120.             CurrentNode=node;
  121.             return CurrentNode->object;
  122.         }
  123.         node=node->next;
  124.         p++;
  125.     }
  126.  
  127.     // failure
  128.     return NULL;
  129. }
  130.  
  131.  
  132. template <class T> inline T* Iterator<T>::first()
  133. {
  134.     // move to first node
  135.     CurrentNode=LocalList->Root.next;
  136.     return CurrentNode->object;
  137. }
  138.  
  139.  
  140. template <class T> inline T* Iterator<T>::last()
  141. {
  142.     // move to last node
  143.     CurrentNode=CurrentNode->prev;
  144.     return CurrentNode->object;
  145. }
  146.  
  147.  
  148. template <class T> inline T* Iterator<T>::next()
  149. {
  150.     // move to next node
  151.     CurrentNode=CurrentNode->next;
  152.     return CurrentNode->object;
  153. }
  154.  
  155.  
  156. template <class T> inline T* Iterator<T>::prev()
  157. {
  158.     // move to prev node
  159.     CurrentNode=CurrentNode->prev;
  160.     return CurrentNode->object;
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. template <class T> inline T* Iterator<T>::current() const
  171. {                              
  172.     // return current object
  173.     return CurrentNode->object;
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. template <class T> inline int Iterator<T>::replace(T const &object)
  183. {
  184.     // allocate store
  185.     T *store=new T;
  186.     if (!store) return 0;
  187.  
  188.     // replace current with store
  189.     *store=object;
  190.     return replace(store);
  191. }
  192.  
  193.  
  194. template <class T> inline int Iterator<T>::replace(T *object)
  195. {
  196.     // replace current object
  197.     if (CurrentNode)
  198.     {
  199.         delete CurrentNode->object;
  200.         CurrentNode->object=object;
  201.         return 1;
  202.     }
  203.     else return 0;
  204. }
  205.  
  206.  
  207. template <class T> inline int Iterator<T>::insert(T const &object)
  208. {
  209.     // allocate store
  210.     T *store=new T;
  211.     if (!store) return 0;
  212.  
  213.     // replace current with store
  214.     *store=object;
  215.     return insert(store);
  216. }
  217.  
  218.  
  219. template <class T> inline int Iterator<T>::insert(T *object)
  220. {
  221.     // fail on null object
  222.     if (!object) return 0;
  223.     
  224.     // create new node
  225.     List<T>::NODE *node=new List<T>::NODE;
  226.     if (!node) return 0;
  227.  
  228.     // setup node
  229.     node->prev=CurrentNode->prev;
  230.     node->next=CurrentNode;
  231.  
  232.     // insert node
  233.     CurrentNode->prev->next=node;
  234.     CurrentNode->prev=node;
  235.  
  236.     // update current node
  237.     CurrentNode=node;
  238.     return 1;
  239. }
  240.  
  241.  
  242. template <class T> inline int Iterator<T>::remove()
  243. {
  244.     // remove current node from list
  245.     if (CurrentNode->object)
  246.     {
  247.         CurrentNode->prev->next=CurrentNode->next;
  248.         CurrentNode->next->prev=CurrentNode->prev;
  249.         List<T>::NODE *old=CurrentNode;
  250.         CurrentNode=CurrentNode->prev;
  251.         delete old;
  252.         return 1;
  253.     }
  254.     else return 0;
  255. }
  256.  
  257.  
  258. template <class T> inline int Iterator<T>::free()
  259. {
  260.     // remove current node from list
  261.     if (CurrentNode->object)
  262.     {
  263.         CurrentNode->prev->next=CurrentNode->next;
  264.         CurrentNode->next->prev=CurrentNode->prev;
  265.         List<T>::NODE *old=CurrentNode;
  266.         CurrentNode=CurrentNode->prev;
  267.         delete old->object;
  268.         delete old;
  269.         return 1;
  270.     }
  271.     else return 0;
  272. }
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. #endif