Overture  Version 25
NTreeNode2GeomADTTuple3dInt.h
Go to the documentation of this file.
1 #ifndef __NTREENODE_GEOMETRIC_ADT_3D_INT__
2 #define __NTREENODE_GEOMETRIC_ADT_3D_INT__
3 
4 
5 // *wdh* turn off assertions.
6 // define ASSERTIONS_ON
7 #undef ASSERTIONS_ON
8 
9 //kkc 040415 #include <iostream.h>
10 #include "OvertureDefine.h"
11 #include OV_STD_INCLUDE(iostream)
12 
13 // AP: Is this really necessary? #include "Overture.h"
14 #include "OvertureTypes.h" // for real
15 #include "NTreeNodeExceptions.h"
16 
17 // NTreeNode2GeomADTTuple3dInt is a class used to build fixed 2 tree container classes,it is not intended for use all by itself
18 
19 #include "GeomADTTuple3dInt.h"
20 
22 {
23 public:
24 
30  inline NTreeNode2GeomADTTuple3dInt(NTreeNode2GeomADTTuple3dInt &node_) : data(node_.data)
31  {
32  for (int i=0; i<2; i++) leaves[i] = node_.leaves[i];
33  trunk = node_.trunk;
34  }
35 
37 
38  inline int add(GeomADTTuple3dInt &data_);
39  inline int add(int d, GeomADTTuple3dInt &data_);
40  inline int add(int d); // add an empty node
41  inline int del(int nDel);
42  inline int change(NTreeNode2GeomADTTuple3dInt *nPtr);
43  inline int change(int d, NTreeNode2GeomADTTuple3dInt *nPtr);
44  inline bool querry(int d); // returns false for NULL and true for alive
45  inline bool querry(); // returns false if there is no trunk, true otherwise
46  inline int nullify();
47 
49  inline const NTreeNode2GeomADTTuple3dInt & getTrunk() const;
50  inline NTreeNode2GeomADTTuple3dInt & getLeaf(int d);
51  inline const NTreeNode2GeomADTTuple3dInt & getLeaf(int d) const;
52  inline GeomADTTuple3dInt & getData() { return data; }
53  inline const GeomADTTuple3dInt & getData() const { return data; }
54 
55 private:
56  int isValid;
57  GeomADTTuple3dInt data;
58 
60  NTreeNode2GeomADTTuple3dInt *leaves[2];
61 };
62 
63 //
64 // inlined methods for NTreeNode2GeomADTTuple3dInt
65 //
66 //\begin{>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{Default Constructor}}
67 
68 inline
71 //======================================================
72 // /Purpose : build and initialize a tree node
73 //\end
74 //======================================================
75 {
76  //
77  // set the internal check isValid to 1
78  //
79  isValid = 1;
80  //
81  // nullify all the leaves
82  //
83  nullify();
84 }
85 
86 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{Constructor}}
87 
88 inline
91 //======================================================
92 // /Purpose : build and initialize a tree node given the data for the node to contain
93 // /data (input) : reference to the data to be stored in the node
94 //\end
95 //======================================================
96 {
97  //
98  // set the internal check isValid to 1
99  //
100  isValid = 1;
101  //
102  // nullify all the leaves
103  //
104  nullify();
105  //
106  // assign the internal data reference
107  //
108  data=data_;
109 
110 }
111 
112 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{Constructor}}
113 
114 inline
117 //======================================================
118 // /Purpose : build and initialize a tree node
119 // /data\_ (input) : reference to the data to be stored in the node
120 // /trunk\_ (input) : pointer to the trunk
121 //\end
122 //======================================================
123 {
124  //
125  // set the internal check isValid to 1
126  //
127  isValid = 1;
128  //
129  // nullify all the leaves
130  //
131  nullify();
132  //
133  // assign data members
134  //
135  data=data_;
136  trunk=trunk_;
137 }
138 
139 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{Constructor}}
140 
141 inline
144 //======================================================
145 // /Purpose : build and initialize a tree node
146 // /data\_ (input) : reference to the data to be stored in the node
147 // /trunk\_ (input) : pointer to the trunk
148 //\end
149 //======================================================
150 {
151  //
152  // set the internal check isValid to 1
153  //
154  isValid = 1;
155  //
156  // nullify all the leaves
157  //
158  nullify();
159  trunk=trunk_;
160 }
161 
162 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{Destructor}}
163 
164 inline
167 //======================================================
168 // /Purpose : destroy a node and all its leaves
169 //\end
170 //======================================================
171 {
172  //
173  // confirm that this node is not corrupt (ie, deallocated elsewhere)
174  //
175 #ifdef ASSERTIONS_ON
176 #if 1
177  AssertException (isValid == 1, InvalidNode());
178 #else
179  AssertException<InvalidNode> (isValid == 1);
180 #endif
181 #endif
182  //
183  // delete the leaves
184  //
185  for (int d=0; d<2; d++)
186  if (leaves[d]!=NULL) del(d);
187 
188  //
189  // make sure the node is invalid before destruction is complete
190  //
191  isValid = -1;
192 
193 }
194 
195 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{add}}
196 
197 inline
198 int
201 //======================================================
202 // /Purpose : add a data leaf to the next available leaf position
203 // /data (input) : data item to be stored
204 // /Returns : 0 on success
205 // /Throws :
206 // \begin{itemize}
207 // \item TreeDegreeViolation : if the tree bookkeeping is corrupt
208 // \item NodeFullError : if all the leaves in this node are ful
209 // \end{itemize}
210 //\end
211 //======================================================
212 {
213  int d=0;
214  while(d<2 && leaves[d]!=NULL)
215  d++;
216 
217 #ifdef ASSERTIONS_ON
218 #if 1
219  AssertException (d<2 && d>=0, TreeDegreeViolation());
220  AssertException (leaves[d]==NULL, NodeFullError());
221 #else
222  AssertException<TreeDegreeViolation> (d<2 && d>=0);
223  AssertException<NodeFullError> (leaves[d]==NULL);
224 #endif
225 #endif
226 
227  //assert(d<2 && leaves[d]==NULL);
228  leaves[d] = new NTreeNode2GeomADTTuple3dInt( data_, this);
229  return 0;
230 }
231 
232 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{add}}
233 
234 inline
235 int
237 add(int d, GeomADTTuple3dInt &data_)
238 //======================================================
239 // /Purpose : add a data leaf to a specified leaf
240 // /d (input) : leaf to store data
241 // /data_ (input) : data item to be stored
242 // /Returns : 0 on success
243 // /Throws :
244 // \begin{itemize}
245 // \item TreeDegreeViolation : if d is not a valid leaf number (if d is greater than the 2 of the tree or less than zero
246 // \item NodeFullError : if d is already used up
247 // \end{itemize}
248 //\end
249 //======================================================
250 {
251  // assert(d<2 && leaves[d]==NULL);
252 #ifdef ASSERTIONS_ON
253 #if 1
254  AssertException (d<2 && d>=0, TreeDegreeViolation());
255  AssertException (leaves[d]==NULL, NodeFullError());
256 #else
257  AssertException<TreeDegreeViolation> (d<2 && d>=0);
258  AssertException<NodeFullError> (leaves[d]==NULL);
259 #endif
260 #endif
261  leaves[d] = new NTreeNode2GeomADTTuple3dInt( data_, this);
262  return 0;
263 }
264 
265 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{add}}
266 
267 inline
268 int
270 add(int d)
271 //======================================================
272 // /Purpose : add a data leaf to a specified leaf
273 // /d (input) : leaf to store data
274 // /data (input) : data item to be stored
275 // /Returns : 0 on success
276 // /Throws :
277 // \begin{itemize}
278 // \item TreeDegreeViolation : if d is not a valid leaf number (if d is greater than the 2 of the tree or less than zero
279 // \item NodeFullError : if d is already used up
280 // \end{itemize}
281 //\end
282 //======================================================
283 {
284  // assert(d<2 && leaves[d]==NULL);
285 #ifdef ASSERTIONS_ON
286 #if 1
287  AssertException (d<2 && d>=0, TreeDegreeViolation());
288  AssertException (leaves[d]==NULL, NodeFullError());
289 #else
290  AssertException<TreeDegreeViolation> (d<2 && d>=0);
291  AssertException<NodeFullError> (leaves[d]==NULL);
292 #endif
293 #endif
294  leaves[d] = new NTreeNode2GeomADTTuple3dInt(this);
295  return 0;
296 }
297 
298 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{del}}
299 
300 inline
301 int
303 del(int nDel)
304 //======================================================
305 // /Purpose : delete a specified leaf
306 // /nDel (input) : leaf to delete
307 // /Returns : 0 on success
308 // /Throws :
309 // \begin{itemize}
310 // \item TreeDegreeViolation : if d is not a valid leaf number (if d is greater than the 2 of the tree or less than zero
311 // \end{itemize}
312 //\end
313 //======================================================
314 {
315  //cout << "inside del"<<endl;
316 #ifdef ASSERTIONS_ON
317 #if 1
318  AssertException (nDel>=0 && nDel<2, TreeDegreeViolation());
319 #else
320  AssertException<TreeDegreeViolation> (nDel>=0 && nDel<2);
321 #endif
322 #endif
323  //assert(nDel>=0 && nDel<2);
324  if (leaves[nDel]!=NULL) {
325  delete leaves[nDel];
326  leaves[nDel] = NULL;
327  }
328  // trunk = NULL;
329  return 0;
330 }
331 
332 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{change}}
333 
334 inline
335 int
338 //======================================================
339 // /Purpose : change a node's trunk
340 // /nPtr (input) : pointer to the new trunk
341 // /Returns : 0 on success
342 // /Throws : nothing
343 //\end
344 //======================================================
345 {
346  trunk = nPtr;
347  return 0;
348 }
349 
350 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{change}}
351 
352 inline
353 int
356 //======================================================
357 // /Purpose : changes a particular leaf
358 // /d (input) : leaf to change
359 // /nPtr (input) : pointer to the new leaf
360 // /Returns : 0 on success
361 // /Throws :
362 // \begin{itemize}
363 // \item TreeDegreeViolation : if d is not a valid leaf number
364 // \end{itemize}
365 //\end
366 //======================================================
367 {
368 #ifdef ASSERTIONS_ON
369 #if 1
370  AssertException (d<2 && d>=0, TreeDegreeViolation());
371 #else
372  AssertException<TreeDegreeViolation> (d<2 && d>=0);
373 #endif
374 #endif
375  //assert(d<2 && d>=0);
376  leaves[d] = nPtr;
377  return 0;
378 }
379 
380 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{change}}
381 
382 inline
383 bool
385 querry(int d)
386 //======================================================
387 // /Purpose : see if a particular leaf has data
388 // /d (input) : leaf to querry
389 // /Returns : false if leaf d is NULL, true otherwise
390 // /Throws :
391 // \begin{itemize}
392 // \item TreeDegreeViolation : if d is not a valid leaf number
393 // \end{itemize}
394 //\end
395 //======================================================
396 {
397 #ifdef ASSERTIONS_ON
398 #if 1
399  AssertException (d<2 && d>=0, TreeDegreeViolation());
400 #else
401  AssertException<TreeDegreeViolation> (d<2 && d>=0);
402 #endif
403 #endif
404  return leaves[d] != NULL;
405 // if (leaves[d] == NULL)
406 // return false;
407 // else
408 // return true;
409 }
410 
411 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{change}}
412 
413 inline
414 bool
417 //======================================================
418 // /Purpose : see if the trunk has data
419 // /d (input) : leaf to querry
420 // /Returns : false if the trunk pointer is NULL, true otherwise
421 // /Throws :
422 // \begin{itemize}
423 // \item TreeDegreeViolation : if d is not a valid leaf number
424 // \end{itemize}
425 //\end
426 //======================================================
427 {
428  return trunk!=NULL;
429 // if (trunk == NULL)
430 // return false;
431 // else
432 // return true;
433 }
434 
435 
436 inline
437 int
440 {
441  trunk = NULL;
442  for (int d=0; d<2; d++)
443  leaves[d] = NULL;
444  return 0;
445 }
446 
447 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{getTrunk}}
448 
449 inline
453 //======================================================
454 // /Purpose : return a reference to the trunk node
455 // /Returns : a reference the the trunk
456 // /Throws : nothing
457 //\end
458 //======================================================
459 {
460  return *trunk;
461 }
462 
463 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{const getTrunk}}
464 
465 inline
468 getTrunk() const
469 //======================================================
470 // /Purpose : return a const reference to the trunk node
471 // /Returns : a const reference the the trunk
472 // /Throws : nothing
473 //\end
474 //======================================================
475 {
476  return *trunk;
477 }
478 
479 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{const getLeaf}}
480 
481 inline
484 //======================================================
485 // /Purpose : return a reference to a specific leaf node
486 // /d (input) : leaf to return
487 // /Returns : a reference to the requested leaf
488 // /Throws :
489 // \begin{itemize}
490 // \item TreeDegreeViolation : if d is not a valid leaf number
491 // \end{itemize}
492 //\end
493 //======================================================
494 {
495 #ifdef ASSERTIONS_ON
496 #if 1
497  AssertException(d<2 && d>=0,TreeDegreeViolation());
498 #else
499  AssertException<TreeDegreeViolation> (d<2 && d>=0);
500 #endif
501 #endif
502 
503  return *leaves[d];
504 }
505 
506 //\begin{>>NTreeNode2GeomADTTuple3dInt.tex}{\subsection{const getLeaf}}
507 
508 inline
511 getLeaf(int d) const
512 //======================================================
513 // /Purpose : return a const reference to a specific leaf node
514 // /d (input) : leaf to return
515 // /Returns : a reference to the requested leaf
516 // /Throws :
517 // \begin{itemize}
518 // \item TreeDegreeViolation : if d is not a valid leaf number
519 // \end{itemize}
520 //\end
521 //======================================================
522 {
523 #ifdef ASSERTIONS_ON
524 #if 1
525  AssertException(d<2 && d>=0, TreeDegreeViolation());
526 #else
527  AssertException<TreeDegreeViolation> (d<2 && d>=0);
528 #endif
529 #endif
530  return *leaves[d];
531 }
532 
533 #if 0
534 
535 inline
536 //const GeomADTTuple3dInt &
539 getData()
540 {
541  return data;
542 }
543 
544 
545 inline
546 //const GeomADTTuple3dInt &
547 const GeomADTTuple3dInt &
549 getData() const
550 {
551  return data;
552 }
553 #endif
554 
555 #undef ASSERTIONS_ON
556 #endif