IntervalTree.cs 28 KB

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