IntervalTree.cs 26 KB

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