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 >
Wrap
C/C++ Source or Header
|
1997-08-27
|
5KB
|
283 lines
//////////////////////////
// linked list iterator //
//////////////////////////
#ifndef __ITERATOR_H
#define __ITERATOR_H
#include "list.h"
template <class T> class Iterator
{
// friends
friend class List<T>;
public:
// setup
Iterator();
Iterator(List<T> &list);
~Iterator();
// navigation
inline T* move(int position);
inline T* first();
inline T* last();
inline T* next();
inline T* prev();
// current object
inline T* current() const;
// operations on current object
inline int replace(T const &object);
inline int replace(T* object);
inline int insert(T const & object);
inline int insert(T* object);
inline int remove();
inline int free();
// iterator status
inline int ok() const;
private:
// current node
List<T>::NODE *CurrentNode;
// local list
List<T> *LocalList;
// null objects
List<T> NullList; // set to static?
List<T>::NODE NullNode;
};
template <class T> inline Iterator<T>::Iterator()
{
// setup null node
NullNode.next=&NullNode;
NullNode.prev=&NullNode;
NullNode.object=NULL;
// defaults
LocalList=&NullList;
CurrentNode=&NullNode;
}
template <class T> inline Iterator<T>::Iterator(List<T> &list)
{
// setup null node
NullNode.next=&NullNode;
NullNode.prev=&NullNode;
NullNode.object=NULL;
// initialize
LocalList=&list;
CurrentNode=LocalList->Root.next;
// register iterator
//LocalList->RegisterIterator(this);
}
template <class T> inline Iterator<T>::~Iterator()
{
// release iterator
//LocalList->ReleaseIterator(this);
}
template <class T> inline T* Iterator<T>::move(int position)
{
// move current node to position
int p=0;
List<T>::NODE *node=LocalList->Root.next;
while (node->object)
{
if (p==position)
{
// found position
CurrentNode=node;
return CurrentNode->object;
}
node=node->next;
p++;
}
// failure
return NULL;
}
template <class T> inline T* Iterator<T>::first()
{
// move to first node
CurrentNode=LocalList->Root.next;
return CurrentNode->object;
}
template <class T> inline T* Iterator<T>::last()
{
// move to last node
CurrentNode=CurrentNode->prev;
return CurrentNode->object;
}
template <class T> inline T* Iterator<T>::next()
{
// move to next node
CurrentNode=CurrentNode->next;
return CurrentNode->object;
}
template <class T> inline T* Iterator<T>::prev()
{
// move to prev node
CurrentNode=CurrentNode->prev;
return CurrentNode->object;
}
template <class T> inline T* Iterator<T>::current() const
{
// return current object
return CurrentNode->object;
}
template <class T> inline int Iterator<T>::replace(T const &object)
{
// allocate store
T *store=new T;
if (!store) return 0;
// replace current with store
*store=object;
return replace(store);
}
template <class T> inline int Iterator<T>::replace(T *object)
{
// replace current object
if (CurrentNode)
{
delete CurrentNode->object;
CurrentNode->object=object;
return 1;
}
else return 0;
}
template <class T> inline int Iterator<T>::insert(T const &object)
{
// allocate store
T *store=new T;
if (!store) return 0;
// replace current with store
*store=object;
return insert(store);
}
template <class T> inline int Iterator<T>::insert(T *object)
{
// fail on null object
if (!object) return 0;
// create new node
List<T>::NODE *node=new List<T>::NODE;
if (!node) return 0;
// setup node
node->prev=CurrentNode->prev;
node->next=CurrentNode;
// insert node
CurrentNode->prev->next=node;
CurrentNode->prev=node;
// update current node
CurrentNode=node;
return 1;
}
template <class T> inline int Iterator<T>::remove()
{
// remove current node from list
if (CurrentNode->object)
{
CurrentNode->prev->next=CurrentNode->next;
CurrentNode->next->prev=CurrentNode->prev;
List<T>::NODE *old=CurrentNode;
CurrentNode=CurrentNode->prev;
delete old;
return 1;
}
else return 0;
}
template <class T> inline int Iterator<T>::free()
{
// remove current node from list
if (CurrentNode->object)
{
CurrentNode->prev->next=CurrentNode->next;
CurrentNode->next->prev=CurrentNode->prev;
List<T>::NODE *old=CurrentNode;
CurrentNode=CurrentNode->prev;
delete old->object;
delete old;
return 1;
}
else return 0;
}
#endif