INET Framework for OMNeT++/OMNEST
inet::IntervalTree Class Reference

Interval tree. More...

#include <IntervalTree.h>

Public Member Functions

 IntervalTree ()
 
 ~IntervalTree ()
 
void print () const
 Print the whole interval tree. More...
 
const IntervaldeleteNode (IntervalTreeNode *node)
 Delete one node of the interval tree. More...
 
void deleteNode (const Interval *ivl)
 delete node stored a given interval More...
 
IntervalTreeNodeinsert (const Interval *new_interval)
 Insert one node of the interval tree. More...
 
IntervalTreeNodegetMinimum (IntervalTreeNode *node) const
 
IntervalTreeNodegetMaximum (IntervalTreeNode *node) const
 
IntervalTreeNodegetPredecessor (IntervalTreeNode *node) const
 get the predecessor of a given node More...
 
IntervalTreeNodegetSuccessor (IntervalTreeNode *node) const
 Get the successor of a given node. More...
 
std::deque< const Interval * > query (simtime_t low, simtime_t high)
 Return result for a given query. More...
 

Protected Member Functions

void leftRotate (IntervalTreeNode *node)
 left rotation of tree node More...
 
void rightRotate (IntervalTreeNode *node)
 right rotation of tree node More...
 
void recursiveInsert (IntervalTreeNode *node)
 recursively insert a node More...
 
void recursivePrint (IntervalTreeNode *node) const
 recursively print a subtree More...
 
IntervalTreeNoderecursiveSearch (IntervalTreeNode *node, const Interval *ivl) const
 recursively find the node corresponding to the interval More...
 
void fixupMaxHigh (IntervalTreeNode *node)
 Travels up to the root fixing the max_high fields after an insertion or deletion. More...
 
void deleteFixup (IntervalTreeNode *node)
 

Protected Attributes

IntervalTreeNoderoot = nullptr
 
IntervalTreeNodenil = nullptr
 

Private Attributes

unsigned int recursion_node_stack_size = 0
 
it_recursion_noderecursion_node_stack = nullptr
 
unsigned int current_parent = 0
 
unsigned int recursion_node_stack_top = 0
 

Friends

class IntervalTreeTest
 

Detailed Description

Interval tree.

Constructor & Destructor Documentation

inet::IntervalTree::IntervalTree ( )

the following are used for the query function

58 {
59  nil = new IntervalTreeNode;
60  nil->left = nil->right = nil->parent = nil;
61  nil->red = false;
62  nil->key = nil->high = nil->max_high = -SimTime::getMaxTime(); //-std::numeric_limits<double>::max();
63  nil->stored_interval = nullptr;
64 
65  root = new IntervalTreeNode;
66  root->parent = root->left = root->right = nil;
67  root->key = root->high = root->max_high = SimTime::getMaxTime(); // std::numeric_limits<double>::max();
68  root->red = false;
69  root->stored_interval = nullptr;
70 
73  recursion_node_stack = (it_recursion_node*) malloc(recursion_node_stack_size * sizeof(it_recursion_node));
75  recursion_node_stack[0].start_node = nullptr;
76 }
simtime_t high
Definition: IntervalTree.h:93
unsigned int recursion_node_stack_size
Definition: IntervalTree.h:181
IntervalTreeNode * nil
Definition: IntervalTree.h:158
it_recursion_node * recursion_node_stack
Definition: IntervalTree.h:182
IntervalTreeNode * parent
Definition: IntervalTree.h:104
IntervalTreeNode * right
Definition: IntervalTree.h:102
void * malloc(YYSIZE_T)
IntervalTreeNode * root
Definition: IntervalTree.h:156
simtime_t key
Definition: IntervalTree.h:91
bool red
red or black node: if red = false then the node is black
Definition: IntervalTree.h:98
simtime_t max_high
Definition: IntervalTree.h:95
unsigned int recursion_node_stack_top
Definition: IntervalTree.h:184
IntervalTreeNode * left
Definition: IntervalTree.h:100
const Interval * stored_interval
interval stored in the node
Definition: IntervalTree.h:89
IntervalTreeNode * start_node
Definition: IntervalTree.h:113
inet::IntervalTree::~IntervalTree ( )
79 {
80  IntervalTreeNode* x = root->left;
81  std::deque<IntervalTreeNode*> nodes_to_free;
82 
83  if (x != nil)
84  {
85  if (x->left != nil)
86  {
87  nodes_to_free.push_back(x->left);
88  }
89  if (x->right != nil)
90  {
91  nodes_to_free.push_back(x->right);
92  }
93 
94  delete x;
95  while (nodes_to_free.size() > 0)
96  {
97  x = nodes_to_free.back();
98  nodes_to_free.pop_back();
99  if (x->left != nil)
100  {
101  nodes_to_free.push_back(x->left);
102  }
103  if (x->right != nil)
104  {
105  nodes_to_free.push_back(x->right);
106  }
107  delete x;
108  }
109  }
110  delete nil;
111  delete root;
113 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
it_recursion_node * recursion_node_stack
Definition: IntervalTree.h:182
IntervalTreeNode * root
Definition: IntervalTree.h:156
IntervalTreeNode * left
Definition: IntervalTree.h:100
void free(void *)

Member Function Documentation

void inet::IntervalTree::deleteFixup ( IntervalTreeNode node)
protected
346 {
347  IntervalTreeNode* w;
348  IntervalTreeNode* root_left_node = root->left;
349 
350  while ((!x->red) && (root_left_node != x))
351  {
352  if (x == x->parent->left)
353  {
354  w = x->parent->right;
355  if (w->red)
356  {
357  w->red = false;
358  x->parent->red = true;
359  leftRotate(x->parent);
360  w = x->parent->right;
361  }
362  if ((!w->right->red) && (!w->left->red))
363  {
364  w->red = true;
365  x = x->parent;
366  }
367  else
368  {
369  if (!w->right->red)
370  {
371  w->left->red = false;
372  w->red = true;
373  rightRotate(w);
374  w = x->parent->right;
375  }
376  w->red = x->parent->red;
377  x->parent->red = false;
378  w->right->red = false;
379  leftRotate(x->parent);
380  x = root_left_node;
381  }
382  }
383  else
384  {
385  w = x->parent->left;
386  if (w->red)
387  {
388  w->red = false;
389  x->parent->red = true;
390  rightRotate(x->parent);
391  w = x->parent->left;
392  }
393  if ((!w->right->red) && (!w->left->red))
394  {
395  w->red = true;
396  x = x->parent;
397  }
398  else
399  {
400  if (!w->left->red)
401  {
402  w->right->red = false;
403  w->red = true;
404  leftRotate(w);
405  w = x->parent->left;
406  }
407  w->red = x->parent->red;
408  x->parent->red = false;
409  w->left->red = false;
410  rightRotate(x->parent);
411  x = root_left_node;
412  }
413  }
414  }
415  x->red = false;
416 }
IntervalTreeNode * parent
Definition: IntervalTree.h:104
IntervalTreeNode * right
Definition: IntervalTree.h:102
IntervalTreeNode * root
Definition: IntervalTree.h:156
void leftRotate(IntervalTreeNode *node)
left rotation of tree node
Definition: IntervalTree.cc:115
bool red
red or black node: if red = false then the node is black
Definition: IntervalTree.h:98
IntervalTreeNode * left
Definition: IntervalTree.h:100
void rightRotate(IntervalTreeNode *node)
right rotation of tree node
Definition: IntervalTree.cc:139
const Interval * inet::IntervalTree::deleteNode ( IntervalTreeNode node)

Delete one node of the interval tree.

y should not be nil in this case y is the node to splice out and x is its child

Referenced by inet::physicallayer::VectorCommunicationCache::removeNonInterferingTransmissions(), and inet::physicallayer::MapCommunicationCache::removeTransmission().

444 {
445  IntervalTreeNode* y;
446  IntervalTreeNode* x;
447  const Interval* node_to_delete = z->stored_interval;
448 
449  y = ((z->left == nil) || (z->right == nil)) ? z : getSuccessor(z);
450  x = (y->left == nil) ? y->right : y->left;
451  if (root == (x->parent = y->parent))
452  {
453  root->left = x;
454  }
455  else
456  {
457  if (y == y->parent->left)
458  {
459  y->parent->left = x;
460  }
461  else
462  {
463  y->parent->right = x;
464  }
465  }
466 
469  if (y != z)
470  {
471  y->max_high = -SimTime::getMaxTime(); // -std::numeric_limits<double>::max();
472  y->left = z->left;
473  y->right = z->right;
474  y->parent = z->parent;
475  z->left->parent = z->right->parent = y;
476  if (z == z->parent->left)
477  z->parent->left = y;
478  else
479  z->parent->right = y;
480 
481  fixupMaxHigh(x->parent);
482  if (!(y->red))
483  {
484  y->red = z->red;
485  deleteFixup(x);
486  }
487  else
488  y->red = z->red;
489  delete z;
490  }
491  else
492  {
493  fixupMaxHigh(x->parent);
494  if (!(y->red))
495  deleteFixup(x);
496  delete y;
497  }
498 
499  return node_to_delete;
500 }
void fixupMaxHigh(IntervalTreeNode *node)
Travels up to the root fixing the max_high fields after an insertion or deletion. ...
Definition: IntervalTree.cc:186
IntervalTreeNode * nil
Definition: IntervalTree.h:158
void deleteFixup(IntervalTreeNode *node)
Definition: IntervalTree.cc:345
IntervalTreeNode * parent
Definition: IntervalTree.h:104
IntervalTreeNode * right
Definition: IntervalTree.h:102
IntervalTreeNode * getSuccessor(IntervalTreeNode *node) const
Get the successor of a given node.
Definition: IntervalTree.cc:272
IntervalTreeNode * root
Definition: IntervalTree.h:156
simtime_t max_high
Definition: IntervalTree.h:95
IntervalTreeNode * left
Definition: IntervalTree.h:100
if(!(yy_init))
Definition: lexer.cc:1644
void inet::IntervalTree::deleteNode ( const Interval ivl)

delete node stored a given interval

419 {
420  IntervalTreeNode* node = recursiveSearch(root, ivl);
421  if (node != nil)
422  deleteNode(node);
423 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
const Interval * deleteNode(IntervalTreeNode *node)
Delete one node of the interval tree.
Definition: IntervalTree.cc:443
IntervalTreeNode * recursiveSearch(IntervalTreeNode *node, const Interval *ivl) const
recursively find the node corresponding to the interval
Definition: IntervalTree.cc:425
IntervalTreeNode * root
Definition: IntervalTree.h:156
void inet::IntervalTree::fixupMaxHigh ( IntervalTreeNode node)
protected

Travels up to the root fixing the max_high fields after an insertion or deletion.

187 {
188  while (x != root)
189  {
190  x->max_high = std::max(x->high, std::max(x->left->max_high, x->right->max_high));
191  x = x->parent;
192  }
193 }
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
IntervalTreeNode * root
Definition: IntervalTree.h:156
IntervalTreeNode * inet::IntervalTree::getMaximum ( IntervalTreeNode node) const
266 {
267  while (x->right != nil)
268  x = x->right;
269  return x;
270 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
IntervalTreeNode * inet::IntervalTree::getMinimum ( IntervalTreeNode node) const
259 {
260  while (x->left != nil)
261  x = x->left;
262  return x;
263 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
IntervalTreeNode * inet::IntervalTree::getPredecessor ( IntervalTreeNode node) const

get the predecessor of a given node

291 {
292  IntervalTreeNode* y;
293 
294  if (nil != x->left)
295  return getMaximum(x->left);
296  else
297  {
298  y = x->parent;
299  while (y != nil && x == y->left)
300  {
301  x = y;
302  y = y->parent;
303  }
304  return y;
305  }
306 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
IntervalTreeNode * getMaximum(IntervalTreeNode *node) const
Definition: IntervalTree.cc:265
IntervalTreeNode * left
Definition: IntervalTree.h:100
IntervalTreeNode * inet::IntervalTree::getSuccessor ( IntervalTreeNode node) const

Get the successor of a given node.

273 {
274  IntervalTreeNode* y;
275 
276  if (nil != x->right)
277  return getMinimum(x->right);
278  else
279  {
280  y = x->parent;
281  while (y != nil && x == y->right)
282  {
283  x = y;
284  y = y->parent;
285  }
286  return y;
287  }
288 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
IntervalTreeNode * right
Definition: IntervalTree.h:102
IntervalTreeNode * getMinimum(IntervalTreeNode *node) const
Definition: IntervalTree.cc:258
IntervalTreeNode * inet::IntervalTree::insert ( const Interval new_interval)

Insert one node of the interval tree.

use sentinel instead of checking for root

Referenced by inet::physicallayer::CommunicationCacheBase::setCachedInterval().

196 {
197  IntervalTreeNode* y;
198  IntervalTreeNode* x;
199  IntervalTreeNode* new_node;
200 
201  x = new IntervalTreeNode(new_interval);
202  recursiveInsert(x);
203  fixupMaxHigh(x->parent);
204  new_node = x;
205  x->red = true;
206  while (x->parent->red)
207  {
209  if (x->parent == x->parent->parent->left)
210  {
211  y = x->parent->parent->right;
212  if (y->red)
213  {
214  x->parent->red = false;
215  y->red = false;
216  x->parent->parent->red = true;
217  x = x->parent->parent;
218  }
219  else
220  {
221  if (x == x->parent->right)
222  {
223  x = x->parent;
224  leftRotate(x);
225  }
226  x->parent->red = false;
227  x->parent->parent->red = true;
228  rightRotate(x->parent->parent);
229  }
230  }
231  else
232  {
233  y = x->parent->parent->left;
234  if (y->red)
235  {
236  x->parent->red = false;
237  y->red = false;
238  x->parent->parent->red = true;
239  x = x->parent->parent;
240  }
241  else
242  {
243  if (x == x->parent->left)
244  {
245  x = x->parent;
246  rightRotate(x);
247  }
248  x->parent->red = false;
249  x->parent->parent->red = true;
250  leftRotate(x->parent->parent);
251  }
252  }
253  }
254  root->left->red = false;
255  return new_node;
256 }
void fixupMaxHigh(IntervalTreeNode *node)
Travels up to the root fixing the max_high fields after an insertion or deletion. ...
Definition: IntervalTree.cc:186
void recursiveInsert(IntervalTreeNode *node)
recursively insert a node
Definition: IntervalTree.cc:163
IntervalTreeNode * root
Definition: IntervalTree.h:156
void leftRotate(IntervalTreeNode *node)
left rotation of tree node
Definition: IntervalTree.cc:115
bool red
red or black node: if red = false then the node is black
Definition: IntervalTree.h:98
IntervalTreeNode * left
Definition: IntervalTree.h:100
void rightRotate(IntervalTreeNode *node)
right rotation of tree node
Definition: IntervalTree.cc:139
void inet::IntervalTree::leftRotate ( IntervalTreeNode node)
protected

left rotation of tree node

116 {
117  IntervalTreeNode* y;
118 
119  y = x->right;
120  x->right = y->left;
121 
122  if (y->left != nil)
123  y->left->parent = x;
124 
125  y->parent = x->parent;
126 
127  if (x == x->parent->left)
128  x->parent->left = y;
129  else
130  x->parent->right = y;
131 
132  y->left = x;
133  x->parent = y;
134 
135  x->max_high = std::max(x->left->max_high, std::max(x->right->max_high, x->high));
136  y->max_high = std::max(x->max_high, std::max(y->right->max_high, y->high));
137 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161
void inet::IntervalTree::print ( ) const

Print the whole interval tree.

341 {
343 }
void recursivePrint(IntervalTreeNode *node) const
recursively print a subtree
Definition: IntervalTree.cc:330
IntervalTreeNode * root
Definition: IntervalTree.h:156
IntervalTreeNode * left
Definition: IntervalTree.h:100
std::deque< const Interval * > inet::IntervalTree::query ( simtime_t  low,
simtime_t  high 
)

Return result for a given query.

Referenced by inet::physicallayer::CommunicationCacheBase::computeInterferingTransmissions().

516 {
517  std::deque<const Interval*> result_stack;
518  IntervalTreeNode* x = root->left;
519  bool run = (x != nil);
520 
521  current_parent = 0;
522 
523  while (run)
524  {
525  if (overlap(low, high, x->key, x->high))
526  {
527  result_stack.push_back(x->stored_interval);
529  }
530  if (x->left->max_high >= low)
531  {
533  {
535  recursion_node_stack = (it_recursion_node *) realloc(recursion_node_stack,
536  recursion_node_stack_size * sizeof(it_recursion_node));
537  if (recursion_node_stack == nullptr)
538  exit(1);
539  }
544  x = x->left;
545  }
546  else
547  x = x->right;
548 
549  run = (x != nil);
550  while ((!run) && (recursion_node_stack_top > 1))
551  {
552  if (recursion_node_stack[--recursion_node_stack_top].try_right_branch)
553  {
557  run = (x != nil);
558  }
559  }
560  }
561  return result_stack;
562 }
unsigned int parent_index
Definition: IntervalTree.h:115
bool overlap(simtime_t a1, simtime_t a2, simtime_t b1, simtime_t b2)
returns 1 if the intervals overlap, and 0 otherwise
Definition: IntervalTree.cc:503
unsigned int recursion_node_stack_size
Definition: IntervalTree.h:181
IntervalTreeNode * nil
Definition: IntervalTree.h:158
it_recursion_node * recursion_node_stack
Definition: IntervalTree.h:182
IntervalTreeNode * right
Definition: IntervalTree.h:102
IntervalTreeNode * root
Definition: IntervalTree.h:156
unsigned int current_parent
Definition: IntervalTree.h:183
unsigned int recursion_node_stack_top
Definition: IntervalTree.h:184
IntervalTreeNode * left
Definition: IntervalTree.h:100
bool try_right_branch
Definition: IntervalTree.h:117
IntervalTreeNode * start_node
Definition: IntervalTree.h:113
void inet::IntervalTree::recursiveInsert ( IntervalTreeNode node)
protected

recursively insert a node

Inserts z into the tree as if it were a regular binary tree.

164 {
165  IntervalTreeNode* x;
166  IntervalTreeNode* y;
167 
168  z->left = z->right = nil;
169  y = root;
170  x = root->left;
171  while (x != nil)
172  {
173  y = x;
174  if (x->key > z->key)
175  x = x->left;
176  else
177  x = x->right;
178  }
179  z->parent = y;
180  if ((y == root) || (y->key > z->key))
181  y->left = z;
182  else
183  y->right = z;
184 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
IntervalTreeNode * parent
Definition: IntervalTree.h:104
IntervalTreeNode * right
Definition: IntervalTree.h:102
IntervalTreeNode * root
Definition: IntervalTree.h:156
IntervalTreeNode * left
Definition: IntervalTree.h:100
void inet::IntervalTree::recursivePrint ( IntervalTreeNode node) const
protected

recursively print a subtree

331 {
332  if (x != nil)
333  {
334  recursivePrint(x->left);
335  x->print(nil, root);
336  recursivePrint(x->right);
337  }
338 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
void recursivePrint(IntervalTreeNode *node) const
recursively print a subtree
Definition: IntervalTree.cc:330
IntervalTreeNode * root
Definition: IntervalTree.h:156
IntervalTreeNode * inet::IntervalTree::recursiveSearch ( IntervalTreeNode node,
const Interval ivl 
) const
protected

recursively find the node corresponding to the interval

426 {
427  if (node != nil)
428  {
429  if (node->stored_interval == ivl)
430  return node;
431 
432  IntervalTreeNode* left = recursiveSearch(node->left, ivl);
433  if (left != nil)
434  return left;
435  IntervalTreeNode* right = recursiveSearch(node->right, ivl);
436  if (right != nil)
437  return right;
438  }
439 
440  return nil;
441 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
IntervalTreeNode * recursiveSearch(IntervalTreeNode *node, const Interval *ivl) const
recursively find the node corresponding to the interval
Definition: IntervalTree.cc:425
void inet::IntervalTree::rightRotate ( IntervalTreeNode node)
protected

right rotation of tree node

140 {
141  IntervalTreeNode* x;
142 
143  x = y->left;
144  y->left = x->right;
145 
146  if (nil != x->right)
147  x->right->parent = y;
148 
149  x->parent = y->parent;
150  if (y == y->parent->left)
151  y->parent->left = x;
152  else
153  y->parent->right = x;
154 
155  x->right = y;
156  y->parent = x;
157 
158  y->max_high = std::max(y->left->max_high, std::max(y->right->max_high, y->high));
159  x->max_high = std::max(x->left->max_high, std::max(y->max_high, x->high));
160 }
IntervalTreeNode * nil
Definition: IntervalTree.h:158
IntervalTreeNode * right
Definition: IntervalTree.h:102
double max(double a, double b)
Returns the greater of the given parameters.
Definition: INETMath.h:161

Friends And Related Function Documentation

friend class IntervalTreeTest
friend

Member Data Documentation

unsigned int inet::IntervalTree::current_parent = 0
private
IntervalTreeNode* inet::IntervalTree::nil = nullptr
protected
it_recursion_node* inet::IntervalTree::recursion_node_stack = nullptr
private
unsigned int inet::IntervalTree::recursion_node_stack_size = 0
private
unsigned int inet::IntervalTree::recursion_node_stack_top = 0
private
IntervalTreeNode* inet::IntervalTree::root = nullptr
protected

The documentation for this class was generated from the following files: