IntervalTree.cs 28 KB

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