00001 #ifndef __LOUT_CONTAINER_HH_
00002 #define __LOUT_CONTAINER_HH_
00003
00004 #include "object.hh"
00005
00018 namespace lout {
00019
00020 namespace container {
00021
00029 namespace untyped {
00030
00034 class Collection0: public object::Object
00035 {
00036 friend class Iterator;
00037
00038 protected:
00043 class AbstractIterator: public object::Object
00044 {
00045 private:
00046 int refcount;
00047
00048 public:
00049 AbstractIterator() { refcount = 1; }
00050
00051 void ref () { refcount++; }
00052 void unref () { refcount--; if (refcount == 0) delete this; }
00053
00054 virtual bool hasNext () = 0;
00055 virtual Object *getNext () = 0;
00056 };
00057
00058 protected:
00059 virtual AbstractIterator* createIterator() = 0;
00060 };
00061
00066 class Iterator
00067 {
00068 friend class Collection;
00069
00070 private:
00071 Collection0::AbstractIterator *impl;
00072
00073
00074 inline Iterator(Collection0::AbstractIterator *impl) { this->impl = impl; }
00075
00076 public:
00077 Iterator();
00078 Iterator(const Iterator &it2);
00079 Iterator(Iterator &it2);
00080 ~Iterator();
00081 Iterator &operator=(const Iterator &it2);
00082 Iterator &operator=(Iterator &it2);
00083
00084 inline bool hasNext() { return impl->hasNext(); }
00085 inline object::Object *getNext() { return impl->getNext(); }
00086 };
00087
00091 class Collection: public Collection0
00092 {
00093 public:
00094 void intoStringBuffer(misc::StringBuffer *sb);
00095 inline Iterator iterator() { Iterator it(createIterator()); return it; }
00096 };
00097
00098
00103 class Vector: public Collection
00104 {
00105 private:
00106 object::Object **array;
00107 int numAlloc, numElements;
00108 bool ownerOfObjects;
00109
00110 protected:
00111 AbstractIterator* createIterator();
00112
00113 public:
00114 Vector(int initSize, bool ownerOfObjects);
00115 ~Vector();
00116
00117 void put(object::Object *newElement, int newPos = -1);
00118 void insert(object::Object *newElement, int pos);
00119 void remove(int pos);
00120 inline object::Object *get(int pos)
00121 { return (pos >= 0 && pos < numElements) ? array[pos] : NULL; }
00122 inline int size() { return numElements; }
00123 void clear();
00124 void sort();
00125 };
00126
00127
00131 class List: public Collection
00132 {
00133 friend class ListIterator;
00134
00135 private:
00136 struct Node
00137 {
00138 public:
00139 object::Object *object;
00140 Node *next;
00141 };
00142
00143 class ListIterator: public AbstractIterator
00144 {
00145 private:
00146 List::Node *current;
00147 public:
00148 ListIterator(List::Node *node) { current = node; }
00149 bool hasNext();
00150 Object *getNext();
00151 };
00152
00153 Node *first, *last;
00154 bool ownerOfObjects;
00155 int numElements;
00156
00157 bool remove0(object::Object *element, bool compare, bool doNotDeleteAtAll);
00158
00159 protected:
00160 AbstractIterator* createIterator();
00161
00162 public:
00163 List(bool ownerOfObjects);
00164 ~List();
00165
00166 void clear();
00167 void append(object::Object *element);
00168 inline bool removeRef(object::Object *element)
00169 { return remove0(element, false, false); }
00170 inline bool remove(object::Object *element)
00171 { return remove0(element, true, false); }
00172 inline bool detachRef(object::Object *element)
00173 { return remove0(element, false, true); }
00174 inline int size() { return numElements; }
00175 inline bool isEmpty() { return numElements == 0; }
00176 inline object::Object *getFirst() { return first->object; }
00177 inline object::Object *getLast() { return last->object; }
00178 };
00179
00180
00184 class HashTable: public Collection
00185 {
00186 friend class HashTableIterator;
00187
00188 private:
00189 struct Node
00190 {
00191 object::Object *key, *value;
00192 Node *next;
00193 };
00194
00195 class HashTableIterator: public Collection0::AbstractIterator
00196 {
00197 private:
00198 HashTable *table;
00199 HashTable::Node *node;
00200 int pos;
00201
00202 void gotoNext();
00203
00204 public:
00205 HashTableIterator(HashTable *table);
00206 bool hasNext();
00207 Object *getNext();
00208 };
00209
00210 Node **table;
00211 int tableSize;
00212 bool ownerOfKeys, ownerOfValues;
00213
00214 private:
00215 inline int calcHashValue(object::Object *key)
00216 {
00217 return abs(key->hashValue()) % tableSize;
00218 }
00219
00220 protected:
00221 AbstractIterator* createIterator();
00222
00223 public:
00224 HashTable(bool ownerOfKeys, bool ownerOfValues, int tableSize = 251);
00225 ~HashTable();
00226
00227 void intoStringBuffer(misc::StringBuffer *sb);
00228
00229 void put (object::Object *key, object::Object *value);
00230 bool contains (object::Object *key);
00231 Object *get (object::Object *key);
00232 bool remove (object::Object *key);
00233 Object *getKey (Object *key);
00234 };
00235
00242 class Stack: public Collection
00243 {
00244 friend class StackIterator;
00245
00246 private:
00247 class Node
00248 {
00249 public:
00250 object::Object *object;
00251 Node *prev;
00252 };
00253
00254 class StackIterator: public AbstractIterator
00255 {
00256 private:
00257 Stack::Node *current;
00258 public:
00259 StackIterator(Stack::Node *node) { current = node; }
00260 bool hasNext();
00261 Object *getNext();
00262 };
00263
00264 Node *bottom, *top;
00265 bool ownerOfObjects;
00266 int numElements;
00267
00268 protected:
00269 AbstractIterator* createIterator();
00270
00271 public:
00272 Stack (bool ownerOfObjects);
00273 ~Stack();
00274
00275 void push (object::Object *object);
00276 void pushUnder (object::Object *object);
00277 inline object::Object *getTop () { return top ? top->object : NULL; }
00278 void pop ();
00279 inline int size() { return numElements; }
00280 };
00281
00282 }
00283
00291 namespace typed {
00292
00293 template <class T> class Collection;
00294
00298 template <class T> class Iterator
00299 {
00300 friend class Collection<T>;
00301
00302 private:
00303 untyped::Iterator base;
00304
00305 public:
00306 inline Iterator() { }
00307 inline Iterator(const Iterator<T> &it2) { this->base = it2.base; }
00308 inline Iterator(Iterator<T> &it2) { this->base = it2.base; }
00309 ~Iterator() { }
00310 inline Iterator &operator=(const Iterator<T> &it2)
00311 { this->base = it2.base; return *this; }
00312 inline Iterator &operator=(Iterator<T> &it2)
00313 { this->base = it2.base; return *this; }
00314
00315 inline bool hasNext() { return this->base.hasNext(); }
00316 inline T *getNext() { return (T*)this->base.getNext(); }
00317 };
00318
00325 template <class T> class Collection: public object::Object
00326 {
00327 protected:
00328 untyped::Collection *base;
00329
00330 public:
00331 void intoStringBuffer(misc::StringBuffer *sb)
00332 { this->base->intoStringBuffer(sb); }
00333
00334 inline Iterator<T> iterator() {
00335 Iterator<T> it; it.base = this->base->iterator(); return it; }
00336 };
00337
00338
00342 template <class T> class Vector: public Collection <T>
00343 {
00344 public:
00345 inline Vector(int initSize, bool ownerOfObjects) {
00346 this->base = new untyped::Vector(initSize, ownerOfObjects); }
00347 ~Vector() { delete this->base; }
00348
00349 inline void put(T *newElement, int newPos = -1)
00350 { ((untyped::Vector*)this->base)->put(newElement, newPos); }
00351 inline void insert(T *newElement, int pos)
00352 { ((untyped::Vector*)this->base)->insert(newElement, pos); }
00353 inline void remove(int pos) { ((untyped::Vector*)this->base)->remove(pos); }
00354 inline T *get(int pos)
00355 { return (T*)((untyped::Vector*)this->base)->get(pos); }
00356 inline int size() { return ((untyped::Vector*)this->base)->size(); }
00357 inline void clear() { ((untyped::Vector*)this->base)->clear(); }
00358 inline void sort() { ((untyped::Vector*)this->base)->sort(); }
00359 };
00360
00361
00365 template <class T> class List: public Collection <T>
00366 {
00367 public:
00368 inline List(bool ownerOfObjects)
00369 { this->base = new untyped::List(ownerOfObjects); }
00370 ~List() { delete this->base; }
00371
00372 inline void clear() { ((untyped::List*)this->base)->clear(); }
00373 inline void append(T *element)
00374 { ((untyped::List*)this->base)->append(element); }
00375 inline bool removeRef(T *element) {
00376 return ((untyped::List*)this->base)->removeRef(element); }
00377 inline bool remove(T *element) {
00378 return ((untyped::List*)this->base)->remove(element); }
00379 inline bool detachRef(T *element) {
00380 return ((untyped::List*)this->base)->detachRef(element); }
00381
00382 inline int size() { return ((untyped::List*)this->base)->size(); }
00383 inline bool isEmpty()
00384 { return ((untyped::List*)this->base)->isEmpty(); }
00385 inline T *getFirst()
00386 { return (T*)((untyped::List*)this->base)->getFirst(); }
00387 inline T *getLast()
00388 { return (T*)((untyped::List*)this->base)->getLast(); }
00389 };
00390
00391
00395 template <class K, class V> class HashTable: public Collection <K>
00396 {
00397 public:
00398 inline HashTable(bool ownerOfKeys, bool ownerOfValues, int tableSize = 251)
00399 { this->base = new untyped::HashTable(ownerOfKeys, ownerOfValues,
00400 tableSize); }
00401 ~HashTable() { delete this->base; }
00402
00403 inline void put(K *key, V *value)
00404 { return ((untyped::HashTable*)this->base)->put(key, value); }
00405 inline bool contains(K *key)
00406 { return ((untyped::HashTable*)this->base)->contains(key); }
00407 inline V *get(K *key)
00408 { return (V*)((untyped::HashTable*)this->base)->get(key); }
00409 inline bool remove(K *key)
00410 { return ((untyped::HashTable*)this->base)->remove(key); }
00411 inline K *getKey(K *key)
00412 { return (K*)((untyped::HashTable*)this->base)->getKey(key); }
00413 };
00414
00418 template <class T> class Stack: public Collection <T>
00419 {
00420 public:
00421 inline Stack (bool ownerOfObjects)
00422 { this->base = new untyped::Stack (ownerOfObjects); }
00423 ~Stack() { delete this->base; }
00424
00425 inline void push (T *object) {
00426 ((untyped::Stack*)this->base)->push (object); }
00427 inline void pushUnder (T *object)
00428 { ((untyped::Stack*)this->base)->pushUnder (object); }
00429 inline T *getTop ()
00430 { return (T*)((untyped::Stack*)this->base)->getTop (); }
00431 inline void pop () { ((untyped::Stack*)this->base)->pop (); }
00432 inline int size() { return ((untyped::Stack*)this->base)->size(); }
00433 };
00434
00435 }
00436
00437 }
00438
00439 }
00440
00441 #endif // __LOUT_CONTAINER_HH_