QueryIntervalOp.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Runtime;
  9. // Based on the Paper: The IBS Tree: A Datastructure for finding all intervals that overlap a point.
  10. // by Eric Hanson & Moez Chaabouni, Nov, 1994
  11. internal enum IntervalOp : byte
  12. {
  13. LessThan,
  14. LessThanEquals
  15. }
  16. internal class Interval
  17. {
  18. QueryBranch branch;
  19. double lowerBound;
  20. IntervalOp lowerOp;
  21. double upperBound;
  22. IntervalOp upperOp;
  23. // Converts expressions of the form:
  24. // x < 5 => -infinity <= x and x < 5
  25. // x <= 5 => -infinity <= x and x <= 5
  26. // x > 5 => 5 < x <= infinity
  27. // x >= 5 => 5 <= x <= infinity
  28. //
  29. // The variable is always to the left
  30. internal Interval(double literal, RelationOperator op)
  31. {
  32. this.lowerBound = double.MinValue;
  33. this.upperBound = double.MaxValue;
  34. this.lowerOp = IntervalOp.LessThanEquals;
  35. this.upperOp = IntervalOp.LessThanEquals;
  36. Fx.Assert(RelationOperator.Eq != op && RelationOperator.Ne != op, "");
  37. switch (op)
  38. {
  39. case RelationOperator.Lt:
  40. this.upperBound = literal;
  41. this.upperOp = IntervalOp.LessThan;
  42. break;
  43. case RelationOperator.Le:
  44. this.upperBound = literal;
  45. break;
  46. case RelationOperator.Gt:
  47. this.lowerBound = literal;
  48. this.lowerOp = IntervalOp.LessThan;
  49. break;
  50. case RelationOperator.Ge:
  51. this.lowerBound = literal;
  52. break;
  53. }
  54. }
  55. #if NO
  56. internal Interval(double lowerBound, IntervalOp lowerOp, double upperBound, IntervalOp upperOp)
  57. {
  58. Fx.Assert(lowerBound <= upperBound, "");
  59. this.lowerBound = lowerBound;
  60. this.upperBound = upperBound;
  61. if (this.lowerBound == this.upperBound)
  62. {
  63. this.lowerOp = IntervalOp.LessThanEquals;
  64. this.upperOp = IntervalOp.LessThanEquals;
  65. }
  66. else
  67. {
  68. this.lowerOp = lowerOp;
  69. this.upperOp = upperOp;
  70. }
  71. }
  72. #endif
  73. internal QueryBranch Branch
  74. {
  75. get
  76. {
  77. return this.branch;
  78. }
  79. set
  80. {
  81. this.branch = value;
  82. }
  83. }
  84. internal double LowerBound
  85. {
  86. get
  87. {
  88. return this.lowerBound;
  89. }
  90. }
  91. internal IntervalOp LowerOp
  92. {
  93. get
  94. {
  95. return this.lowerOp;
  96. }
  97. }
  98. internal double UpperBound
  99. {
  100. get
  101. {
  102. return this.upperBound;
  103. }
  104. }
  105. internal IntervalOp UpperOp
  106. {
  107. get
  108. {
  109. return this.upperOp;
  110. }
  111. }
  112. #if NO
  113. internal bool Equals(Interval interval)
  114. {
  115. Fx.Assert(null != interval, "");
  116. return this.Equals(interval.lowerBound, interval.lowerOp, interval.upperBound, interval.upperOp);
  117. }
  118. #endif
  119. internal bool Equals(double lowerBound, IntervalOp lowerOp, double upperBound, IntervalOp upperOp)
  120. {
  121. return (this.lowerBound == lowerBound && this.lowerOp == lowerOp && this.upperBound == upperBound && this.upperOp == upperOp);
  122. }
  123. internal bool HasMatchingEndPoint(double endpoint)
  124. {
  125. return (this.lowerBound == endpoint || this.upperBound == endpoint);
  126. }
  127. }
  128. /// <summary>
  129. /// IntervalCollection
  130. /// All Contains/Find operations are currently linear, since they are required only during
  131. /// Inserts/Removes from the Interval Tree, which is not anticipated to be performance critical.
  132. /// </summary>
  133. internal class IntervalCollection : ArrayList
  134. {
  135. internal IntervalCollection()
  136. : base(1)
  137. {
  138. }
  139. internal bool HasIntervals
  140. {
  141. get
  142. {
  143. return (this.Count > 0);
  144. }
  145. }
  146. internal new Interval this[int index]
  147. {
  148. get
  149. {
  150. return (Interval)base[index];
  151. }
  152. }
  153. internal int Add(Interval interval)
  154. {
  155. Fx.Assert(null != interval, "");
  156. this.Capacity = this.Count + 1;
  157. return base.Add(interval);
  158. }
  159. internal int AddUnique(Interval interval)
  160. {
  161. Fx.Assert(null != interval, "");
  162. int index = this.IndexOf(interval);
  163. if (-1 == index)
  164. {
  165. return this.Add(interval);
  166. }
  167. return index;
  168. }
  169. internal IntervalCollection GetIntervalsWithEndPoint(double endPoint)
  170. {
  171. IntervalCollection matches = new IntervalCollection();
  172. int count = this.Count;
  173. for (int i = 0; i < count; ++i)
  174. {
  175. Interval interval = this[i];
  176. if (interval.HasMatchingEndPoint(endPoint))
  177. {
  178. matches.Add(interval);
  179. }
  180. }
  181. return matches;
  182. }
  183. internal int IndexOf(Interval interval)
  184. {
  185. Fx.Assert(null != interval, "");
  186. return base.IndexOf(interval);
  187. }
  188. internal int IndexOf(double endPoint)
  189. {
  190. int count = this.Count;
  191. for (int i = 0; i < count; ++i)
  192. {
  193. Interval interval = this[i];
  194. if (interval.HasMatchingEndPoint(endPoint))
  195. {
  196. return i;
  197. }
  198. }
  199. return -1;
  200. }
  201. internal int IndexOf(double lowerBound, IntervalOp lowerOp, double upperBound, IntervalOp upperOp)
  202. {
  203. int count = this.Count;
  204. for (int i = 0; i < count; ++i)
  205. {
  206. if (this[i].Equals(lowerBound, lowerOp, upperBound, upperOp))
  207. {
  208. return i;
  209. }
  210. }
  211. return -1;
  212. }
  213. internal void Remove(Interval interval)
  214. {
  215. Fx.Assert(null != interval, "");
  216. base.Remove(interval);
  217. this.TrimToSize();
  218. }
  219. internal void Trim()
  220. {
  221. this.TrimToSize();
  222. }
  223. }
  224. internal class IntervalBoundary
  225. {
  226. IntervalCollection eqSlot;
  227. IntervalCollection gtSlot;
  228. IntervalBoundary left;
  229. IntervalCollection ltSlot;
  230. IntervalBoundary parent;
  231. IntervalBoundary right;
  232. double val;
  233. internal IntervalBoundary(double val, IntervalBoundary parent)
  234. {
  235. this.val = val;
  236. this.parent = parent;
  237. }
  238. internal IntervalCollection EqSlot
  239. {
  240. get
  241. {
  242. return this.eqSlot;
  243. }
  244. }
  245. internal IntervalCollection GtSlot
  246. {
  247. get
  248. {
  249. return this.gtSlot;
  250. }
  251. }
  252. internal IntervalBoundary Left
  253. {
  254. get
  255. {
  256. return this.left;
  257. }
  258. set
  259. {
  260. this.left = value;
  261. }
  262. }
  263. internal IntervalCollection LtSlot
  264. {
  265. get
  266. {
  267. return this.ltSlot;
  268. }
  269. }
  270. internal IntervalBoundary Parent
  271. {
  272. get
  273. {
  274. return this.parent;
  275. }
  276. set
  277. {
  278. this.parent = value;
  279. }
  280. }
  281. internal IntervalBoundary Right
  282. {
  283. get
  284. {
  285. return this.right;
  286. }
  287. set
  288. {
  289. this.right = value;
  290. }
  291. }
  292. internal double Value
  293. {
  294. get
  295. {
  296. return this.val;
  297. }
  298. set
  299. {
  300. this.val = value;
  301. }
  302. }
  303. internal void AddToEqSlot(Interval interval)
  304. {
  305. Fx.Assert(null != interval, "");
  306. this.AddToSlot(ref this.eqSlot, interval);
  307. }
  308. internal void AddToGtSlot(Interval interval)
  309. {
  310. Fx.Assert(null != interval, "");
  311. this.AddToSlot(ref this.gtSlot, interval);
  312. }
  313. internal void AddToLtSlot(Interval interval)
  314. {
  315. Fx.Assert(null != interval, "");
  316. this.AddToSlot(ref this.ltSlot, interval);
  317. }
  318. void AddToSlot(ref IntervalCollection slot, Interval interval)
  319. {
  320. if (null == slot)
  321. {
  322. slot = new IntervalCollection();
  323. }
  324. slot.AddUnique(interval);
  325. }
  326. internal IntervalBoundary EnsureLeft(double val)
  327. {
  328. if (null == this.left)
  329. {
  330. this.left = new IntervalBoundary(val, this);
  331. }
  332. return this.left;
  333. }
  334. internal IntervalBoundary EnsureRight(double val)
  335. {
  336. if (null == this.right)
  337. {
  338. this.right = new IntervalBoundary(val, this);
  339. }
  340. return this.right;
  341. }
  342. #if NO
  343. internal Interval GetInterval(double lowerBound, IntervalOp lowerOp, double upperBound, IntervalOp upperOp)
  344. {
  345. Interval interval;
  346. if (
  347. null != (interval = this.GetIntervalFromSlot(this.eqSlot, lowerBound, lowerOp, upperBound, upperOp))
  348. || null != (interval = this.GetIntervalFromSlot(this.ltSlot, lowerBound, lowerOp, upperBound, upperOp))
  349. || null != (interval = this.GetIntervalFromSlot(this.gtSlot, lowerBound, lowerOp, upperBound, upperOp))
  350. )
  351. {
  352. return interval;
  353. }
  354. return null;
  355. }
  356. internal Interval GetIntervalByData(object data)
  357. {
  358. Interval interval;
  359. if (
  360. null != (interval = this.GetIntervalFromSlot(this.eqSlot, data))
  361. || null != (interval = this.GetIntervalFromSlot(this.ltSlot, data))
  362. || null != (interval = this.GetIntervalFromSlot(this.gtSlot, data))
  363. )
  364. {
  365. return interval;
  366. }
  367. return null;
  368. }
  369. Interval GetIntervalFromSlot(IntervalCollection slot, object data)
  370. {
  371. int index;
  372. if (null != slot && -1 != (index = slot.IndexOf(data)))
  373. {
  374. return slot[index];
  375. }
  376. return null;
  377. }
  378. Interval GetIntervalFromSlot(IntervalCollection slot, double lowerBound, IntervalOp lowerOp, double upperBound, IntervalOp upperOp)
  379. {
  380. int index;
  381. if (null != slot && -1 != (index = slot.IndexOf(lowerBound, lowerOp, upperBound, upperOp)))
  382. {
  383. return slot[index];
  384. }
  385. return null;
  386. }
  387. #endif
  388. internal void RemoveFromEqSlot(Interval interval)
  389. {
  390. Fx.Assert(null != interval, "");
  391. this.RemoveFromSlot(ref this.eqSlot, interval);
  392. }
  393. internal void RemoveFromGtSlot(Interval interval)
  394. {
  395. Fx.Assert(null != interval, "");
  396. this.RemoveFromSlot(ref this.gtSlot, interval);
  397. }
  398. internal void RemoveFromLtSlot(Interval interval)
  399. {
  400. Fx.Assert(null != interval, "");
  401. this.RemoveFromSlot(ref this.ltSlot, interval);
  402. }
  403. void RemoveFromSlot(ref IntervalCollection slot, Interval interval)
  404. {
  405. if (null != slot)
  406. {
  407. slot.Remove(interval);
  408. if (!slot.HasIntervals)
  409. {
  410. slot = null;
  411. }
  412. }
  413. }
  414. internal void Trim()
  415. {
  416. if (this.eqSlot != null)
  417. {
  418. this.eqSlot.Trim();
  419. }
  420. if (this.gtSlot != null)
  421. {
  422. this.gtSlot.Trim();
  423. }
  424. if (this.ltSlot != null)
  425. {
  426. this.ltSlot.Trim();
  427. }
  428. if (this.left != null)
  429. {
  430. this.left.Trim();
  431. }
  432. if (this.right != null)
  433. {
  434. this.right.Trim();
  435. }
  436. }
  437. }
  438. internal struct IntervalTreeTraverser
  439. {
  440. IntervalBoundary currentNode;
  441. IntervalBoundary nextNode;
  442. IntervalCollection slot;
  443. double val;
  444. internal IntervalTreeTraverser(double val, IntervalBoundary root)
  445. {
  446. Fx.Assert(null != root, "");
  447. this.currentNode = null;
  448. this.slot = null;
  449. this.nextNode = root;
  450. this.val = val;
  451. }
  452. internal IntervalCollection Slot
  453. {
  454. get
  455. {
  456. return this.slot;
  457. }
  458. }
  459. internal bool MoveNext()
  460. {
  461. while (null != this.nextNode)
  462. {
  463. this.currentNode = this.nextNode;
  464. double currentVal = this.currentNode.Value;
  465. if (val < currentVal)
  466. {
  467. this.slot = this.currentNode.LtSlot;
  468. this.nextNode = this.currentNode.Left;
  469. }
  470. else if (val > currentVal)
  471. {
  472. this.slot = this.currentNode.GtSlot;
  473. this.nextNode = this.currentNode.Right;
  474. }
  475. else
  476. {
  477. this.slot = this.currentNode.EqSlot;
  478. this.nextNode = null;
  479. }
  480. if (null != this.slot)
  481. {
  482. return true;
  483. }
  484. }
  485. return false;
  486. }
  487. }
  488. internal class IntervalTree
  489. {
  490. IntervalCollection intervals;
  491. IntervalBoundary root;
  492. internal IntervalTree()
  493. {
  494. }
  495. internal int Count
  496. {
  497. get
  498. {
  499. return (null != this.intervals) ? this.intervals.Count : 0;
  500. }
  501. }
  502. internal IntervalCollection Intervals
  503. {
  504. get
  505. {
  506. if (null == this.intervals)
  507. {
  508. return new IntervalCollection();
  509. }
  510. return this.intervals;
  511. }
  512. }
  513. #if NO
  514. internal bool IsEmpty
  515. {
  516. get
  517. {
  518. return (this.root == null);
  519. }
  520. }
  521. #endif
  522. internal IntervalBoundary Root
  523. {
  524. get
  525. {
  526. return this.root;
  527. }
  528. }
  529. internal void Add(Interval interval)
  530. {
  531. Fx.Assert(null != interval, "");
  532. this.AddIntervalToTree(interval);
  533. this.EnsureIntervals();
  534. this.intervals.Add(interval);
  535. }
  536. void AddIntervalToTree(Interval interval)
  537. {
  538. this.EditLeft(interval, true);
  539. this.EditRight(interval, true);
  540. }
  541. #if NO
  542. internal bool Contains(double lowerBound, IntervalOp lowerOp, double upperBound, IntervalOp upperOp)
  543. {
  544. if (null != this.intervals)
  545. {
  546. return (this.intervals.IndexOf(lowerBound, lowerOp, upperBound, upperOp) >= 0);
  547. }
  548. return false;
  549. }
  550. #endif
  551. void EditLeft(Interval interval, bool add)
  552. {
  553. if (add)
  554. {
  555. this.EnsureRoot(interval.LowerBound);
  556. }
  557. IntervalBoundary root = this.root;
  558. IntervalBoundary leftAncestor = null;
  559. while (true)
  560. {
  561. double rootVal = root.Value;
  562. if (rootVal < interval.LowerBound)
  563. {
  564. // root is outside the interval range because it is < the lower bound
  565. root = add ? root.EnsureRight(interval.LowerBound) : root.Right;
  566. continue;
  567. }
  568. // rootVal is >= to interval.lowerBound
  569. //
  570. // All values in thee subtree at 'root' are < leftAncestor.Value
  571. // All values to the left of this node cannot be in the interval because they are < the interval's
  572. // lower bound.
  573. // Values to the right of this node lie in range (root.Value to leftAncestor.Value)
  574. // Thus, the entire right subtree of root will be inside the range if the interval.upperBound
  575. // is >= leftAncestor.Value
  576. if (null != leftAncestor && leftAncestor.Value <= interval.UpperBound)
  577. {
  578. if (add)
  579. {
  580. root.AddToGtSlot(interval);
  581. }
  582. else
  583. {
  584. root.RemoveFromGtSlot(interval);
  585. }
  586. }
  587. if (rootVal > interval.LowerBound)
  588. { // This node itself lies in the range if it is also < the upper bound
  589. if (rootVal < interval.UpperBound)
  590. {
  591. if (add)
  592. {
  593. root.AddToEqSlot(interval);
  594. }
  595. else
  596. {
  597. root.RemoveFromEqSlot(interval);
  598. }
  599. }
  600. leftAncestor = root;
  601. root = add ? root.EnsureLeft(interval.LowerBound) : root.Left;
  602. continue;
  603. }
  604. // lowerBound == rootVal. We're done.
  605. if (IntervalOp.LessThanEquals == interval.LowerOp)
  606. {
  607. // If the range is inclusive of the lower bound (>=), then since this node == lowerBound,
  608. // it must be in the range.
  609. if (add)
  610. {
  611. root.AddToEqSlot(interval);
  612. }
  613. else
  614. {
  615. root.RemoveFromEqSlot(interval);
  616. }
  617. }
  618. break;
  619. }
  620. }
  621. void EditRight(Interval interval, bool add)
  622. {
  623. if (add)
  624. {
  625. this.EnsureRoot(interval.UpperBound);
  626. }
  627. IntervalBoundary root = this.root;
  628. IntervalBoundary rightAncestor = null;
  629. while (true)
  630. {
  631. double rootVal = root.Value;
  632. if (rootVal > interval.UpperBound)
  633. {
  634. // root is outside the interval range because it is > the upper bound
  635. root = add ? root.EnsureLeft(interval.UpperBound) : root.Left;
  636. continue;
  637. }
  638. // rootVal is <= to interval.UpperBound
  639. //
  640. // All values in the subtree at 'root' are > leftAncestor.Value
  641. // All values to the right of this node cannot be in the interval because they are > the interval's
  642. // upper bound.
  643. // Values to the left of this node lie in range (rightAncestor.Value to root.Value)
  644. // Thus, the entire left subtree of root will be inside the range if the interval.lowerBound
  645. // is <= rightAncestor.Value
  646. if (null != rightAncestor && rightAncestor.Value >= interval.LowerBound)
  647. {
  648. if (add)
  649. {
  650. root.AddToLtSlot(interval);
  651. }
  652. else
  653. {
  654. root.RemoveFromLtSlot(interval);
  655. }
  656. }
  657. if (rootVal < interval.UpperBound)
  658. {
  659. // This node itself lies in the range if it is also > the lower bound
  660. if (rootVal > interval.LowerBound)
  661. {
  662. if (add)
  663. {
  664. root.AddToEqSlot(interval);
  665. }
  666. else
  667. {
  668. root.RemoveFromEqSlot(interval);
  669. }
  670. }
  671. rightAncestor = root;
  672. root = add ? root.EnsureRight(interval.UpperBound) : root.Right;
  673. continue;
  674. }
  675. // upperBound == rootVal. We're done.
  676. // If upperBound == lowerBound, we already inserted this when doing AddLeft
  677. if (IntervalOp.LessThanEquals == interval.UpperOp)
  678. {
  679. // If the range is inclusive of the upper bound, then since this node == upperBound,
  680. // it must be in the range.
  681. if (add)
  682. {
  683. root.AddToEqSlot(interval);
  684. }
  685. else
  686. {
  687. root.RemoveFromEqSlot(interval);
  688. }
  689. }
  690. break;
  691. }
  692. }
  693. void EnsureIntervals()
  694. {
  695. if (null == this.intervals)
  696. {
  697. this.intervals = new IntervalCollection();
  698. }
  699. }
  700. void EnsureRoot(double val)
  701. {
  702. if (null == this.root)
  703. {
  704. this.root = new IntervalBoundary(val, null);
  705. }
  706. }
  707. internal IntervalBoundary FindBoundaryNode(double val)
  708. {
  709. return this.FindBoundaryNode(root, val);
  710. }
  711. internal IntervalBoundary FindBoundaryNode(IntervalBoundary root, double val)
  712. {
  713. IntervalBoundary boundary = null;
  714. if (null != root)
  715. {
  716. if (root.Value == val)
  717. {
  718. boundary = root;
  719. }
  720. else
  721. {
  722. if (null == (boundary = this.FindBoundaryNode(root.Left, val)))
  723. {
  724. boundary = this.FindBoundaryNode(root.Right, val);
  725. }
  726. }
  727. }
  728. return boundary;
  729. }
  730. internal Interval FindInterval(Interval interval)
  731. {
  732. return this.FindInterval(interval.LowerBound, interval.LowerOp, interval.UpperBound, interval.UpperOp);
  733. }
  734. internal Interval FindInterval(double lowerBound, IntervalOp lowerOp, double upperBound, IntervalOp upperOp)
  735. {
  736. if (null != this.intervals)
  737. {
  738. int index;
  739. if (-1 != (index = this.intervals.IndexOf(lowerBound, lowerOp, upperBound, upperOp)))
  740. {
  741. return this.intervals[index];
  742. }
  743. }
  744. return null;
  745. }
  746. /// <summary>
  747. /// An interval has been removed. Prune the tree appropriately
  748. /// </summary>
  749. /// <param name="intervalRemoved">interval that was removed</param>
  750. void PruneTree(Interval intervalRemoved)
  751. {
  752. // Delete endpoints if no other intervals have them
  753. int index;
  754. if (-1 == (index = this.intervals.IndexOf(intervalRemoved.LowerBound)))
  755. {
  756. this.RemoveBoundary(this.FindBoundaryNode(intervalRemoved.LowerBound));
  757. }
  758. if (intervalRemoved.LowerBound != intervalRemoved.UpperBound && -1 == (index = this.intervals.IndexOf(intervalRemoved.UpperBound)))
  759. {
  760. this.RemoveBoundary(this.FindBoundaryNode(intervalRemoved.UpperBound));
  761. }
  762. }
  763. internal void Remove(Interval interval)
  764. {
  765. Fx.Assert(null != interval, "");
  766. Fx.Assert(null != this.intervals, "");
  767. // First, delete all occurences of interval in the tree. Note: we do a reference equals
  768. this.RemoveIntervalFromTree(interval);
  769. // Remove interval from interval collection
  770. this.intervals.Remove(interval);
  771. // It may be possible to prune the tree.. this will do the necessary, if required
  772. this.PruneTree(interval);
  773. }
  774. void RemoveBoundary(IntervalBoundary boundary)
  775. {
  776. IntervalCollection replacementIntervals = null;
  777. int replacementCount = 0;
  778. if (null != boundary.Left && null != boundary.Right)
  779. {
  780. // Neither left/right are null. Typical binary tree node removal - replace the removed node
  781. // with the symmetric order predecessor
  782. IntervalBoundary replacement = boundary.Left;
  783. while (null != replacement.Right)
  784. {
  785. replacement = replacement.Right;
  786. }
  787. // Find all intervals with endpoint y in the tree
  788. replacementIntervals = this.intervals.GetIntervalsWithEndPoint(replacement.Value);
  789. // Remove the intervals from the tree
  790. replacementCount = replacementIntervals.Count;
  791. for (int i = 0; i < replacementCount; ++i)
  792. {
  793. this.RemoveIntervalFromTree(replacementIntervals[i]);
  794. }
  795. double val = boundary.Value;
  796. boundary.Value = replacement.Value;
  797. replacement.Value = val;
  798. boundary = replacement;
  799. }
  800. if (null != boundary.Left)
  801. {
  802. this.Replace(boundary, boundary.Left);
  803. }
  804. else
  805. {
  806. this.Replace(boundary, boundary.Right);
  807. }
  808. // Discard the node
  809. boundary.Parent = null;
  810. boundary.Left = null;
  811. boundary.Right = null;
  812. // Reinstall Intervals
  813. for (int i = 0; i < replacementCount; ++i)
  814. {
  815. this.AddIntervalToTree(replacementIntervals[i]);
  816. }
  817. }
  818. void RemoveIntervalFromTree(Interval interval)
  819. {
  820. this.EditLeft(interval, false);
  821. this.EditRight(interval, false);
  822. }
  823. void Replace(IntervalBoundary replace, IntervalBoundary with)
  824. {
  825. IntervalBoundary parent = replace.Parent;
  826. if (null != parent)
  827. {
  828. if (replace == parent.Left)
  829. {
  830. parent.Left = with;
  831. }
  832. else if (replace == parent.Right)
  833. {
  834. parent.Right = with;
  835. }
  836. }
  837. else
  838. {
  839. // Replacing root
  840. this.root = with;
  841. }
  842. if (null != with)
  843. {
  844. with.Parent = parent;
  845. }
  846. }
  847. internal void Trim()
  848. {
  849. this.intervals.Trim();
  850. this.root.Trim();
  851. }
  852. }
  853. internal class NumberRelationOpcode : LiteralRelationOpcode
  854. {
  855. double literal;
  856. RelationOperator op;
  857. internal NumberRelationOpcode(double literal, RelationOperator op)
  858. : this(OpcodeID.NumberRelation, literal, op)
  859. {
  860. }
  861. protected NumberRelationOpcode(OpcodeID id, double literal, RelationOperator op)
  862. : base(id)
  863. {
  864. this.literal = literal;
  865. this.op = op;
  866. }
  867. #if NO
  868. internal override ValueDataType DataType
  869. {
  870. get
  871. {
  872. return ValueDataType.Double;
  873. }
  874. }
  875. #endif
  876. internal override object Literal
  877. {
  878. get
  879. {
  880. return this.literal;
  881. }
  882. }
  883. #if NO
  884. internal RelationOperator Op
  885. {
  886. get
  887. {
  888. return this.op;
  889. }
  890. }
  891. #endif
  892. internal override bool Equals(Opcode opcode)
  893. {
  894. if (base.Equals(opcode))
  895. {
  896. NumberRelationOpcode numOp = (NumberRelationOpcode)opcode;
  897. return (numOp.op == this.op && numOp.literal == this.literal);
  898. }
  899. return false;
  900. }
  901. internal override Opcode Eval(ProcessingContext context)
  902. {
  903. Value[] values = context.Values;
  904. StackFrame arg = context.TopArg;
  905. for (int i = arg.basePtr; i <= arg.endPtr; ++i)
  906. {
  907. values[i].Update(context, values[i].CompareTo(this.literal, op));
  908. }
  909. return this.next;
  910. }
  911. internal Interval ToInterval()
  912. {
  913. return new Interval(this.literal, this.op);
  914. }
  915. }
  916. internal class NumberIntervalOpcode : NumberRelationOpcode
  917. {
  918. Interval interval;
  919. internal NumberIntervalOpcode(double literal, RelationOperator op)
  920. : base(OpcodeID.NumberInterval, literal, op)
  921. {
  922. }
  923. internal override object Literal
  924. {
  925. get
  926. {
  927. if (null == this.interval)
  928. {
  929. this.interval = this.ToInterval();
  930. }
  931. return this.interval;
  932. }
  933. }
  934. internal override void Add(Opcode op)
  935. {
  936. NumberIntervalOpcode intervalOp = op as NumberIntervalOpcode;
  937. if (null == intervalOp)
  938. {
  939. base.Add(op);
  940. return;
  941. }
  942. Fx.Assert(null != this.prev, "");
  943. NumberIntervalBranchOpcode branch = new NumberIntervalBranchOpcode();
  944. this.prev.Replace(this, branch);
  945. branch.Add(this);
  946. branch.Add(intervalOp);
  947. }
  948. }
  949. internal class IntervalBranchIndex : QueryBranchIndex
  950. {
  951. IntervalTree intervalTree;
  952. internal IntervalBranchIndex()
  953. {
  954. this.intervalTree = new IntervalTree();
  955. }
  956. internal override int Count
  957. {
  958. get
  959. {
  960. return this.intervalTree.Count;
  961. }
  962. }
  963. internal override QueryBranch this[object key]
  964. {
  965. get
  966. {
  967. Interval interval = this.intervalTree.FindInterval((Interval)key);
  968. if (null != interval)
  969. {
  970. return interval.Branch;
  971. }
  972. return null;
  973. }
  974. set
  975. {
  976. Interval interval = (Interval)key;
  977. interval.Branch = value;
  978. this.intervalTree.Add(interval);
  979. }
  980. }
  981. internal override void CollectXPathFilters(ICollection<MessageFilter> filters)
  982. {
  983. for (int i = 0; i < this.intervalTree.Intervals.Count; ++i)
  984. {
  985. this.intervalTree.Intervals[i].Branch.Branch.CollectXPathFilters(filters);
  986. }
  987. }
  988. #if NO
  989. internal override IEnumerator GetEnumerator()
  990. {
  991. return this.intervalTree.Intervals.GetEnumerator();
  992. }
  993. #endif
  994. void Match(int valIndex, double point, QueryBranchResultSet results)
  995. {
  996. IntervalTreeTraverser traverser = new IntervalTreeTraverser(point, this.intervalTree.Root);
  997. while (traverser.MoveNext())
  998. {
  999. IntervalCollection matches = traverser.Slot;
  1000. for (int i = 0, count = matches.Count; i < count; ++i)
  1001. {
  1002. QueryBranch branch = matches[i].Branch;
  1003. if (null != branch)
  1004. {
  1005. results.Add(branch, valIndex);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. internal override void Match(int valIndex, ref Value val, QueryBranchResultSet results)
  1011. {
  1012. if (ValueDataType.Sequence == val.Type)
  1013. {
  1014. NodeSequence sequence = val.Sequence;
  1015. for (int i = 0; i < sequence.Count; ++i)
  1016. {
  1017. this.Match(valIndex, sequence.Items[i].NumberValue(), results);
  1018. }
  1019. }
  1020. else
  1021. {
  1022. this.Match(valIndex, val.ToDouble(), results);
  1023. }
  1024. }
  1025. internal override void Remove(object key)
  1026. {
  1027. this.intervalTree.Remove((Interval)key);
  1028. }
  1029. internal override void Trim()
  1030. {
  1031. this.intervalTree.Trim();
  1032. }
  1033. }
  1034. internal class NumberIntervalBranchOpcode : QueryConditionalBranchOpcode
  1035. {
  1036. internal NumberIntervalBranchOpcode()
  1037. : base(OpcodeID.NumberIntervalBranch, new IntervalBranchIndex())
  1038. {
  1039. }
  1040. internal override LiteralRelationOpcode ValidateOpcode(Opcode opcode)
  1041. {
  1042. NumberIntervalOpcode numOp = opcode as NumberIntervalOpcode;
  1043. if (null != numOp)
  1044. {
  1045. return numOp;
  1046. }
  1047. return null;
  1048. }
  1049. }
  1050. }