libUTL++
Heap.h
1 #pragma once
2 
4 
5 #include <libutl/Collection.h>
6 #include <libutl/Vector.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
14 class HeapIt;
15 
17 
53 
55 class Heap : public Collection
56 {
58  friend class HeapIt;
59 
60 public:
61  typedef HeapIt iterator;
62 
63 public:
72  Heap(size_t size,
73  size_t increment = size_t_max,
74  bool owner = true,
75  bool multiSet = true,
76  Ordering* ordering = nullptr)
77  {
78  init(size, increment, owner, multiSet, ordering);
79  }
80 
86  Heap(bool owner, bool multiSet = true)
87  {
88  init(16, size_t_max, owner, multiSet);
89  }
90 
91  virtual void steal(Object& rhs);
92 
93  virtual size_t innerAllocatedSize() const;
94 
96 
97 
101  void
102  reserve(size_t newSize)
103  {
104  _array.grow(newSize);
105  }
107 
109 
110  virtual Object* find(const Object& key) const;
111 
112  iterator findIt(const Object& key) const;
113 
114  iterator findIt(const Object& key);
115 
116  void
117  findIt(const Object& key, BidIt& it) const
118  {
119  const_cast_this->findIt(key, it);
120  IFDEBUG(it.setConst(true));
121  }
122 
123  virtual void findIt(const Object& key, BidIt& it);
124 
126  Object*
127  getMax() const
128  {
129  if (_items == 0)
130  return nullptr;
131  else
132  return _array[0];
133  }
135 
137 
138  bool
139  add(const Object& object)
140  {
141  return super::add(object);
142  }
143 
144  virtual bool add(const Object* object);
145 
146  void
147  add(const Collection& collection)
148  {
149  super::add(collection);
150  }
152 
154 
155 
156  virtual void clear();
157 
159  Object* pop();
160 
161  bool
162  remove(const Object* key)
163  {
164  ASSERTD(key != nullptr);
165  return remove(*key);
166  }
167 
168  virtual bool remove(const Object& key);
169 
170  virtual void removeIt(BidIt& it);
172 
174 
175  inline iterator begin() const;
176 
177  inline iterator begin();
178 
179  inline virtual BidIt* beginNew() const;
180 
181  inline virtual BidIt* beginNew();
182 
183  inline iterator end() const;
184 
185  inline iterator end();
186 
187  inline virtual BidIt* endNew() const;
188 
189  inline virtual BidIt* endNew();
191 
192 private:
193  void init(size_t size = 16,
194  size_t increment = size_t_max,
195  bool owner = true,
196  bool multiSet = true,
197  Ordering* ordering = nullptr);
198 
199  void deInit();
200 
201  inline int
202  compareObjects(const Object* lhs, const Object* rhs) const
203  {
204  return super::compareObjects(lhs, rhs);
205  }
206 
207  void find(const Object& key, size_t& idx) const;
208 
209  void removeIdx(size_t idx);
210 
211  void swapUp(size_t idx);
212 
213  void swapDown(size_t idx);
214 
215 private:
216  Vector<Object*> _array;
217 };
218 
220 
221 UTL_NS_END;
222 
224 
225 #include <libutl/THeap.h>
Heap(size_t size, size_t increment=size_t_max, bool owner=true, bool multiSet=true, Ordering *ordering=nullptr)
Constructor.
Definition: Heap.h:72
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
void deInit()
De-initialize UTL++.
Object comparison abstraction.
Definition: Ordering.h:30
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Templated proxy for BidIt.
Definition: TBidIt.h:19
const size_t size_t_max
Maximum size_t value.
#define IFDEBUG(x)
Do x in DEBUG mode only.
Heap(bool owner, bool multiSet=true)
Constructor.
Definition: Heap.h:86
Heap data structure.
Definition: Heap.h:55
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
Bi-directional Heap iterator.
Definition: HeapIt.h:22
Object * getMax() const
Return the largest contained object (nullptr if empty()).
Definition: Heap.h:127
void reserve(size_t newSize)
Grow to the given size (at least).
Definition: Heap.h:102
Bi-directional iterator abstraction.
Definition: BidIt.h:25
A sequence of same-type objects.
Definition: Ordering.h:14
Root of UTL++ class hierarchy.
Definition: Object.h:52
void init()
Initialize UTL++.
#define ASSERTD
Do an assertion in DEBUG mode only.
A collection of objects.
Definition: Collection.h:62