KrisLibrary  1.0.0
FixedSizeHeap.h
1 #ifndef FIXED_SIZE_HEAP_H
2 #define FIXED_SIZE_HEAP_H
3 
4 #include <KrisLibrary/Logger.h>
5 #include <vector>
6 #include <iostream>
7 #include <KrisLibrary/errors.h>
8 
12 template <class ptype>
14 {
15 public:
16  FixedSizeHeap(int maxValue)
17  :objectToHeapIndex(maxValue),h(1)
18  {
19  for(int i=0;i<maxValue;i++)
20  objectToHeapIndex[i]=0;
21  h.reserve(maxValue+1);
22  }
23 
25  :h(1)
26  {
27  }
28 
29  void init(int maxValue)
30  {
31  objectToHeapIndex.resize(maxValue);
32  std::fill(objectToHeapIndex.begin(),objectToHeapIndex.end(),0);
33  h.resize(1);
34  h.reserve(maxValue+1);
35  }
36 
37  void increaseCapacity(int maxValue)
38  {
39  objectToHeapIndex.resize(maxValue,0);
40  h.reserve(maxValue+1);
41  }
42 
43  inline int top() const { return h[1].x; }
44 
45  inline ptype topPriority() const { return h[1].p; }
46 
47  inline ptype priority(int item) const { return h[objectToHeapIndex[item]].p; }
48 
49  void pop()
50  {
51  Assert(!empty());
52  objectToHeapIndex[h[1].x]=0;
53  h[1]=h.back();
54  h.resize(h.size()-1);
55  if(h.size()>1) heapifyDown(1);
56  }
57 
58  void push(int x,const ptype& p)
59  {
60  Assert(objectToHeapIndex[x]==0);
61  objectToHeapIndex[x] = (int)h.size();
62  item it;
63  it.x=x;
64  it.p=p;
65  h.push_back(it);
66  heapifyUp((int)h.size()-1);
67  }
68 
69  int find(const int x) const
70  {
71  Assert(x >= 0 && x < (int)objectToHeapIndex.size());
72  return objectToHeapIndex[x];
73  }
74 
75 
76  void adjust(const int x, const ptype& p)
77  {
78  int i=find(x);
79  if(i) adjustByHeapIndex(i,p);
80  else push(x,p);
81  }
82 
83  void adjustByHeapIndex(int i,const ptype& p)
84  {
85  Assert(i>=1 && i<=size());
86  if(h[i].p < p) { //increase
87  h[i].p=p;
88  heapifyUp(i);
89  }
90  else { //decrease
91  h[i].p=p;
92  heapifyDown(i);
93  }
94  }
95 
96  inline void clear() { h.resize(1); std::fill(objectToHeapIndex.begin(),objectToHeapIndex.end(),0); }
97  inline bool empty() const { return h.size()==1; }
98  inline int size() const { return (int)h.size()-1; }
99  inline int maxObjects() const { return (int)objectToHeapIndex.size(); }
100 
101  bool isHeap() const
102  {
103  for(size_t i=1;i<h.size();i++)
104  Assert(objectToHeapIndex[h[i].x] == (int)i);
105  for(size_t i=0;i<objectToHeapIndex.size();i++)
106  if(objectToHeapIndex[i]!=0)
107  Assert(h[objectToHeapIndex[i]].x == i);
108 
109  for(int i=2;i<=size();i++)
110  if(h[parent(i)].p < h[i].p) return false;
111  return true;
112  }
113 
114  void print() const {
115  int level=1;
116  for(int i=1;i<=size();i++) {
117  if(i == (1<<level)) {
118  LOG4CXX_INFO(KrisLibrary::logger(),"\n");
119  level++;
120  }
121  LOG4CXX_INFO(KrisLibrary::logger(),"("<<h[i].x<<","<<h[i].p<<")"<<" ");
122  }
123  LOG4CXX_INFO(KrisLibrary::logger(),"\n");
124  }
125 
126 private:
127  struct item
128  {
129  int x; //the object index
130  ptype p; //the priority key
131  };
132 
133  //assume 1-based array
134  inline int parent(int i) const { return i>>1; }
135  inline int child1(int i) const { return i<<1; }
136  inline int child2(int i) const { return (i<<1)+1; }
137 
138  void heapifyUp(int i)
139  {
140  item it=h[i];
141  while(i>1) {
142  int par=parent(i);
143  if(it.p>h[par].p) {
144  h[i]=h[par];
145  objectToHeapIndex[h[i].x]=i;
146  }
147  else break;
148  i=par;
149  }
150  h[i]=it;
151  objectToHeapIndex[h[i].x]=i;
152  }
153 
154  void heapifyDown(int i)
155  {
156  item it=h[i];
157  int child;
158  int size = (int)h.size();
159  while(child1(i)<size) {
160  child = child1(i);
161  if(child+1<size && h[child+1].p > h[child].p)
162  child++;
163  if(it.p < h[child].p) {
164  h[i]=h[child];
165  objectToHeapIndex[h[i].x]=i;
166  }
167  else break;
168  i=child;
169  }
170  h[i]=it;
171  objectToHeapIndex[h[i].x]=i;
172  }
173 
174  //stores the mapping from object identifiers to heap items
175  std::vector<int> objectToHeapIndex;
176  //stores the items in heap order, from indices 1 to N
177  std::vector<item> h;
178 };
179 
180 
181 #endif
A heap of fixed maximum size N. Each element is indexed by an integer 0...N-1. The priority key of ea...
Definition: FixedSizeHeap.h:13
The logging system used in KrisLibrary.