PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Mono.C5/C5/Interfaces.cs

https://bitbucket.org/danipen/mono
C# | 2059 lines | 270 code | 312 blank | 1477 comment | 0 complexity | acad36844a521f3d20e032808784f8f1 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. /*
  2. Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. */
  19. using System;
  20. using SCG = System.Collections.Generic;
  21. namespace C5
  22. {
  23. /// <summary>
  24. /// A generic collection, that can be enumerated backwards.
  25. /// </summary>
  26. public interface IDirectedEnumerable<T> : SCG.IEnumerable<T>
  27. {
  28. /// <summary>
  29. /// Create a collection containing the same items as this collection, but
  30. /// whose enumerator will enumerate the items backwards. The new collection
  31. /// will become invalid if the original is modified. Method typically used as in
  32. /// <code>foreach (T x in coll.Backwards()) {...}</code>
  33. /// </summary>
  34. /// <returns>The backwards collection.</returns>
  35. IDirectedEnumerable<T> Backwards();
  36. /// <summary>
  37. /// <code>Forwards</code> if same, else <code>Backwards</code>
  38. /// </summary>
  39. /// <value>The enumeration direction relative to the original collection.</value>
  40. EnumerationDirection Direction { get;}
  41. }
  42. /// <summary>
  43. /// A generic collection that may be enumerated and can answer
  44. /// efficiently how many items it contains. Like <code>IEnumerable&lt;T&gt;</code>,
  45. /// this interface does not prescribe any operations to initialize or update the
  46. /// collection. The main usage for this interface is to be the return type of
  47. /// query operations on generic collection.
  48. /// </summary>
  49. public interface ICollectionValue<T> : SCG.IEnumerable<T>, IShowable
  50. {
  51. /// <summary>
  52. /// A flag bitmap of the events subscribable to by this collection.
  53. /// </summary>
  54. /// <value></value>
  55. EventTypeEnum ListenableEvents { get;}
  56. /// <summary>
  57. /// A flag bitmap of the events currently subscribed to by this collection.
  58. /// </summary>
  59. /// <value></value>
  60. EventTypeEnum ActiveEvents { get;}
  61. /// <summary>
  62. /// The change event. Will be raised for every change operation on the collection.
  63. /// </summary>
  64. event CollectionChangedHandler<T> CollectionChanged;
  65. /// <summary>
  66. /// The change event. Will be raised for every clear operation on the collection.
  67. /// </summary>
  68. event CollectionClearedHandler<T> CollectionCleared;
  69. /// <summary>
  70. /// The item added event. Will be raised for every individual addition to the collection.
  71. /// </summary>
  72. event ItemsAddedHandler<T> ItemsAdded;
  73. /// <summary>
  74. /// The item inserted event. Will be raised for every individual insertion to the collection.
  75. /// </summary>
  76. event ItemInsertedHandler<T> ItemInserted;
  77. /// <summary>
  78. /// The item removed event. Will be raised for every individual removal from the collection.
  79. /// </summary>
  80. event ItemsRemovedHandler<T> ItemsRemoved;
  81. /// <summary>
  82. /// The item removed at event. Will be raised for every individual removal at from the collection.
  83. /// </summary>
  84. event ItemRemovedAtHandler<T> ItemRemovedAt;
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <value>True if this collection is empty.</value>
  89. bool IsEmpty { get;}
  90. /// <summary>
  91. /// </summary>
  92. /// <value>The number of items in this collection</value>
  93. int Count { get;}
  94. /// <summary>
  95. /// The value is symbolic indicating the type of asymptotic complexity
  96. /// in terms of the size of this collection (worst-case or amortized as
  97. /// relevant).
  98. /// </summary>
  99. /// <value>A characterization of the speed of the
  100. /// <code>Count</code> property in this collection.</value>
  101. Speed CountSpeed { get;}
  102. /// <summary>
  103. /// Copy the items of this collection to a contiguous part of an array.
  104. /// </summary>
  105. /// <param name="array">The array to copy to</param>
  106. /// <param name="index">The index at which to copy the first item</param>
  107. void CopyTo(T[] array, int index);
  108. /// <summary>
  109. /// Create an array with the items of this collection (in the same order as an
  110. /// enumerator would output them).
  111. /// </summary>
  112. /// <returns>The array</returns>
  113. T[] ToArray();
  114. /// <summary>
  115. /// Apply a delegate to all items of this collection.
  116. /// </summary>
  117. /// <param name="action">The delegate to apply</param>
  118. void Apply(Act<T> action);
  119. /// <summary>
  120. /// Check if there exists an item that satisfies a
  121. /// specific predicate in this collection.
  122. /// </summary>
  123. /// <param name="predicate">A delegate
  124. /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
  125. /// <returns>True is such an item exists</returns>
  126. bool Exists(Fun<T, bool> predicate);
  127. /// <summary>
  128. /// Check if there exists an item that satisfies a
  129. /// specific predicate in this collection and return the first one in enumeration order.
  130. /// </summary>
  131. /// <param name="predicate">A delegate
  132. /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
  133. /// <param name="item"></param>
  134. /// <returns>True is such an item exists</returns>
  135. bool Find(Fun<T, bool> predicate, out T item);
  136. /// <summary>
  137. /// Check if all items in this collection satisfies a specific predicate.
  138. /// </summary>
  139. /// <param name="predicate">A delegate
  140. /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
  141. /// <returns>True if all items satisfies the predicate</returns>
  142. bool All(Fun<T, bool> predicate);
  143. /// <summary>
  144. /// Choose some item of this collection.
  145. /// <para>Implementations must assure that the item
  146. /// returned may be efficiently removed.</para>
  147. /// <para>Implementors may decide to implement this method in a way such that repeated
  148. /// calls do not necessarily give the same result, i.e. so that the result of the following
  149. /// test is undetermined:
  150. /// <code>coll.Choose() == coll.Choose()</code></para>
  151. /// </summary>
  152. /// <exception cref="NoSuchItemException">if collection is empty.</exception>
  153. /// <returns></returns>
  154. T Choose();
  155. /// <summary>
  156. /// Create an enumerable, enumerating the items of this collection that satisfies
  157. /// a certain condition.
  158. /// </summary>
  159. /// <param name="filter">The T->bool filter delegate defining the condition</param>
  160. /// <returns>The filtered enumerable</returns>
  161. SCG.IEnumerable<T> Filter(Fun<T, bool> filter);
  162. }
  163. /// <summary>
  164. /// A sized generic collection, that can be enumerated backwards.
  165. /// </summary>
  166. public interface IDirectedCollectionValue<T> : ICollectionValue<T>, IDirectedEnumerable<T>
  167. {
  168. /// <summary>
  169. /// Create a collection containing the same items as this collection, but
  170. /// whose enumerator will enumerate the items backwards. The new collection
  171. /// will become invalid if the original is modified. Method typically used as in
  172. /// <code>foreach (T x in coll.Backwards()) {...}</code>
  173. /// </summary>
  174. /// <returns>The backwards collection.</returns>
  175. new IDirectedCollectionValue<T> Backwards();
  176. /// <summary>
  177. /// Check if there exists an item that satisfies a
  178. /// specific predicate in this collection and return the first one in enumeration order.
  179. /// </summary>
  180. /// <param name="predicate">A delegate
  181. /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
  182. /// <param name="item"></param>
  183. /// <returns>True is such an item exists</returns>
  184. bool FindLast(Fun<T, bool> predicate, out T item);
  185. }
  186. /// <summary>
  187. /// A generic collection to which one may add items. This is just the intersection
  188. /// of the main stream generic collection interfaces and the priority queue interface,
  189. /// <see cref="T:C5.ICollection`1"/> and <see cref="T:C5.IPriorityQueue`1"/>.
  190. /// </summary>
  191. public interface IExtensible<T> : ICollectionValue<T>, ICloneable
  192. {
  193. /// <summary>
  194. /// If true any call of an updating operation will throw an
  195. /// <code>ReadOnlyCollectionException</code>
  196. /// </summary>
  197. /// <value>True if this collection is read-only.</value>
  198. bool IsReadOnly { get;}
  199. //TODO: wonder where the right position of this is
  200. /// <summary>
  201. ///
  202. /// </summary>
  203. /// <value>False if this collection has set semantics, true if bag semantics.</value>
  204. bool AllowsDuplicates { get;}
  205. //TODO: wonder where the right position of this is. And the semantics.
  206. /// <summary>
  207. /// (Here should be a discussion of the role of equalityComparers. Any ).
  208. /// </summary>
  209. /// <value>The equalityComparer used by this collection to check equality of items.
  210. /// Or null (????) if collection does not check equality at all or uses a comparer.</value>
  211. SCG.IEqualityComparer<T> EqualityComparer { get;}
  212. //ItemEqualityTypeEnum ItemEqualityType {get ;}
  213. //TODO: find a good name
  214. /// <summary>
  215. /// By convention this is true for any collection with set semantics.
  216. /// </summary>
  217. /// <value>True if only one representative of a group of equal items
  218. /// is kept in the collection together with the total count.</value>
  219. bool DuplicatesByCounting { get;}
  220. /// <summary>
  221. /// Add an item to this collection if possible. If this collection has set
  222. /// semantics, the item will be added if not already in the collection. If
  223. /// bag semantics, the item will always be added.
  224. /// </summary>
  225. /// <param name="item">The item to add.</param>
  226. /// <returns>True if item was added.</returns>
  227. bool Add(T item);
  228. /// <summary>
  229. /// Add the elements from another collection with a more specialized item type
  230. /// to this collection. If this
  231. /// collection has set semantics, only items not already in the collection
  232. /// will be added.
  233. /// </summary>
  234. /// <typeparam name="U">The type of items to add</typeparam>
  235. /// <param name="items">The items to add</param>
  236. void AddAll<U>(SCG.IEnumerable<U> items) where U : T;
  237. //void Clear(); // for priority queue
  238. //int Count why not?
  239. /// <summary>
  240. /// Check the integrity of the internal data structures of this collection.
  241. /// <i>This is only relevant for developers of the library</i>
  242. /// </summary>
  243. /// <returns>True if check was passed.</returns>
  244. bool Check();
  245. }
  246. /// <summary>
  247. /// The simplest interface of a main stream generic collection
  248. /// with lookup, insertion and removal operations.
  249. /// </summary>
  250. public interface ICollection<T> : IExtensible<T>, SCG.ICollection<T>
  251. {
  252. //This is somewhat similar to the RandomAccess marker itf in java
  253. /// <summary>
  254. /// The value is symbolic indicating the type of asymptotic complexity
  255. /// in terms of the size of this collection (worst-case or amortized as
  256. /// relevant).
  257. /// <para>See <see cref="T:C5.Speed"/> for the set of symbols.</para>
  258. /// </summary>
  259. /// <value>A characterization of the speed of lookup operations
  260. /// (<code>Contains()</code> etc.) of the implementation of this collection.</value>
  261. Speed ContainsSpeed { get;}
  262. /// <summary>
  263. /// </summary>
  264. /// <value>The number of items in this collection</value>
  265. new int Count { get; }
  266. /// <summary>
  267. /// If true any call of an updating operation will throw an
  268. /// <code>ReadOnlyCollectionException</code>
  269. /// </summary>
  270. /// <value>True if this collection is read-only.</value>
  271. new bool IsReadOnly { get; }
  272. /// <summary>
  273. /// Add an item to this collection if possible. If this collection has set
  274. /// semantics, the item will be added if not already in the collection. If
  275. /// bag semantics, the item will always be added.
  276. /// </summary>
  277. /// <param name="item">The item to add.</param>
  278. /// <returns>True if item was added.</returns>
  279. new bool Add(T item);
  280. /// <summary>
  281. /// Copy the items of this collection to a contiguous part of an array.
  282. /// </summary>
  283. /// <param name="array">The array to copy to</param>
  284. /// <param name="index">The index at which to copy the first item</param>
  285. new void CopyTo(T[] array, int index);
  286. /// <summary>
  287. /// The unordered collection hashcode is defined as the sum of
  288. /// <code>h(hashcode(item))</code> over the items
  289. /// of the collection, where the function <code>h</code> is a function from
  290. /// int to int of the form <code> t -> (a0*t+b0)^(a1*t+b1)^(a2*t+b2)</code>, where
  291. /// the ax and bx are the same for all collection classes.
  292. /// <para>The current implementation uses fixed values for the ax and bx,
  293. /// specified as constants in the code.</para>
  294. /// </summary>
  295. /// <returns>The unordered hashcode of this collection.</returns>
  296. int GetUnsequencedHashCode();
  297. /// <summary>
  298. /// Compare the contents of this collection to another one without regards to
  299. /// the sequence order. The comparison will use this collection's itemequalityComparer
  300. /// to compare individual items.
  301. /// </summary>
  302. /// <param name="otherCollection">The collection to compare to.</param>
  303. /// <returns>True if this collection and that contains the same items.</returns>
  304. bool UnsequencedEquals(ICollection<T> otherCollection);
  305. /// <summary>
  306. /// Check if this collection contains (an item equivalent to according to the
  307. /// itemequalityComparer) a particular value.
  308. /// </summary>
  309. /// <param name="item">The value to check for.</param>
  310. /// <returns>True if the items is in this collection.</returns>
  311. new bool Contains(T item);
  312. /// <summary>
  313. /// Count the number of items of the collection equal to a particular value.
  314. /// Returns 0 if and only if the value is not in the collection.
  315. /// </summary>
  316. /// <param name="item">The value to count.</param>
  317. /// <returns>The number of copies found.</returns>
  318. int ContainsCount(T item);
  319. /// <summary>
  320. ///
  321. /// </summary>
  322. /// <returns></returns>
  323. ICollectionValue<T> UniqueItems();
  324. /// <summary>
  325. ///
  326. /// </summary>
  327. /// <returns></returns>
  328. ICollectionValue<KeyValuePair<T, int>> ItemMultiplicities();
  329. /// <summary>
  330. /// Check whether this collection contains all the values in another collection.
  331. /// If this collection has bag semantics (<code>AllowsDuplicates==true</code>)
  332. /// the check is made with respect to multiplicities, else multiplicities
  333. /// are not taken into account.
  334. /// </summary>
  335. /// <param name="items">The </param>
  336. /// <typeparam name="U"></typeparam>
  337. /// <returns>True if all values in <code>items</code>is in this collection.</returns>
  338. bool ContainsAll<U>(SCG.IEnumerable<U> items) where U : T;
  339. /// <summary>
  340. /// Check if this collection contains an item equivalent according to the
  341. /// itemequalityComparer to a particular value. If so, return in the ref argument (a
  342. /// binary copy of) the actual value found.
  343. /// </summary>
  344. /// <param name="item">The value to look for.</param>
  345. /// <returns>True if the items is in this collection.</returns>
  346. bool Find(ref T item);
  347. //This should probably just be bool Add(ref T item); !!!
  348. /// <summary>
  349. /// Check if this collection contains an item equivalent according to the
  350. /// itemequalityComparer to a particular value. If so, return in the ref argument (a
  351. /// binary copy of) the actual value found. Else, add the item to the collection.
  352. /// </summary>
  353. /// <param name="item">The value to look for.</param>
  354. /// <returns>True if the item was found (hence not added).</returns>
  355. bool FindOrAdd(ref T item);
  356. /// <summary>
  357. /// Check if this collection contains an item equivalent according to the
  358. /// itemequalityComparer to a particular value. If so, update the item in the collection
  359. /// with a (binary copy of) the supplied value. If the collection has bag semantics,
  360. /// it depends on the value of DuplicatesByCounting if this updates all equivalent copies in
  361. /// the collection or just one.
  362. /// </summary>
  363. /// <param name="item">Value to update.</param>
  364. /// <returns>True if the item was found and hence updated.</returns>
  365. bool Update(T item);
  366. /// <summary>
  367. /// Check if this collection contains an item equivalent according to the
  368. /// itemequalityComparer to a particular value. If so, update the item in the collection
  369. /// with a (binary copy of) the supplied value. If the collection has bag semantics,
  370. /// it depends on the value of DuplicatesByCounting if this updates all equivalent copies in
  371. /// the collection or just one.
  372. /// </summary>
  373. /// <param name="item">Value to update.</param>
  374. /// <param name="olditem">On output the olditem, if found.</param>
  375. /// <returns>True if the item was found and hence updated.</returns>
  376. bool Update(T item, out T olditem);
  377. /// <summary>
  378. /// Check if this collection contains an item equivalent according to the
  379. /// itemequalityComparer to a particular value. If so, update the item in the collection
  380. /// to with a binary copy of the supplied value; else add the value to the collection.
  381. /// </summary>
  382. /// <param name="item">Value to add or update.</param>
  383. /// <returns>True if the item was found and updated (hence not added).</returns>
  384. bool UpdateOrAdd(T item);
  385. /// <summary>
  386. /// Check if this collection contains an item equivalent according to the
  387. /// itemequalityComparer to a particular value. If so, update the item in the collection
  388. /// to with a binary copy of the supplied value; else add the value to the collection.
  389. /// </summary>
  390. /// <param name="item">Value to add or update.</param>
  391. /// <param name="olditem">On output the olditem, if found.</param>
  392. /// <returns>True if the item was found and updated (hence not added).</returns>
  393. bool UpdateOrAdd(T item, out T olditem);
  394. /// <summary>
  395. /// Remove a particular item from this collection. If the collection has bag
  396. /// semantics only one copy equivalent to the supplied item is removed.
  397. /// </summary>
  398. /// <param name="item">The value to remove.</param>
  399. /// <returns>True if the item was found (and removed).</returns>
  400. new bool Remove(T item);
  401. /// <summary>
  402. /// Remove a particular item from this collection if found. If the collection
  403. /// has bag semantics only one copy equivalent to the supplied item is removed,
  404. /// which one is implementation dependent.
  405. /// If an item was removed, report a binary copy of the actual item removed in
  406. /// the argument.
  407. /// </summary>
  408. /// <param name="item">The value to remove.</param>
  409. /// <param name="removeditem">The value removed if any.</param>
  410. /// <returns>True if the item was found (and removed).</returns>
  411. bool Remove(T item, out T removeditem);
  412. /// <summary>
  413. /// Remove all items equivalent to a given value.
  414. /// </summary>
  415. /// <param name="item">The value to remove.</param>
  416. void RemoveAllCopies(T item);
  417. /// <summary>
  418. /// Remove all items in another collection from this one. If this collection
  419. /// has bag semantics, take multiplicities into account.
  420. /// </summary>
  421. /// <typeparam name="U"></typeparam>
  422. /// <param name="items">The items to remove.</param>
  423. void RemoveAll<U>(SCG.IEnumerable<U> items) where U : T;
  424. //void RemoveAll(Fun<T, bool> predicate);
  425. /// <summary>
  426. /// Remove all items from this collection.
  427. /// </summary>
  428. new void Clear();
  429. /// <summary>
  430. /// Remove all items not in some other collection from this one. If this collection
  431. /// has bag semantics, take multiplicities into account.
  432. /// </summary>
  433. /// <typeparam name="U"></typeparam>
  434. /// <param name="items">The items to retain.</param>
  435. void RetainAll<U>(SCG.IEnumerable<U> items) where U : T;
  436. //void RetainAll(Fun<T, bool> predicate);
  437. //IDictionary<T> UniqueItems()
  438. }
  439. /// <summary>
  440. /// An editable collection maintaining a definite sequence order of the items.
  441. ///
  442. /// <i>Implementations of this interface must compute the hash code and
  443. /// equality exactly as prescribed in the method definitions in order to
  444. /// be consistent with other collection classes implementing this interface.</i>
  445. /// <i>This interface is usually implemented by explicit interface implementation,
  446. /// not as ordinary virtual methods.</i>
  447. /// </summary>
  448. public interface ISequenced<T> : ICollection<T>, IDirectedCollectionValue<T>
  449. {
  450. /// <summary>
  451. /// The hashcode is defined as <code>h(...h(h(h(x1),x2),x3),...,xn)</code> for
  452. /// <code>h(a,b)=CONSTANT*a+b</code> and the x's the hash codes of the items of
  453. /// this collection.
  454. /// </summary>
  455. /// <returns>The sequence order hashcode of this collection.</returns>
  456. int GetSequencedHashCode();
  457. /// <summary>
  458. /// Compare this sequenced collection to another one in sequence order.
  459. /// </summary>
  460. /// <param name="otherCollection">The sequenced collection to compare to.</param>
  461. /// <returns>True if this collection and that contains equal (according to
  462. /// this collection's itemequalityComparer) in the same sequence order.</returns>
  463. bool SequencedEquals(ISequenced<T> otherCollection);
  464. }
  465. /// <summary>
  466. /// A sequenced collection, where indices of items in the order are maintained
  467. /// </summary>
  468. public interface IIndexed<T> : ISequenced<T>
  469. {
  470. /// <summary>
  471. /// </summary>
  472. /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
  473. /// &gt;= the size of the collection.</exception>
  474. /// <value>The <code>index</code>'th item of this list.</value>
  475. /// <param name="index">the index to lookup</param>
  476. T this[int index] { get;}
  477. /// <summary>
  478. ///
  479. /// </summary>
  480. /// <value></value>
  481. Speed IndexingSpeed { get;}
  482. /// <summary>
  483. /// </summary>
  484. /// <exception cref="ArgumentOutOfRangeException"></exception>
  485. /// <value>The directed collection of items in a specific index interval.</value>
  486. /// <param name="start">The low index of the interval (inclusive).</param>
  487. /// <param name="count">The size of the range.</param>
  488. IDirectedCollectionValue<T> this[int start, int count] { get;}
  489. /// <summary>
  490. /// Searches for an item in the list going forwards from the start.
  491. /// </summary>
  492. /// <param name="item">Item to search for.</param>
  493. /// <returns>Index of item from start. A negative number if item not found,
  494. /// namely the one's complement of the index at which the Add operation would put the item.</returns>
  495. int IndexOf(T item);
  496. /// <summary>
  497. /// Searches for an item in the list going backwards from the end.
  498. /// </summary>
  499. /// <param name="item">Item to search for.</param>
  500. /// <returns>Index of of item from the end. A negative number if item not found,
  501. /// namely the two-complement of the index at which the Add operation would put the item.</returns>
  502. int LastIndexOf(T item);
  503. /// <summary>
  504. /// Check if there exists an item that satisfies a
  505. /// specific predicate in this collection and return the index of the first one.
  506. /// </summary>
  507. /// <param name="predicate">A delegate
  508. /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
  509. /// <returns>the index, if found, a negative value else</returns>
  510. int FindIndex(Fun<T, bool> predicate);
  511. /// <summary>
  512. /// Check if there exists an item that satisfies a
  513. /// specific predicate in this collection and return the index of the last one.
  514. /// </summary>
  515. /// <param name="predicate">A delegate
  516. /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
  517. /// <returns>the index, if found, a negative value else</returns>
  518. int FindLastIndex(Fun<T, bool> predicate);
  519. /// <summary>
  520. /// Remove the item at a specific position of the list.
  521. /// </summary>
  522. /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
  523. /// &gt;= the size of the collection.</exception>
  524. /// <param name="index">The index of the item to remove.</param>
  525. /// <returns>The removed item.</returns>
  526. T RemoveAt(int index);
  527. /// <summary>
  528. /// Remove all items in an index interval.
  529. /// </summary>
  530. /// <exception cref="ArgumentOutOfRangeException"> if start or count
  531. /// is negative or start+count &gt; the size of the collection.</exception>
  532. /// <param name="start">The index of the first item to remove.</param>
  533. /// <param name="count">The number of items to remove.</param>
  534. void RemoveInterval(int start, int count);
  535. }
  536. //TODO: decide if this should extend ICollection
  537. /// <summary>
  538. /// The interface describing the operations of a LIFO stack data structure.
  539. /// </summary>
  540. /// <typeparam name="T">The item type</typeparam>
  541. public interface IStack<T> : IDirectedCollectionValue<T>
  542. {
  543. /// <summary>
  544. ///
  545. /// </summary>
  546. /// <value></value>
  547. bool AllowsDuplicates { get;}
  548. /// <summary>
  549. /// Get the <code>index</code>'th element of the stack. The bottom of the stack has index 0.
  550. /// </summary>
  551. /// <param name="index"></param>
  552. /// <returns></returns>
  553. T this[int index] { get;}
  554. /// <summary>
  555. /// Push an item to the top of the stack.
  556. /// </summary>
  557. /// <param name="item">The item</param>
  558. void Push(T item);
  559. /// <summary>
  560. /// Pop the item at the top of the stack from the stack.
  561. /// </summary>
  562. /// <returns>The popped item.</returns>
  563. T Pop();
  564. }
  565. /// <summary>
  566. /// The interface describing the operations of a FIFO queue data structure.
  567. /// </summary>
  568. /// <typeparam name="T">The item type</typeparam>
  569. public interface IQueue<T> : IDirectedCollectionValue<T>
  570. {
  571. /// <summary>
  572. ///
  573. /// </summary>
  574. /// <value></value>
  575. bool AllowsDuplicates { get;}
  576. /// <summary>
  577. /// Get the <code>index</code>'th element of the queue. The front of the queue has index 0.
  578. /// </summary>
  579. /// <param name="index"></param>
  580. /// <returns></returns>
  581. T this[int index] { get;}
  582. /// <summary>
  583. /// Enqueue an item at the back of the queue.
  584. /// </summary>
  585. /// <param name="item">The item</param>
  586. void Enqueue(T item);
  587. /// <summary>
  588. /// Dequeue an item from the front of the queue.
  589. /// </summary>
  590. /// <returns>The item</returns>
  591. T Dequeue();
  592. }
  593. /// <summary>
  594. /// This is an indexed collection, where the item order is chosen by
  595. /// the user at insertion time.
  596. ///
  597. /// NBNBNB: we need a description of the view functionality here!
  598. /// </summary>
  599. public interface IList<T> : IIndexed<T>, IDisposable, SCG.IList<T>, System.Collections.IList
  600. {
  601. /// <summary>
  602. /// </summary>
  603. /// <exception cref="NoSuchItemException"> if this list is empty.</exception>
  604. /// <value>The first item in this list.</value>
  605. T First { get;}
  606. /// <summary>
  607. /// </summary>
  608. /// <exception cref="NoSuchItemException"> if this list is empty.</exception>
  609. /// <value>The last item in this list.</value>
  610. T Last { get;}
  611. /// <summary>
  612. /// Since <code>Add(T item)</code> always add at the end of the list,
  613. /// this describes if list has FIFO or LIFO semantics.
  614. /// </summary>
  615. /// <value>True if the <code>Remove()</code> operation removes from the
  616. /// start of the list, false if it removes from the end.</value>
  617. bool FIFO { get; set;}
  618. /// <summary>
  619. ///
  620. /// </summary>
  621. bool IsFixedSize { get; }
  622. /// <summary>
  623. /// On this list, this indexer is read/write.
  624. /// </summary>
  625. /// <exception cref="IndexOutOfRangeException"> if index is negative or
  626. /// &gt;= the size of the collection.</exception>
  627. /// <value>The index'th item of this list.</value>
  628. /// <param name="index">The index of the item to fetch or store.</param>
  629. new T this[int index] { get; set;}
  630. #region Ambiguous calls when extending SCG.IList<T>
  631. #region SCG.ICollection<T>
  632. /// <summary>
  633. ///
  634. /// </summary>
  635. new int Count { get; }
  636. /// <summary>
  637. ///
  638. /// </summary>
  639. new bool IsReadOnly { get; }
  640. /// <summary>
  641. ///
  642. /// </summary>
  643. /// <param name="item"></param>
  644. /// <returns></returns>
  645. new bool Add(T item);
  646. /// <summary>
  647. ///
  648. /// </summary>
  649. new void Clear();
  650. /// <summary>
  651. ///
  652. /// </summary>
  653. /// <param name="item"></param>
  654. /// <returns></returns>
  655. new bool Contains(T item);
  656. /// <summary>
  657. ///
  658. /// </summary>
  659. /// <param name="array"></param>
  660. /// <param name="index"></param>
  661. new void CopyTo(T[] array, int index);
  662. /// <summary>
  663. ///
  664. /// </summary>
  665. /// <param name="item"></param>
  666. /// <returns></returns>
  667. new bool Remove(T item);
  668. #endregion
  669. #region SCG.IList<T> proper
  670. /// <summary>
  671. /// Searches for an item in the list going forwards from the start.
  672. /// </summary>
  673. /// <param name="item">Item to search for.</param>
  674. /// <returns>Index of item from start. A negative number if item not found,
  675. /// namely the one's complement of the index at which the Add operation would put the item.</returns>
  676. new int IndexOf(T item);
  677. /// <summary>
  678. /// Remove the item at a specific position of the list.
  679. /// </summary>
  680. /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
  681. /// &gt;= the size of the collection.</exception>
  682. /// <param name="index">The index of the item to remove.</param>
  683. /// <returns>The removed item.</returns>
  684. new T RemoveAt(int index);
  685. #endregion
  686. #endregion
  687. /*/// <summary>
  688. /// Insert an item at a specific index location in this list.
  689. /// </summary>
  690. /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
  691. /// &gt; the size of the collection.</exception>
  692. /// <exception cref="DuplicateNotAllowedException"> if the list has
  693. /// <code>AllowsDuplicates==false</code> and the item is
  694. /// already in the list.</exception>
  695. /// <param name="index">The index at which to insert.</param>
  696. /// <param name="item">The item to insert.</param>
  697. void Insert(int index, T item);*/
  698. /// <summary>
  699. /// Insert an item at the end of a compatible view, used as a pointer.
  700. /// <para>The <code>pointer</code> must be a view on the same list as
  701. /// <code>this</code> and the endpoitn of <code>pointer</code> must be
  702. /// a valid insertion point of <code>this</code></para>
  703. /// </summary>
  704. /// <exception cref="IncompatibleViewException">If <code>pointer</code>
  705. /// is not a view on the same list as <code>this</code></exception>
  706. /// <exception cref="IndexOutOfRangeException"><b>??????</b> if the endpoint of
  707. /// <code>pointer</code> is not inside <code>this</code></exception>
  708. /// <exception cref="DuplicateNotAllowedException"> if the list has
  709. /// <code>AllowsDuplicates==false</code> and the item is
  710. /// already in the list.</exception>
  711. /// <param name="pointer"></param>
  712. /// <param name="item"></param>
  713. void Insert(IList<T> pointer, T item);
  714. /// <summary>
  715. /// Insert an item at the front of this list.
  716. /// <exception cref="DuplicateNotAllowedException"/> if the list has
  717. /// <code>AllowsDuplicates==false</code> and the item is
  718. /// already in the list.
  719. /// </summary>
  720. /// <param name="item">The item to insert.</param>
  721. void InsertFirst(T item);
  722. /// <summary>
  723. /// Insert an item at the back of this list.
  724. /// <exception cref="DuplicateNotAllowedException"/> if the list has
  725. /// <code>AllowsDuplicates==false</code> and the item is
  726. /// already in the list.
  727. /// </summary>
  728. /// <param name="item">The item to insert.</param>
  729. void InsertLast(T item);
  730. /// <summary>
  731. /// Insert into this list all items from an enumerable collection starting
  732. /// at a particular index.
  733. /// </summary>
  734. /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
  735. /// &gt; the size of the collection.</exception>
  736. /// <exception cref="DuplicateNotAllowedException"> if the list has
  737. /// <code>AllowsDuplicates==false</code> and one of the items to insert is
  738. /// already in the list.</exception>
  739. /// <param name="index">Index to start inserting at</param>
  740. /// <param name="items">Items to insert</param>
  741. /// <typeparam name="U"></typeparam>
  742. void InsertAll<U>(int index, SCG.IEnumerable<U> items) where U : T;
  743. /// <summary>
  744. /// Create a new list consisting of the items of this list satisfying a
  745. /// certain predicate.
  746. /// </summary>
  747. /// <param name="filter">The filter delegate defining the predicate.</param>
  748. /// <returns>The new list.</returns>
  749. IList<T> FindAll(Fun<T, bool> filter);
  750. /// <summary>
  751. /// Create a new list consisting of the results of mapping all items of this
  752. /// list. The new list will use the default equalityComparer for the item type V.
  753. /// </summary>
  754. /// <typeparam name="V">The type of items of the new list</typeparam>
  755. /// <param name="mapper">The delegate defining the map.</param>
  756. /// <returns>The new list.</returns>
  757. IList<V> Map<V>(Fun<T, V> mapper);
  758. /// <summary>
  759. /// Create a new list consisting of the results of mapping all items of this
  760. /// list. The new list will use a specified equalityComparer for the item type.
  761. /// </summary>
  762. /// <typeparam name="V">The type of items of the new list</typeparam>
  763. /// <param name="mapper">The delegate defining the map.</param>
  764. /// <param name="equalityComparer">The equalityComparer to use for the new list</param>
  765. /// <returns>The new list.</returns>
  766. IList<V> Map<V>(Fun<T, V> mapper, SCG.IEqualityComparer<V> equalityComparer);
  767. /// <summary>
  768. /// Remove one item from the list: from the front if <code>FIFO</code>
  769. /// is true, else from the back.
  770. /// <exception cref="NoSuchItemException"/> if this list is empty.
  771. /// </summary>
  772. /// <returns>The removed item.</returns>
  773. T Remove();
  774. /// <summary>
  775. /// Remove one item from the front of the list.
  776. /// <exception cref="NoSuchItemException"/> if this list is empty.
  777. /// </summary>
  778. /// <returns>The removed item.</returns>
  779. T RemoveFirst();
  780. /// <summary>
  781. /// Remove one item from the back of the list.
  782. /// <exception cref="NoSuchItemException"/> if this list is empty.
  783. /// </summary>
  784. /// <returns>The removed item.</returns>
  785. T RemoveLast();
  786. /// <summary>
  787. /// Create a list view on this list.
  788. /// <exception cref="ArgumentOutOfRangeException"/> if the view would not fit into
  789. /// this list.
  790. /// </summary>
  791. /// <param name="start">The index in this list of the start of the view.</param>
  792. /// <param name="count">The size of the view.</param>
  793. /// <returns>The new list view.</returns>
  794. IList<T> View(int start, int count);
  795. /// <summary>
  796. /// Create a list view on this list containing the (first) occurrence of a particular item.
  797. /// <exception cref="NoSuchItemException"/> if the item is not in this list.
  798. /// </summary>
  799. /// <param name="item">The item to find.</param>
  800. /// <returns>The new list view.</returns>
  801. IList<T> ViewOf(T item);
  802. /// <summary>
  803. /// Create a list view on this list containing the last occurrence of a particular item.
  804. /// <exception cref="NoSuchItemException"/> if the item is not in this list.
  805. /// </summary>
  806. /// <param name="item">The item to find.</param>
  807. /// <returns>The new list view.</returns>
  808. IList<T> LastViewOf(T item);
  809. /// <summary>
  810. /// Null if this list is not a view.
  811. /// </summary>
  812. /// <value>Underlying list for view.</value>
  813. IList<T> Underlying { get;}
  814. /// <summary>
  815. /// </summary>
  816. /// <value>Offset for this list view or 0 for an underlying list.</value>
  817. int Offset { get;}
  818. /// <summary>
  819. ///
  820. /// </summary>
  821. /// <value></value>
  822. bool IsValid { get;}
  823. /// <summary>
  824. /// Slide this list view along the underlying list.
  825. /// </summary>
  826. /// <exception cref="NotAViewException"> if this list is not a view.</exception>
  827. /// <exception cref="ArgumentOutOfRangeException"> if the operation
  828. /// would bring either end of the view outside the underlying list.</exception>
  829. /// <param name="offset">The signed amount to slide: positive to slide
  830. /// towards the end.</param>
  831. IList<T> Slide(int offset);
  832. /// <summary>
  833. /// Slide this list view along the underlying list, changing its size.
  834. ///
  835. /// </summary>
  836. /// <exception cref="NotAViewException"> if this list is not a view.</exception>
  837. /// <exception cref="ArgumentOutOfRangeException"> if the operation
  838. /// would bring either end of the view outside the underlying list.</exception>
  839. /// <param name="offset">The signed amount to slide: positive to slide
  840. /// towards the end.</param>
  841. /// <param name="size">The new size of the view.</param>
  842. IList<T> Slide(int offset, int size);
  843. /// <summary>
  844. ///
  845. /// </summary>
  846. /// <param name="offset"></param>
  847. /// <returns></returns>
  848. bool TrySlide(int offset);
  849. /// <summary>
  850. ///
  851. /// </summary>
  852. /// <param name="offset"></param>
  853. /// <param name="size"></param>
  854. /// <returns></returns>
  855. bool TrySlide(int offset, int size);
  856. /// <summary>
  857. ///
  858. /// <para>Returns null if <code>otherView</code> is strictly to the left of this view</para>
  859. /// </summary>
  860. /// <param name="otherView"></param>
  861. /// <exception cref="IncompatibleViewException">If otherView does not have the same underlying list as this</exception>
  862. /// <exception cref="ArgumentOutOfRangeException">If <code>otherView</code> is strictly to the left of this view</exception>
  863. /// <returns></returns>
  864. IList<T> Span(IList<T> otherView);
  865. /// <summary>
  866. /// Reverse the list so the items are in the opposite sequence order.
  867. /// </summary>
  868. void Reverse();
  869. /// <summary>
  870. /// Check if this list is sorted according to the default sorting order
  871. /// for the item type T, as defined by the <see cref="T:C5.Comparer`1"/> class
  872. /// </summary>
  873. /// <exception cref="NotComparableException">if T is not comparable</exception>
  874. /// <returns>True if the list is sorted, else false.</returns>
  875. bool IsSorted();
  876. /// <summary>
  877. /// Check if this list is sorted according to a specific sorting order.
  878. /// </summary>
  879. /// <param name="comparer">The comparer defining the sorting order.</param>
  880. /// <returns>True if the list is sorted, else false.</returns>
  881. bool IsSorted(SCG.IComparer<T> comparer);
  882. /// <summary>
  883. /// Sort the items of the list according to the default sorting order
  884. /// for the item type T, as defined by the <see cref="T:C5.Comparer`1"/> class
  885. /// </summary>
  886. /// <exception cref="NotComparableException">if T is not comparable</exception>
  887. void Sort();
  888. /// <summary>
  889. /// Sort the items of the list according to a specified sorting order.
  890. /// <para>The sorting does not perform duplicate elimination or identify items
  891. /// according to the comparer or itemequalityComparer. I.e. the list as an
  892. /// unsequenced collection with binary equality, will not change.
  893. /// </para>
  894. /// </summary>
  895. /// <param name="comparer">The comparer defining the sorting order.</param>
  896. void Sort(SCG.IComparer<T> comparer);
  897. /// <summary>
  898. /// Randomly shuffle the items of this list.
  899. /// </summary>
  900. void Shuffle();
  901. /// <summary>
  902. /// Shuffle the items of this list according to a specific random source.
  903. /// </summary>
  904. /// <param name="rnd">The random source.</param>
  905. void Shuffle(Random rnd);
  906. }
  907. /// <summary>
  908. /// The base type of a priority queue handle
  909. /// </summary>
  910. /// <typeparam name="T"></typeparam>
  911. public interface IPriorityQueueHandle<T>
  912. {
  913. //TODO: make abstract and prepare for double dispatch:
  914. //public virtual bool Delete(IPriorityQueue<T> q) { throw new InvalidFooException();}
  915. //bool Replace(T item);
  916. }
  917. /// <summary>
  918. /// A generic collection of items prioritized by a comparison (order) relation.
  919. /// Supports adding items and reporting or removing extremal elements.
  920. /// <para>
  921. ///
  922. /// </para>
  923. /// When adding an item, the user may choose to have a handle allocated for this item in the queue.
  924. /// The resulting handle may be used for deleting the item even if not extremal, and for replacing the item.
  925. /// A priority queue typically only holds numeric priorities associated with some objects
  926. /// maintained separately in other collection objects.
  927. /// </summary>
  928. public interface IPriorityQueue<T> : IExtensible<T>
  929. {
  930. /// <summary>
  931. /// Find the current least item of this priority queue.
  932. /// </summary>
  933. /// <returns>The least item.</returns>
  934. T FindMin();
  935. /// <summary>
  936. /// Remove the least item from this priority queue.
  937. /// </summary>
  938. /// <returns>The removed item.</returns>
  939. T DeleteMin();
  940. /// <summary>
  941. /// Find the current largest item of this priority queue.
  942. /// </summary>
  943. /// <returns>The largest item.</returns>
  944. T FindMax();
  945. /// <summary>
  946. /// Remove the largest item from this priority queue.
  947. /// </summary>
  948. /// <returns>The removed item.</returns>
  949. T DeleteMax();
  950. /// <summary>
  951. /// The comparer object supplied at creation time for this collection
  952. /// </summary>
  953. /// <value>The comparer</value>
  954. SCG.IComparer<T> Comparer { get;}
  955. /// <summary>
  956. /// Get or set the item corresponding to a handle. Throws exceptions on
  957. /// invalid handles.
  958. /// </summary>
  959. /// <param name="handle"></param>
  960. /// <returns></returns>
  961. T this[IPriorityQueueHandle<T> handle] { get; set;}
  962. /// <summary>
  963. /// Check if the entry corresponding to a handle is in the priority queue.
  964. /// </summary>
  965. /// <param name="handle"></param>
  966. /// <param name="item"></param>
  967. /// <returns></returns>
  968. bool Find(IPriorityQueueHandle<T> handle, out T item);
  969. /// <summary>
  970. /// Add an item to the priority queue, receiving a
  971. /// handle for the item in the queue,
  972. /// or reusing an existing unused handle.
  973. /// </summary>
  974. /// <param name="handle">On output: a handle for the added item.
  975. /// On input: null for allocating a new handle, or a currently unused handle for reuse.
  976. /// A handle for reuse must be compatible with this priority queue,
  977. /// by being created by a priority queue of the same runtime type, but not
  978. /// necessarily the same priority queue object.</param>
  979. /// <param name="item"></param>
  980. /// <returns></returns>
  981. bool Add(ref IPriorityQueueHandle<T> handle, T item);
  982. /// <summary>
  983. /// Delete an item with a handle from a priority queue
  984. /// </summary>
  985. /// <param name="handle">The handle for the item. The handle will be invalidated, but reusable.</param>
  986. /// <returns>The deleted item</returns>
  987. T Delete(IPriorityQueueHandle<T> handle);
  988. /// <summary>
  989. /// Replace an item with a handle in a priority queue with a new item.
  990. /// Typically used for changing the priority of some queued object.
  991. /// </summary>
  992. /// <param name="handle">The handle for the old item</param>
  993. /// <param name="item">The new item</param>
  994. /// <returns>The old item</returns>
  995. T Replace(IPriorityQueueHandle<T> handle, T item);
  996. /// <summary>
  997. /// Find the current least item of this priority queue.
  998. /// </summary>
  999. /// <param name="handle">On return: the handle of the item.</param>
  1000. /// <returns>The least item.</returns>
  1001. T FindMin(out IPriorityQueueHandle<T> handle);
  1002. /// <summary>
  1003. /// Find the current largest item of this priority queue.
  1004. /// </summary>
  1005. /// <param name="handle">On return: the handle of the item.</param>
  1006. /// <returns>The largest item.</returns>
  1007. T FindMax(out IPriorityQueueHandle<T> handle);
  1008. /// <summary>
  1009. /// Remove the least item from this priority queue.
  1010. /// </summary>
  1011. /// <param name="handle">On return: the handle of the removed item.</param>
  1012. /// <returns>The removed item.</returns>
  1013. T DeleteMin(out IPriorityQueueHandle<T> handle);
  1014. /// <summary>
  1015. /// Remove the largest item from this priority queue.
  1016. /// </summary>
  1017. /// <param name="handle">On return: the handle of the removed item.</param>
  1018. /// <returns>The removed item.</returns>
  1019. T DeleteMax(out IPriorityQueueHandle<T> handle);
  1020. }
  1021. /// <summary>
  1022. /// A sorted collection, i.e. a collection where items are maintained and can be searched for in sorted order.
  1023. /// Thus the sequence order is given as a sorting order.
  1024. ///
  1025. /// <para>The sorting order is defined by a comparer, an object of type IComparer&lt;T&gt;
  1026. /// (<see cref="T:C5.IComparer`1"/>). Implementors of this interface will normally let the user
  1027. /// define the comparer as an argument to a constructor.
  1028. /// Usually there will also be constructors without a comparer argument, in which case the
  1029. /// comparer should be the defalt comparer for the item type, <see cref="P:C5.Comparer`1.Default"/>.</para>
  1030. ///
  1031. /// <para>The comparer of the sorted collection is available as the <code>Comparer</code> property
  1032. /// (<see cref="P:C5.ISorted`1.Comparer"/>).</para>
  1033. ///
  1034. /// <para>The methods are grouped according to
  1035. /// <list>
  1036. /// <item>Extrema: report or report and delete an extremal item. This is reminiscent of simplified priority queues.</item>
  1037. /// <item>Nearest neighbor: report predecessor or successor in the collection of an item. Cut belongs to this group.</item>
  1038. /// <item>Range: report a view of a range of elements or remove all elements in a range.</item>
  1039. /// <item>AddSorted: add a collection of items known to be sorted in the same order (should be faster) (to be removed?)</item>
  1040. /// </list>
  1041. /// </para>
  1042. ///
  1043. /// <para>Since this interface extends ISequenced&lt;T&gt;, sorted collections will also have an
  1044. /// item equalityComparer (<see cref="P:C5.IExtensible`1.EqualityComparer"/>). This equalityComparer will not be used in connection with
  1045. /// the inner workings of the sorted collection, but will be used if the sorted collection is used as
  1046. /// an item in a collection of unsequenced or sequenced collections,
  1047. /// (<see cref="T:C5.ICollection`1"/> and <see cref="T:C5.ISequenced`1"/>)</para>
  1048. ///
  1049. /// <para>Note that code may check if two sorted collections has the same sorting order
  1050. /// by checking if the Comparer properties are equal. This is done a few places in this library
  1051. /// for optimization purposes.</para>
  1052. /// </summary>
  1053. public interface ISorted<T> : ISequenced<T>
  1054. {
  1055. /// <summary>
  1056. /// Find the current least item of this sorted collection.
  1057. /// </summary>
  1058. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1059. /// <returns>The least item.</returns>
  1060. T FindMin();
  1061. /// <summary>
  1062. /// Remove the least item from this sorted collection.
  1063. /// </summary>
  1064. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1065. /// <returns>The removed item.</returns>
  1066. T DeleteMin();
  1067. /// <summary>
  1068. /// Find the current largest item of this sorted collection.
  1069. /// </summary>
  1070. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1071. /// <returns>The largest item.</returns>
  1072. T FindMax();
  1073. /// <summary>
  1074. /// Remove the largest item from this sorted collection.
  1075. /// </summary>
  1076. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1077. /// <returns>The removed item.</returns>
  1078. T DeleteMax();
  1079. /// <summary>
  1080. /// The comparer object supplied at creation time for this sorted collection.
  1081. /// </summary>
  1082. /// <value>The comparer</value>
  1083. SCG.IComparer<T> Comparer { get; }
  1084. /// <summary>
  1085. /// Find the strict predecessor of item in the sorted collection,
  1086. /// that is, the greatest item in the collection smaller than the item.
  1087. /// </summary>
  1088. /// <param name="item">The item to find the predecessor for.</param>
  1089. /// <param name="res">The predecessor, if any; otherwise the default value for T.</param>
  1090. /// <returns>True if item has a predecessor; otherwise false.</returns>
  1091. bool TryPredecessor(T item, out T res);
  1092. /// <summary>
  1093. /// Find the strict successor of item in the sorted collection,
  1094. /// that is, the least item in the collection greater than the supplied value.
  1095. /// </summary>
  1096. /// <param name="item">The item to find the successor for.</param>
  1097. /// <param name="res">The successor, if any; otherwise the default value for T.</param>
  1098. /// <returns>True if item has a successor; otherwise false.</returns>
  1099. bool TrySuccessor(T item, out T res);
  1100. /// <summary>
  1101. /// Find the weak predecessor of item in the sorted collection,
  1102. /// that is, the greatest item in the collection smaller than or equal to the item.
  1103. /// </summary>
  1104. /// <param name="item">The item to find the weak predecessor for.</param>
  1105. /// <param name="res">The weak predecessor, if any; otherwise the default value for T.</param>
  1106. /// <returns>True if item has a weak predecessor; otherwise false.</returns>
  1107. bool TryWeakPredecessor(T item, out T res);
  1108. /// <summary>
  1109. /// Find the weak successor of item in the sorted collection,
  1110. /// that is, the least item in the collection greater than or equal to the supplied value.
  1111. /// </summary>
  1112. /// <param name="item">The item to find the weak successor for.</param>
  1113. /// <param name="res">The weak successor, if any; otherwise the default value for T.</param>
  1114. /// <returns>True if item has a weak successor; otherwise false.</returns>
  1115. bool TryWeakSuccessor(T item, out T res);
  1116. /// <summary>
  1117. /// Find the strict predecessor in the sorted collection of a particular value,
  1118. /// that is, the largest item in the collection less than the supplied value.
  1119. /// </summary>
  1120. /// <exception cref="NoSuchItemException"> if no such element exists (the
  1121. /// supplied value is less than or equal to the minimum of this collection.)</exception>
  1122. /// <param name="item">The item to find the predecessor for.</param>
  1123. /// <returns>The predecessor.</returns>
  1124. T Predecessor(T item);
  1125. /// <summary>
  1126. /// Find the strict successor in the sorted collection of a particular value,
  1127. /// that is, the least item in the collection greater than the supplied value.
  1128. /// </summary>
  1129. /// <exception cref="NoSuchItemException"> if no such element exists (the
  1130. /// supplied value is greater than or equal to the maximum of this collection.)</exception>
  1131. /// <param name="item">The item to find the successor for.</param>
  1132. /// <returns>The successor.</returns>
  1133. T Successor(T item);
  1134. /// <summary>
  1135. /// Find the weak predecessor in the sorted collection of a particular value,
  1136. /// that is, the largest item in the collection less than or equal to the supplied value.
  1137. /// </summary>
  1138. /// <exception cref="NoSuchItemException"> if no such element exists (the
  1139. /// supplied value is less than the minimum of this collection.)</exception>
  1140. /// <param name="item">The item to find the weak predecessor for.</param>
  1141. /// <returns>The weak predecessor.</returns>
  1142. T WeakPredecessor(T item);
  1143. /// <summary>
  1144. /// Find the weak successor in the sorted collection of a particular value,
  1145. /// that is, the least item in the collection greater than or equal to the supplied value.
  1146. /// </summary>
  1147. /// <exception cref="NoSuchItemException"> if no such element exists (the
  1148. /// supplied value is greater than the maximum of this collection.)</exception>
  1149. ///<param name="item">The item to find the weak successor for.</param>
  1150. /// <returns>The weak successor.</returns>
  1151. T WeakSuccessor(T item);
  1152. /// <summary>
  1153. /// Given a "cut" function from the items of the sorted collection to <code>int</code>
  1154. /// whose only sign changes when going through items in increasing order
  1155. /// can be
  1156. /// <list>
  1157. /// <item>from positive to zero</item>
  1158. /// <item>from positive to negative</item>
  1159. /// <item>from zero to negative</item>
  1160. /// </list>
  1161. /// The "cut" function is supplied as the <code>CompareTo</code> method
  1162. /// of an object <code>c</code> implementing
  1163. /// <code>IComparable&lt;T&gt;</code>.
  1164. /// A typical example is the case where <code>T</code> is comparable and
  1165. /// <code>cutFunction</code> is itself of type <code>T</code>.
  1166. /// <para>This method performs a search in the sorted collection for the ranges in which the
  1167. /// "cut" function is negative, zero respectively positive. If <code>T</code> is comparable
  1168. /// and <code>c</code> is of type <code>T</code>, this is a safe way (no exceptions thrown)
  1169. /// to find predecessor and successor of <code>c</code>.
  1170. /// </para>
  1171. /// <para> If the supplied cut function does not satisfy the sign-change condition,
  1172. /// the result of this call is undefined.
  1173. /// </para>
  1174. ///
  1175. /// </summary>
  1176. /// <param name="cutFunction">The cut function <code>T</code> to <code>int</code>, given
  1177. /// by the <code>CompareTo</code> method of an object implementing
  1178. /// <code>IComparable&lt;T&gt;</code>.</param>
  1179. /// <param name="low">Returns the largest item in the collection, where the
  1180. /// cut function is positive (if any).</param>
  1181. /// <param name="lowIsValid">Returns true if the cut function is positive somewhere
  1182. /// on this collection.</param>
  1183. /// <param name="high">Returns the least item in the collection, where the
  1184. /// cut function is negative (if any).</param>
  1185. /// <param name="highIsValid">Returns true if the cut function is negative somewhere
  1186. /// on this collection.</param>
  1187. /// <returns>True if the cut function is zero somewhere
  1188. /// on this collection.</returns>
  1189. bool Cut(IComparable<T> cutFunction, out T low, out bool lowIsValid, out T high, out bool highIsValid);
  1190. /// <summary>
  1191. /// Query this sorted collection for items greater than or equal to a supplied value.
  1192. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1193. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1194. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1195. /// </summary>
  1196. /// <param name="bot">The lower bound (inclusive).</param>
  1197. /// <returns>The result directed collection.</returns>
  1198. IDirectedEnumerable<T> RangeFrom(T bot);
  1199. /// <summary>
  1200. /// Query this sorted collection for items between two supplied values.
  1201. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1202. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1203. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1204. /// </summary>
  1205. /// <param name="bot">The lower bound (inclusive).</param>
  1206. /// <param name="top">The upper bound (exclusive).</param>
  1207. /// <returns>The result directed collection.</returns>
  1208. IDirectedEnumerable<T> RangeFromTo(T bot, T top);
  1209. /// <summary>
  1210. /// Query this sorted collection for items less than a supplied value.
  1211. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1212. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1213. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1214. /// </summary>
  1215. /// <param name="top">The upper bound (exclusive).</param>
  1216. /// <returns>The result directed collection.</returns>
  1217. IDirectedEnumerable<T> RangeTo(T top);
  1218. /// <summary>
  1219. /// Create a directed collection with the same items as this collection.
  1220. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1221. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1222. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1223. /// </summary>
  1224. /// <returns>The result directed collection.</returns>
  1225. IDirectedCollectionValue<T> RangeAll();
  1226. //TODO: remove now that we assume that we can check the sorting order?
  1227. /// <summary>
  1228. /// Add all the items from another collection with an enumeration order that
  1229. /// is increasing in the items.
  1230. /// </summary>
  1231. /// <exception cref="ArgumentException"> if the enumerated items turns out
  1232. /// not to be in increasing order.</exception>
  1233. /// <param name="items">The collection to add.</param>
  1234. /// <typeparam name="U"></typeparam>
  1235. void AddSorted<U>(SCG.IEnumerable<U> items) where U : T;
  1236. /// <summary>
  1237. /// Remove all items of this collection above or at a supplied threshold.
  1238. /// </summary>
  1239. /// <param name="low">The lower threshold (inclusive).</param>
  1240. void RemoveRangeFrom(T low);
  1241. /// <summary>
  1242. /// Remove all items of this collection between two supplied thresholds.
  1243. /// </summary>
  1244. /// <param name="low">The lower threshold (inclusive).</param>
  1245. /// <param name="hi">The upper threshold (exclusive).</param>
  1246. void RemoveRangeFromTo(T low, T hi);
  1247. /// <summary>
  1248. /// Remove all items of this collection below a supplied threshold.
  1249. /// </summary>
  1250. /// <param name="hi">The upper threshold (exclusive).</param>
  1251. void RemoveRangeTo(T hi);
  1252. }
  1253. /// <summary>
  1254. /// A collection where items are maintained in sorted order together
  1255. /// with their indexes in that order.
  1256. /// </summary>
  1257. public interface IIndexedSorted<T> : ISorted<T>, IIndexed<T>
  1258. {
  1259. /// <summary>
  1260. /// Determine the number of items at or above a supplied threshold.
  1261. /// </summary>
  1262. /// <param name="bot">The lower bound (inclusive)</param>
  1263. /// <returns>The number of matcing items.</returns>
  1264. int CountFrom(T bot);
  1265. /// <summary>
  1266. /// Determine the number of items between two supplied thresholds.
  1267. /// </summary>
  1268. /// <param name="bot">The lower bound (inclusive)</param>
  1269. /// <param name="top">The upper bound (exclusive)</param>
  1270. /// <returns>The number of matcing items.</returns>
  1271. int CountFromTo(T bot, T top);
  1272. /// <summary>
  1273. /// Determine the number of items below a supplied threshold.
  1274. /// </summary>
  1275. /// <param name="top">The upper bound (exclusive)</param>
  1276. /// <returns>The number of matcing items.</returns>
  1277. int CountTo(T top);
  1278. /// <summary>
  1279. /// Query this sorted collection for items greater than or equal to a supplied value.
  1280. /// </summary>
  1281. /// <param name="bot">The lower bound (inclusive).</param>
  1282. /// <returns>The result directed collection.</returns>
  1283. new IDirectedCollectionValue<T> RangeFrom(T bot);
  1284. /// <summary>
  1285. /// Query this sorted collection for items between two supplied values.
  1286. /// </summary>
  1287. /// <param name="bot">The lower bound (inclusive).</param>
  1288. /// <param name="top">The upper bound (exclusive).</param>
  1289. /// <returns>The result directed collection.</returns>
  1290. new IDirectedCollectionValue<T> RangeFromTo(T bot, T top);
  1291. /// <summary>
  1292. /// Query this sorted collection for items less than a supplied value.
  1293. /// </summary>
  1294. /// <param name="top">The upper bound (exclusive).</param>
  1295. /// <returns>The result directed collection.</returns>
  1296. new IDirectedCollectionValue<T> RangeTo(T top);
  1297. /// <summary>
  1298. /// Create a new indexed sorted collection consisting of the items of this
  1299. /// indexed sorted collection satisfying a certain predicate.
  1300. /// </summary>
  1301. /// <param name="predicate">The filter delegate defining the predicate.</param>
  1302. /// <returns>The new indexed sorted collection.</returns>
  1303. IIndexedSorted<T> FindAll(Fun<T, bool> predicate);
  1304. /// <summary>
  1305. /// Create a new indexed sorted collection consisting of the results of
  1306. /// mapping all items of this list.
  1307. /// <exception cref="ArgumentException"/> if the map is not increasing over
  1308. /// the items of this collection (with respect to the two given comparison
  1309. /// relations).
  1310. /// </summary>
  1311. /// <param name="mapper">The delegate definging the map.</param>
  1312. /// <param name="comparer">The comparion relation to use for the result.</param>
  1313. /// <returns>The new sorted collection.</returns>
  1314. IIndexedSorted<V> Map<V>(Fun<T, V> mapper, SCG.IComparer<V> comparer);
  1315. }
  1316. /// <summary>
  1317. /// The type of a sorted collection with persistence
  1318. /// </summary>
  1319. public interface IPersistentSorted<T> : ISorted<T>, IDisposable
  1320. {
  1321. /// <summary>
  1322. /// Make a (read-only) snap shot of this collection.
  1323. /// </summary>
  1324. /// <returns>The snap shot.</returns>
  1325. ISorted<T> Snapshot();
  1326. }
  1327. /*************************************************************************/
  1328. /// <summary>
  1329. /// A dictionary with keys of type K and values of type V. Equivalent to a
  1330. /// finite partial map from K to V.
  1331. /// </summary>
  1332. public interface IDictionary<K, V> : ICollectionValue<KeyValuePair<K, V>>, ICloneable
  1333. {
  1334. /// <summary>
  1335. /// The key equalityComparer.
  1336. /// </summary>
  1337. /// <value></value>
  1338. SCG.IEqualityComparer<K> EqualityComparer { get;}
  1339. /// <summary>
  1340. /// Indexer for dictionary.
  1341. /// </summary>
  1342. /// <exception cref="NoSuchItemException"> if no entry is found. </exception>
  1343. /// <value>The value corresponding to the key</value>
  1344. V this[K key] { get; set;}
  1345. /// <summary>
  1346. ///
  1347. /// </summary>
  1348. /// <value>True if dictionary is read-only</value>
  1349. bool IsReadOnly { get;}
  1350. /// <summary>
  1351. ///
  1352. /// </summary>
  1353. /// <value>A collection containg the all the keys of the dictionary</value>
  1354. ICollectionValue<K> Keys { get;}
  1355. /// <summary>
  1356. ///
  1357. /// </summary>
  1358. /// <value>A collection containing all the values of the dictionary</value>
  1359. ICollectionValue<V> Values { get;}
  1360. /// <summary>
  1361. ///
  1362. /// </summary>
  1363. /// <value>A delegate of type <see cref="T:C5.Fun`2"/> defining the partial function from K to V give by the dictionary.</value>
  1364. Fun<K, V> Fun { get; }
  1365. //TODO: resolve inconsistency: Add thows exception if key already there, AddAll ignores keys already There?
  1366. /// <summary>
  1367. /// Add a new (key, value) pair (a mapping) to the dictionary.
  1368. /// </summary>
  1369. /// <exception cref="DuplicateNotAllowedException"> if there already is an entry with the same key. </exception>>
  1370. /// <param name="key">Key to add</param>
  1371. /// <param name="val">Value to add</param>
  1372. void Add(K key, V val);
  1373. /// <summary>
  1374. /// Add the entries from a collection of <see cref="T:C5.KeyValuePair`2"/> pairs to this dictionary.
  1375. /// </summary>
  1376. /// <exception cref="DuplicateNotAllowedException">
  1377. /// If the input contains duplicate keys or a key already present in this dictionary.</exception>
  1378. /// <param name="entries"></param>
  1379. void AddAll<U, W>(SCG.IEnumerable<KeyValuePair<U, W>> entries)
  1380. where U : K
  1381. where W : V
  1382. ;
  1383. /// <summary>
  1384. /// The value is symbolic indicating the type of asymptotic complexity
  1385. /// in terms of the size of this collection (worst-case or amortized as
  1386. /// relevant).
  1387. /// <para>See <see cref="T:C5.Speed"/> for the set of symbols.</para>
  1388. /// </summary>
  1389. /// <value>A characterization of the speed of lookup operations
  1390. /// (<code>Contains()</code> etc.) of the implementation of this dictionary.</value>
  1391. Speed ContainsSpeed { get;}
  1392. /// <summary>
  1393. /// Check whether this collection contains all the values in another collection.
  1394. /// If this collection has bag semantics (<code>AllowsDuplicates==true</code>)
  1395. /// the check is made with respect to multiplicities, else multiplicities
  1396. /// are not taken into account.
  1397. /// </summary>
  1398. /// <param name="items">The </param>
  1399. /// <returns>True if all values in <code>items</code>is in this collection.</returns>
  1400. bool ContainsAll<H>(SCG.IEnumerable<H> items) where H : K;
  1401. /// <summary>
  1402. /// Remove an entry with a given key from the dictionary
  1403. /// </summary>
  1404. /// <param name="key">The key of the entry to remove</param>
  1405. /// <returns>True if an entry was found (and removed)</returns>
  1406. bool Remove(K key);
  1407. /// <summary>
  1408. /// Remove an entry with a given key from the dictionary and report its value.
  1409. /// </summary>
  1410. /// <param name="key">The key of the entry to remove</param>
  1411. /// <param name="val">On exit, the value of the removed entry</param>
  1412. /// <returns>True if an entry was found (and removed)</returns>
  1413. bool Remove(K key, out V val);
  1414. /// <summary>
  1415. /// Remove all entries from the dictionary
  1416. /// </summary>
  1417. void Clear();
  1418. /// <summary>
  1419. /// Check if there is an entry with a specified key
  1420. /// </summary>
  1421. /// <param name="key">The key to look for</param>
  1422. /// <returns>True if key was found</returns>
  1423. bool Contains(K key);
  1424. /// <summary>
  1425. /// Check if there is an entry with a specified key and report the corresponding
  1426. /// value if found. This can be seen as a safe form of "val = this[key]".
  1427. /// </summary>
  1428. /// <param name="key">The key to look for</param>
  1429. /// <param name="val">On exit, the value of the entry</param>
  1430. /// <returns>True if key was found</returns>
  1431. bool Find(K key, out V val);
  1432. /// <summary>
  1433. /// Check if there is an entry with a specified key and report the corresponding
  1434. /// value if found. This can be seen as a safe form of "val = this[key]".
  1435. /// </summary>
  1436. /// <param name="key">The key to look for</param>
  1437. /// <param name="val">On exit, the value of the entry</param>
  1438. /// <returns>True if key was found</returns>
  1439. bool Find(ref K key, out V val);
  1440. /// <summary>
  1441. /// Look for a specific key in the dictionary and if found replace the value with a new one.
  1442. /// This can be seen as a non-adding version of "this[key] = val".
  1443. /// </summary>
  1444. /// <param name="key">The key to look for</param>
  1445. /// <param name="val">The new value</param>
  1446. /// <returns>True if key was found</returns>
  1447. bool Update(K key, V val); //no-adding
  1448. /// <summary>
  1449. /// Look for a specific key in the dictionary and if found replace the value with a new one.
  1450. /// This can be seen as a non-adding version of "this[key] = val" reporting the old value.
  1451. /// </summary>
  1452. /// <param name="key">The key to look for</param>
  1453. /// <param name="val">The new value</param>
  1454. /// <param name="oldval">The old value if any</param>
  1455. /// <returns>True if key was found</returns>
  1456. bool Update(K key, V val, out V oldval); //no-adding
  1457. /// <summary>
  1458. /// Look for a specific key in the dictionary. If found, report the corresponding value,
  1459. /// else add an entry with the key and the supplied value.
  1460. /// </summary>
  1461. /// <param name="key">The key to look for</param>
  1462. /// <param name="val">On entry the value to add if the key is not found.
  1463. /// On exit the value found if any.</param>
  1464. /// <returns>True if key was found</returns>
  1465. bool FindOrAdd(K key, ref V val); //mixture
  1466. /// <summary>
  1467. /// Update value in dictionary corresponding to key if found, else add new entry.
  1468. /// More general than "this[key] = val;" by reporting if key was found.
  1469. /// </summary>
  1470. /// <param name="key">The key to look for</param>
  1471. /// <param name="val">The value to add or replace with.</param>
  1472. /// <returns>True if key was found and value updated.</returns>
  1473. bool UpdateOrAdd(K key, V val);
  1474. /// <summary>
  1475. /// Update value in dictionary corresponding to key if found, else add new entry.
  1476. /// More general than "this[key] = val;" by reporting if key was found.
  1477. /// </summary>
  1478. /// <param name="key">The key to look for</param>
  1479. /// <param name="val">The value to add or replace with.</param>
  1480. /// <param name="oldval">The old value if any</param>
  1481. /// <returns>True if key was found and value updated.</returns>
  1482. bool UpdateOrAdd(K key, V val, out V oldval);
  1483. /// <summary>
  1484. /// Check the integrity of the internal data structures of this dictionary.
  1485. /// Only avaliable in DEBUG builds???
  1486. /// </summary>
  1487. /// <returns>True if check does not fail.</returns>
  1488. bool Check();
  1489. }
  1490. /// <summary>
  1491. /// A dictionary with sorted keys.
  1492. /// </summary>
  1493. public interface ISortedDictionary<K, V> : IDictionary<K, V>
  1494. {
  1495. /// <summary>
  1496. ///
  1497. /// </summary>
  1498. /// <value></value>
  1499. new ISorted<K> Keys { get;}
  1500. /// <summary>
  1501. /// Find the current least item of this sorted collection.
  1502. /// </summary>
  1503. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1504. /// <returns>The least item.</returns>
  1505. KeyValuePair<K, V> FindMin();
  1506. /// <summary>
  1507. /// Remove the least item from this sorted collection.
  1508. /// </summary>
  1509. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1510. /// <returns>The removed item.</returns>
  1511. KeyValuePair<K, V> DeleteMin();
  1512. /// <summary>
  1513. /// Find the current largest item of this sorted collection.
  1514. /// </summary>
  1515. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1516. /// <returns>The largest item.</returns>
  1517. KeyValuePair<K, V> FindMax();
  1518. /// <summary>
  1519. /// Remove the largest item from this sorted collection.
  1520. /// </summary>
  1521. /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
  1522. /// <returns>The removed item.</returns>
  1523. KeyValuePair<K, V> DeleteMax();
  1524. /// <summary>
  1525. /// The key comparer used by this dictionary.
  1526. /// </summary>
  1527. /// <value></value>
  1528. SCG.IComparer<K> Comparer { get;}
  1529. /// <summary>
  1530. /// Find the entry in the dictionary whose key is the
  1531. /// predecessor of the specified key.
  1532. /// </summary>
  1533. /// <param name="key">The key</param>
  1534. /// <param name="res">The predecessor, if any</param>
  1535. /// <returns>True if key has a predecessor</returns>
  1536. bool TryPredecessor(K key, out KeyValuePair<K, V> res);
  1537. /// <summary>
  1538. /// Find the entry in the dictionary whose key is the
  1539. /// successor of the specified key.
  1540. /// </summary>
  1541. /// <param name="key">The key</param>
  1542. /// <param name="res">The successor, if any</param>
  1543. /// <returns>True if the key has a successor</returns>
  1544. bool TrySuccessor(K key, out KeyValuePair<K, V> res);
  1545. /// <summary>
  1546. /// Find the entry in the dictionary whose key is the
  1547. /// weak predecessor of the specified key.
  1548. /// </summary>
  1549. /// <param name="key">The key</param>
  1550. /// <param name="res">The predecessor, if any</param>
  1551. /// <returns>True if key has a weak predecessor</returns>
  1552. bool TryWeakPredecessor(K key, out KeyValuePair<K, V> res);
  1553. /// <summary>
  1554. /// Find the entry in the dictionary whose key is the
  1555. /// weak successor of the specified key.
  1556. /// </summary>
  1557. /// <param name="key">The key</param>
  1558. /// <param name="res">The weak successor, if any</param>
  1559. /// <returns>True if the key has a weak successor</returns>
  1560. bool TryWeakSuccessor(K key, out KeyValuePair<K, V> res);
  1561. /// <summary>
  1562. /// Find the entry with the largest key less than a given key.
  1563. /// </summary>
  1564. /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
  1565. /// <param name="key">The key to compare to</param>
  1566. /// <returns>The entry</returns>
  1567. KeyValuePair<K, V> Predecessor(K key);
  1568. /// <summary>
  1569. /// Find the entry with the least key greater than a given key.
  1570. /// </summary>
  1571. /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
  1572. /// <param name="key">The key to compare to</param>
  1573. /// <returns>The entry</returns>
  1574. KeyValuePair<K, V> Successor(K key);
  1575. /// <summary>
  1576. /// Find the entry with the largest key less than or equal to a given key.
  1577. /// </summary>
  1578. /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
  1579. /// <param name="key">The key to compare to</param>
  1580. /// <returns>The entry</returns>
  1581. KeyValuePair<K, V> WeakPredecessor(K key);
  1582. /// <summary>
  1583. /// Find the entry with the least key greater than or equal to a given key.
  1584. /// </summary>
  1585. /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
  1586. /// <param name="key">The key to compare to</param>
  1587. /// <returns>The entry</returns>
  1588. KeyValuePair<K, V> WeakSuccessor(K key);
  1589. /// <summary>
  1590. /// Given a "cut" function from the items of the sorted collection to <code>int</code>
  1591. /// whose only sign changes when going through items in increasing order
  1592. /// can be
  1593. /// <list>
  1594. /// <item>from positive to zero</item>
  1595. /// <item>from positive to negative</item>
  1596. /// <item>from zero to negative</item>
  1597. /// </list>
  1598. /// The "cut" function is supplied as the <code>CompareTo</code> method
  1599. /// of an object <code>c</code> implementing
  1600. /// <code>IComparable&lt;K&gt;</code>.
  1601. /// A typical example is the case where <code>K</code> is comparable and
  1602. /// <code>c</code> is itself of type <code>K</code>.
  1603. /// <para>This method performs a search in the sorted collection for the ranges in which the
  1604. /// "cut" function is negative, zero respectively positive. If <code>K</code> is comparable
  1605. /// and <code>c</code> is of type <code>K</code>, this is a safe way (no exceptions thrown)
  1606. /// to find predecessor and successor of <code>c</code>.
  1607. /// </para>
  1608. /// <para> If the supplied cut function does not satisfy the sign-change condition,
  1609. /// the result of this call is undefined.
  1610. /// </para>
  1611. ///
  1612. /// </summary>
  1613. /// <param name="cutFunction">The cut function <code>K</code> to <code>int</code>, given
  1614. /// by the <code>CompareTo</code> method of an object implementing
  1615. /// <code>IComparable&lt;K&gt;</code>.</param>
  1616. /// <param name="lowEntry">Returns the largest item in the collection, where the
  1617. /// cut function is positive (if any).</param>
  1618. /// <param name="lowIsValid">Returns true if the cut function is positive somewhere
  1619. /// on this collection.</param>
  1620. /// <param name="highEntry">Returns the least item in the collection, where the
  1621. /// cut function is negative (if any).</param>
  1622. /// <param name="highIsValid">Returns true if the cut function is negative somewhere
  1623. /// on this collection.</param>
  1624. /// <returns>True if the cut function is zero somewhere
  1625. /// on this collection.</returns>
  1626. bool Cut(IComparable<K> cutFunction, out KeyValuePair<K, V> lowEntry, out bool lowIsValid, out KeyValuePair<K, V> highEntry, out bool highIsValid);
  1627. /// <summary>
  1628. /// Query this sorted collection for items greater than or equal to a supplied value.
  1629. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1630. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1631. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1632. /// </summary>
  1633. /// <param name="bot">The lower bound (inclusive).</param>
  1634. /// <returns>The result directed collection.</returns>
  1635. IDirectedEnumerable<KeyValuePair<K, V>> RangeFrom(K bot);
  1636. /// <summary>
  1637. /// Query this sorted collection for items between two supplied values.
  1638. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1639. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1640. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1641. /// </summary>
  1642. /// <param name="lowerBound">The lower bound (inclusive).</param>
  1643. /// <param name="upperBound">The upper bound (exclusive).</param>
  1644. /// <returns>The result directed collection.</returns>
  1645. IDirectedEnumerable<KeyValuePair<K, V>> RangeFromTo(K lowerBound, K upperBound);
  1646. /// <summary>
  1647. /// Query this sorted collection for items less than a supplied value.
  1648. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1649. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1650. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1651. /// </summary>
  1652. /// <param name="top">The upper bound (exclusive).</param>
  1653. /// <returns>The result directed collection.</returns>
  1654. IDirectedEnumerable<KeyValuePair<K, V>> RangeTo(K top);
  1655. /// <summary>
  1656. /// Create a directed collection with the same items as this collection.
  1657. /// <para>The returned collection is not a copy but a view into the collection.</para>
  1658. /// <para>The view is fragile in the sense that changes to the underlying collection will
  1659. /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
  1660. /// </summary>
  1661. /// <returns>The result directed collection.</returns>
  1662. IDirectedCollectionValue<KeyValuePair<K, V>> RangeAll();
  1663. //TODO: remove now that we assume that we can check the sorting order?
  1664. /// <summary>
  1665. /// Add all the items from another collection with an enumeration order that
  1666. /// is increasing in the items.
  1667. /// </summary>
  1668. /// <exception cref="ArgumentException"> if the enumerated items turns out
  1669. /// not to be in increasing order.</exception>
  1670. /// <param name="items">The collection to add.</param>
  1671. void AddSorted(SCG.IEnumerable<KeyValuePair<K, V>> items);
  1672. /// <summary>
  1673. /// Remove all items of this collection above or at a supplied threshold.
  1674. /// </summary>
  1675. /// <param name="low">The lower threshold (inclusive).</param>
  1676. void RemoveRangeFrom(K low);
  1677. /// <summary>
  1678. /// Remove all items of this collection between two supplied thresholds.
  1679. /// </summary>
  1680. /// <param name="low">The lower threshold (inclusive).</param>
  1681. /// <param name="hi">The upper threshold (exclusive).</param>
  1682. void RemoveRangeFromTo(K low, K hi);
  1683. /// <summary>
  1684. /// Remove all items of this collection below a supplied threshold.
  1685. /// </summary>
  1686. /// <param name="hi">The upper threshold (exclusive).</param>
  1687. void RemoveRangeTo(K hi);
  1688. }
  1689. /*******************************************************************/
  1690. /*/// <summary>
  1691. /// The type of an item comparer
  1692. /// <i>Implementations of this interface must asure that the method is self-consistent
  1693. /// and defines a sorting order on items, or state precise conditions under which this is true.</i>
  1694. /// <i>Implementations <b>must</b> assure that repeated calls of
  1695. /// the method to the same (in reference or binary identity sense) arguments
  1696. /// will return values with the same sign (-1, 0 or +1), or state precise conditions
  1697. /// under which the user
  1698. /// can be assured repeated calls will return the same sign.</i>
  1699. /// <i>Implementations of this interface must always return values from the method
  1700. /// and never throw exceptions.</i>
  1701. /// <i>This interface is identical to System.Collections.Generic.IComparer&lt;T&gt;</i>
  1702. /// </summary>
  1703. public interface IComparer<T>
  1704. {
  1705. /// <summary>
  1706. /// Compare two items with respect to this item comparer
  1707. /// </summary>
  1708. /// <param name="item1">First item</param>
  1709. /// <param name="item2">Second item</param>
  1710. /// <returns>Positive if item1 is greater than item2, 0 if they are equal, negative if item1 is less than item2</returns>
  1711. int Compare(T item1, T item2);
  1712. }
  1713. /// <summary>
  1714. /// The type of an item equalityComparer.
  1715. /// <i>Implementations of this interface <b>must</b> assure that the methods are
  1716. /// consistent, that is, that whenever two items i1 and i2 satisfies that Equals(i1,i2)
  1717. /// returns true, then GetHashCode returns the same value for i1 and i2.</i>
  1718. /// <i>Implementations of this interface <b>must</b> assure that repeated calls of
  1719. /// the methods to the same (in reference or binary identity sense) arguments
  1720. /// will return the same values, or state precise conditions under which the user
  1721. /// can be assured repeated calls will return the same values.</i>
  1722. /// <i>Implementations of this interface must always return values from the methods
  1723. /// and never throw exceptions.</i>
  1724. /// <i>This interface is similar in function to System.IKeyComparer&lt;T&gt;</i>
  1725. /// </summary>
  1726. public interface SCG.IEqualityComparer<T>
  1727. {
  1728. /// <summary>
  1729. /// Get the hash code with respect to this item equalityComparer
  1730. /// </summary>
  1731. /// <param name="item">The item</param>
  1732. /// <returns>The hash code</returns>
  1733. int GetHashCode(T item);
  1734. /// <summary>
  1735. /// Check if two items are equal with respect to this item equalityComparer
  1736. /// </summary>
  1737. /// <param name="item1">first item</param>
  1738. /// <param name="item2">second item</param>
  1739. /// <returns>True if equal</returns>
  1740. bool Equals(T item1, T item2);
  1741. }*/
  1742. }