Dillo
container.hh
Go to the documentation of this file.
1 #ifndef __LOUT_CONTAINER_HH_
2 #define __LOUT_CONTAINER_HH_
3 
4 #include "object.hh"
5 
6 namespace lout {
7 
20 namespace container {
21 
29 namespace untyped {
30 
35 {
36  friend class Iterator;
37 
38 protected:
44  {
45  private:
46  int refcount;
47 
48  public:
49  AbstractIterator() { refcount = 1; }
50 
51  void ref () { refcount++; }
52  void unref () { refcount--; if (refcount == 0) delete this; }
53 
54  virtual bool hasNext () = 0;
55  virtual Object *getNext () = 0;
56  };
57 
58 protected:
59  virtual AbstractIterator* createIterator() = 0;
60 };
61 
66 class Iterator
67 {
68  friend class Collection;
69 
70 private:
72 
73  // Should not instantiated directly.
74  inline Iterator(Collection0::AbstractIterator *impl) { this->impl = impl; }
75 
76 public:
77  Iterator();
78  Iterator(const Iterator &it2);
79  Iterator(Iterator &it2);
80  ~Iterator();
81  Iterator &operator=(const Iterator &it2);
83 
84  inline bool hasNext() { return impl->hasNext(); }
85  inline object::Object *getNext() { return impl->getNext(); }
86 };
87 
91 class Collection: public Collection0
92 {
93 public:
95  inline Iterator iterator() { Iterator it(createIterator()); return it; }
96  virtual int size() = 0;
97 };
98 
99 
104 class Vector: public Collection
105 {
106  friend class VectorIterator;
107 
108 private:
112 
114  {
115  private:
117  int index;
118 
119  public:
120  VectorIterator(Vector *vector) { this->vector = vector; index = -1; }
121  bool hasNext();
122  Object *getNext();
123  };
124 
125 protected:
126  AbstractIterator* createIterator();
127 
128 public:
129  Vector(int initSize, bool ownerOfObjects);
130  ~Vector();
131 
132  int size();
133 
134  void put(object::Object *newElement, int newPos = -1);
135  void insert(object::Object *newElement, int pos);
136 
143  inline int insertSorted(object::Object *newElement,
144  object::Comparator *comparator =
146  { int pos = bsearch (newElement, false, comparator);
147  insert (newElement, pos); return pos; }
148 
149  void remove(int pos);
150  inline object::Object *get(int pos) const
151  { return (pos >= 0 && pos < numElements) ? array[pos] : NULL; }
152  void clear();
154  int bsearch(Object *key, bool mustExist, int start, int end,
156  inline int bsearch(Object *key, bool mustExist,
157  object::Comparator *comparator =
159  { return bsearch (key, mustExist, 0, size () - 1, comparator); }
160 };
161 
162 
166 class List: public Collection
167 {
168  friend class ListIterator;
169 
170 private:
171  struct Node
172  {
173  public:
176  };
177 
178  class ListIterator: public AbstractIterator
179  {
180  private:
182  public:
183  ListIterator(List::Node *node) { current = node; }
184  bool hasNext();
185  Object *getNext();
186  };
187 
191 
192  bool remove0(object::Object *element, bool compare, bool doNotDeleteAtAll);
193 
194 protected:
196 
197 public:
198  List(bool ownerOfObjects);
199  ~List();
200 
201  bool equals(Object *other);
202  int hashValue();
203 
204  int size ();
205 
206  void clear();
207  void append(object::Object *element);
208  bool insertBefore(object::Object *beforeThis, object::Object *neew);
209  inline bool removeRef(object::Object *element)
210  { return remove0(element, false, false); }
211  inline bool remove(object::Object *element)
212  { return remove0(element, true, false); }
213  inline bool detachRef(object::Object *element)
214  { return remove0(element, false, true); }
215  inline int size() const { return numElements; }
216  inline bool isEmpty() const { return numElements == 0; }
217  inline object::Object *getFirst() const { return first->object; }
218  inline object::Object *getLast() const { return last->object; }
219 };
220 
221 
225 class HashSet: public Collection
226 {
227  friend class HashSetIterator;
228 
229 protected:
230  struct Node
231  {
234  };
235 
239 
240  inline int calcHashValue(object::Object *object) const
241  {
242  return abs(object->hashValue()) % tableSize;
243  }
244 
245  virtual Node *createNode();
246  virtual void clearNode(Node *node);
247 
248  Node *findNode(object::Object *object) const;
249  Node *insertNode(object::Object *object);
250 
251  AbstractIterator* createIterator();
252 
253 private:
255  {
256  private:
259  int pos;
260 
261  void gotoNext();
262 
263  public:
264  HashSetIterator(HashSet *set);
265  bool hasNext();
266  Object *getNext();
267  };
268 
269 public:
270  HashSet(bool ownerOfObjects, int tableSize = 251);
271  ~HashSet();
272 
273  int size ();
274 
275  void put (object::Object *object);
276  bool contains (object::Object *key) const;
277  bool remove (object::Object *key);
278  //Object *getReference (object::Object *object);
279 };
280 
284 class HashTable: public HashSet
285 {
286 private:
288 
289  struct KeyValuePair: public Node
290  {
292  };
293 
294 protected:
295  Node *createNode();
296  void clearNode(Node *node);
297 
298 public:
299  HashTable(bool ownerOfKeys, bool ownerOfValues, int tableSize = 251);
300  ~HashTable();
301 
303 
304  void put (object::Object *key, object::Object *value);
305  object::Object *get (object::Object *key) const;
306 };
307 
315 class Stack: public Collection
316 {
317  friend class StackIterator;
318 
319 private:
320  class Node
321  {
322  public:
325  };
326 
328  {
329  private:
331  public:
332  StackIterator(Stack::Node *node) { current = node; }
333  bool hasNext();
334  Object *getNext();
335  };
336 
340 
341 protected:
343 
344 public:
345  Stack (bool ownerOfObjects);
346  ~Stack();
347 
348  int size ();
349 
350  void push (object::Object *object);
351  void pushUnder (object::Object *object);
352  inline object::Object *getTop () const { return top ? top->object : NULL; }
353  void pop ();
354  inline int size() const { return numElements; }
355 };
356 
357 } // namespace untyped
358 
366 namespace typed {
367 
368 template <class T> class Collection;
369 
373 template <class T> class Iterator
374 {
375  friend class Collection<T>;
376 
377 private:
379 
380 public:
381  inline Iterator() { }
382  inline Iterator(const Iterator<T> &it2) { this->base = it2.base; }
383  inline Iterator(Iterator<T> &it2) { this->base = it2.base; }
384  ~Iterator() { }
385  inline Iterator &operator=(const Iterator<T> &it2)
386  { this->base = it2.base; return *this; }
388  { this->base = it2.base; return *this; }
389 
390  inline bool hasNext() { return this->base.hasNext(); }
391  inline T *getNext() { return (T*)this->base.getNext(); }
392 };
393 
400 template <class T> class Collection: public object::Object
401 {
402 protected:
404 
405 public:
406  Collection () { this->base = NULL; }
407  ~Collection () { if (this->base) delete this->base; }
408 
409  bool equals(Object *other)
410  { return this->base->equals (((Collection<T>*)other)->base); }
411 
412  int hashValue() { return this->base->hashValue (); }
413 
415  { this->base->intoStringBuffer(sb); }
417  Iterator<T> it; it.base = this->base->iterator(); return it; }
418  inline int size() { return this->base->size (); }
419 };
420 
421 
425 template <class T> class Vector: public Collection <T>
426 {
427 public:
428  inline Vector(int initSize, bool ownerOfObjects) {
429  this->base = new untyped::Vector(initSize, ownerOfObjects); }
430 
431  inline void put(T *newElement, int newPos = -1)
432  { ((untyped::Vector*)this->base)->put(newElement, newPos); }
433  inline void insert(T *newElement, int pos)
434  { ((untyped::Vector*)this->base)->insert(newElement, pos); }
435  inline int insertSorted(T *newElement,
436  object::Comparator *comparator =
438  { return ((untyped::Vector*)this->base)->insertSorted(newElement,
439  comparator); }
440  inline void remove(int pos) { ((untyped::Vector*)this->base)->remove(pos); }
441  inline T *get(int pos) const
442  { return (T*)((untyped::Vector*)this->base)->get(pos); }
443  inline void clear() { ((untyped::Vector*)this->base)->clear(); }
444  inline void sort(object::Comparator *comparator =
446  { ((untyped::Vector*)this->base)->sort(comparator); }
447  inline int bsearch(T *key, bool mustExist, int start, int end,
448  object::Comparator *comparator =
450  { return ((untyped::Vector*)this->base)->bsearch(key, mustExist, start, end,
451  comparator); }
452  inline int bsearch(T *key, bool mustExist,
453  object::Comparator *comparator =
455  { return ((untyped::Vector*)this->base)->bsearch(key, mustExist,
456  comparator); }
457 };
458 
459 
463 template <class T> class List: public Collection <T>
464 {
465 public:
466  inline List(bool ownerOfObjects)
467  { this->base = new untyped::List(ownerOfObjects); }
468 
469  inline void clear() { ((untyped::List*)this->base)->clear(); }
470  inline void append(T *element)
471  { ((untyped::List*)this->base)->append(element); }
472  inline bool insertBefore(object::Object *beforeThis, object::Object *neew)
473  { return ((untyped::List*)this->base)->insertBefore(beforeThis, neew); }
474  inline bool removeRef(T *element) {
475  return ((untyped::List*)this->base)->removeRef(element); }
476  inline bool remove(T *element) {
477  return ((untyped::List*)this->base)->remove(element); }
478  inline bool detachRef(T *element) {
479  return ((untyped::List*)this->base)->detachRef(element); }
480 
481  inline bool isEmpty() const
482  { return ((untyped::List*)this->base)->isEmpty(); }
483  inline T *getFirst() const
484  { return (T*)((untyped::List*)this->base)->getFirst(); }
485  inline T *getLast() const
486  { return (T*)((untyped::List*)this->base)->getLast(); }
487 };
488 
492 template <class T> class HashSet: public Collection <T>
493 {
494 protected:
495  inline HashSet() { }
496 
497 public:
498  inline HashSet(bool owner, int tableSize = 251)
499  { this->base = new untyped::HashSet(owner, tableSize); }
500 
501  inline void put(T *object)
502  { return ((untyped::HashSet*)this->base)->put(object); }
503  inline bool contains(T *object) const
504  { return ((untyped::HashSet*)this->base)->contains(object); }
505  inline bool remove(T *object)
506  { return ((untyped::HashSet*)this->base)->remove(object); }
507  //inline T *getReference(T *object)
508  //{ return (T*)((untyped::HashSet*)this->base)->getReference(object); }
509 };
510 
514 template <class K, class V> class HashTable: public HashSet <K>
515 {
516 public:
517  inline HashTable(bool ownerOfKeys, bool ownerOfValues, int tableSize = 251)
518  { this->base = new untyped::HashTable(ownerOfKeys, ownerOfValues,
519  tableSize); }
520 
521  inline void put(K *key, V *value)
522  { return ((untyped::HashTable*)this->base)->put(key, value); }
523  inline V *get(K *key) const
524  { return (V*)((untyped::HashTable*)this->base)->get(key); }
525 };
526 
530 template <class T> class Stack: public Collection <T>
531 {
532 public:
533  inline Stack (bool ownerOfObjects)
534  { this->base = new untyped::Stack (ownerOfObjects); }
535 
536  inline void push (T *object) {
537  ((untyped::Stack*)this->base)->push (object); }
538  inline void pushUnder (T *object)
539  { ((untyped::Stack*)this->base)->pushUnder (object); }
540  inline T *getTop () const
541  { return (T*)((untyped::Stack*)this->base)->getTop (); }
542  inline void pop () { ((untyped::Stack*)this->base)->pop (); }
543 };
544 
545 } // namespace untyped
546 
547 } // namespace container
548 
549 } // namespace lout
550 
551 #endif // __LOUT_CONTAINER_HH_
void insert(object::Object *newElement, int pos)
Definition: container.cc:167
int size() const
Definition: container.hh:354
Stack::Node * current
Definition: container.hh:330
~Iterator()
Definition: container.hh:384
HashSet(bool owner, int tableSize=251)
Definition: container.hh:498
int calcHashValue(object::Object *object) const
Definition: container.hh:240
Definition: container.hh:171
Iterator & operator=(Iterator< T > &it2)
Definition: container.hh:387
bool isEmpty() const
Definition: container.hh:481
Typed version of container::untyped::Vector.
Definition: container.hh:425
void pop()
Definition: container.cc:761
Node * top
Definition: container.hh:337
virtual bool equals(Object *other)
Returns, whether two objects are equal.
Definition: object.cc:50
int index
Definition: container.hh:117
A class for fast concatenation of a large number of strings.
Definition: misc.hh:565
VectorIterator(Vector *vector)
Definition: container.hh:120
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: container.cc:309
void push(object::Object *object)
Definition: container.cc:733
object::Object * object
Definition: container.hh:323
Collection()
Definition: container.hh:406
Typed version of container::untyped::HashTable.
Definition: container.hh:514
int insertSorted(T *newElement, object::Comparator *comparator=&object::standardComparator)
Definition: container.hh:435
untyped::Iterator base
Definition: container.hh:378
HashTable(bool ownerOfKeys, bool ownerOfValues, int tableSize=251)
Definition: container.cc:631
bool ownerOfObjects
Definition: container.hh:111
Typed version of container::untyped::Stack.
Definition: container.hh:530
int size()
Definition: container.cc:123
Node * createNode()
Definition: container.cc:653
Node * insertNode(object::Object *object)
Definition: container.cc:506
...
Definition: container.hh:34
HashSet(bool ownerOfObjects, int tableSize=251)
Definition: container.cc:440
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: container.cc:670
object::Object * getFirst() const
Definition: container.hh:217
Definition: container.hh:320
bool isEmpty() const
Definition: container.hh:216
int size()
Definition: container.cc:477
bool removeRef(T *element)
Definition: container.hh:474
This is the base class for many other classes, which defines very common virtual methods.
Definition: object.hh:24
Node ** table
Definition: container.hh:236
int bsearch(Object *key, bool mustExist, int start, int end, object::Comparator *comparator=&object::standardComparator)
Use binary search to find an element in a sorted vector.
Definition: container.cc:216
int numElements
Definition: container.hh:110
HashSet()
Definition: container.hh:495
int size()
Definition: container.hh:418
Collection0::AbstractIterator * impl
Definition: container.hh:71
int size()
Definition: container.cc:304
void clear()
Definition: container.cc:330
Iterator(const Iterator< T > &it2)
Definition: container.hh:382
int bsearch(Object *key, bool mustExist, object::Comparator *comparator=&object::standardComparator)
Definition: container.hh:156
The base class for all iterators, as created by container::untyped::Collection::createIterator.
Definition: container.hh:43
void gotoNext()
Definition: container.cc:591
Abstract base class for all container objects in container::untyped.
Definition: container.hh:91
T * getLast() const
Definition: container.hh:485
Used for other orders as the one defined by Comparable.
Definition: object.hh:66
void put(T *newElement, int newPos=-1)
Definition: container.hh:431
int insertSorted(object::Object *newElement, object::Comparator *comparator=&object::standardComparator)
Insert into an already sorted vector.
Definition: container.hh:143
Iterator(Collection0::AbstractIterator *impl)
Definition: container.hh:74
Iterator< T > iterator()
Definition: container.hh:416
int numElements
Definition: container.hh:190
~List()
Definition: container.cc:299
void put(T *object)
Definition: container.hh:501
bool insertBefore(object::Object *beforeThis, object::Object *neew)
Definition: container.hh:472
bool detachRef(object::Object *element)
Definition: container.hh:213
StackIterator(Stack::Node *node)
Definition: container.hh:332
AbstractIterator * createIterator()
Definition: container.cc:283
Container, which is implemented by an array, which is dynamically resized.
Definition: container.hh:104
object::Object * getLast() const
Definition: container.hh:218
bool ownerOfObjects
Definition: container.hh:238
int tableSize
Definition: container.hh:237
Iterator()
Definition: container.cc:39
~Collection()
Definition: container.hh:407
HashSet::Node * node
Definition: container.hh:258
AbstractIterator * createIterator()
Definition: container.cc:430
Object * getNext()
Definition: container.cc:270
Iterator()
Definition: container.hh:381
int bsearch(T *key, bool mustExist, int start, int end, object::Comparator *comparator=&object::standardComparator)
Definition: container.hh:447
~Vector()
Definition: container.cc:115
object::Object * object
Definition: container.hh:174
Node * next
Definition: container.hh:175
int bsearch(T *key, bool mustExist, object::Comparator *comparator=&object::standardComparator)
Definition: container.hh:452
void sort(object::Comparator *comparator=&object::standardComparator)
Definition: container.hh:444
Object * getNext()
Definition: container.cc:412
Typed version of container::untyped::HashSet.
Definition: container.hh:492
void clear()
Definition: container.hh:443
Definition: container.hh:230
Node * first
Definition: container.hh:188
Vector(int initSize, bool ownerOfObjects)
Definition: container.cc:105
int hashValue()
Return a hash value for the object.
Definition: container.cc:322
void pushUnder(T *object)
Definition: container.hh:538
Vector(int initSize, bool ownerOfObjects)
Definition: container.hh:428
bool detachRef(T *element)
Definition: container.hh:478
Object * getNext()
Definition: container.cc:776
virtual void clearNode(Node *node)
Definition: container.cc:487
~HashTable()
Definition: container.cc:637
int hashValue()
Return a hash value for the object.
Definition: container.hh:412
void clear()
Definition: container.cc:156
AbstractIterator * createIterator()
Definition: container.cc:794
void push(T *object)
Definition: container.hh:536
A hash set.
Definition: container.hh:225
Abstract base class template for all container objects in container::typed.
Definition: container.hh:368
void put(K *key, V *value)
Definition: container.hh:521
object::Object * getNext()
Definition: container.hh:85
~Stack()
Definition: container.cc:722
int size()
Definition: container.cc:728
List(bool ownerOfObjects)
Definition: container.hh:466
Definition: container.cc:27
Node * findNode(object::Object *object) const
Definition: container.cc:495
Vector * vector
Definition: container.hh:116
object::Object * getTop() const
Definition: container.hh:352
Node * bottom
Definition: container.hh:337
bool ownerOfObjects
Definition: container.hh:338
void put(object::Object *object)
Definition: container.cc:526
T * getTop() const
Definition: container.hh:540
HashSet * set
Definition: container.hh:257
bool remove0(object::Object *element, bool compare, bool doNotDeleteAtAll)
Definition: container.cc:382
virtual int hashValue()
Return a hash value for the object.
Definition: object.cc:59
object::Object ** array
Definition: container.hh:109
Iterator(Iterator< T > &it2)
Definition: container.hh:383
void append(object::Object *element)
Definition: container.cc:344
StandardComparator standardComparator
Definition: object.cc:148
HashSetIterator(HashSet *set)
Definition: container.cc:583
bool ownerOfValues
Definition: container.hh:287
bool hasNext()
Definition: container.hh:390
T * getNext()
Definition: container.hh:391
T * getFirst() const
Definition: container.hh:483
Node * last
Definition: container.hh:188
bool hasNext()
Definition: container.cc:278
Object * getNext()
Definition: container.cc:605
untyped::Collection * base
Definition: container.hh:403
bool hasNext()
Definition: container.cc:617
Iterator & operator=(const Iterator &it2)
Definition: container.cc:58
ListIterator(List::Node *node)
Definition: container.hh:183
void sort(object::Comparator *comparator=&object::standardComparator)
Definition: container.cc:201
void append(T *element)
Definition: container.hh:470
int numElements
Definition: container.hh:339
A single-linked list.
Definition: container.hh:166
List(bool ownerOfObjects)
Definition: container.cc:292
Stack(bool ownerOfObjects)
Definition: container.hh:533
Node * prev
Definition: container.hh:324
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: container.cc:88
AbstractIterator * createIterator()
Definition: container.cc:622
bool hasNext()
Definition: container.cc:425
virtual AbstractIterator * createIterator()=0
Node * next
Definition: container.hh:233
HashTable(bool ownerOfKeys, bool ownerOfValues, int tableSize=251)
Definition: container.hh:517
void put(object::Object *newElement, int newPos=-1)
Definition: container.cc:128
Iterator & operator=(const Iterator< T > &it2)
Definition: container.hh:385
Iterator iterator()
Definition: container.hh:95
void insert(T *newElement, int pos)
Definition: container.hh:433
void clearNode(Node *node)
Definition: container.cc:658
bool contains(T *object) const
Definition: container.hh:503
A stack (LIFO). Can be used as Queue (FIFO) when pushUnder() is used instead of push().
Definition: container.hh:315
bool ownerOfObjects
Definition: container.hh:189
void pushUnder(object::Object *object)
Definition: container.cc:746
int numElements
Definition: container.hh:237
int numAlloc
Definition: container.hh:110
virtual Node * createNode()
Definition: container.cc:482
Typed version of container::untyped::Iterator.
Definition: container.hh:373
This is a small wrapper for AbstractIterator, which may be used directly, not as a pointer...
Definition: container.hh:66
bool equals(Object *other)
Returns, whether two objects are equal.
Definition: container.hh:409
bool hasNext()
Definition: container.hh:84
void clear()
Definition: container.hh:469
~Iterator()
Definition: container.cc:78
~HashSet()
Definition: container.cc:452
bool insertBefore(object::Object *beforeThis, object::Object *neew)
Definition: container.cc:359
bool contains(object::Object *key) const
Definition: container.cc:531
void pop()
Definition: container.hh:542
A hash table.
Definition: container.hh:284
bool removeRef(object::Object *element)
Definition: container.hh:209
Typed version of container::untyped::List.
Definition: container.hh:463
bool hasNext()
Definition: container.cc:789
void intoStringBuffer(misc::StringBuffer *sb)
Store a textual representation of the object in a misc::StringBuffer.
Definition: container.hh:414
Stack(bool ownerOfObjects)
Definition: container.cc:715
void put(object::Object *key, object::Object *value)
Definition: container.cc:696
object::Object * value
Definition: container.hh:291
object::Object * object
Definition: container.hh:232
List::Node * current
Definition: container.hh:181
int size() const
Definition: container.hh:215