IntervalTree.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ARMeilleure.Translation
  4. {
  5. /// <summary>
  6. /// An Augmented Interval Tree based off of the "TreeDictionary"'s Red-Black Tree. Allows fast overlap checking of ranges.
  7. /// </summary>
  8. /// <typeparam name="K">Key</typeparam>
  9. /// <typeparam name="V">Value</typeparam>
  10. class IntervalTree<K, V> where K : IComparable<K>
  11. {
  12. private const int ArrayGrowthSize = 32;
  13. private const bool Black = true;
  14. private const bool Red = false;
  15. private IntervalTreeNode<K, V> _root = null;
  16. private int _count = 0;
  17. public int Count => _count;
  18. public IntervalTree() { }
  19. #region Public Methods
  20. /// <summary>
  21. /// Gets the values of the interval whose key is <paramref name="key"/>.
  22. /// </summary>
  23. /// <param name="key">Key of the node value to get</param>
  24. /// <param name="value">Value with the given <paramref name="key"/></param>
  25. /// <returns>True if the key is on the dictionary, false otherwise</returns>
  26. public bool TryGet(K key, out V value)
  27. {
  28. IntervalTreeNode<K, V> node = GetNode(key);
  29. if (node == null)
  30. {
  31. value = default;
  32. return false;
  33. }
  34. value = node.Value;
  35. return true;
  36. }
  37. /// <summary>
  38. /// Returns the start addresses of the intervals whose start and end keys overlap the given range.
  39. /// </summary>
  40. /// <param name="start">Start of the range</param>
  41. /// <param name="end">End of the range</param>
  42. /// <param name="overlaps">Overlaps array to place results in</param>
  43. /// <param name="overlapCount">Index to start writing results into the array. Defaults to 0</param>
  44. /// <returns>Number of intervals found</returns>
  45. public int Get(K start, K end, ref K[] overlaps, int overlapCount = 0)
  46. {
  47. GetKeys(_root, start, end, ref overlaps, ref overlapCount);
  48. return overlapCount;
  49. }
  50. /// <summary>
  51. /// Adds a new interval into the tree whose start is <paramref name="start"/>, end is <paramref name="end"/> and value is <paramref name="value"/>.
  52. /// </summary>
  53. /// <param name="start">Start of the range to add</param>
  54. /// <param name="end">End of the range to insert</param>
  55. /// <param name="value">Value to add</param>
  56. /// <param name="updateFactoryCallback">Optional factory used to create a new value if <paramref name="start"/> is already on the tree</param>
  57. /// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
  58. /// <returns>True if the value was added, false if the start key was already in the dictionary</returns>
  59. public bool AddOrUpdate(K start, K end, V value, Func<K, V, V> updateFactoryCallback)
  60. {
  61. if (value == null)
  62. {
  63. throw new ArgumentNullException(nameof(value));
  64. }
  65. return BSTInsert(start, end, value, updateFactoryCallback, out IntervalTreeNode<K, V> node);
  66. }
  67. /// <summary>
  68. /// Gets an existing or adds a new interval into the tree whose start is <paramref name="start"/>, end is <paramref name="end"/> and value is <paramref name="value"/>.
  69. /// </summary>
  70. /// <param name="start">Start of the range to add</param>
  71. /// <param name="end">End of the range to insert</param>
  72. /// <param name="value">Value to add</param>
  73. /// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
  74. /// <returns><paramref name="value"/> if <paramref name="start"/> is not yet on the tree, or the existing value otherwise</returns>
  75. public V GetOrAdd(K start, K end, V value)
  76. {
  77. if (value == null)
  78. {
  79. throw new ArgumentNullException(nameof(value));
  80. }
  81. BSTInsert(start, end, value, null, out IntervalTreeNode<K, V> node);
  82. return node.Value;
  83. }
  84. /// <summary>
  85. /// Removes a value from the tree, searching for it with <paramref name="key"/>.
  86. /// </summary>
  87. /// <param name="key">Key of the node to remove</param>
  88. /// <returns>Number of deleted values</returns>
  89. public int Remove(K key)
  90. {
  91. int removed = Delete(key);
  92. _count -= removed;
  93. return removed;
  94. }
  95. /// <summary>
  96. /// Adds all the nodes in the dictionary into <paramref name="list"/>.
  97. /// </summary>
  98. /// <returns>A list of all values sorted by Key Order</returns>
  99. public List<V> AsList()
  100. {
  101. List<V> list = new List<V>();
  102. AddToList(_root, list);
  103. return list;
  104. }
  105. #endregion
  106. #region Private Methods (BST)
  107. /// <summary>
  108. /// Adds all values that are children of or contained within <paramref name="node"/> into <paramref name="list"/>, in Key Order.
  109. /// </summary>
  110. /// <param name="node">The node to search for values within</param>
  111. /// <param name="list">The list to add values to</param>
  112. private void AddToList(IntervalTreeNode<K, V> node, List<V> list)
  113. {
  114. if (node == null)
  115. {
  116. return;
  117. }
  118. AddToList(node.Left, list);
  119. list.Add(node.Value);
  120. AddToList(node.Right, list);
  121. }
  122. /// <summary>
  123. /// Retrieve the node reference whose key is <paramref name="key"/>, or null if no such node exists.
  124. /// </summary>
  125. /// <param name="key">Key of the node to get</param>
  126. /// <exception cref="ArgumentNullException"><paramref name="key"/> is null</exception>
  127. /// <returns>Node reference in the tree</returns>
  128. private IntervalTreeNode<K, V> GetNode(K key)
  129. {
  130. if (key == null)
  131. {
  132. throw new ArgumentNullException(nameof(key));
  133. }
  134. IntervalTreeNode<K, V> node = _root;
  135. while (node != null)
  136. {
  137. int cmp = key.CompareTo(node.Start);
  138. if (cmp < 0)
  139. {
  140. node = node.Left;
  141. }
  142. else if (cmp > 0)
  143. {
  144. node = node.Right;
  145. }
  146. else
  147. {
  148. return node;
  149. }
  150. }
  151. return null;
  152. }
  153. /// <summary>
  154. /// Retrieve all keys that overlap the given start and end keys.
  155. /// </summary>
  156. /// <param name="start">Start of the range</param>
  157. /// <param name="end">End of the range</param>
  158. /// <param name="overlaps">Overlaps array to place results in</param>
  159. /// <param name="overlapCount">Overlaps count to update</param>
  160. private void GetKeys(IntervalTreeNode<K, V> node, K start, K end, ref K[] overlaps, ref int overlapCount)
  161. {
  162. if (node == null || start.CompareTo(node.Max) >= 0)
  163. {
  164. return;
  165. }
  166. GetKeys(node.Left, start, end, ref overlaps, ref overlapCount);
  167. bool endsOnRight = end.CompareTo(node.Start) > 0;
  168. if (endsOnRight)
  169. {
  170. if (start.CompareTo(node.End) < 0)
  171. {
  172. if (overlaps.Length >= overlapCount)
  173. {
  174. Array.Resize(ref overlaps, overlapCount + ArrayGrowthSize);
  175. }
  176. overlaps[overlapCount++] = node.Start;
  177. }
  178. GetKeys(node.Right, start, end, ref overlaps, ref overlapCount);
  179. }
  180. }
  181. /// <summary>
  182. /// Propagate an increase in max value starting at the given node, heading up the tree.
  183. /// This should only be called if the max increases - not for rebalancing or removals.
  184. /// </summary>
  185. /// <param name="node">The node to start propagating from</param>
  186. private void PropagateIncrease(IntervalTreeNode<K, V> node)
  187. {
  188. K max = node.Max;
  189. IntervalTreeNode<K, V> ptr = node;
  190. while ((ptr = ptr.Parent) != null)
  191. {
  192. if (max.CompareTo(ptr.Max) > 0)
  193. {
  194. ptr.Max = max;
  195. }
  196. else
  197. {
  198. break;
  199. }
  200. }
  201. }
  202. /// <summary>
  203. /// Propagate recalculating max value starting at the given node, heading up the tree.
  204. /// This fully recalculates the max value from all children when there is potential for it to decrease.
  205. /// </summary>
  206. /// <param name="node">The node to start propagating from</param>
  207. private void PropagateFull(IntervalTreeNode<K, V> node)
  208. {
  209. IntervalTreeNode<K, V> ptr = node;
  210. do
  211. {
  212. K max = ptr.End;
  213. if (ptr.Left != null && ptr.Left.Max.CompareTo(max) > 0)
  214. {
  215. max = ptr.Left.Max;
  216. }
  217. if (ptr.Right != null && ptr.Right.Max.CompareTo(max) > 0)
  218. {
  219. max = ptr.Right.Max;
  220. }
  221. ptr.Max = max;
  222. } while ((ptr = ptr.Parent) != null);
  223. }
  224. /// <summary>
  225. /// Insertion Mechanism for the interval tree. Similar to a BST insert, with the start of the range as the key.
  226. /// Iterates the tree starting from the root and inserts a new node where all children in the left subtree are less than <paramref name="start"/>, and all children in the right subtree are greater than <paramref name="start"/>.
  227. /// Each node can contain multiple values, and has an end address which is the maximum of all those values.
  228. /// Post insertion, the "max" value of the node and all parents are updated.
  229. /// </summary>
  230. /// <param name="start">Start of the range to insert</param>
  231. /// <param name="end">End of the range to insert</param>
  232. /// <param name="value">Value to insert</param>
  233. /// <param name="updateFactoryCallback">Optional factory used to create a new value if <paramref name="start"/> is already on the tree</param>
  234. /// <param name="outNode">Node that was inserted or modified</param>
  235. /// <returns>True if <paramref name="start"/> was not yet on the tree, false otherwise</returns>
  236. private bool BSTInsert(K start, K end, V value, Func<K, V, V> updateFactoryCallback, out IntervalTreeNode<K, V> outNode)
  237. {
  238. IntervalTreeNode<K, V> parent = null;
  239. IntervalTreeNode<K, V> node = _root;
  240. while (node != null)
  241. {
  242. parent = node;
  243. int cmp = start.CompareTo(node.Start);
  244. if (cmp < 0)
  245. {
  246. node = node.Left;
  247. }
  248. else if (cmp > 0)
  249. {
  250. node = node.Right;
  251. }
  252. else
  253. {
  254. outNode = node;
  255. if (updateFactoryCallback != null)
  256. {
  257. // Replace
  258. node.Value = updateFactoryCallback(start, node.Value);
  259. int endCmp = end.CompareTo(node.End);
  260. if (endCmp > 0)
  261. {
  262. node.End = end;
  263. if (end.CompareTo(node.Max) > 0)
  264. {
  265. node.Max = end;
  266. PropagateIncrease(node);
  267. RestoreBalanceAfterInsertion(node);
  268. }
  269. }
  270. else if (endCmp < 0)
  271. {
  272. node.End = end;
  273. PropagateFull(node);
  274. }
  275. }
  276. return false;
  277. }
  278. }
  279. IntervalTreeNode<K, V> newNode = new IntervalTreeNode<K, V>(start, end, value, parent);
  280. if (newNode.Parent == null)
  281. {
  282. _root = newNode;
  283. }
  284. else if (start.CompareTo(parent.Start) < 0)
  285. {
  286. parent.Left = newNode;
  287. }
  288. else
  289. {
  290. parent.Right = newNode;
  291. }
  292. PropagateIncrease(newNode);
  293. _count++;
  294. RestoreBalanceAfterInsertion(newNode);
  295. outNode = newNode;
  296. return true;
  297. }
  298. /// <summary>
  299. /// Removes the value from the dictionary after searching for it with <paramref name="key">.
  300. /// </summary>
  301. /// <param name="key">Key to search for</param>
  302. /// <returns>Number of deleted values</returns>
  303. private int Delete(K key)
  304. {
  305. IntervalTreeNode<K, V> nodeToDelete = GetNode(key);
  306. if (nodeToDelete == null)
  307. {
  308. return 0;
  309. }
  310. IntervalTreeNode<K, V> replacementNode;
  311. if (LeftOf(nodeToDelete) == null || RightOf(nodeToDelete) == null)
  312. {
  313. replacementNode = nodeToDelete;
  314. }
  315. else
  316. {
  317. replacementNode = PredecessorOf(nodeToDelete);
  318. }
  319. IntervalTreeNode<K, V> tmp = LeftOf(replacementNode) ?? RightOf(replacementNode);
  320. if (tmp != null)
  321. {
  322. tmp.Parent = ParentOf(replacementNode);
  323. }
  324. if (ParentOf(replacementNode) == null)
  325. {
  326. _root = tmp;
  327. }
  328. else if (replacementNode == LeftOf(ParentOf(replacementNode)))
  329. {
  330. ParentOf(replacementNode).Left = tmp;
  331. }
  332. else
  333. {
  334. ParentOf(replacementNode).Right = tmp;
  335. }
  336. if (replacementNode != nodeToDelete)
  337. {
  338. nodeToDelete.Start = replacementNode.Start;
  339. nodeToDelete.Value = replacementNode.Value;
  340. nodeToDelete.End = replacementNode.End;
  341. nodeToDelete.Max = replacementNode.Max;
  342. }
  343. PropagateFull(replacementNode);
  344. if (tmp != null && ColorOf(replacementNode) == Black)
  345. {
  346. RestoreBalanceAfterRemoval(tmp);
  347. }
  348. return 1;
  349. }
  350. /// <summary>
  351. /// Returns the node with the largest key where <paramref name="node"/> is considered the root node.
  352. /// </summary>
  353. /// <param name="node">Root Node</param>
  354. /// <returns>Node with the maximum key in the tree of <paramref name="node"/></returns>
  355. private static IntervalTreeNode<K, V> Maximum(IntervalTreeNode<K, V> node)
  356. {
  357. IntervalTreeNode<K, V> tmp = node;
  358. while (tmp.Right != null)
  359. {
  360. tmp = tmp.Right;
  361. }
  362. return tmp;
  363. }
  364. /// <summary>
  365. /// Finds the node whose key is immediately less than <paramref name="node"/>.
  366. /// </summary>
  367. /// <param name="node">Node to find the predecessor of</param>
  368. /// <returns>Predecessor of <paramref name="node"/></returns>
  369. private static IntervalTreeNode<K, V> PredecessorOf(IntervalTreeNode<K, V> node)
  370. {
  371. if (node.Left != null)
  372. {
  373. return Maximum(node.Left);
  374. }
  375. IntervalTreeNode<K, V> parent = node.Parent;
  376. while (parent != null && node == parent.Left)
  377. {
  378. node = parent;
  379. parent = parent.Parent;
  380. }
  381. return parent;
  382. }
  383. #endregion
  384. #region Private Methods (RBL)
  385. private void RestoreBalanceAfterRemoval(IntervalTreeNode<K, V> balanceNode)
  386. {
  387. IntervalTreeNode<K, V> ptr = balanceNode;
  388. while (ptr != _root && ColorOf(ptr) == Black)
  389. {
  390. if (ptr == LeftOf(ParentOf(ptr)))
  391. {
  392. IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ptr));
  393. if (ColorOf(sibling) == Red)
  394. {
  395. SetColor(sibling, Black);
  396. SetColor(ParentOf(ptr), Red);
  397. RotateLeft(ParentOf(ptr));
  398. sibling = RightOf(ParentOf(ptr));
  399. }
  400. if (ColorOf(LeftOf(sibling)) == Black && ColorOf(RightOf(sibling)) == Black)
  401. {
  402. SetColor(sibling, Red);
  403. ptr = ParentOf(ptr);
  404. }
  405. else
  406. {
  407. if (ColorOf(RightOf(sibling)) == Black)
  408. {
  409. SetColor(LeftOf(sibling), Black);
  410. SetColor(sibling, Red);
  411. RotateRight(sibling);
  412. sibling = RightOf(ParentOf(ptr));
  413. }
  414. SetColor(sibling, ColorOf(ParentOf(ptr)));
  415. SetColor(ParentOf(ptr), Black);
  416. SetColor(RightOf(sibling), Black);
  417. RotateLeft(ParentOf(ptr));
  418. ptr = _root;
  419. }
  420. }
  421. else
  422. {
  423. IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ptr));
  424. if (ColorOf(sibling) == Red)
  425. {
  426. SetColor(sibling, Black);
  427. SetColor(ParentOf(ptr), Red);
  428. RotateRight(ParentOf(ptr));
  429. sibling = LeftOf(ParentOf(ptr));
  430. }
  431. if (ColorOf(RightOf(sibling)) == Black && ColorOf(LeftOf(sibling)) == Black)
  432. {
  433. SetColor(sibling, Red);
  434. ptr = ParentOf(ptr);
  435. }
  436. else
  437. {
  438. if (ColorOf(LeftOf(sibling)) == Black)
  439. {
  440. SetColor(RightOf(sibling), Black);
  441. SetColor(sibling, Red);
  442. RotateLeft(sibling);
  443. sibling = LeftOf(ParentOf(ptr));
  444. }
  445. SetColor(sibling, ColorOf(ParentOf(ptr)));
  446. SetColor(ParentOf(ptr), Black);
  447. SetColor(LeftOf(sibling), Black);
  448. RotateRight(ParentOf(ptr));
  449. ptr = _root;
  450. }
  451. }
  452. }
  453. SetColor(ptr, Black);
  454. }
  455. private void RestoreBalanceAfterInsertion(IntervalTreeNode<K, V> balanceNode)
  456. {
  457. SetColor(balanceNode, Red);
  458. while (balanceNode != null && balanceNode != _root && ColorOf(ParentOf(balanceNode)) == Red)
  459. {
  460. if (ParentOf(balanceNode) == LeftOf(ParentOf(ParentOf(balanceNode))))
  461. {
  462. IntervalTreeNode<K, V> sibling = RightOf(ParentOf(ParentOf(balanceNode)));
  463. if (ColorOf(sibling) == Red)
  464. {
  465. SetColor(ParentOf(balanceNode), Black);
  466. SetColor(sibling, Black);
  467. SetColor(ParentOf(ParentOf(balanceNode)), Red);
  468. balanceNode = ParentOf(ParentOf(balanceNode));
  469. }
  470. else
  471. {
  472. if (balanceNode == RightOf(ParentOf(balanceNode)))
  473. {
  474. balanceNode = ParentOf(balanceNode);
  475. RotateLeft(balanceNode);
  476. }
  477. SetColor(ParentOf(balanceNode), Black);
  478. SetColor(ParentOf(ParentOf(balanceNode)), Red);
  479. RotateRight(ParentOf(ParentOf(balanceNode)));
  480. }
  481. }
  482. else
  483. {
  484. IntervalTreeNode<K, V> sibling = LeftOf(ParentOf(ParentOf(balanceNode)));
  485. if (ColorOf(sibling) == Red)
  486. {
  487. SetColor(ParentOf(balanceNode), Black);
  488. SetColor(sibling, Black);
  489. SetColor(ParentOf(ParentOf(balanceNode)), Red);
  490. balanceNode = ParentOf(ParentOf(balanceNode));
  491. }
  492. else
  493. {
  494. if (balanceNode == LeftOf(ParentOf(balanceNode)))
  495. {
  496. balanceNode = ParentOf(balanceNode);
  497. RotateRight(balanceNode);
  498. }
  499. SetColor(ParentOf(balanceNode), Black);
  500. SetColor(ParentOf(ParentOf(balanceNode)), Red);
  501. RotateLeft(ParentOf(ParentOf(balanceNode)));
  502. }
  503. }
  504. }
  505. SetColor(_root, Black);
  506. }
  507. private void RotateLeft(IntervalTreeNode<K, V> node)
  508. {
  509. if (node != null)
  510. {
  511. IntervalTreeNode<K, V> right = RightOf(node);
  512. node.Right = LeftOf(right);
  513. if (node.Right != null)
  514. {
  515. node.Right.Parent = node;
  516. }
  517. IntervalTreeNode<K, V> nodeParent = ParentOf(node);
  518. right.Parent = nodeParent;
  519. if (nodeParent == null)
  520. {
  521. _root = right;
  522. }
  523. else if (node == LeftOf(nodeParent))
  524. {
  525. nodeParent.Left = right;
  526. }
  527. else
  528. {
  529. nodeParent.Right = right;
  530. }
  531. right.Left = node;
  532. node.Parent = right;
  533. PropagateFull(node);
  534. }
  535. }
  536. private void RotateRight(IntervalTreeNode<K, V> node)
  537. {
  538. if (node != null)
  539. {
  540. IntervalTreeNode<K, V> left = LeftOf(node);
  541. node.Left = RightOf(left);
  542. if (node.Left != null)
  543. {
  544. node.Left.Parent = node;
  545. }
  546. IntervalTreeNode<K, V> nodeParent = ParentOf(node);
  547. left.Parent = nodeParent;
  548. if (nodeParent == null)
  549. {
  550. _root = left;
  551. }
  552. else if (node == RightOf(nodeParent))
  553. {
  554. nodeParent.Right = left;
  555. }
  556. else
  557. {
  558. nodeParent.Left = left;
  559. }
  560. left.Right = node;
  561. node.Parent = left;
  562. PropagateFull(node);
  563. }
  564. }
  565. #endregion
  566. #region Safety-Methods
  567. // These methods save memory by allowing us to forego sentinel nil nodes, as well as serve as protection against NullReferenceExceptions.
  568. /// <summary>
  569. /// Returns the color of <paramref name="node"/>, or Black if it is null.
  570. /// </summary>
  571. /// <param name="node">Node</param>
  572. /// <returns>The boolean color of <paramref name="node"/>, or black if null</returns>
  573. private static bool ColorOf(IntervalTreeNode<K, V> node)
  574. {
  575. return node == null || node.Color;
  576. }
  577. /// <summary>
  578. /// Sets the color of <paramref name="node"/> node to <paramref name="color"/>.
  579. /// <br></br>
  580. /// This method does nothing if <paramref name="node"/> is null.
  581. /// </summary>
  582. /// <param name="node">Node to set the color of</param>
  583. /// <param name="color">Color (Boolean)</param>
  584. private static void SetColor(IntervalTreeNode<K, V> node, bool color)
  585. {
  586. if (node != null)
  587. {
  588. node.Color = color;
  589. }
  590. }
  591. /// <summary>
  592. /// This method returns the left node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
  593. /// </summary>
  594. /// <param name="node">Node to retrieve the left child from</param>
  595. /// <returns>Left child of <paramref name="node"/></returns>
  596. private static IntervalTreeNode<K, V> LeftOf(IntervalTreeNode<K, V> node)
  597. {
  598. return node?.Left;
  599. }
  600. /// <summary>
  601. /// This method returns the right node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
  602. /// </summary>
  603. /// <param name="node">Node to retrieve the right child from</param>
  604. /// <returns>Right child of <paramref name="node"/></returns>
  605. private static IntervalTreeNode<K, V> RightOf(IntervalTreeNode<K, V> node)
  606. {
  607. return node?.Right;
  608. }
  609. /// <summary>
  610. /// Returns the parent node of <paramref name="node"/>, or null if <paramref name="node"/> is null.
  611. /// </summary>
  612. /// <param name="node">Node to retrieve the parent from</param>
  613. /// <returns>Parent of <paramref name="node"/></returns>
  614. private static IntervalTreeNode<K, V> ParentOf(IntervalTreeNode<K, V> node)
  615. {
  616. return node?.Parent;
  617. }
  618. #endregion
  619. public bool ContainsKey(K key)
  620. {
  621. return GetNode(key) != null;
  622. }
  623. public void Clear()
  624. {
  625. _root = null;
  626. _count = 0;
  627. }
  628. }
  629. /// <summary>
  630. /// Represents a node in the IntervalTree which contains start and end keys of type K, and a value of generic type V.
  631. /// </summary>
  632. /// <typeparam name="K">Key type of the node</typeparam>
  633. /// <typeparam name="V">Value type of the node</typeparam>
  634. class IntervalTreeNode<K, V>
  635. {
  636. public bool Color = true;
  637. public IntervalTreeNode<K, V> Left = null;
  638. public IntervalTreeNode<K, V> Right = null;
  639. public IntervalTreeNode<K, V> Parent = null;
  640. /// <summary>
  641. /// The start of the range.
  642. /// </summary>
  643. public K Start;
  644. /// <summary>
  645. /// The end of the range.
  646. /// </summary>
  647. public K End;
  648. /// <summary>
  649. /// The maximum end value of this node and all its children.
  650. /// </summary>
  651. public K Max;
  652. /// <summary>
  653. /// Value stored on this node.
  654. /// </summary>
  655. public V Value;
  656. public IntervalTreeNode(K start, K end, V value, IntervalTreeNode<K, V> parent)
  657. {
  658. Start = start;
  659. End = end;
  660. Max = end;
  661. Value = value;
  662. Parent = parent;
  663. }
  664. }
  665. }