QueryUtil.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Runtime;
  10. #if NO
  11. internal interface IQueryBufferPool
  12. {
  13. // Clear all pools
  14. void Reset();
  15. // Trim pools
  16. void Trim();
  17. }
  18. #endif
  19. //
  20. // Generic struct representing ranges within buffers
  21. //
  22. internal struct QueryRange
  23. {
  24. internal int end; // INCLUSIVE - the end of the range
  25. internal int start; // INCLUSIVE - the start of the range
  26. #if NO
  27. internal QueryRange(int offset, QueryRange range)
  28. {
  29. this.start = range.start + offset;
  30. this.end = range.end + offset;
  31. }
  32. #endif
  33. internal QueryRange(int start, int end)
  34. {
  35. this.start = start;
  36. this.end = end;
  37. }
  38. internal int Count
  39. {
  40. get
  41. {
  42. return this.end - this.start + 1;
  43. }
  44. }
  45. #if NO
  46. internal int this[int offset]
  47. {
  48. get
  49. {
  50. return this.start + offset;
  51. }
  52. }
  53. internal bool IsNotEmpty
  54. {
  55. get
  56. {
  57. return (this.end >= this.start);
  58. }
  59. }
  60. internal void Clear()
  61. {
  62. this.end = this.start - 1;
  63. }
  64. internal void Grow(int offset)
  65. {
  66. this.end += offset;
  67. }
  68. #endif
  69. internal bool IsInRange(int point)
  70. {
  71. return (this.start <= point && point <= this.end);
  72. }
  73. #if NO
  74. internal void Set(int start, int end)
  75. {
  76. this.start = start;
  77. this.end = end;
  78. }
  79. #endif
  80. internal void Shift(int offset)
  81. {
  82. this.start += offset;
  83. this.end += offset;
  84. }
  85. }
  86. /// <summary>
  87. /// Our own buffer management
  88. /// There are a few reasons why we don't reuse something in System.Collections.Generic
  89. /// 1. We want Clear() to NOT reallocate the internal array. We want it to simply set the Count = 0
  90. /// This allows us to reuse buffers with impunity.
  91. /// 2. We want to be able to replace the internal buffer in a collection with a different one. Again,
  92. /// this is to help with pooling
  93. /// 3. We want to be able to control how fast buffers grow.
  94. /// 4. Does absolutely no bounds or null checking. As fast as we can make it. All checking should be done
  95. /// by whoever wraps this. Checking is unnecessary for many internal uses where we need optimal perf.
  96. /// 5. Does more precise trimming
  97. /// 6. AND this is a struct
  98. ///
  99. /// </summary>
  100. internal struct QueryBuffer<T>
  101. {
  102. internal T[] buffer; // buffer of T. Frequently larger than count
  103. internal int count; // Actual # of items
  104. internal static T[] EmptyBuffer = new T[0];
  105. /// <summary>
  106. /// Construct a new buffer
  107. /// </summary>
  108. /// <param name="capacity"></param>
  109. internal QueryBuffer(int capacity)
  110. {
  111. if (0 == capacity)
  112. {
  113. this.buffer = QueryBuffer<T>.EmptyBuffer;
  114. }
  115. else
  116. {
  117. this.buffer = new T[capacity];
  118. }
  119. this.count = 0;
  120. }
  121. #if NO
  122. internal QueryBuffer(QueryBuffer<T> buffer)
  123. {
  124. this.buffer = (T[]) buffer.buffer.Clone();
  125. this.count = buffer.count;
  126. }
  127. internal QueryBuffer(T[] buffer)
  128. {
  129. Fx.Assert(null != buffer, "");
  130. this.buffer = buffer;
  131. this.count = 0;
  132. }
  133. /// <summary>
  134. /// Get and set the internal buffer
  135. /// If you set the buffer, the count will automatically be set to 0
  136. /// </summary>
  137. internal T[] Buffer
  138. {
  139. get
  140. {
  141. return this.buffer;
  142. }
  143. set
  144. {
  145. Fx.Assert(null != value, "");
  146. this.buffer = value;
  147. this.count = 0;
  148. }
  149. }
  150. #endif
  151. /// <summary>
  152. /// # of items
  153. /// </summary>
  154. internal int Count
  155. {
  156. get
  157. {
  158. return this.count;
  159. }
  160. #if NO
  161. set
  162. {
  163. Fx.Assert(value >= 0 && value <= this.buffer.Length, "");
  164. this.count = value;
  165. }
  166. #endif
  167. }
  168. #if NO
  169. /// <summary>
  170. /// How much can it hold
  171. /// </summary>
  172. internal int Capacity
  173. {
  174. get
  175. {
  176. return this.buffer.Length;
  177. }
  178. set
  179. {
  180. Fx.Assert(value >= this.count, "");
  181. if (value > this.buffer.Length)
  182. {
  183. Array.Resize<T>(ref this.buffer, value);
  184. }
  185. }
  186. }
  187. #endif
  188. internal T this[int index]
  189. {
  190. get
  191. {
  192. return this.buffer[index];
  193. }
  194. set
  195. {
  196. this.buffer[index] = value;
  197. }
  198. }
  199. #if NO
  200. internal void Add()
  201. {
  202. if (this.count == this.buffer.Length)
  203. {
  204. Array.Resize<T>(ref this.buffer, this.count > 0 ? this.count * 2 : 16);
  205. }
  206. this.count++;
  207. }
  208. #endif
  209. /// <summary>
  210. /// Add an element to the buffer
  211. /// </summary>
  212. internal void Add(T t)
  213. {
  214. if (this.count == this.buffer.Length)
  215. {
  216. Array.Resize<T>(ref this.buffer, this.count > 0 ? this.count * 2 : 16);
  217. }
  218. this.buffer[this.count++] = t;
  219. }
  220. #if NO
  221. /// <summary>
  222. /// Useful when this is a buffer of structs
  223. /// </summary>
  224. internal void AddReference(ref T t)
  225. {
  226. if (this.count == this.buffer.Length)
  227. {
  228. Array.Resize<T>(ref this.buffer, this.count > 0 ? this.count * 2 : 16);
  229. }
  230. this.buffer[this.count++] = t;
  231. }
  232. #endif
  233. /// <summary>
  234. /// Add all the elements in the given buffer to this one
  235. /// We can do this very efficiently using an Array Copy
  236. /// </summary>
  237. internal void Add(ref QueryBuffer<T> addBuffer)
  238. {
  239. if (1 == addBuffer.count)
  240. {
  241. this.Add(addBuffer.buffer[0]);
  242. return;
  243. }
  244. int newCount = this.count + addBuffer.count;
  245. if (newCount >= this.buffer.Length)
  246. {
  247. this.Grow(newCount);
  248. }
  249. // Copy all the new elements in
  250. Array.Copy(addBuffer.buffer, 0, this.buffer, this.count, addBuffer.count);
  251. this.count = newCount;
  252. }
  253. #if NO
  254. internal void Add(T[] addBuffer, int startAt, int addCount)
  255. {
  256. int newCount = this.count + addCount;
  257. if (newCount >= this.buffer.Length)
  258. {
  259. this.Grow(newCount);
  260. }
  261. // Copy all the new elements in
  262. Array.Copy(addBuffer, startAt, this.buffer, this.count, addCount);
  263. this.count = newCount;
  264. }
  265. /// <summary>
  266. /// Add without attempting to grow the buffer. Faster, but must be used with care.
  267. /// Caller must ensure that the buffer is large enough.
  268. /// </summary>
  269. internal void AddOnly(T t)
  270. {
  271. this.buffer[this.count++] = t;
  272. }
  273. #endif
  274. /// <summary>
  275. /// Set the count to zero but do NOT get rid of the actual buffer
  276. /// </summary>
  277. internal void Clear()
  278. {
  279. this.count = 0;
  280. }
  281. #if NO
  282. //
  283. // Copy from one location in the buffer to another
  284. //
  285. internal void Copy(int from, int to)
  286. {
  287. this.buffer[to] = this.buffer[from];
  288. }
  289. internal void Copy(int from, int to, int count)
  290. {
  291. Array.Copy(this.buffer, from, this.buffer, to, count);
  292. }
  293. #endif
  294. internal void CopyFrom(ref QueryBuffer<T> addBuffer)
  295. {
  296. int addCount = addBuffer.count;
  297. switch (addCount)
  298. {
  299. default:
  300. if (addCount > this.buffer.Length)
  301. {
  302. this.buffer = new T[addCount];
  303. }
  304. // Copy all the new elements in
  305. Array.Copy(addBuffer.buffer, 0, this.buffer, 0, addCount);
  306. this.count = addCount;
  307. break;
  308. case 0:
  309. this.count = 0;
  310. break;
  311. case 1:
  312. if (this.buffer.Length == 0)
  313. {
  314. this.buffer = new T[1];
  315. }
  316. this.buffer[0] = addBuffer.buffer[0];
  317. this.count = 1;
  318. break;
  319. }
  320. }
  321. internal void CopyTo(T[] dest)
  322. {
  323. Array.Copy(this.buffer, dest, this.count);
  324. }
  325. #if NO
  326. /// <summary>
  327. /// Ensure that the internal buffer has adequate capacity
  328. /// </summary>
  329. internal void EnsureCapacity(int capacity)
  330. {
  331. if (capacity > this.buffer.Length)
  332. {
  333. this.Grow(capacity);
  334. }
  335. }
  336. internal void Erase()
  337. {
  338. Array.Clear(this.buffer, 0, this.count);
  339. this.count = 0;
  340. }
  341. #endif
  342. void Grow(int capacity)
  343. {
  344. int newCapacity = this.buffer.Length * 2;
  345. Array.Resize<T>(ref this.buffer, capacity > newCapacity ? capacity : newCapacity);
  346. }
  347. internal int IndexOf(T t)
  348. {
  349. for (int i = 0; i < this.count; ++i)
  350. {
  351. if (t.Equals(this.buffer[i]))
  352. {
  353. return i;
  354. }
  355. }
  356. return -1;
  357. }
  358. internal int IndexOf(T t, int startAt)
  359. {
  360. for (int i = startAt; i < this.count; ++i)
  361. {
  362. if (t.Equals(this.buffer[i]))
  363. {
  364. return i;
  365. }
  366. }
  367. return -1;
  368. }
  369. #if NO
  370. internal void InsertAt(T t, int at)
  371. {
  372. this.ReserveAt(at, 1);
  373. this.buffer[at] = t;
  374. }
  375. #endif
  376. internal bool IsValidIndex(int index)
  377. {
  378. return (index >= 0 && index < this.count);
  379. }
  380. #if NO
  381. internal T Pop()
  382. {
  383. Fx.Assert(this.count > 0, "");
  384. return this.buffer[--this.count];
  385. }
  386. internal void Push(T t)
  387. {
  388. this.Add(t);
  389. }
  390. #endif
  391. /// <summary>
  392. /// Reserve enough space for count elements
  393. /// </summary>
  394. internal void Reserve(int reserveCount)
  395. {
  396. int newCount = this.count + reserveCount;
  397. if (newCount >= this.buffer.Length)
  398. {
  399. this.Grow(newCount);
  400. }
  401. this.count = newCount;
  402. }
  403. internal void ReserveAt(int index, int reserveCount)
  404. {
  405. if (index == this.count)
  406. {
  407. this.Reserve(reserveCount);
  408. return;
  409. }
  410. int newCount;
  411. if (index > this.count)
  412. {
  413. // We want to reserve starting at a location past what is current committed.
  414. // No shifting needed
  415. newCount = index + reserveCount + 1;
  416. if (newCount >= this.buffer.Length)
  417. {
  418. this.Grow(newCount);
  419. }
  420. }
  421. else
  422. {
  423. // reserving space within an already allocated portion of the buffer
  424. // we'll ensure that the buffer can fit 'newCount' items, then shift by reserveCount starting at index
  425. newCount = this.count + reserveCount;
  426. if (newCount >= this.buffer.Length)
  427. {
  428. this.Grow(newCount);
  429. }
  430. // Move to make room
  431. Array.Copy(this.buffer, index, this.buffer, index + reserveCount, this.count - index);
  432. }
  433. this.count = newCount;
  434. }
  435. internal void Remove(T t)
  436. {
  437. int index = this.IndexOf(t);
  438. if (index >= 0)
  439. {
  440. this.RemoveAt(index);
  441. }
  442. }
  443. internal void RemoveAt(int index)
  444. {
  445. if (index < this.count - 1)
  446. {
  447. Array.Copy(this.buffer, index + 1, this.buffer, index, this.count - index - 1);
  448. }
  449. this.count--;
  450. }
  451. internal void Sort(IComparer<T> comparer)
  452. {
  453. Array.Sort<T>(this.buffer, 0, this.count, comparer);
  454. }
  455. #if NO
  456. /// <summary>
  457. /// Reduce the buffer capacity so that it is no greater than twice the element count
  458. /// </summary>
  459. internal void Trim()
  460. {
  461. int maxSize = this.count * 2;
  462. if (maxSize < this.buffer.Length / 2)
  463. {
  464. if (0 == maxSize)
  465. {
  466. this.buffer = QueryBuffer<T>.EmptyBuffer;
  467. }
  468. else
  469. {
  470. T[] newBuffer = new T[maxSize];
  471. Array.Copy(this.buffer, newBuffer, maxSize);
  472. }
  473. }
  474. }
  475. #endif
  476. /// <summary>
  477. /// Reduce the buffer capacity so that its size is exactly == to the element count
  478. /// </summary>
  479. internal void TrimToCount()
  480. {
  481. if (this.count < this.buffer.Length)
  482. {
  483. if (0 == this.count)
  484. {
  485. this.buffer = QueryBuffer<T>.EmptyBuffer;
  486. }
  487. else
  488. {
  489. T[] newBuffer = new T[this.count];
  490. Array.Copy(this.buffer, newBuffer, this.count);
  491. }
  492. }
  493. }
  494. }
  495. internal struct SortedBuffer<T, C>
  496. where C : IComparer<T>
  497. {
  498. int size;
  499. T[] buffer;
  500. static DefaultComparer Comparer;
  501. internal SortedBuffer(C comparerInstance)
  502. {
  503. this.size = 0;
  504. this.buffer = null;
  505. if (Comparer == null)
  506. {
  507. Comparer = new DefaultComparer(comparerInstance);
  508. }
  509. else
  510. {
  511. Fx.Assert(object.ReferenceEquals(DefaultComparer.Comparer, comparerInstance), "The SortedBuffer type has already been initialized with a different comparer instance.");
  512. }
  513. }
  514. internal T this[int index]
  515. {
  516. get
  517. {
  518. return GetAt(index);
  519. }
  520. }
  521. internal int Capacity
  522. {
  523. #if NO
  524. get
  525. {
  526. return this.buffer == null ? 0 : this.buffer.Length;
  527. }
  528. #endif
  529. set
  530. {
  531. if (this.buffer != null)
  532. {
  533. if (value != this.buffer.Length)
  534. {
  535. Fx.Assert(value >= this.size, "New capacity must be >= size");
  536. if (value > 0)
  537. {
  538. Array.Resize(ref this.buffer, value);
  539. }
  540. else
  541. {
  542. this.buffer = null;
  543. }
  544. }
  545. }
  546. else
  547. {
  548. this.buffer = new T[value];
  549. }
  550. }
  551. }
  552. internal int Count
  553. {
  554. get
  555. {
  556. return this.size;
  557. }
  558. }
  559. internal int Add(T item)
  560. {
  561. int i = Search(item);
  562. if (i < 0)
  563. {
  564. i = ~i;
  565. InsertAt(i, item);
  566. }
  567. return i;
  568. }
  569. #if NO
  570. internal void CopyTo(T[] array)
  571. {
  572. CopyTo(array, 0, this.size);
  573. }
  574. internal void CopyTo(T[] array, int start, int length)
  575. {
  576. Fx.Assert(array != null, "");
  577. Fx.Assert(start >= 0, "");
  578. Fx.Assert(length >= 0, "");
  579. Fx.Assert(start + length < this.size, "");
  580. Array.Copy(this.buffer, 0, array, start, length);
  581. }
  582. #endif
  583. internal void Clear()
  584. {
  585. this.size = 0;
  586. }
  587. #if NO
  588. internal bool Contains(T item)
  589. {
  590. return IndexOf(item) >= 0;
  591. }
  592. #endif
  593. internal void Exchange(T old, T replace)
  594. {
  595. if (Comparer.Compare(old, replace) == 0)
  596. {
  597. int i = IndexOf(old);
  598. if (i >= 0)
  599. {
  600. this.buffer[i] = replace;
  601. }
  602. else
  603. {
  604. Insert(replace);
  605. }
  606. }
  607. else
  608. {
  609. // PERF, [....], can this be made more efficient? Does it need to be?
  610. Remove(old);
  611. Insert(replace);
  612. }
  613. }
  614. internal T GetAt(int index)
  615. {
  616. Fx.Assert(index < this.size, "Index is greater than size");
  617. return this.buffer[index];
  618. }
  619. internal int IndexOf(T item)
  620. {
  621. return Search(item);
  622. }
  623. internal int IndexOfKey<K>(K key, IItemComparer<K, T> itemComp)
  624. {
  625. return Search(key, itemComp);
  626. }
  627. internal int Insert(T item)
  628. {
  629. int i = Search(item);
  630. if (i >= 0)
  631. {
  632. throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new ArgumentException(SR.GetString(SR.QueryItemAlreadyExists)));
  633. }
  634. // If an item is not found, Search returns the bitwise negation of
  635. // the index an item should inserted at;
  636. InsertAt(~i, item);
  637. return ~i;
  638. }
  639. void InsertAt(int index, T item)
  640. {
  641. Fx.Assert(index >= 0 && index <= this.size, "");
  642. if (this.buffer == null)
  643. {
  644. this.buffer = new T[1];
  645. }
  646. else if (this.buffer.Length == this.size)
  647. {
  648. // PERF, [....], how should we choose a new size?
  649. T[] tmp = new T[this.size + 1];
  650. if (index == 0)
  651. {
  652. Array.Copy(this.buffer, 0, tmp, 1, this.size);
  653. }
  654. else if (index == this.size)
  655. {
  656. Array.Copy(this.buffer, 0, tmp, 0, this.size);
  657. }
  658. else
  659. {
  660. Array.Copy(this.buffer, 0, tmp, 0, index);
  661. Array.Copy(this.buffer, index, tmp, index + 1, this.size - index);
  662. }
  663. this.buffer = tmp;
  664. }
  665. else
  666. {
  667. Array.Copy(this.buffer, index, this.buffer, index + 1, this.size - index);
  668. }
  669. this.buffer[index] = item;
  670. ++this.size;
  671. }
  672. internal bool Remove(T item)
  673. {
  674. int i = IndexOf(item);
  675. if (i >= 0)
  676. {
  677. RemoveAt(i);
  678. return true;
  679. }
  680. return false;
  681. }
  682. internal void RemoveAt(int index)
  683. {
  684. Fx.Assert(index >= 0 && index < this.size, "");
  685. if (index < this.size - 1)
  686. {
  687. Array.Copy(this.buffer, index + 1, this.buffer, index, this.size - index - 1);
  688. }
  689. this.buffer[--this.size] = default(T);
  690. }
  691. int Search(T item)
  692. {
  693. if (size == 0)
  694. return ~0;
  695. return Search(item, Comparer);
  696. }
  697. int Search<K>(K key, IItemComparer<K, T> comparer)
  698. {
  699. if (this.size <= 8)
  700. {
  701. return LinearSearch<K>(key, comparer, 0, this.size);
  702. }
  703. else
  704. {
  705. return BinarySearch(key, comparer);
  706. }
  707. }
  708. int BinarySearch<K>(K key, IItemComparer<K, T> comparer)
  709. {
  710. // [low, high)
  711. int low = 0;
  712. int high = this.size;
  713. int mid, result;
  714. // Binary search is implemented here so we could look for a type that is different from the
  715. // buffer type. Also, the search switches to linear for 8 or fewer elements.
  716. while (high - low > 8)
  717. {
  718. mid = (high + low) / 2;
  719. result = comparer.Compare(key, this.buffer[mid]);
  720. if (result < 0)
  721. {
  722. high = mid;
  723. }
  724. else if (result > 0)
  725. {
  726. low = mid + 1;
  727. }
  728. else
  729. {
  730. return mid;
  731. }
  732. }
  733. return LinearSearch<K>(key, comparer, low, high);
  734. }
  735. // [start, bound)
  736. int LinearSearch<K>(K key, IItemComparer<K, T> comparer, int start, int bound)
  737. {
  738. int result;
  739. for (int i = start; i < bound; ++i)
  740. {
  741. result = comparer.Compare(key, this.buffer[i]);
  742. if (result == 0)
  743. {
  744. return i;
  745. }
  746. if (result < 0)
  747. {
  748. // Return the bitwise negation of the insertion index
  749. return ~i;
  750. }
  751. }
  752. // Return the bitwise negation of the insertion index
  753. return ~bound;
  754. }
  755. #if NO
  756. internal T[] ToArray()
  757. {
  758. T[] tmp = new T[this.size];
  759. Array.Copy(this.buffer, 0, tmp, 0, this.size);
  760. return tmp;
  761. }
  762. #endif
  763. internal void Trim()
  764. {
  765. this.Capacity = this.size;
  766. }
  767. internal class DefaultComparer : IItemComparer<T, T>
  768. {
  769. public static IComparer<T> Comparer;
  770. public DefaultComparer(C comparer)
  771. {
  772. Comparer = comparer;
  773. }
  774. public int Compare(T item1, T item2)
  775. {
  776. return Comparer.Compare(item1, item2);
  777. }
  778. }
  779. }
  780. internal interface IItemComparer<K, V>
  781. {
  782. int Compare(K key, V value);
  783. }
  784. }