gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
HeapTreeType.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef HEAP_TREE_TYPE_H_
18 #define HEAP_TREE_TYPE_H_
19 
20 #include <stdlib.h>
21 #include <iostream>
22 #include <math.h>
23 
24 //#include "Array.H"
25 #include <vector>
26 
27 using namespace std;
28 
29 #include <assert.h>
30 
36 public:
37  unsigned int level;
38  unsigned int child;
39 
40 // /** Overloads the output operator.
41 // This is usefull for debugging.
42 // \param c The ostream to output to
43 // \param cell The cell to put into the ostream
44 // \return A reference to the ostream for further operations.
45 // */
46 // friend ostream& operator<<(ostream& c, const HeapTreeCell& cell);
47 };
48 //ostream& operator<<(ostream& c, const HeapTreeCell& cell){
49 // c<<"("<<cell.level<<", "<<cell.child<<")";
50 //}
51 
66 template<class HT_TYPE>
67 class HeapTreeType : protected vector<HT_TYPE> {
68  float base;
69 
75  void swapIfBigger(unsigned int index){
76  //this-> dump(); cout<<endl;
77  HeapTreeCell childL, childR;
78  findChildren(index, childL, childR);
79  unsigned int indexL, indexR;
80  indexL=findIndex(childL); indexR=findIndex(childR);
81  int useIndexL=0, useIndexR=0;
82  int res;
83  if (indexL<unsortedCount)
84  if ((*this)[index]<(*this)[indexL])
85  useIndexL=1;
86  if (indexR<unsortedCount)
87  if ((*this)[index]<(*this)[indexR])
88  useIndexR=1;
89  if (useIndexR && useIndexL)
90  if ((*this)[indexL]<(*this)[indexR])
91  useIndexL=0;
92  else
93  useIndexR=0;
94  if (useIndexL){
95  swap(index,indexL);
96  swapIfBigger(indexL);
97  }
98  if (useIndexR){
99  swap(index,indexR);
100  swapIfBigger(indexR);
101  }
102  }
103 protected:
105 
110  void swap(unsigned int one, unsigned int two){
111  HT_TYPE temp=(*this)[one];
112  (*this)[one]=(*this)[two];
113  (*this)[two]=temp;
114  }
115 
116 public:
118  HeapTreeType(int baseIn=2) {
119  assert(baseIn==2);
120  base=(float)baseIn; // binary tree
121  deleteElements();
122  }
123 
127  void add(HT_TYPE value){
128  unsortedCount+=1; // add
129  if (unsortedCount+1>vector<HT_TYPE>::size())
130  vector<HT_TYPE>::resize(unsortedCount+1);
131  (*this)[unsortedCount]=value;
132  // bubble up
133  int index=unsortedCount;
134  //this->dump(); cout<<endl;
135  while (index>0){
136  int parent=findParent(index);
137  if ((*this)[parent]<(*this)[index]){
138  HeapTreeType<HT_TYPE>::swap(parent, index);
139  index=parent;
140  //this->dump(); cout<<endl;
141  } else
142  break;
143  }
144  //this->dump(); cout<<endl;
145  }
146 
151  void findCell(unsigned int index, HeapTreeCell &cell){
152  findCell(&(*this)[index], cell);
153  }
154 
159  void findCell(HT_TYPE* index, HeapTreeCell &cell){
160  HT_TYPE* root=&(*this)[0];
161  //cout<<"index="<<index<<'\t'<<"root="<<root<<"\tsizeof(HT_TYPE)="<<sizeof(HT_TYPE)<<"index-root"<<index-root<<endl;
162  int location = (int)(index-root); // no need to divide by sizeof here
163  cell.level=0;
164 // cout<<"pow = "<<base<<"^"<<cell.level<<" = "<<pow(base,(float)cell.level)<<endl;
165  while ((location-=(int)pow(base,(float)cell.level))>=0)
166  cell.level++;
167  location+=(int)pow(base,(float)cell.level);
168  cell.child=location;
169  return;
170  }
171 
176  unsigned int findIndex(HeapTreeCell &cell){
177  if (!cell.level) return 0;
178  return (unsigned int)(pow(base,(float)cell.level)+cell.child-base+1.0);
179  }
180 
185  unsigned int findParent(unsigned int index){
186  if (!index){
187  cerr<<"error: the root parent has no parent"<<endl;
188  exit(-1);
189  }
190  HeapTreeCell child, parent;
191  findCell(index, child); // find the cell of the child
192  parent.level=child.level-1;
193  parent.child=(unsigned int)floor((float)child.child/base);
194  return findIndex(parent);
195  }
196 
202  void findChildren(unsigned int parent, HeapTreeCell& childL, HeapTreeCell& childR){
203 // cout<<"findChildren"<<parent<<endl;
204  HeapTreeCell cell;
205  findCell(parent, cell);
206  childL.level=cell.level+1; childR.level=cell.level+1;
207  childL.child=cell.child*2; childR.child=cell.child*2+1;
208  }
209 
210 // void parent(HT_TYPE* index, HeapTreeCell& cell){
211 // HT_TYPE* root=&(*this)[0];
212 // //cout<<"index="<<index<<'\t'<<"root="<<root<<"\tsizeof(HT_TYPE)="<<sizeof(HT_TYPE)<<"index-root"<<index-root<<endl;
213 // unsigned int location = (int)(index-root); // no need to divide by sizeof here
214 // }
215 
218  void dump(){
219  for (int i=0;i<vector<HT_TYPE>::size();i++)
220  cout<<(*this)[i]<<'\t';
221  }
222 
226  for (int i=0;i<vector<HT_TYPE>::size();i++)
227  cout<<*((*this)[i])<<'\t';
228  }
229 
232  void dumpLPR(HeapTreeCell& cell){
233  HeapTreeCell childL, childR;
234  unsigned int myIndex=findIndex(cell);
235  findChildren(myIndex, childL, childR);
236  if (findIndex(childL)<=unsortedCount)
237  dumpLPR(childL);
238  cout<<(*this)[myIndex]<<'\t';
239  if (findIndex(childR)<=unsortedCount)
240  dumpLPR(childR);
241  }
242 
245  void sort(void){
246  // store first and bubble up
247  unsigned int unsortedCountOrig=unsortedCount;
248  while (unsortedCount>0){
249  unsigned int index=0;
250  swap(index,unsortedCount);
251  swapIfBigger(index);
252  unsortedCount-=1;
253  //this->dump();cout<<"\tsize="<<unsortedCount<<endl;
254  }
255  }
256 
259  void resize(size_t s, HT_TYPE c=HT_TYPE()){
260  vector<HT_TYPE>::resize(s,c);
261  }
262 
265  virtual void deleteElements(void){
266  this->resize(0);
267  unsortedCount=-1;
268  }
269 
270 
274 // HeapTreeType &operator<<=(HT_TYPE var){
279 // add(var);
280 // return *this;
281 // }
282 //
283 
284 // HT_TYPE operator[](int i){
285 // return vector<HT_TYPE>::operator[](i);
286 // }
287 //
288 // HT_TYPE operator[](int i){
289 // if (i>unsortedCount){
290 // cerr<<"ERROR: Current tree unsortedCount = "<<unsortedCount<<" you requested index = "<<i<<endl;
291 // assert(-1);
292 // }
293 // return (*this)[i];
294 // }
295 };
296 #endif //HEAP_TREE_TYPE_H_
size(signal)
void sort(void)
Definition: HeapTreeType.H:245
unsigned int child
The numeric count of the child in the tree from the top.
Definition: HeapTreeType.H:38
STL namespace.
void findChildren(unsigned int parent, HeapTreeCell &childL, HeapTreeCell &childR)
Definition: HeapTreeType.H:202
void swap(unsigned int one, unsigned int two)
Definition: HeapTreeType.H:110
void dumpDereference()
Definition: HeapTreeType.H:225
unsigned int findIndex(HeapTreeCell &cell)
Definition: HeapTreeType.H:176
void findCell(HT_TYPE *index, HeapTreeCell &cell)
Definition: HeapTreeType.H:159
void swapIfBigger(unsigned int index)
Definition: HeapTreeType.H:75
void add(HT_TYPE value)
Definition: HeapTreeType.H:127
void resize(size_t s, HT_TYPE c=HT_TYPE())
Definition: HeapTreeType.H:259
unsigned int level
The level of the tree which is pointed to, from the top.
Definition: HeapTreeType.H:37
virtual void deleteElements(void)
Definition: HeapTreeType.H:265
HeapTreeType(int baseIn=2)
Constructor - currrently only supports baseIn=2 : to be expanded in the future.
Definition: HeapTreeType.H:118
unsigned int findParent(unsigned int index)
Definition: HeapTreeType.H:185
void dumpLPR(HeapTreeCell &cell)
Definition: HeapTreeType.H:232
void findCell(unsigned int index, HeapTreeCell &cell)
Definition: HeapTreeType.H:151
gtkIOStream: /tmp/gtkiostream/include/mffm/HeapTreeType.H Source File
GTK+ IOStream  Beta