gSAFE  1.3.8
speedypair.h
1 /*
2  HSpeedyPair - Template Lib
3  A dual AVL tree based pair storing template code
4 
5  (C) 2006 Peter Deak (hyper80@gmail.com)
6 
7  speedypair.h
8 */
9 #ifndef __H_SPEEDYPAIR_CONTAINER_TEMPLATE__
10 #define __H_SPEEDYPAIR_CONTAINER_TEMPLATE__
11 
12 #ifndef NULL
13 #ifdef __cplusplus
14 #define NULL 0
15 #else
16 #define NULL ((void *)0)
17 #endif
18 #endif
19 
20 #define max(a,b) ( ((a)>(b)) ? (a) : (b) )
21 
22 
24 template <class KEY,class VALUE>
25 class HPair
26 {
27  public:
28  KEY key;
29  VALUE value;
30 };
31 
32 /* NOT DOCUMENTED: HSpeedypairNode: For internal use only! See HSpeedyPair */
33 template <class MYVAL,class TOVAL>
34 class HSpeedypairNode
35 {
36  public:
37 
38  MYVAL data;
39  HSpeedypairNode<MYVAL,TOVAL> *left,*right;
40  HSpeedypairNode<TOVAL,MYVAL> *pair;
41 
42  HSpeedypairNode(MYVAL d)
43  {
44  left = NULL;
45  right = NULL;
46  pair = NULL;
47  data = d;
48  }
49 
50  ~HSpeedypairNode(void)
51  {
52  if(left != NULL)
53  delete left;
54  left = NULL;
55 
56  if(right != NULL)
57  delete right;
58  right = NULL;
59 
60  #ifndef ONE_SIDE_ONLY
61  ;
62  #else
63  if(pair != NULL)
64  delete pair;
65  #endif
66  pair = NULL;
67  }
68 
69  static void inorder(HSpeedypairNode<MYVAL,TOVAL> *here,int *seq,void *array,int mode = 0);
70 
71  static void insertTo(HSpeedypairNode<MYVAL,TOVAL> * &here, HSpeedypairNode<MYVAL,TOVAL> *e);
72 
73  //rotations
74  static void rleft (HSpeedypairNode<MYVAL,TOVAL> * &here);
75  static void rright (HSpeedypairNode<MYVAL,TOVAL> * &here);
76  static void drleft (HSpeedypairNode<MYVAL,TOVAL> * &here);
77  static void drright(HSpeedypairNode<MYVAL,TOVAL> * &here);
78 
79  static int height(HSpeedypairNode<MYVAL,TOVAL> *t);
80  static HSpeedypairNode* find_min(HSpeedypairNode<MYVAL,TOVAL> *t);
81 
82 };
83 
84 
87 template <class KEY,class VALUE>
89 {
90  private:
91  int nume;
92  HSpeedypairNode<KEY,VALUE> *root_key;
93 
94  #ifndef ONE_SIDE_ONLY
95  HSpeedypairNode<VALUE,KEY> *root_value;
96  #endif
97 
98  bool f;
99  //do not use:
100  KEY *arrayk;
101  VALUE *arrayv;
102  HPair<KEY,VALUE> *arrayp;
103  int seq;
104 
105  public:
110  public:
111  HSpeedyPair(void);
112  HSpeedyPair(KEY kn,VALUE vn);
113  ~HSpeedyPair(void);
114 
115  void clear(void);
116  void addPair(KEY k,VALUE v);
117  VALUE getValue(KEY key );
118 
119  #ifndef ONE_SIDE_ONLY
120  KEY getKey (VALUE value);
121  #endif
122 
123  int num(void) { return nume; }
124  int heightKTree(void) { return HSpeedypairNode<KEY,VALUE>::height(root_key); }
125 
126  #ifndef ONE_SIDE_ONLY
127  int heightVTree(void) { return HSpeedypairNode<VALUE,KEY>::height(root_value); }
128  #endif
129 
130  bool found(void) { return f; }
131 
132  KEY *getKArray(void);
133 
134  #ifndef ONE_SIDE_ONLY
135  VALUE *getVArray(void);
136  #endif
137 
138  HPair<KEY,VALUE> *getArray(void);
139 };
140 
141 
142 /* Program code */
143 
144 template <class KEY,class VALUE>
146 {
147  root_key = NULL;
148 
149  #ifndef ONE_SIDE_ONLY
150  root_value = NULL;
151  #endif
152 
153  nume = 0;
154 }
155 
156 template <class KEY,class VALUE>
158 {
159  root_key = NULL;
160 
161  #ifndef ONE_SIDE_ONLY
162  root_value = NULL;
163  #endif
164 
165  nume = 0;
166  key_notfound_data = kn;
167  value_notfound_data = vn;
168 }
169 
170 template <class KEY,class VALUE>
172 {
173  clear();
174 }
175 
176 template <class KEY,class VALUE>
178 {
179  if(root_key != NULL)
180  delete root_key;
181  root_key = NULL;
182 
183  #ifndef ONE_SIDE_ONLY
184  if(root_value != NULL)
185  delete root_value;
186  root_value = NULL;
187  #endif
188 
189  nume = 0;
190 }
191 
192 template <class KEY,class VALUE>
193 void HSpeedyPair<KEY,VALUE>::addPair(KEY k,VALUE v)
194 {
195  ++nume;
196  HSpeedypairNode<KEY,VALUE>* nk=new HSpeedypairNode<KEY,VALUE>(k);
197  HSpeedypairNode<VALUE,KEY>* nv=new HSpeedypairNode<VALUE,KEY>(v);
198 
199  //Set cross reference
200  nk->pair = nv;
201  nv->pair = nk;
202 
203  HSpeedypairNode<KEY,VALUE>::insertTo(root_key ,nk); //calls to the private insert function
204 
205  #ifndef ONE_SIDE_ONLY
206  HSpeedypairNode<VALUE,KEY>::insertTo(root_value,nv); //calls to the private insert function
207  #endif
208 }
209 
210 template <class KEY,class VALUE>
212 {
213  HSpeedypairNode<KEY,VALUE> *t = root_key;
214 
215  f = true;
216  while(1)
217  {
218  if(t == NULL)
219  { //Not found!
220  f = false;
221  return value_notfound_data;
222  }
223  if(t->data < key)
224  {
225  t = t->right;
226  continue;
227  }
228  if(t->data > key)
229  {
230  t = t->left;
231  continue;
232  }
233  return t->pair->data;
234  }
235 }
236 
237 #ifndef ONE_SIDE_ONLY
238 template <class KEY,class VALUE>
239 KEY HSpeedyPair<KEY,VALUE>::getKey(VALUE value)
240 {
241  HSpeedypairNode<VALUE,KEY> *t = root_value;
242 
243  f = true;
244  while(1)
245  {
246  if(t == NULL)
247  { //Not found!
248  f = false;
249  return key_notfound_data;
250  }
251  if(t->data < value)
252  {
253  t = t->right;
254  continue;
255  }
256  if(t->data > value)
257  {
258  t = t->left;
259  continue;
260  }
261  return t->pair->data;
262  }
263 }
264 #endif
265 
266 template <class MYVAL,class TOVAL>
267 void HSpeedypairNode<MYVAL,TOVAL>::insertTo(HSpeedypairNode<MYVAL,TOVAL> * &here,
268  HSpeedypairNode<MYVAL,TOVAL> * e)
269 {
270  if(here == NULL) //Root node is null, the new element insert as root.
271  here = e;
272 
273  else
274  {
275  if(here->data < e->data)
276  {
277  insertTo(here->right,e);
278  if(HSpeedypairNode<MYVAL,TOVAL>::height(here->right) -
279  HSpeedypairNode<MYVAL,TOVAL>::height(here->left) == 2)
280  {
281  if(e->data > here->right->data)
282  HSpeedypairNode<MYVAL,TOVAL>::rleft(here);
283  else
284  HSpeedypairNode<MYVAL,TOVAL>::drright(here);
285  }
286  }
287  else if(here->data > e->data)
288  {
289  insertTo(here->left,e);
290  if(HSpeedypairNode<MYVAL,TOVAL>::height(here->left) -
291  HSpeedypairNode<MYVAL,TOVAL>::height(here->right) == 2)
292  {
293  if(e->data < here->left->data)
294  HSpeedypairNode<MYVAL,TOVAL>::rright(here);
295  else
296  HSpeedypairNode<MYVAL,TOVAL>::drleft(here);
297  }
298  }
299  }
300 }
301 
302 template <class MYVAL,class TOVAL>
303 void HSpeedypairNode<MYVAL,TOVAL>::rleft(HSpeedypairNode<MYVAL,TOVAL> * &here)
304 {
305  HSpeedypairNode<MYVAL,TOVAL> *tmp = here->right;
306  here->right = tmp->left;
307  tmp->left = here;
308  here = tmp;
309 }
310 
311 template <class MYVAL,class TOVAL>
312 void HSpeedypairNode<MYVAL,TOVAL>::rright(HSpeedypairNode<MYVAL,TOVAL> * &here)
313 {
314  HSpeedypairNode<MYVAL,TOVAL> *tmp = here->left;
315  here->left = tmp->right;
316  tmp->right = here;
317  here = tmp;
318 }
319 
320 template <class MYVAL,class TOVAL>
321 void HSpeedypairNode<MYVAL,TOVAL>::drleft(HSpeedypairNode<MYVAL,TOVAL> * &here)
322 {
323  HSpeedypairNode<MYVAL,TOVAL> *tmp2;
324  HSpeedypairNode<MYVAL,TOVAL> *tmp1;
325 
326  tmp2 = here->left;
327  tmp1 = tmp2->right;
328  tmp2->right = tmp1->left;
329  tmp1->left = tmp2;
330  here->left = tmp1;
331  HSpeedypairNode<MYVAL,TOVAL> * tmp4 = here->left;
332  here->left = tmp4->right;
333  tmp4->right = here;
334  here=tmp4;
335 }
336 
337 template <class MYVAL,class TOVAL>
338 void HSpeedypairNode<MYVAL,TOVAL>::drright(HSpeedypairNode<MYVAL,TOVAL> * &here)
339 {
340  HSpeedypairNode<MYVAL,TOVAL> *tmp2;
341  HSpeedypairNode<MYVAL,TOVAL> *tmp1;
342 
343  tmp2 = here->right;
344  tmp1 = tmp2->left;
345  tmp2->left = tmp1->right;
346  tmp1->right = tmp2;
347  here->right = tmp1;
348  HSpeedypairNode<MYVAL,TOVAL> *tmp4 = here->right;
349  here->right = tmp4->left;
350  tmp4->left=here;
351  here=tmp4;
352 }
353 
354 
355 template <class MYVAL,class TOVAL>
356 int HSpeedypairNode<MYVAL,TOVAL>::height(HSpeedypairNode<MYVAL,TOVAL> *t)
357 {
358  if(t==NULL)
359  {
360  return -1;
361  }
362  else
363  return 1 + max((HSpeedypairNode<MYVAL,TOVAL>::height(t->left)),
364  (HSpeedypairNode<MYVAL,TOVAL>::height(t->right)));
365 }
366 
367 template <class MYVAL,class TOVAL>
368 HSpeedypairNode<MYVAL,TOVAL> * HSpeedypairNode<MYVAL,TOVAL>::find_min(HSpeedypairNode<MYVAL,TOVAL> *t)
369 {
370  if(t==NULL)
371  return NULL;
372  else
373  if(t->left==NULL)
374  return t;
375  else
376  return(HSpeedypairNode<MYVAL,TOVAL>::find_min(t->left));
377 }
378 
379 template <class MYVAL,class TOVAL>
380 void HSpeedypairNode<MYVAL,TOVAL>::inorder(HSpeedypairNode<MYVAL,TOVAL> *here,int *seq,void *array,int mode)
381 {
382  if(here->left != NULL)
383  HSpeedypairNode<MYVAL,TOVAL>::inorder(here->left ,seq,array,mode);
384 
385  if(mode)
386  {
387  ((HPair<MYVAL,TOVAL> *)array)[(*seq) ].key = here->data;
388  ((HPair<MYVAL,TOVAL> *)array)[(*seq)++].value = here->pair->data;
389  }
390  else
391  ((MYVAL *)array)[(*seq)++] = here->data;
392 
393  if(here->right != NULL)
394  HSpeedypairNode<MYVAL,TOVAL>::inorder(here->right,seq,array,mode);
395 }
396 
397 template <class KEY,class VALUE>
399 {
400  arrayk = new KEY[num()];
401  seq = 0;
402  HSpeedypairNode<KEY,VALUE>::inorder(root_key,&seq,arrayk);
403  return arrayk;
404 }
405 
406 #ifndef ONE_SIDE_ONLY
407 template <class KEY,class VALUE>
409 {
410  arrayv = new VALUE[num()];
411  seq = 0;
412  HSpeedypairNode<VALUE,KEY>::inorder(root_value,&seq,arrayv);
413  return arrayv;
414 }
415 #endif
416 
417 template <class KEY,class VALUE>
419 {
420  arrayp = new HPair<KEY,VALUE>[num()];
421  seq = 0;
422  HSpeedypairNode<KEY,VALUE>::inorder(root_key,&seq,arrayp,1);
423  return arrayp;
424 }
425 #endif
426 //end code
VALUE value_notfound_data
Definition: speedypair.h:109
KEY key_notfound_data
Definition: speedypair.h:107