Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

790 lines
26KB

  1. // Uncomment this to enable the following debugging aids:
  2. // LeftLeaningRedBlackTree.HtmlFragment
  3. // LeftLeaningRedBlackTree.EntityView.HtmlFragment
  4. // LeftLeaningRedBlackTree.AssertInvariants
  5. // #define DEBUGGING
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. /// <summary>
  10. /// Implements a left-leaning red-black tree.
  11. /// </summary>
  12. /// <remarks>
  13. /// Based on the research paper "Left-leaning Red-Black Trees"
  14. /// by Robert Sedgewick. More information available at:
  15. /// http://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf
  16. /// http://www.cs.princeton.edu/~rs/talks/LLRB/08Penn.pdf
  17. /// </remarks>
  18. /// <typeparam name="TKey">Type of keys.</typeparam>
  19. /// <typeparam name="TValue">Type of values.</typeparam>
  20. public class LeftLeaningRedBlackTree<TKey, TValue>
  21. {
  22. /// <summary>
  23. /// Stores the key comparison function.
  24. /// </summary>
  25. private Comparison<TKey> _keyComparison;
  26. /// <summary>
  27. /// Stores the value comparison function.
  28. /// </summary>
  29. private Comparison<TValue> _valueComparison;
  30. /// <summary>
  31. /// Stores the root entityView of the tree.
  32. /// </summary>
  33. private EntityView _rootEntityView;
  34. /// <summary>
  35. /// Represents a entityView of the tree.
  36. /// </summary>
  37. /// <remarks>
  38. /// Using fields instead of properties drops execution time by about 40%.
  39. /// </remarks>
  40. [DebuggerDisplay("Key={Key}, Value={Value}, Siblings={Siblings}")]
  41. private class EntityView
  42. {
  43. /// <summary>
  44. /// Gets or sets the entityView's key.
  45. /// </summary>
  46. public TKey Key;
  47. /// <summary>
  48. /// Gets or sets the entityView's value.
  49. /// </summary>
  50. public TValue Value;
  51. /// <summary>
  52. /// Gets or sets the left entityView.
  53. /// </summary>
  54. public EntityView Left;
  55. /// <summary>
  56. /// Gets or sets the right entityView.
  57. /// </summary>
  58. public EntityView Right;
  59. /// <summary>
  60. /// Gets or sets the color of the entityView.
  61. /// </summary>
  62. public bool IsBlack;
  63. /// <summary>
  64. /// Gets or sets the number of "siblings" (entityViews with the same key/value).
  65. /// </summary>
  66. public int Siblings;
  67. #if DEBUGGING
  68. /// <summary>
  69. /// Gets an HTML fragment representing the entityView and its children.
  70. /// </summary>
  71. public string HtmlFragment
  72. {
  73. get
  74. {
  75. return
  76. "<table border='1'>" +
  77. "<tr>" +
  78. "<td colspan='2' align='center' bgcolor='" + (IsBlack ? "gray" : "red") + "'>" + Key + ", " + Value + " [" + Siblings + "]</td>" +
  79. "</tr>" +
  80. "<tr>" +
  81. "<td valign='top'>" + (null != Left ? Left.HtmlFragment : "[null]") + "</td>" +
  82. "<td valign='top'>" + (null != Right ? Right.HtmlFragment : "[null]") + "</td>" +
  83. "</tr>" +
  84. "</table>";
  85. }
  86. }
  87. #endif
  88. }
  89. /// <summary>
  90. /// Initializes a new instance of the LeftLeaningRedBlackTree class implementing a normal dictionary.
  91. /// </summary>
  92. /// <param name="keyComparison">The key comparison function.</param>
  93. public LeftLeaningRedBlackTree(Comparison<TKey> keyComparison)
  94. {
  95. if (null == keyComparison)
  96. {
  97. throw new ArgumentNullException("keyComparison");
  98. }
  99. _keyComparison = keyComparison;
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the LeftLeaningRedBlackTree class implementing an ordered multi-dictionary.
  103. /// </summary>
  104. /// <param name="keyComparison">The key comparison function.</param>
  105. /// <param name="valueComparison">The value comparison function.</param>
  106. public LeftLeaningRedBlackTree(Comparison<TKey> keyComparison, Comparison<TValue> valueComparison)
  107. : this(keyComparison)
  108. {
  109. if (null == valueComparison)
  110. {
  111. throw new ArgumentNullException("valueComparison");
  112. }
  113. _valueComparison = valueComparison;
  114. }
  115. /// <summary>
  116. /// Gets a value indicating whether the tree is acting as an ordered multi-dictionary.
  117. /// </summary>
  118. private bool IsMultiDictionary
  119. {
  120. get { return null != _valueComparison; }
  121. }
  122. /// <summary>
  123. /// Adds a key/value pair to the tree.
  124. /// </summary>
  125. /// <param name="key">Key to add.</param>
  126. /// <param name="value">Value to add.</param>
  127. public void Add(TKey key, TValue value)
  128. {
  129. _rootEntityView = Add(_rootEntityView, key, value);
  130. _rootEntityView.IsBlack = true;
  131. #if DEBUGGING
  132. AssertInvariants();
  133. #endif
  134. }
  135. /// <summary>
  136. /// Removes a key (and its associated value) from a normal (non-multi) dictionary.
  137. /// </summary>
  138. /// <param name="key">Key to remove.</param>
  139. /// <returns>True if key present and removed.</returns>
  140. public bool Remove(TKey key)
  141. {
  142. if (IsMultiDictionary)
  143. {
  144. throw new InvalidOperationException("Remove is only supported when acting as a normal (non-multi) dictionary.");
  145. }
  146. return Remove(key, default(TValue));
  147. }
  148. /// <summary>
  149. /// Removes a key/value pair from the tree.
  150. /// </summary>
  151. /// <param name="key">Key to remove.</param>
  152. /// <param name="value">Value to remove.</param>
  153. /// <returns>True if key/value present and removed.</returns>
  154. public bool Remove(TKey key, TValue value)
  155. {
  156. int initialCount = Count;
  157. if (null != _rootEntityView)
  158. {
  159. _rootEntityView = Remove(_rootEntityView, key, value);
  160. if (null != _rootEntityView)
  161. {
  162. _rootEntityView.IsBlack = true;
  163. }
  164. }
  165. #if DEBUGGING
  166. AssertInvariants();
  167. #endif
  168. return initialCount != Count;
  169. }
  170. /// <summary>
  171. /// Removes all entityViews in the tree.
  172. /// </summary>
  173. public void Clear()
  174. {
  175. _rootEntityView = null;
  176. Count = 0;
  177. #if DEBUGGING
  178. AssertInvariants();
  179. #endif
  180. }
  181. /// <summary>
  182. /// Gets a sorted list of keys in the tree.
  183. /// </summary>
  184. /// <returns>Sorted list of keys.</returns>
  185. public IEnumerable<TKey> GetKeys()
  186. {
  187. TKey lastKey = default(TKey);
  188. bool lastKeyValid = false;
  189. return Traverse(
  190. _rootEntityView,
  191. n => !lastKeyValid || !object.Equals(lastKey, n.Key),
  192. n =>
  193. {
  194. lastKey = n.Key;
  195. lastKeyValid = true;
  196. return lastKey;
  197. });
  198. }
  199. /// <summary>
  200. /// Gets the value associated with the specified key in a normal (non-multi) dictionary.
  201. /// </summary>
  202. /// <param name="key">Specified key.</param>
  203. /// <returns>Value associated with the specified key.</returns>
  204. public TValue GetValueForKey(TKey key)
  205. {
  206. if (IsMultiDictionary)
  207. {
  208. throw new InvalidOperationException("GetValueForKey is only supported when acting as a normal (non-multi) dictionary.");
  209. }
  210. EntityView entityView = GetEntityViewForKey(key);
  211. if (null != entityView)
  212. {
  213. return entityView.Value;
  214. }
  215. else
  216. {
  217. throw new KeyNotFoundException();
  218. }
  219. }
  220. /// <summary>
  221. /// Gets a sequence of the values associated with the specified key.
  222. /// </summary>
  223. /// <param name="key">Specified key.</param>
  224. /// <returns>Sequence of values.</returns>
  225. public IEnumerable<TValue> GetValuesForKey(TKey key)
  226. {
  227. return Traverse(GetEntityViewForKey(key), n => 0 == _keyComparison(n.Key, key), n => n.Value);
  228. }
  229. /// <summary>
  230. /// Gets a sequence of all the values in the tree.
  231. /// </summary>
  232. /// <returns>Sequence of all values.</returns>
  233. public IEnumerable<TValue> GetValuesForAllKeys()
  234. {
  235. return Traverse(_rootEntityView, n => true, n => n.Value);
  236. }
  237. /// <summary>
  238. /// Gets the count of key/value pairs in the tree.
  239. /// </summary>
  240. public int Count { get; private set; }
  241. /// <summary>
  242. /// Gets the minimum key in the tree.
  243. /// </summary>
  244. public TKey MinimumKey
  245. {
  246. get { return GetExtreme(_rootEntityView, n => n.Left, n => n.Key); }
  247. }
  248. /// <summary>
  249. /// Gets the maximum key in the tree.
  250. /// </summary>
  251. public TKey MaximumKey
  252. {
  253. get { return GetExtreme(_rootEntityView, n => n.Right, n => n.Key); }
  254. }
  255. /// <summary>
  256. /// Returns true if the specified entityView is red.
  257. /// </summary>
  258. /// <param name="entityView">Specified entityView.</param>
  259. /// <returns>True if specified entityView is red.</returns>
  260. private static bool IsRed(EntityView entityView)
  261. {
  262. if (null == entityView)
  263. {
  264. // "Virtual" leaf entityViews are always black
  265. return false;
  266. }
  267. return !entityView.IsBlack;
  268. }
  269. /// <summary>
  270. /// Adds the specified key/value pair below the specified root entityView.
  271. /// </summary>
  272. /// <param name="entityView">Specified entityView.</param>
  273. /// <param name="key">Key to add.</param>
  274. /// <param name="value">Value to add.</param>
  275. /// <returns>New root entityView.</returns>
  276. private EntityView Add(EntityView entityView, TKey key, TValue value)
  277. {
  278. if (null == entityView)
  279. {
  280. // Insert new entityView
  281. Count++;
  282. return new EntityView { Key = key, Value = value };
  283. }
  284. if (IsRed(entityView.Left) && IsRed(entityView.Right))
  285. {
  286. // Split entityView with two red children
  287. FlipColor(entityView);
  288. }
  289. // Find right place for new entityView
  290. int comparisonResult = KeyAndValueComparison(key, value, entityView.Key, entityView.Value);
  291. if (comparisonResult < 0)
  292. {
  293. entityView.Left = Add(entityView.Left, key, value);
  294. }
  295. else if (0 < comparisonResult)
  296. {
  297. entityView.Right = Add(entityView.Right, key, value);
  298. }
  299. else
  300. {
  301. if (IsMultiDictionary)
  302. {
  303. // Store the presence of a "duplicate" entityView
  304. entityView.Siblings++;
  305. Count++;
  306. }
  307. else
  308. {
  309. // Replace the value of the existing entityView
  310. entityView.Value = value;
  311. }
  312. }
  313. if (IsRed(entityView.Right))
  314. {
  315. // Rotate to prevent red entityView on right
  316. entityView = RotateLeft(entityView);
  317. }
  318. if (IsRed(entityView.Left) && IsRed(entityView.Left.Left))
  319. {
  320. // Rotate to prevent consecutive red entityViews
  321. entityView = RotateRight(entityView);
  322. }
  323. return entityView;
  324. }
  325. /// <summary>
  326. /// Removes the specified key/value pair from below the specified entityView.
  327. /// </summary>
  328. /// <param name="entityView">Specified entityView.</param>
  329. /// <param name="key">Key to remove.</param>
  330. /// <param name="value">Value to remove.</param>
  331. /// <returns>True if key/value present and removed.</returns>
  332. private EntityView Remove(EntityView entityView, TKey key, TValue value)
  333. {
  334. int comparisonResult = KeyAndValueComparison(key, value, entityView.Key, entityView.Value);
  335. if (comparisonResult < 0)
  336. {
  337. // * Continue search if left is present
  338. if (null != entityView.Left)
  339. {
  340. if (!IsRed(entityView.Left) && !IsRed(entityView.Left.Left))
  341. {
  342. // Move a red entityView over
  343. entityView = MoveRedLeft(entityView);
  344. }
  345. // Remove from left
  346. entityView.Left = Remove(entityView.Left, key, value);
  347. }
  348. }
  349. else
  350. {
  351. if (IsRed(entityView.Left))
  352. {
  353. // Flip a 3 entityView or unbalance a 4 entityView
  354. entityView = RotateRight(entityView);
  355. }
  356. if ((0 == KeyAndValueComparison(key, value, entityView.Key, entityView.Value)) && (null == entityView.Right))
  357. {
  358. // Remove leaf entityView
  359. Debug.Assert(null == entityView.Left, "About to remove an extra entityView.");
  360. Count--;
  361. if (0 < entityView.Siblings)
  362. {
  363. // Record the removal of the "duplicate" entityView
  364. Debug.Assert(IsMultiDictionary, "Should not have siblings if tree is not a multi-dictionary.");
  365. entityView.Siblings--;
  366. return entityView;
  367. }
  368. else
  369. {
  370. // Leaf entityView is gone
  371. return null;
  372. }
  373. }
  374. // * Continue search if right is present
  375. if (null != entityView.Right)
  376. {
  377. if (!IsRed(entityView.Right) && !IsRed(entityView.Right.Left))
  378. {
  379. // Move a red entityView over
  380. entityView = MoveRedRight(entityView);
  381. }
  382. if (0 == KeyAndValueComparison(key, value, entityView.Key, entityView.Value))
  383. {
  384. // Remove leaf entityView
  385. Count--;
  386. if (0 < entityView.Siblings)
  387. {
  388. // Record the removal of the "duplicate" entityView
  389. Debug.Assert(IsMultiDictionary, "Should not have siblings if tree is not a multi-dictionary.");
  390. entityView.Siblings--;
  391. }
  392. else
  393. {
  394. // Find the smallest entityView on the right, swap, and remove it
  395. EntityView m = GetExtreme(entityView.Right, n => n.Left, n => n);
  396. entityView.Key = m.Key;
  397. entityView.Value = m.Value;
  398. entityView.Siblings = m.Siblings;
  399. entityView.Right = DeleteMinimum(entityView.Right);
  400. }
  401. }
  402. else
  403. {
  404. // Remove from right
  405. entityView.Right = Remove(entityView.Right, key, value);
  406. }
  407. }
  408. }
  409. // Maintain invariants
  410. return FixUp(entityView);
  411. }
  412. /// <summary>
  413. /// Flip the colors of the specified entityView and its direct children.
  414. /// </summary>
  415. /// <param name="entityView">Specified entityView.</param>
  416. private static void FlipColor(EntityView entityView)
  417. {
  418. entityView.IsBlack = !entityView.IsBlack;
  419. entityView.Left.IsBlack = !entityView.Left.IsBlack;
  420. entityView.Right.IsBlack = !entityView.Right.IsBlack;
  421. }
  422. /// <summary>
  423. /// Rotate the specified entityView "left".
  424. /// </summary>
  425. /// <param name="entityView">Specified entityView.</param>
  426. /// <returns>New root entityView.</returns>
  427. private static EntityView RotateLeft(EntityView entityView)
  428. {
  429. EntityView x = entityView.Right;
  430. entityView.Right = x.Left;
  431. x.Left = entityView;
  432. x.IsBlack = entityView.IsBlack;
  433. entityView.IsBlack = false;
  434. return x;
  435. }
  436. /// <summary>
  437. /// Rotate the specified entityView "right".
  438. /// </summary>
  439. /// <param name="entityView">Specified entityView.</param>
  440. /// <returns>New root entityView.</returns>
  441. private static EntityView RotateRight(EntityView entityView)
  442. {
  443. EntityView x = entityView.Left;
  444. entityView.Left = x.Right;
  445. x.Right = entityView;
  446. x.IsBlack = entityView.IsBlack;
  447. entityView.IsBlack = false;
  448. return x;
  449. }
  450. /// <summary>
  451. /// Moves a red entityView from the right child to the left child.
  452. /// </summary>
  453. /// <param name="entityView">Parent entityView.</param>
  454. /// <returns>New root entityView.</returns>
  455. private static EntityView MoveRedLeft(EntityView entityView)
  456. {
  457. FlipColor(entityView);
  458. if (IsRed(entityView.Right.Left))
  459. {
  460. entityView.Right = RotateRight(entityView.Right);
  461. entityView = RotateLeft(entityView);
  462. FlipColor(entityView);
  463. // * Avoid creating right-leaning entityViews
  464. if (IsRed(entityView.Right.Right))
  465. {
  466. entityView.Right = RotateLeft(entityView.Right);
  467. }
  468. }
  469. return entityView;
  470. }
  471. /// <summary>
  472. /// Moves a red entityView from the left child to the right child.
  473. /// </summary>
  474. /// <param name="entityView">Parent entityView.</param>
  475. /// <returns>New root entityView.</returns>
  476. private static EntityView MoveRedRight(EntityView entityView)
  477. {
  478. FlipColor(entityView);
  479. if (IsRed(entityView.Left.Left))
  480. {
  481. entityView = RotateRight(entityView);
  482. FlipColor(entityView);
  483. }
  484. return entityView;
  485. }
  486. /// <summary>
  487. /// Deletes the minimum entityView under the specified entityView.
  488. /// </summary>
  489. /// <param name="entityView">Specified entityView.</param>
  490. /// <returns>New root entityView.</returns>
  491. private EntityView DeleteMinimum(EntityView entityView)
  492. {
  493. if (null == entityView.Left)
  494. {
  495. // Nothing to do
  496. return null;
  497. }
  498. if (!IsRed(entityView.Left) && !IsRed(entityView.Left.Left))
  499. {
  500. // Move red entityView left
  501. entityView = MoveRedLeft(entityView);
  502. }
  503. // Recursively delete
  504. entityView.Left = DeleteMinimum(entityView.Left);
  505. // Maintain invariants
  506. return FixUp(entityView);
  507. }
  508. /// <summary>
  509. /// Maintains invariants by adjusting the specified entityViews children.
  510. /// </summary>
  511. /// <param name="entityView">Specified entityView.</param>
  512. /// <returns>New root entityView.</returns>
  513. private static EntityView FixUp(EntityView entityView)
  514. {
  515. if (IsRed(entityView.Right))
  516. {
  517. // Avoid right-leaning entityView
  518. entityView = RotateLeft(entityView);
  519. }
  520. if (IsRed(entityView.Left) && IsRed(entityView.Left.Left))
  521. {
  522. // Balance 4-entityView
  523. entityView = RotateRight(entityView);
  524. }
  525. if (IsRed(entityView.Left) && IsRed(entityView.Right))
  526. {
  527. // Push red up
  528. FlipColor(entityView);
  529. }
  530. // * Avoid leaving behind right-leaning entityViews
  531. if ((null != entityView.Left) && IsRed(entityView.Left.Right) && !IsRed(entityView.Left.Left))
  532. {
  533. entityView.Left = RotateLeft(entityView.Left);
  534. if (IsRed(entityView.Left))
  535. {
  536. // Balance 4-entityView
  537. entityView = RotateRight(entityView);
  538. }
  539. }
  540. return entityView;
  541. }
  542. /// <summary>
  543. /// Gets the (first) entityView corresponding to the specified key.
  544. /// </summary>
  545. /// <param name="key">Key to search for.</param>
  546. /// <returns>Corresponding entityView or null if none found.</returns>
  547. private EntityView GetEntityViewForKey(TKey key)
  548. {
  549. // Initialize
  550. EntityView entityView = _rootEntityView;
  551. while (null != entityView)
  552. {
  553. // Compare keys and go left/right
  554. int comparisonResult = _keyComparison(key, entityView.Key);
  555. if (comparisonResult < 0)
  556. {
  557. entityView = entityView.Left;
  558. }
  559. else if (0 < comparisonResult)
  560. {
  561. entityView = entityView.Right;
  562. }
  563. else
  564. {
  565. // Match; return entityView
  566. return entityView;
  567. }
  568. }
  569. // No match found
  570. return null;
  571. }
  572. /// <summary>
  573. /// Gets an extreme (ex: minimum/maximum) value.
  574. /// </summary>
  575. /// <typeparam name="T">Type of value.</typeparam>
  576. /// <param name="entityView">EntityView to start from.</param>
  577. /// <param name="successor">Successor function.</param>
  578. /// <param name="selector">Selector function.</param>
  579. /// <returns>Extreme value.</returns>
  580. private static T GetExtreme<T>(EntityView entityView, Func<EntityView, EntityView> successor, Func<EntityView, T> selector)
  581. {
  582. // Initialize
  583. T extreme = default(T);
  584. EntityView current = entityView;
  585. while (null != current)
  586. {
  587. // Go to extreme
  588. extreme = selector(current);
  589. current = successor(current);
  590. }
  591. return extreme;
  592. }
  593. /// <summary>
  594. /// Traverses a subset of the sequence of entityViews in order and selects the specified entityViews.
  595. /// </summary>
  596. /// <typeparam name="T">Type of elements.</typeparam>
  597. /// <param name="entityView">Starting entityView.</param>
  598. /// <param name="condition">Condition method.</param>
  599. /// <param name="selector">Selector method.</param>
  600. /// <returns>Sequence of selected entityViews.</returns>
  601. private IEnumerable<T> Traverse<T>(EntityView entityView, Func<EntityView, bool> condition, Func<EntityView, T> selector)
  602. {
  603. // Create a stack to avoid recursion
  604. Stack<EntityView> stack = new Stack<EntityView>();
  605. EntityView current = entityView;
  606. while (null != current)
  607. {
  608. if (null != current.Left)
  609. {
  610. // Save current state and go left
  611. stack.Push(current);
  612. current = current.Left;
  613. }
  614. else
  615. {
  616. do
  617. {
  618. for (int i = 0; i <= current.Siblings; i++)
  619. {
  620. // Select current entityView if relevant
  621. if (condition(current))
  622. {
  623. yield return selector(current);
  624. }
  625. }
  626. // Go right - or up if nothing to the right
  627. current = current.Right;
  628. }
  629. while ((null == current) &&
  630. (0 < stack.Count) &&
  631. (null != (current = stack.Pop())));
  632. }
  633. }
  634. }
  635. /// <summary>
  636. /// Compares the specified keys (primary) and values (secondary).
  637. /// </summary>
  638. /// <param name="leftKey">The left key.</param>
  639. /// <param name="leftValue">The left value.</param>
  640. /// <param name="rightKey">The right key.</param>
  641. /// <param name="rightValue">The right value.</param>
  642. /// <returns>CompareTo-style results: -1 if left is less, 0 if equal, and 1 if greater than right.</returns>
  643. private int KeyAndValueComparison(TKey leftKey, TValue leftValue, TKey rightKey, TValue rightValue)
  644. {
  645. // Compare keys
  646. int comparisonResult = _keyComparison(leftKey, rightKey);
  647. if ((0 == comparisonResult) && (null != _valueComparison))
  648. {
  649. // Keys match; compare values
  650. comparisonResult = _valueComparison(leftValue, rightValue);
  651. }
  652. return comparisonResult;
  653. }
  654. #if DEBUGGING
  655. /// <summary>
  656. /// Asserts that tree invariants are not violated.
  657. /// </summary>
  658. private void AssertInvariants()
  659. {
  660. // Root is black
  661. Debug.Assert((null == _rootEntityView) || _rootEntityView.IsBlack, "Root is not black");
  662. // Every path contains the same number of black entityViews
  663. Dictionary<EntityView, EntityView> parents = new Dictionary<LeftLeaningRedBlackTree<TKey, TValue>.EntityView, LeftLeaningRedBlackTree<TKey, TValue>.EntityView>();
  664. foreach (EntityView entityView in Traverse(_rootEntityView, n => true, n => n))
  665. {
  666. if (null != entityView.Left)
  667. {
  668. parents[entityView.Left] = entityView;
  669. }
  670. if (null != entityView.Right)
  671. {
  672. parents[entityView.Right] = entityView;
  673. }
  674. }
  675. if (null != _rootEntityView)
  676. {
  677. parents[_rootEntityView] = null;
  678. }
  679. int treeCount = -1;
  680. foreach (EntityView entityView in Traverse(_rootEntityView, n => (null == n.Left) || (null == n.Right), n => n))
  681. {
  682. int pathCount = 0;
  683. EntityView current = entityView;
  684. while (null != current)
  685. {
  686. if (current.IsBlack)
  687. {
  688. pathCount++;
  689. }
  690. current = parents[current];
  691. }
  692. Debug.Assert((-1 == treeCount) || (pathCount == treeCount), "Not all paths have the same number of black entityViews.");
  693. treeCount = pathCount;
  694. }
  695. // Verify entityView properties...
  696. foreach (EntityView entityView in Traverse(_rootEntityView, n => true, n => n))
  697. {
  698. // Left entityView is less
  699. if (null != entityView.Left)
  700. {
  701. Debug.Assert(0 > KeyAndValueComparison(entityView.Left.Key, entityView.Left.Value, entityView.Key, entityView.Value), "Left entityView is greater than its parent.");
  702. }
  703. // Right entityView is greater
  704. if (null != entityView.Right)
  705. {
  706. Debug.Assert(0 < KeyAndValueComparison(entityView.Right.Key, entityView.Right.Value, entityView.Key, entityView.Value), "Right entityView is less than its parent.");
  707. }
  708. // Both children of a red entityView are black
  709. Debug.Assert(!IsRed(entityView) || (!IsRed(entityView.Left) && !IsRed(entityView.Right)), "Red entityView has a red child.");
  710. // Always left-leaning
  711. Debug.Assert(!IsRed(entityView.Right) || IsRed(entityView.Left), "EntityView is not left-leaning.");
  712. // No consecutive reds (subset of previous rule)
  713. //Debug.Assert(!(IsRed(entityView) && IsRed(entityView.Left)));
  714. }
  715. }
  716. /// <summary>
  717. /// Gets an HTML fragment representing the tree.
  718. /// </summary>
  719. public string HtmlDocument
  720. {
  721. get
  722. {
  723. return
  724. "<html>" +
  725. "<body>" +
  726. (null != _rootEntityView ? _rootEntityView.HtmlFragment : "[null]") +
  727. "</body>" +
  728. "</html>";
  729. }
  730. }
  731. #endif
  732. }