00001 // 00002 // List.h 00003 // 00004 // List: A List class which holds objects of type Object. 00005 // 00006 // Part of the ht://Dig package <http://www.htdig.org/> 00007 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group 00008 // For copyright details, see the file COPYING in your distribution 00009 // or the GNU General Public License version 2 or later 00010 // <http://www.gnu.org/copyleft/gpl.html> 00011 // 00012 // $Id: List_8h-source.html,v 1.1 2008/06/08 10:12:54 sebdiaz Exp $ 00013 // 00014 00015 #ifndef _List_h_ 00016 #define _List_h_ 00017 00018 #include "Object.h" 00019 00020 // 00021 // Behaviour of the Remove method. See comment before method 00022 // declaration for more information. 00023 // 00024 #define LIST_REMOVE_DESTROY 1 00025 #define LIST_REMOVE_RELEASE 2 00026 00027 class List; 00028 class listnode; 00029 00030 class ListCursor { 00031 public: 00032 ListCursor() { current = 0; current_index = -1; } 00033 void Clear() { current = 0; current_index = -1; } 00034 00035 // 00036 // Support for the Start_Get and Get_Next routines 00037 // 00038 listnode *current; 00039 int current_index; 00040 }; 00041 00042 class List : public Object 00043 { 00044 public: 00045 // 00046 // Constructor/Destructor 00047 // 00048 List(); 00049 virtual ~List(); 00050 00051 // 00052 // Insert at beginning of list. 00053 // 00054 virtual void Unshift(Object *o) { Insert(o, 0); } 00055 // 00056 // Remove from the beginning of the list and return the 00057 // object. 00058 // 00059 virtual Object* Shift(int action = LIST_REMOVE_DESTROY) { 00060 Object* o = Nth(0); 00061 if(Remove(0, action) == NOTOK) return 0; 00062 return o; 00063 } 00064 // 00065 // Append an Object to the end of the list 00066 // 00067 virtual void Push(Object *o) { Add(o); } 00068 // 00069 // Remove the last object from the list and return it. 00070 // 00071 virtual Object *Pop(int action = LIST_REMOVE_DESTROY); 00072 00073 // 00074 // Add() will append an Object to the end of the list 00075 // 00076 virtual void Add(Object *); 00077 00078 // 00079 // Insert() will insert an object at the given position. If the 00080 // position is larger than the number of objects in the list, the 00081 // object is appended; no new objects are created between the end 00082 // of the list and the given position. 00083 // 00084 virtual void Insert(Object *, int position); 00085 00086 // 00087 // Assign() will replace the object already at the given position 00088 // with the new object. If there is no object at the position,the 00089 // list is extended with nil objects until the position is reached 00090 // and then the given object is put there. (This really makes the 00091 // List analogous to a dynamic array...) 00092 // 00093 virtual void Assign(Object *, int position); 00094 00095 // 00096 // Find the given object in the list and remove it from the list. 00097 // The object will NOT be deleted. If the object is not found, 00098 // NOTOK will be returned, else OK. 00099 // 00100 virtual int Remove(Object *); 00101 00102 // 00103 // Remove object at position from the list. If action is 00104 // LIST_REMOVE_DESTROY delete the object stored at position. 00105 // If action is LIST_REMOVE_RELEASE the object is not deleted. 00106 // If the object is not found, NOTOK will be returned, else OK. 00107 // 00108 virtual int Remove(int position, int action = LIST_REMOVE_DESTROY); 00109 00110 // 00111 // Release() will set the list to empty. This call will NOT 00112 // delete objects that were in the list before this call. 00113 // 00114 virtual void Release(); 00115 00116 // 00117 // Destroy() will delete all the objects in the list. This is 00118 // equivalent to calling the destructor 00119 // 00120 virtual void Destroy(); 00121 00122 // 00123 // List traversel 00124 // 00125 void Start_Get() { Start_Get(cursor); } 00126 void Start_Get(ListCursor& cursor0) const { cursor0.current = head; cursor0.current_index = -1;} 00127 Object *Get_Next() { return Get_Next(cursor); } 00128 Object *Get_Next(ListCursor& cursor) const; 00129 Object *Get_First(); 00130 Object *Next(Object *current); 00131 Object *Previous(Object *current); 00132 Object *Last(); 00133 00134 // 00135 // Direct access to list items. This can only be used to retrieve 00136 // objects from the list. To assign new objects, use Insert(), 00137 // Add(), or Assign(). 00138 // 00139 Object *operator[] (int n) { return Nth(n); } 00140 const Object *operator[] (int n) const { return Nth(((List*)this)->cursor, n); } 00141 const Object *Nth(ListCursor& cursor, int n) const; 00142 const Object *Nth(int n) const { return Nth(((List*)this)->cursor, n); } 00143 Object *Nth(int n) { return (Object*)((List*)this)->Nth(((List*)this)->cursor, n); } 00144 00145 // 00146 // Access to the number of elements 00147 // 00148 int Count() const { return number; } 00149 00150 // 00151 // Get the index number of an object. If the object is not found, 00152 // returnes -1 00153 // 00154 int Index(Object *); 00155 00156 // 00157 // Deep copy member function 00158 // 00159 Object *Copy() const; 00160 00161 // 00162 // Assignment 00163 // 00164 List &operator= (List *list) {return *this = *list;} 00165 List &operator= (List &list); 00166 00167 // Move one list to the end of another, emptying the other list. 00168 void AppendList (List &list); 00169 00170 protected: 00171 // 00172 // Pointers into the list 00173 // 00174 listnode *head; 00175 listnode *tail; 00176 00177 // 00178 // For list traversal it is nice to know where we are... 00179 // 00180 ListCursor cursor; 00181 00182 // 00183 // Its nice to keep track of how many things we contain... 00184 // 00185 int number; 00186 }; 00187 00188 #endif