3 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
\r
4 Permission is hereby granted, free of charge, to any person obtaining a copy
\r
5 of this software and associated documentation files (the "Software"), to deal
\r
6 in the Software without restriction, including without limitation the rights
\r
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
8 copies of the Software, and to permit persons to whom the Software is
\r
9 furnished to do so, subject to the following conditions:
\r
11 The above copyright notice and this permission notice shall be included in
\r
12 all copies or substantial portions of the Software.
\r
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
24 using SCG = System.Collections.Generic;
\r
28 /// A generic collection, that can be enumerated backwards.
\r
30 public interface IDirectedEnumerable<T> : SCG.IEnumerable<T>
\r
33 /// Create a collection containing the same items as this collection, but
\r
34 /// whose enumerator will enumerate the items backwards. The new collection
\r
35 /// will become invalid if the original is modified. Method typically used as in
\r
36 /// <code>foreach (T x in coll.Backwards()) {...}</code>
\r
38 /// <returns>The backwards collection.</returns>
\r
39 IDirectedEnumerable<T> Backwards();
\r
43 /// <code>Forwards</code> if same, else <code>Backwards</code>
\r
45 /// <value>The enumeration direction relative to the original collection.</value>
\r
46 EnumerationDirection Direction { get;}
\r
50 /// A generic collection that may be enumerated and can answer
\r
51 /// efficiently how many items it contains. Like <code>IEnumerable<T></code>,
\r
52 /// this interface does not prescribe any operations to initialize or update the
\r
53 /// collection. The main usage for this interface is to be the return type of
\r
54 /// query operations on generic collection.
\r
56 public interface ICollectionValue<T> : SCG.IEnumerable<T>, IShowable
\r
59 /// A flag bitmap of the events subscribable to by this collection.
\r
62 EventTypeEnum ListenableEvents { get;}
\r
65 /// A flag bitmap of the events currently subscribed to by this collection.
\r
68 EventTypeEnum ActiveEvents { get;}
\r
71 /// The change event. Will be raised for every change operation on the collection.
\r
73 event CollectionChangedHandler<T> CollectionChanged;
\r
76 /// The change event. Will be raised for every clear operation on the collection.
\r
78 event CollectionClearedHandler<T> CollectionCleared;
\r
81 /// The item added event. Will be raised for every individual addition to the collection.
\r
83 event ItemsAddedHandler<T> ItemsAdded;
\r
86 /// The item inserted event. Will be raised for every individual insertion to the collection.
\r
88 event ItemInsertedHandler<T> ItemInserted;
\r
91 /// The item removed event. Will be raised for every individual removal from the collection.
\r
93 event ItemsRemovedHandler<T> ItemsRemoved;
\r
96 /// The item removed at event. Will be raised for every individual removal at from the collection.
\r
98 event ItemRemovedAtHandler<T> ItemRemovedAt;
\r
103 /// <value>True if this collection is empty.</value>
\r
104 bool IsEmpty { get;}
\r
109 /// <value>The number of items in this collection</value>
\r
113 /// The value is symbolic indicating the type of asymptotic complexity
\r
114 /// in terms of the size of this collection (worst-case or amortized as
\r
117 /// <value>A characterization of the speed of the
\r
118 /// <code>Count</code> property in this collection.</value>
\r
119 Speed CountSpeed { get;}
\r
122 /// Copy the items of this collection to a contiguous part of an array.
\r
124 /// <param name="array">The array to copy to</param>
\r
125 /// <param name="index">The index at which to copy the first item</param>
\r
126 void CopyTo(T[] array, int index);
\r
129 /// Create an array with the items of this collection (in the same order as an
\r
130 /// enumerator would output them).
\r
132 /// <returns>The array</returns>
\r
136 /// Apply a delegate to all items of this collection.
\r
138 /// <param name="action">The delegate to apply</param>
\r
139 void Apply(Act<T> action);
\r
143 /// Check if there exists an item that satisfies a
\r
144 /// specific predicate in this collection.
\r
146 /// <param name="predicate">A delegate
\r
147 /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
\r
148 /// <returns>True is such an item exists</returns>
\r
149 bool Exists(Fun<T, bool> predicate);
\r
152 /// Check if there exists an item that satisfies a
\r
153 /// specific predicate in this collection and return the first one in enumeration order.
\r
155 /// <param name="predicate">A delegate
\r
156 /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
\r
157 /// <param name="item"></param>
\r
158 /// <returns>True is such an item exists</returns>
\r
159 bool Find(Fun<T, bool> predicate, out T item);
\r
163 /// Check if all items in this collection satisfies a specific predicate.
\r
165 /// <param name="predicate">A delegate
\r
166 /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
\r
167 /// <returns>True if all items satisfies the predicate</returns>
\r
168 bool All(Fun<T, bool> predicate);
\r
171 /// Choose some item of this collection.
\r
172 /// <para>Implementations must assure that the item
\r
173 /// returned may be efficiently removed.</para>
\r
174 /// <para>Implementors may decide to implement this method in a way such that repeated
\r
175 /// calls do not necessarily give the same result, i.e. so that the result of the following
\r
176 /// test is undetermined:
\r
177 /// <code>coll.Choose() == coll.Choose()</code></para>
\r
179 /// <exception cref="NoSuchItemException">if collection is empty.</exception>
\r
180 /// <returns></returns>
\r
184 /// Create an enumerable, enumerating the items of this collection that satisfies
\r
185 /// a certain condition.
\r
187 /// <param name="filter">The T->bool filter delegate defining the condition</param>
\r
188 /// <returns>The filtered enumerable</returns>
\r
189 SCG.IEnumerable<T> Filter(Fun<T, bool> filter);
\r
195 /// A sized generic collection, that can be enumerated backwards.
\r
197 public interface IDirectedCollectionValue<T> : ICollectionValue<T>, IDirectedEnumerable<T>
\r
200 /// Create a collection containing the same items as this collection, but
\r
201 /// whose enumerator will enumerate the items backwards. The new collection
\r
202 /// will become invalid if the original is modified. Method typically used as in
\r
203 /// <code>foreach (T x in coll.Backwards()) {...}</code>
\r
205 /// <returns>The backwards collection.</returns>
\r
206 new IDirectedCollectionValue<T> Backwards();
\r
209 /// Check if there exists an item that satisfies a
\r
210 /// specific predicate in this collection and return the first one in enumeration order.
\r
212 /// <param name="predicate">A delegate
\r
213 /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
\r
214 /// <param name="item"></param>
\r
215 /// <returns>True is such an item exists</returns>
\r
216 bool FindLast(Fun<T, bool> predicate, out T item);
\r
221 /// A generic collection to which one may add items. This is just the intersection
\r
222 /// of the main stream generic collection interfaces and the priority queue interface,
\r
223 /// <see cref="T:C5.ICollection`1"/> and <see cref="T:C5.IPriorityQueue`1"/>.
\r
225 public interface IExtensible<T> : ICollectionValue<T>, ICloneable
\r
228 /// If true any call of an updating operation will throw an
\r
229 /// <code>ReadOnlyCollectionException</code>
\r
231 /// <value>True if this collection is read-only.</value>
\r
232 bool IsReadOnly { get;}
\r
234 //TODO: wonder where the right position of this is
\r
238 /// <value>False if this collection has set semantics, true if bag semantics.</value>
\r
239 bool AllowsDuplicates { get;}
\r
241 //TODO: wonder where the right position of this is. And the semantics.
\r
243 /// (Here should be a discussion of the role of equalityComparers. Any ).
\r
245 /// <value>The equalityComparer used by this collection to check equality of items.
\r
246 /// Or null (????) if collection does not check equality at all or uses a comparer.</value>
\r
247 SCG.IEqualityComparer<T> EqualityComparer { get;}
\r
249 //ItemEqualityTypeEnum ItemEqualityType {get ;}
\r
251 //TODO: find a good name
\r
254 /// By convention this is true for any collection with set semantics.
\r
256 /// <value>True if only one representative of a group of equal items
\r
257 /// is kept in the collection together with the total count.</value>
\r
258 bool DuplicatesByCounting { get;}
\r
261 /// Add an item to this collection if possible. If this collection has set
\r
262 /// semantics, the item will be added if not already in the collection. If
\r
263 /// bag semantics, the item will always be added.
\r
265 /// <param name="item">The item to add.</param>
\r
266 /// <returns>True if item was added.</returns>
\r
270 /// Add the elements from another collection with a more specialized item type
\r
271 /// to this collection. If this
\r
272 /// collection has set semantics, only items not already in the collection
\r
275 /// <typeparam name="U">The type of items to add</typeparam>
\r
276 /// <param name="items">The items to add</param>
\r
277 void AddAll<U>(SCG.IEnumerable<U> items) where U : T;
\r
279 //void Clear(); // for priority queue
\r
280 //int Count why not?
\r
282 /// Check the integrity of the internal data structures of this collection.
\r
283 /// <i>This is only relevant for developers of the library</i>
\r
285 /// <returns>True if check was passed.</returns>
\r
290 /// The simplest interface of a main stream generic collection
\r
291 /// with lookup, insertion and removal operations.
\r
293 public interface ICollection<T> : IExtensible<T>
\r
295 //This is somewhat similar to the RandomAccess marker itf in java
\r
297 /// The value is symbolic indicating the type of asymptotic complexity
\r
298 /// in terms of the size of this collection (worst-case or amortized as
\r
300 /// <para>See <see cref="T:C5.Speed"/> for the set of symbols.</para>
\r
302 /// <value>A characterization of the speed of lookup operations
\r
303 /// (<code>Contains()</code> etc.) of the implementation of this collection.</value>
\r
304 Speed ContainsSpeed { get;}
\r
307 /// The unordered collection hashcode is defined as the sum of
\r
308 /// <code>h(hashcode(item))</code> over the items
\r
309 /// of the collection, where the function <code>h</code> is a function from
\r
310 /// int to int of the form <code> t -> (a0*t+b0)^(a1*t+b1)^(a2*t+b2)</code>, where
\r
311 /// the ax and bx are the same for all collection classes.
\r
312 /// <para>The current implementation uses fixed values for the ax and bx,
\r
313 /// specified as constants in the code.</para>
\r
315 /// <returns>The unordered hashcode of this collection.</returns>
\r
316 int GetUnsequencedHashCode();
\r
320 /// Compare the contents of this collection to another one without regards to
\r
321 /// the sequence order. The comparison will use this collection's itemequalityComparer
\r
322 /// to compare individual items.
\r
324 /// <param name="otherCollection">The collection to compare to.</param>
\r
325 /// <returns>True if this collection and that contains the same items.</returns>
\r
326 bool UnsequencedEquals(ICollection<T> otherCollection);
\r
330 /// Check if this collection contains (an item equivalent to according to the
\r
331 /// itemequalityComparer) a particular value.
\r
333 /// <param name="item">The value to check for.</param>
\r
334 /// <returns>True if the items is in this collection.</returns>
\r
335 bool Contains(T item);
\r
339 /// Count the number of items of the collection equal to a particular value.
\r
340 /// Returns 0 if and only if the value is not in the collection.
\r
342 /// <param name="item">The value to count.</param>
\r
343 /// <returns>The number of copies found.</returns>
\r
344 int ContainsCount(T item);
\r
349 /// <returns></returns>
\r
350 ICollectionValue<T> UniqueItems();
\r
355 /// <returns></returns>
\r
356 ICollectionValue<KeyValuePair<T, int>> ItemMultiplicities();
\r
359 /// Check whether this collection contains all the values in another collection.
\r
360 /// If this collection has bag semantics (<code>AllowsDuplicates==true</code>)
\r
361 /// the check is made with respect to multiplicities, else multiplicities
\r
362 /// are not taken into account.
\r
364 /// <param name="items">The </param>
\r
365 /// <typeparam name="U"></typeparam>
\r
366 /// <returns>True if all values in <code>items</code>is in this collection.</returns>
\r
367 bool ContainsAll<U>(SCG.IEnumerable<U> items) where U : T;
\r
371 /// Check if this collection contains an item equivalent according to the
\r
372 /// itemequalityComparer to a particular value. If so, return in the ref argument (a
\r
373 /// binary copy of) the actual value found.
\r
375 /// <param name="item">The value to look for.</param>
\r
376 /// <returns>True if the items is in this collection.</returns>
\r
377 bool Find(ref T item);
\r
380 //This should probably just be bool Add(ref T item); !!!
\r
382 /// Check if this collection contains an item equivalent according to the
\r
383 /// itemequalityComparer to a particular value. If so, return in the ref argument (a
\r
384 /// binary copy of) the actual value found. Else, add the item to the collection.
\r
386 /// <param name="item">The value to look for.</param>
\r
387 /// <returns>True if the item was found (hence not added).</returns>
\r
388 bool FindOrAdd(ref T item);
\r
392 /// Check if this collection contains an item equivalent according to the
\r
393 /// itemequalityComparer to a particular value. If so, update the item in the collection
\r
394 /// with a (binary copy of) the supplied value. If the collection has bag semantics,
\r
395 /// it depends on the value of DuplicatesByCounting if this updates all equivalent copies in
\r
396 /// the collection or just one.
\r
398 /// <param name="item">Value to update.</param>
\r
399 /// <returns>True if the item was found and hence updated.</returns>
\r
400 bool Update(T item);
\r
403 /// Check if this collection contains an item equivalent according to the
\r
404 /// itemequalityComparer to a particular value. If so, update the item in the collection
\r
405 /// with a (binary copy of) the supplied value. If the collection has bag semantics,
\r
406 /// it depends on the value of DuplicatesByCounting if this updates all equivalent copies in
\r
407 /// the collection or just one.
\r
409 /// <param name="item">Value to update.</param>
\r
410 /// <param name="olditem">On output the olditem, if found.</param>
\r
411 /// <returns>True if the item was found and hence updated.</returns>
\r
412 bool Update(T item, out T olditem);
\r
416 /// Check if this collection contains an item equivalent according to the
\r
417 /// itemequalityComparer to a particular value. If so, update the item in the collection
\r
418 /// to with a binary copy of the supplied value; else add the value to the collection.
\r
420 /// <param name="item">Value to add or update.</param>
\r
421 /// <returns>True if the item was found and updated (hence not added).</returns>
\r
422 bool UpdateOrAdd(T item);
\r
426 /// Check if this collection contains an item equivalent according to the
\r
427 /// itemequalityComparer to a particular value. If so, update the item in the collection
\r
428 /// to with a binary copy of the supplied value; else add the value to the collection.
\r
430 /// <param name="item">Value to add or update.</param>
\r
431 /// <param name="olditem">On output the olditem, if found.</param>
\r
432 /// <returns>True if the item was found and updated (hence not added).</returns>
\r
433 bool UpdateOrAdd(T item, out T olditem);
\r
436 /// Remove a particular item from this collection. If the collection has bag
\r
437 /// semantics only one copy equivalent to the supplied item is removed.
\r
439 /// <param name="item">The value to remove.</param>
\r
440 /// <returns>True if the item was found (and removed).</returns>
\r
441 bool Remove(T item);
\r
445 /// Remove a particular item from this collection if found. If the collection
\r
446 /// has bag semantics only one copy equivalent to the supplied item is removed,
\r
447 /// which one is implementation dependent.
\r
448 /// If an item was removed, report a binary copy of the actual item removed in
\r
451 /// <param name="item">The value to remove.</param>
\r
452 /// <param name="removeditem">The value removed if any.</param>
\r
453 /// <returns>True if the item was found (and removed).</returns>
\r
454 bool Remove(T item, out T removeditem);
\r
458 /// Remove all items equivalent to a given value.
\r
460 /// <param name="item">The value to remove.</param>
\r
461 void RemoveAllCopies(T item);
\r
465 /// Remove all items in another collection from this one. If this collection
\r
466 /// has bag semantics, take multiplicities into account.
\r
468 /// <typeparam name="U"></typeparam>
\r
469 /// <param name="items">The items to remove.</param>
\r
470 void RemoveAll<U>(SCG.IEnumerable<U> items) where U : T;
\r
472 //void RemoveAll(Fun<T, bool> predicate);
\r
475 /// Remove all items from this collection.
\r
481 /// Remove all items not in some other collection from this one. If this collection
\r
482 /// has bag semantics, take multiplicities into account.
\r
484 /// <typeparam name="U"></typeparam>
\r
485 /// <param name="items">The items to retain.</param>
\r
486 void RetainAll<U>(SCG.IEnumerable<U> items) where U : T;
\r
488 //void RetainAll(Fun<T, bool> predicate);
\r
489 //IDictionary<T> UniqueItems()
\r
495 /// An editable collection maintaining a definite sequence order of the items.
\r
497 /// <i>Implementations of this interface must compute the hash code and
\r
498 /// equality exactly as prescribed in the method definitions in order to
\r
499 /// be consistent with other collection classes implementing this interface.</i>
\r
500 /// <i>This interface is usually implemented by explicit interface implementation,
\r
501 /// not as ordinary virtual methods.</i>
\r
503 public interface ISequenced<T> : ICollection<T>, IDirectedCollectionValue<T>
\r
506 /// The hashcode is defined as <code>h(...h(h(h(x1),x2),x3),...,xn)</code> for
\r
507 /// <code>h(a,b)=CONSTANT*a+b</code> and the x's the hash codes of the items of
\r
508 /// this collection.
\r
510 /// <returns>The sequence order hashcode of this collection.</returns>
\r
511 int GetSequencedHashCode();
\r
515 /// Compare this sequenced collection to another one in sequence order.
\r
517 /// <param name="otherCollection">The sequenced collection to compare to.</param>
\r
518 /// <returns>True if this collection and that contains equal (according to
\r
519 /// this collection's itemequalityComparer) in the same sequence order.</returns>
\r
520 bool SequencedEquals(ISequenced<T> otherCollection);
\r
526 /// A sequenced collection, where indices of items in the order are maintained
\r
528 public interface IIndexed<T> : ISequenced<T>
\r
532 /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
\r
533 /// >= the size of the collection.</exception>
\r
534 /// <value>The <code>index</code>'th item of this list.</value>
\r
535 /// <param name="index">the index to lookup</param>
\r
536 T this[int index] { get;}
\r
541 /// <value></value>
\r
542 Speed IndexingSpeed { get;}
\r
546 /// <exception cref="ArgumentOutOfRangeException"></exception>
\r
547 /// <value>The directed collection of items in a specific index interval.</value>
\r
548 /// <param name="start">The low index of the interval (inclusive).</param>
\r
549 /// <param name="count">The size of the range.</param>
\r
550 IDirectedCollectionValue<T> this[int start, int count] { get;}
\r
554 /// Searches for an item in the list going forwards from the start.
\r
556 /// <param name="item">Item to search for.</param>
\r
557 /// <returns>Index of item from start. A negative number if item not found,
\r
558 /// namely the two-complement of the index at which the Add operation would put the item.</returns>
\r
559 int IndexOf(T item);
\r
563 /// Searches for an item in the list going backwards from the end.
\r
565 /// <param name="item">Item to search for.</param>
\r
566 /// <returns>Index of of item from the end. A negative number if item not found,
\r
567 /// namely the two-complement of the index at which the Add operation would put the item.</returns>
\r
568 int LastIndexOf(T item);
\r
571 /// Check if there exists an item that satisfies a
\r
572 /// specific predicate in this collection and return the index of the first one.
\r
574 /// <param name="predicate">A delegate
\r
575 /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
\r
576 /// <returns>the index, if found, a negative value else</returns>
\r
577 int FindIndex(Fun<T, bool> predicate);
\r
580 /// Check if there exists an item that satisfies a
\r
581 /// specific predicate in this collection and return the index of the last one.
\r
583 /// <param name="predicate">A delegate
\r
584 /// (<see cref="T:C5.Fun`2"/> with <code>R == bool</code>) defining the predicate</param>
\r
585 /// <returns>the index, if found, a negative value else</returns>
\r
586 int FindLastIndex(Fun<T, bool> predicate);
\r
590 /// Remove the item at a specific position of the list.
\r
592 /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
\r
593 /// >= the size of the collection.</exception>
\r
594 /// <param name="index">The index of the item to remove.</param>
\r
595 /// <returns>The removed item.</returns>
\r
596 T RemoveAt(int index);
\r
600 /// Remove all items in an index interval.
\r
602 /// <exception cref="ArgumentOutOfRangeException"> if start or count
\r
603 /// is negative or start+count > the size of the collection.</exception>
\r
604 /// <param name="start">The index of the first item to remove.</param>
\r
605 /// <param name="count">The number of items to remove.</param>
\r
606 void RemoveInterval(int start, int count);
\r
609 //TODO: decide if this should extend ICollection
\r
611 /// The interface describing the operations of a LIFO stack data structure.
\r
613 /// <typeparam name="T">The item type</typeparam>
\r
614 public interface IStack<T> : IDirectedCollectionValue<T>
\r
619 /// <value></value>
\r
620 bool AllowsDuplicates { get;}
\r
622 /// Get the <code>index</code>'th element of the stack. The bottom of the stack has index 0.
\r
624 /// <param name="index"></param>
\r
625 /// <returns></returns>
\r
626 T this[int index] { get;}
\r
628 /// Push an item to the top of the stack.
\r
630 /// <param name="item">The item</param>
\r
633 /// Pop the item at the top of the stack from the stack.
\r
635 /// <returns>The popped item.</returns>
\r
640 /// The interface describing the operations of a FIFO queue data structure.
\r
642 /// <typeparam name="T">The item type</typeparam>
\r
643 public interface IQueue<T> : IDirectedCollectionValue<T>
\r
648 /// <value></value>
\r
649 bool AllowsDuplicates { get;}
\r
651 /// Get the <code>index</code>'th element of the queue. The front of the queue has index 0.
\r
653 /// <param name="index"></param>
\r
654 /// <returns></returns>
\r
655 T this[int index] { get;}
\r
657 /// Enqueue an item at the back of the queue.
\r
659 /// <param name="item">The item</param>
\r
660 void Enqueue(T item);
\r
662 /// Dequeue an item from the front of the queue.
\r
664 /// <returns>The item</returns>
\r
670 /// This is an indexed collection, where the item order is chosen by
\r
671 /// the user at insertion time.
\r
673 /// NBNBNB: we need a description of the view functionality here!
\r
675 public interface IList<T> : IIndexed<T>, IDisposable
\r
679 /// <exception cref="NoSuchItemException"> if this list is empty.</exception>
\r
680 /// <value>The first item in this list.</value>
\r
685 /// <exception cref="NoSuchItemException"> if this list is empty.</exception>
\r
686 /// <value>The last item in this list.</value>
\r
690 /// Since <code>Add(T item)</code> always add at the end of the list,
\r
691 /// this describes if list has FIFO or LIFO semantics.
\r
693 /// <value>True if the <code>Remove()</code> operation removes from the
\r
694 /// start of the list, false if it removes from the end.</value>
\r
695 bool FIFO { get; set;}
\r
700 bool IsFixedSize { get; }
\r
703 /// On this list, this indexer is read/write.
\r
705 /// <exception cref="IndexOutOfRangeException"> if index is negative or
\r
706 /// >= the size of the collection.</exception>
\r
707 /// <value>The index'th item of this list.</value>
\r
708 /// <param name="index">The index of the item to fetch or store.</param>
\r
709 new T this[int index] { get; set;}
\r
712 /// Insert an item at a specific index location in this list.
\r
714 /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
\r
715 /// > the size of the collection.</exception>
\r
716 /// <exception cref="DuplicateNotAllowedException"> if the list has
\r
717 /// <code>AllowsDuplicates==false</code> and the item is
\r
718 /// already in the list.</exception>
\r
719 /// <param name="index">The index at which to insert.</param>
\r
720 /// <param name="item">The item to insert.</param>
\r
721 void Insert(int index, T item);
\r
724 /// Insert an item at the end of a compatible view, used as a pointer.
\r
725 /// <para>The <code>pointer</code> must be a view on the same list as
\r
726 /// <code>this</code> and the endpoitn of <code>pointer</code> must be
\r
727 /// a valid insertion point of <code>this</code></para>
\r
729 /// <exception cref="IncompatibleViewException">If <code>pointer</code>
\r
730 /// is not a view on the same list as <code>this</code></exception>
\r
731 /// <exception cref="IndexOutOfRangeException"><b>??????</b> if the endpoint of
\r
732 /// <code>pointer</code> is not inside <code>this</code></exception>
\r
733 /// <exception cref="DuplicateNotAllowedException"> if the list has
\r
734 /// <code>AllowsDuplicates==false</code> and the item is
\r
735 /// already in the list.</exception>
\r
736 /// <param name="pointer"></param>
\r
737 /// <param name="item"></param>
\r
738 void Insert(IList<T> pointer, T item);
\r
741 /// Insert an item at the front of this list.
\r
742 /// <exception cref="DuplicateNotAllowedException"/> if the list has
\r
743 /// <code>AllowsDuplicates==false</code> and the item is
\r
744 /// already in the list.
\r
746 /// <param name="item">The item to insert.</param>
\r
747 void InsertFirst(T item);
\r
750 /// Insert an item at the back of this list.
\r
751 /// <exception cref="DuplicateNotAllowedException"/> if the list has
\r
752 /// <code>AllowsDuplicates==false</code> and the item is
\r
753 /// already in the list.
\r
755 /// <param name="item">The item to insert.</param>
\r
756 void InsertLast(T item);
\r
759 /// Insert into this list all items from an enumerable collection starting
\r
760 /// at a particular index.
\r
762 /// <exception cref="IndexOutOfRangeException"> if <code>index</code> is negative or
\r
763 /// > the size of the collection.</exception>
\r
764 /// <exception cref="DuplicateNotAllowedException"> if the list has
\r
765 /// <code>AllowsDuplicates==false</code> and one of the items to insert is
\r
766 /// already in the list.</exception>
\r
767 /// <param name="index">Index to start inserting at</param>
\r
768 /// <param name="items">Items to insert</param>
\r
769 /// <typeparam name="U"></typeparam>
\r
770 void InsertAll<U>(int index, SCG.IEnumerable<U> items) where U : T;
\r
773 /// Create a new list consisting of the items of this list satisfying a
\r
774 /// certain predicate.
\r
776 /// <param name="filter">The filter delegate defining the predicate.</param>
\r
777 /// <returns>The new list.</returns>
\r
778 IList<T> FindAll(Fun<T, bool> filter);
\r
781 /// Create a new list consisting of the results of mapping all items of this
\r
782 /// list. The new list will use the default equalityComparer for the item type V.
\r
784 /// <typeparam name="V">The type of items of the new list</typeparam>
\r
785 /// <param name="mapper">The delegate defining the map.</param>
\r
786 /// <returns>The new list.</returns>
\r
787 IList<V> Map<V>(Fun<T, V> mapper);
\r
790 /// Create a new list consisting of the results of mapping all items of this
\r
791 /// list. The new list will use a specified equalityComparer for the item type.
\r
793 /// <typeparam name="V">The type of items of the new list</typeparam>
\r
794 /// <param name="mapper">The delegate defining the map.</param>
\r
795 /// <param name="equalityComparer">The equalityComparer to use for the new list</param>
\r
796 /// <returns>The new list.</returns>
\r
797 IList<V> Map<V>(Fun<T, V> mapper, SCG.IEqualityComparer<V> equalityComparer);
\r
800 /// Remove one item from the list: from the front if <code>FIFO</code>
\r
801 /// is true, else from the back.
\r
802 /// <exception cref="NoSuchItemException"/> if this list is empty.
\r
804 /// <returns>The removed item.</returns>
\r
808 /// Remove one item from the front of the list.
\r
809 /// <exception cref="NoSuchItemException"/> if this list is empty.
\r
811 /// <returns>The removed item.</returns>
\r
815 /// Remove one item from the back of the list.
\r
816 /// <exception cref="NoSuchItemException"/> if this list is empty.
\r
818 /// <returns>The removed item.</returns>
\r
822 /// Create a list view on this list.
\r
823 /// <exception cref="ArgumentOutOfRangeException"/> if the view would not fit into
\r
826 /// <param name="start">The index in this list of the start of the view.</param>
\r
827 /// <param name="count">The size of the view.</param>
\r
828 /// <returns>The new list view.</returns>
\r
829 IList<T> View(int start, int count);
\r
832 /// Create a list view on this list containing the (first) occurrence of a particular item.
\r
833 /// <exception cref="NoSuchItemException"/> if the item is not in this list.
\r
835 /// <param name="item">The item to find.</param>
\r
836 /// <returns>The new list view.</returns>
\r
837 IList<T> ViewOf(T item);
\r
840 /// Create a list view on this list containing the last occurrence of a particular item.
\r
841 /// <exception cref="NoSuchItemException"/> if the item is not in this list.
\r
843 /// <param name="item">The item to find.</param>
\r
844 /// <returns>The new list view.</returns>
\r
845 IList<T> LastViewOf(T item);
\r
848 /// Null if this list is not a view.
\r
850 /// <value>Underlying list for view.</value>
\r
851 IList<T> Underlying { get;}
\r
855 /// <value>Offset for this list view or 0 for an underlying list.</value>
\r
861 /// <value></value>
\r
862 bool IsValid { get;}
\r
865 /// Slide this list view along the underlying list.
\r
867 /// <exception cref="NotAViewException"> if this list is not a view.</exception>
\r
868 /// <exception cref="ArgumentOutOfRangeException"> if the operation
\r
869 /// would bring either end of the view outside the underlying list.</exception>
\r
870 /// <param name="offset">The signed amount to slide: positive to slide
\r
871 /// towards the end.</param>
\r
872 IList<T> Slide(int offset);
\r
875 /// Slide this list view along the underlying list, changing its size.
\r
878 /// <exception cref="NotAViewException"> if this list is not a view.</exception>
\r
879 /// <exception cref="ArgumentOutOfRangeException"> if the operation
\r
880 /// would bring either end of the view outside the underlying list.</exception>
\r
881 /// <param name="offset">The signed amount to slide: positive to slide
\r
882 /// towards the end.</param>
\r
883 /// <param name="size">The new size of the view.</param>
\r
884 IList<T> Slide(int offset, int size);
\r
889 /// <param name="offset"></param>
\r
890 /// <returns></returns>
\r
891 bool TrySlide(int offset);
\r
896 /// <param name="offset"></param>
\r
897 /// <param name="size"></param>
\r
898 /// <returns></returns>
\r
899 bool TrySlide(int offset, int size);
\r
903 /// <para>Returns null if <code>otherView</code> is strictly to the left of this view</para>
\r
905 /// <param name="otherView"></param>
\r
906 /// <exception cref="IncompatibleViewException">If otherView does not have the same underlying list as this</exception>
\r
907 /// <exception cref="ArgumentOutOfRangeException">If <code>otherView</code> is strictly to the left of this view</exception>
\r
908 /// <returns></returns>
\r
909 IList<T> Span(IList<T> otherView);
\r
912 /// Reverse the list so the items are in the opposite sequence order.
\r
917 /// Check if this list is sorted according to the default sorting order
\r
918 /// for the item type T, as defined by the <see cref="T:C5.Comparer`1"/> class
\r
920 /// <exception cref="NotComparableException">if T is not comparable</exception>
\r
921 /// <returns>True if the list is sorted, else false.</returns>
\r
925 /// Check if this list is sorted according to a specific sorting order.
\r
927 /// <param name="comparer">The comparer defining the sorting order.</param>
\r
928 /// <returns>True if the list is sorted, else false.</returns>
\r
929 bool IsSorted(SCG.IComparer<T> comparer);
\r
932 /// Sort the items of the list according to the default sorting order
\r
933 /// for the item type T, as defined by the <see cref="T:C5.Comparer`1"/> class
\r
935 /// <exception cref="NotComparableException">if T is not comparable</exception>
\r
939 /// Sort the items of the list according to a specified sorting order.
\r
940 /// <para>The sorting does not perform duplicate elimination or identify items
\r
941 /// according to the comparer or itemequalityComparer. I.e. the list as an
\r
942 /// unsequenced collection with binary equality, will not change.
\r
945 /// <param name="comparer">The comparer defining the sorting order.</param>
\r
946 void Sort(SCG.IComparer<T> comparer);
\r
950 /// Randomly shuffle the items of this list.
\r
956 /// Shuffle the items of this list according to a specific random source.
\r
958 /// <param name="rnd">The random source.</param>
\r
959 void Shuffle(Random rnd);
\r
964 /// The base type of a priority queue handle
\r
966 /// <typeparam name="T"></typeparam>
\r
967 public interface IPriorityQueueHandle<T>
\r
969 //TODO: make abstract and prepare for double dispatch:
\r
970 //public virtual bool Delete(IPriorityQueue<T> q) { throw new InvalidFooException();}
\r
971 //bool Replace(T item);
\r
976 /// A generic collection of items prioritized by a comparison (order) relation.
\r
977 /// Supports adding items and reporting or removing extremal elements.
\r
981 /// When adding an item, the user may choose to have a handle allocated for this item in the queue.
\r
982 /// The resulting handle may be used for deleting the item even if not extremal, and for replacing the item.
\r
983 /// A priority queue typically only holds numeric priorities associated with some objects
\r
984 /// maintained separately in other collection objects.
\r
986 public interface IPriorityQueue<T> : IExtensible<T>
\r
989 /// Find the current least item of this priority queue.
\r
991 /// <returns>The least item.</returns>
\r
996 /// Remove the least item from this priority queue.
\r
998 /// <returns>The removed item.</returns>
\r
1003 /// Find the current largest item of this priority queue.
\r
1005 /// <returns>The largest item.</returns>
\r
1010 /// Remove the largest item from this priority queue.
\r
1012 /// <returns>The removed item.</returns>
\r
1016 /// The comparer object supplied at creation time for this collection
\r
1018 /// <value>The comparer</value>
\r
1019 SCG.IComparer<T> Comparer { get;}
\r
1021 /// Get or set the item corresponding to a handle. Throws exceptions on
\r
1022 /// invalid handles.
\r
1024 /// <param name="handle"></param>
\r
1025 /// <returns></returns>
\r
1026 T this[IPriorityQueueHandle<T> handle] { get; set;}
\r
1029 /// Check if the entry corresponding to a handle is in the priority queue.
\r
1031 /// <param name="handle"></param>
\r
1032 /// <param name="item"></param>
\r
1033 /// <returns></returns>
\r
1034 bool Find(IPriorityQueueHandle<T> handle, out T item);
\r
1037 /// Add an item to the priority queue, receiving a
\r
1038 /// handle for the item in the queue,
\r
1039 /// or reusing an existing unused handle.
\r
1041 /// <param name="handle">On output: a handle for the added item.
\r
1042 /// On input: null for allocating a new handle, or a currently unused handle for reuse.
\r
1043 /// A handle for reuse must be compatible with this priority queue,
\r
1044 /// by being created by a priority queue of the same runtime type, but not
\r
1045 /// necessarily the same priority queue object.</param>
\r
1046 /// <param name="item"></param>
\r
1047 /// <returns></returns>
\r
1048 bool Add(ref IPriorityQueueHandle<T> handle, T item);
\r
1051 /// Delete an item with a handle from a priority queue
\r
1053 /// <param name="handle">The handle for the item. The handle will be invalidated, but reusable.</param>
\r
1054 /// <returns>The deleted item</returns>
\r
1055 T Delete(IPriorityQueueHandle<T> handle);
\r
1058 /// Replace an item with a handle in a priority queue with a new item.
\r
1059 /// Typically used for changing the priority of some queued object.
\r
1061 /// <param name="handle">The handle for the old item</param>
\r
1062 /// <param name="item">The new item</param>
\r
1063 /// <returns>The old item</returns>
\r
1064 T Replace(IPriorityQueueHandle<T> handle, T item);
\r
1067 /// Find the current least item of this priority queue.
\r
1069 /// <param name="handle">On return: the handle of the item.</param>
\r
1070 /// <returns>The least item.</returns>
\r
1071 T FindMin(out IPriorityQueueHandle<T> handle);
\r
1074 /// Find the current largest item of this priority queue.
\r
1076 /// <param name="handle">On return: the handle of the item.</param>
\r
1077 /// <returns>The largest item.</returns>
\r
1079 T FindMax(out IPriorityQueueHandle<T> handle);
\r
1082 /// Remove the least item from this priority queue.
\r
1084 /// <param name="handle">On return: the handle of the removed item.</param>
\r
1085 /// <returns>The removed item.</returns>
\r
1087 T DeleteMin(out IPriorityQueueHandle<T> handle);
\r
1090 /// Remove the largest item from this priority queue.
\r
1092 /// <param name="handle">On return: the handle of the removed item.</param>
\r
1093 /// <returns>The removed item.</returns>
\r
1094 T DeleteMax(out IPriorityQueueHandle<T> handle);
\r
1100 /// A sorted collection, i.e. a collection where items are maintained and can be searched for in sorted order.
\r
1101 /// Thus the sequence order is given as a sorting order.
\r
1103 /// <para>The sorting order is defined by a comparer, an object of type IComparer<T>
\r
1104 /// (<see cref="T:C5.IComparer`1"/>). Implementors of this interface will normally let the user
\r
1105 /// define the comparer as an argument to a constructor.
\r
1106 /// Usually there will also be constructors without a comparer argument, in which case the
\r
1107 /// comparer should be the defalt comparer for the item type, <see cref="P:C5.Comparer`1.Default"/>.</para>
\r
1109 /// <para>The comparer of the sorted collection is available as the <code>Comparer</code> property
\r
1110 /// (<see cref="P:C5.ISorted`1.Comparer"/>).</para>
\r
1112 /// <para>The methods are grouped according to
\r
1114 /// <item>Extrema: report or report and delete an extremal item. This is reminiscent of simplified priority queues.</item>
\r
1115 /// <item>Nearest neighbor: report predecessor or successor in the collection of an item. Cut belongs to this group.</item>
\r
1116 /// <item>Range: report a view of a range of elements or remove all elements in a range.</item>
\r
1117 /// <item>AddSorted: add a collection of items known to be sorted in the same order (should be faster) (to be removed?)</item>
\r
1121 /// <para>Since this interface extends ISequenced<T>, sorted collections will also have an
\r
1122 /// item equalityComparer (<see cref="P:C5.IExtensible`1.EqualityComparer"/>). This equalityComparer will not be used in connection with
\r
1123 /// the inner workings of the sorted collection, but will be used if the sorted collection is used as
\r
1124 /// an item in a collection of unsequenced or sequenced collections,
\r
1125 /// (<see cref="T:C5.ICollection`1"/> and <see cref="T:C5.ISequenced`1"/>)</para>
\r
1127 /// <para>Note that code may check if two sorted collections has the same sorting order
\r
1128 /// by checking if the Comparer properties are equal. This is done a few places in this library
\r
1129 /// for optimization purposes.</para>
\r
1131 public interface ISorted<T> : ISequenced<T>
\r
1134 /// Find the current least item of this sorted collection.
\r
1136 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1137 /// <returns>The least item.</returns>
\r
1142 /// Remove the least item from this sorted collection.
\r
1144 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1145 /// <returns>The removed item.</returns>
\r
1150 /// Find the current largest item of this sorted collection.
\r
1152 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1153 /// <returns>The largest item.</returns>
\r
1158 /// Remove the largest item from this sorted collection.
\r
1160 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1161 /// <returns>The removed item.</returns>
\r
1165 /// The comparer object supplied at creation time for this sorted collection.
\r
1167 /// <value>The comparer</value>
\r
1168 SCG.IComparer<T> Comparer { get;}
\r
1171 /// Find the strict predecessor in the sorted collection of a particular value,
\r
1172 /// that is, the largest item in the collection less than the supplied value.
\r
1174 /// <exception cref="NoSuchItemException"> if no such element exists (the
\r
1175 /// supplied value is less than or equal to the minimum of this collection.)</exception>
\r
1176 /// <param name="item">The item to find the predecessor for.</param>
\r
1177 /// <returns>The predecessor.</returns>
\r
1178 T Predecessor(T item);
\r
1182 /// Find the strict successor in the sorted collection of a particular value,
\r
1183 /// that is, the least item in the collection greater than the supplied value.
\r
1185 /// <exception cref="NoSuchItemException"> if no such element exists (the
\r
1186 /// supplied value is greater than or equal to the maximum of this collection.)</exception>
\r
1187 /// <param name="item">The item to find the successor for.</param>
\r
1188 /// <returns>The successor.</returns>
\r
1189 T Successor(T item);
\r
1193 /// Find the weak predecessor in the sorted collection of a particular value,
\r
1194 /// that is, the largest item in the collection less than or equal to the supplied value.
\r
1196 /// <exception cref="NoSuchItemException"> if no such element exists (the
\r
1197 /// supplied value is less than the minimum of this collection.)</exception>
\r
1198 /// <param name="item">The item to find the weak predecessor for.</param>
\r
1199 /// <returns>The weak predecessor.</returns>
\r
1200 T WeakPredecessor(T item);
\r
1204 /// Find the weak successor in the sorted collection of a particular value,
\r
1205 /// that is, the least item in the collection greater than or equal to the supplied value.
\r
1207 /// <exception cref="NoSuchItemException"> if no such element exists (the
\r
1208 /// supplied value is greater than the maximum of this collection.)</exception>
\r
1209 ///<param name="item">The item to find the weak successor for.</param>
\r
1210 /// <returns>The weak successor.</returns>
\r
1211 T WeakSuccessor(T item);
\r
1215 /// Given a "cut" function from the items of the sorted collection to <code>int</code>
\r
1216 /// whose only sign changes when going through items in increasing order
\r
1219 /// <item>from positive to zero</item>
\r
1220 /// <item>from positive to negative</item>
\r
1221 /// <item>from zero to negative</item>
\r
1223 /// The "cut" function is supplied as the <code>CompareTo</code> method
\r
1224 /// of an object <code>c</code> implementing
\r
1225 /// <code>IComparable<T></code>.
\r
1226 /// A typical example is the case where <code>T</code> is comparable and
\r
1227 /// <code>cutFunction</code> is itself of type <code>T</code>.
\r
1228 /// <para>This method performs a search in the sorted collection for the ranges in which the
\r
1229 /// "cut" function is negative, zero respectively positive. If <code>T</code> is comparable
\r
1230 /// and <code>c</code> is of type <code>T</code>, this is a safe way (no exceptions thrown)
\r
1231 /// to find predecessor and successor of <code>c</code>.
\r
1233 /// <para> If the supplied cut function does not satisfy the sign-change condition,
\r
1234 /// the result of this call is undefined.
\r
1238 /// <param name="cutFunction">The cut function <code>T</code> to <code>int</code>, given
\r
1239 /// by the <code>CompareTo</code> method of an object implementing
\r
1240 /// <code>IComparable<T></code>.</param>
\r
1241 /// <param name="low">Returns the largest item in the collection, where the
\r
1242 /// cut function is positive (if any).</param>
\r
1243 /// <param name="lowIsValid">Returns true if the cut function is positive somewhere
\r
1244 /// on this collection.</param>
\r
1245 /// <param name="high">Returns the least item in the collection, where the
\r
1246 /// cut function is negative (if any).</param>
\r
1247 /// <param name="highIsValid">Returns true if the cut function is negative somewhere
\r
1248 /// on this collection.</param>
\r
1249 /// <returns>True if the cut function is zero somewhere
\r
1250 /// on this collection.</returns>
\r
1251 bool Cut(IComparable<T> cutFunction, out T low, out bool lowIsValid, out T high, out bool highIsValid);
\r
1255 /// Query this sorted collection for items greater than or equal to a supplied value.
\r
1256 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1257 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1258 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1260 /// <param name="bot">The lower bound (inclusive).</param>
\r
1261 /// <returns>The result directed collection.</returns>
\r
1262 IDirectedEnumerable<T> RangeFrom(T bot);
\r
1266 /// Query this sorted collection for items between two supplied values.
\r
1267 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1268 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1269 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1271 /// <param name="bot">The lower bound (inclusive).</param>
\r
1272 /// <param name="top">The upper bound (exclusive).</param>
\r
1273 /// <returns>The result directed collection.</returns>
\r
1274 IDirectedEnumerable<T> RangeFromTo(T bot, T top);
\r
1278 /// Query this sorted collection for items less than a supplied value.
\r
1279 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1280 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1281 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1283 /// <param name="top">The upper bound (exclusive).</param>
\r
1284 /// <returns>The result directed collection.</returns>
\r
1285 IDirectedEnumerable<T> RangeTo(T top);
\r
1289 /// Create a directed collection with the same items as this collection.
\r
1290 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1291 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1292 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1294 /// <returns>The result directed collection.</returns>
\r
1295 IDirectedCollectionValue<T> RangeAll();
\r
1298 //TODO: remove now that we assume that we can check the sorting order?
\r
1300 /// Add all the items from another collection with an enumeration order that
\r
1301 /// is increasing in the items.
\r
1303 /// <exception cref="ArgumentException"> if the enumerated items turns out
\r
1304 /// not to be in increasing order.</exception>
\r
1305 /// <param name="items">The collection to add.</param>
\r
1306 /// <typeparam name="U"></typeparam>
\r
1307 void AddSorted<U>(SCG.IEnumerable<U> items) where U : T;
\r
1311 /// Remove all items of this collection above or at a supplied threshold.
\r
1313 /// <param name="low">The lower threshold (inclusive).</param>
\r
1314 void RemoveRangeFrom(T low);
\r
1318 /// Remove all items of this collection between two supplied thresholds.
\r
1320 /// <param name="low">The lower threshold (inclusive).</param>
\r
1321 /// <param name="hi">The upper threshold (exclusive).</param>
\r
1322 void RemoveRangeFromTo(T low, T hi);
\r
1326 /// Remove all items of this collection below a supplied threshold.
\r
1328 /// <param name="hi">The upper threshold (exclusive).</param>
\r
1329 void RemoveRangeTo(T hi);
\r
1335 /// A collection where items are maintained in sorted order together
\r
1336 /// with their indexes in that order.
\r
1338 public interface IIndexedSorted<T> : ISorted<T>, IIndexed<T>
\r
1341 /// Determine the number of items at or above a supplied threshold.
\r
1343 /// <param name="bot">The lower bound (inclusive)</param>
\r
1344 /// <returns>The number of matcing items.</returns>
\r
1345 int CountFrom(T bot);
\r
1349 /// Determine the number of items between two supplied thresholds.
\r
1351 /// <param name="bot">The lower bound (inclusive)</param>
\r
1352 /// <param name="top">The upper bound (exclusive)</param>
\r
1353 /// <returns>The number of matcing items.</returns>
\r
1354 int CountFromTo(T bot, T top);
\r
1358 /// Determine the number of items below a supplied threshold.
\r
1360 /// <param name="top">The upper bound (exclusive)</param>
\r
1361 /// <returns>The number of matcing items.</returns>
\r
1362 int CountTo(T top);
\r
1366 /// Query this sorted collection for items greater than or equal to a supplied value.
\r
1368 /// <param name="bot">The lower bound (inclusive).</param>
\r
1369 /// <returns>The result directed collection.</returns>
\r
1370 new IDirectedCollectionValue<T> RangeFrom(T bot);
\r
1374 /// Query this sorted collection for items between two supplied values.
\r
1376 /// <param name="bot">The lower bound (inclusive).</param>
\r
1377 /// <param name="top">The upper bound (exclusive).</param>
\r
1378 /// <returns>The result directed collection.</returns>
\r
1379 new IDirectedCollectionValue<T> RangeFromTo(T bot, T top);
\r
1383 /// Query this sorted collection for items less than a supplied value.
\r
1385 /// <param name="top">The upper bound (exclusive).</param>
\r
1386 /// <returns>The result directed collection.</returns>
\r
1387 new IDirectedCollectionValue<T> RangeTo(T top);
\r
1391 /// Create a new indexed sorted collection consisting of the items of this
\r
1392 /// indexed sorted collection satisfying a certain predicate.
\r
1394 /// <param name="predicate">The filter delegate defining the predicate.</param>
\r
1395 /// <returns>The new indexed sorted collection.</returns>
\r
1396 IIndexedSorted<T> FindAll(Fun<T, bool> predicate);
\r
1400 /// Create a new indexed sorted collection consisting of the results of
\r
1401 /// mapping all items of this list.
\r
1402 /// <exception cref="ArgumentException"/> if the map is not increasing over
\r
1403 /// the items of this collection (with respect to the two given comparison
\r
1406 /// <param name="mapper">The delegate definging the map.</param>
\r
1407 /// <param name="comparer">The comparion relation to use for the result.</param>
\r
1408 /// <returns>The new sorted collection.</returns>
\r
1409 IIndexedSorted<V> Map<V>(Fun<T, V> mapper, SCG.IComparer<V> comparer);
\r
1415 /// The type of a sorted collection with persistence
\r
1417 public interface IPersistentSorted<T> : ISorted<T>, IDisposable
\r
1420 /// Make a (read-only) snap shot of this collection.
\r
1422 /// <returns>The snap shot.</returns>
\r
1423 ISorted<T> Snapshot();
\r
1428 /*************************************************************************/
\r
1430 /// A dictionary with keys of type K and values of type V. Equivalent to a
\r
1431 /// finite partial map from K to V.
\r
1433 public interface IDictionary<K, V> : ICollectionValue<KeyValuePair<K, V>>, ICloneable
\r
1436 /// The key equalityComparer.
\r
1438 /// <value></value>
\r
1439 SCG.IEqualityComparer<K> EqualityComparer { get;}
\r
1442 /// Indexer for dictionary.
\r
1444 /// <exception cref="NoSuchItemException"> if no entry is found. </exception>
\r
1445 /// <value>The value corresponding to the key</value>
\r
1446 V this[K key] { get; set;}
\r
1452 /// <value>True if dictionary is read-only</value>
\r
1453 bool IsReadOnly { get;}
\r
1459 /// <value>A collection containg the all the keys of the dictionary</value>
\r
1460 ICollectionValue<K> Keys { get;}
\r
1466 /// <value>A collection containing all the values of the dictionary</value>
\r
1467 ICollectionValue<V> Values { get;}
\r
1472 /// <value>A delegate of type <see cref="T:C5.Fun`2"/> defining the partial function from K to V give by the dictionary.</value>
\r
1473 Fun<K, V> Fun { get; }
\r
1476 //TODO: resolve inconsistency: Add thows exception if key already there, AddAll ignores keys already There?
\r
1478 /// Add a new (key, value) pair (a mapping) to the dictionary.
\r
1480 /// <exception cref="DuplicateNotAllowedException"> if there already is an entry with the same key. </exception>>
\r
1481 /// <param name="key">Key to add</param>
\r
1482 /// <param name="val">Value to add</param>
\r
1483 void Add(K key, V val);
\r
1486 /// Add the entries from a collection of <see cref="T:C5.KeyValuePair`2"/> pairs to this dictionary.
\r
1488 /// <exception cref="DuplicateNotAllowedException">
\r
1489 /// If the input contains duplicate keys or a key already present in this dictionary.</exception>
\r
1490 /// <param name="entries"></param>
\r
1491 void AddAll<U, W>(SCG.IEnumerable<KeyValuePair<U, W>> entries)
\r
1497 /// The value is symbolic indicating the type of asymptotic complexity
\r
1498 /// in terms of the size of this collection (worst-case or amortized as
\r
1500 /// <para>See <see cref="T:C5.Speed"/> for the set of symbols.</para>
\r
1502 /// <value>A characterization of the speed of lookup operations
\r
1503 /// (<code>Contains()</code> etc.) of the implementation of this dictionary.</value>
\r
1504 Speed ContainsSpeed { get;}
\r
1507 /// Check whether this collection contains all the values in another collection.
\r
1508 /// If this collection has bag semantics (<code>AllowsDuplicates==true</code>)
\r
1509 /// the check is made with respect to multiplicities, else multiplicities
\r
1510 /// are not taken into account.
\r
1512 /// <param name="items">The </param>
\r
1513 /// <returns>True if all values in <code>items</code>is in this collection.</returns>
\r
1514 bool ContainsAll<H>(SCG.IEnumerable<H> items) where H : K;
\r
1517 /// Remove an entry with a given key from the dictionary
\r
1519 /// <param name="key">The key of the entry to remove</param>
\r
1520 /// <returns>True if an entry was found (and removed)</returns>
\r
1521 bool Remove(K key);
\r
1525 /// Remove an entry with a given key from the dictionary and report its value.
\r
1527 /// <param name="key">The key of the entry to remove</param>
\r
1528 /// <param name="val">On exit, the value of the removed entry</param>
\r
1529 /// <returns>True if an entry was found (and removed)</returns>
\r
1530 bool Remove(K key, out V val);
\r
1534 /// Remove all entries from the dictionary
\r
1540 /// Check if there is an entry with a specified key
\r
1542 /// <param name="key">The key to look for</param>
\r
1543 /// <returns>True if key was found</returns>
\r
1544 bool Contains(K key);
\r
1548 /// Check if there is an entry with a specified key and report the corresponding
\r
1549 /// value if found. This can be seen as a safe form of "val = this[key]".
\r
1551 /// <param name="key">The key to look for</param>
\r
1552 /// <param name="val">On exit, the value of the entry</param>
\r
1553 /// <returns>True if key was found</returns>
\r
1554 bool Find(K key, out V val);
\r
1557 /// Check if there is an entry with a specified key and report the corresponding
\r
1558 /// value if found. This can be seen as a safe form of "val = this[key]".
\r
1560 /// <param name="key">The key to look for</param>
\r
1561 /// <param name="val">On exit, the value of the entry</param>
\r
1562 /// <returns>True if key was found</returns>
\r
1563 bool Find(ref K key, out V val);
\r
1567 /// Look for a specific key in the dictionary and if found replace the value with a new one.
\r
1568 /// This can be seen as a non-adding version of "this[key] = val".
\r
1570 /// <param name="key">The key to look for</param>
\r
1571 /// <param name="val">The new value</param>
\r
1572 /// <returns>True if key was found</returns>
\r
1573 bool Update(K key, V val); //no-adding
\r
1577 /// Look for a specific key in the dictionary and if found replace the value with a new one.
\r
1578 /// This can be seen as a non-adding version of "this[key] = val" reporting the old value.
\r
1580 /// <param name="key">The key to look for</param>
\r
1581 /// <param name="val">The new value</param>
\r
1582 /// <param name="oldval">The old value if any</param>
\r
1583 /// <returns>True if key was found</returns>
\r
1584 bool Update(K key, V val, out V oldval); //no-adding
\r
1587 /// Look for a specific key in the dictionary. If found, report the corresponding value,
\r
1588 /// else add an entry with the key and the supplied value.
\r
1590 /// <param name="key">The key to look for</param>
\r
1591 /// <param name="val">On entry the value to add if the key is not found.
\r
1592 /// On exit the value found if any.</param>
\r
1593 /// <returns>True if key was found</returns>
\r
1594 bool FindOrAdd(K key, ref V val); //mixture
\r
1598 /// Update value in dictionary corresponding to key if found, else add new entry.
\r
1599 /// More general than "this[key] = val;" by reporting if key was found.
\r
1601 /// <param name="key">The key to look for</param>
\r
1602 /// <param name="val">The value to add or replace with.</param>
\r
1603 /// <returns>True if key was found and value updated.</returns>
\r
1604 bool UpdateOrAdd(K key, V val);
\r
1608 /// Update value in dictionary corresponding to key if found, else add new entry.
\r
1609 /// More general than "this[key] = val;" by reporting if key was found.
\r
1611 /// <param name="key">The key to look for</param>
\r
1612 /// <param name="val">The value to add or replace with.</param>
\r
1613 /// <param name="oldval">The old value if any</param>
\r
1614 /// <returns>True if key was found and value updated.</returns>
\r
1615 bool UpdateOrAdd(K key, V val, out V oldval);
\r
1619 /// Check the integrity of the internal data structures of this dictionary.
\r
1620 /// Only avaliable in DEBUG builds???
\r
1622 /// <returns>True if check does not fail.</returns>
\r
1629 /// A dictionary with sorted keys.
\r
1631 public interface ISortedDictionary<K, V> : IDictionary<K, V>
\r
1636 /// <value></value>
\r
1637 new ISorted<K> Keys { get;}
\r
1640 /// Find the current least item of this sorted collection.
\r
1642 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1643 /// <returns>The least item.</returns>
\r
1644 KeyValuePair<K, V> FindMin();
\r
1648 /// Remove the least item from this sorted collection.
\r
1650 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1651 /// <returns>The removed item.</returns>
\r
1652 KeyValuePair<K, V> DeleteMin();
\r
1656 /// Find the current largest item of this sorted collection.
\r
1658 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1659 /// <returns>The largest item.</returns>
\r
1660 KeyValuePair<K, V> FindMax();
\r
1664 /// Remove the largest item from this sorted collection.
\r
1666 /// <exception cref="NoSuchItemException"> if the collection is empty.</exception>
\r
1667 /// <returns>The removed item.</returns>
\r
1668 KeyValuePair<K, V> DeleteMax();
\r
1671 /// The key comparer used by this dictionary.
\r
1673 /// <value></value>
\r
1674 SCG.IComparer<K> Comparer { get;}
\r
1677 /// Find the entry with the largest key less than a given key.
\r
1679 /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
\r
1680 /// <param name="key">The key to compare to</param>
\r
1681 /// <returns>The entry</returns>
\r
1682 KeyValuePair<K, V> Predecessor(K key);
\r
1686 /// Find the entry with the least key greater than a given key.
\r
1688 /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
\r
1689 /// <param name="key">The key to compare to</param>
\r
1690 /// <returns>The entry</returns>
\r
1691 KeyValuePair<K, V> Successor(K key);
\r
1695 /// Find the entry with the largest key less than or equal to a given key.
\r
1697 /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
\r
1698 /// <param name="key">The key to compare to</param>
\r
1699 /// <returns>The entry</returns>
\r
1700 KeyValuePair<K, V> WeakPredecessor(K key);
\r
1704 /// Find the entry with the least key greater than or equal to a given key.
\r
1706 /// <exception cref="NoSuchItemException"> if there is no such entry. </exception>
\r
1707 /// <param name="key">The key to compare to</param>
\r
1708 /// <returns>The entry</returns>
\r
1709 KeyValuePair<K, V> WeakSuccessor(K key);
\r
1712 /// Given a "cut" function from the items of the sorted collection to <code>int</code>
\r
1713 /// whose only sign changes when going through items in increasing order
\r
1716 /// <item>from positive to zero</item>
\r
1717 /// <item>from positive to negative</item>
\r
1718 /// <item>from zero to negative</item>
\r
1720 /// The "cut" function is supplied as the <code>CompareTo</code> method
\r
1721 /// of an object <code>c</code> implementing
\r
1722 /// <code>IComparable<K></code>.
\r
1723 /// A typical example is the case where <code>K</code> is comparable and
\r
1724 /// <code>c</code> is itself of type <code>K</code>.
\r
1725 /// <para>This method performs a search in the sorted collection for the ranges in which the
\r
1726 /// "cut" function is negative, zero respectively positive. If <code>K</code> is comparable
\r
1727 /// and <code>c</code> is of type <code>K</code>, this is a safe way (no exceptions thrown)
\r
1728 /// to find predecessor and successor of <code>c</code>.
\r
1730 /// <para> If the supplied cut function does not satisfy the sign-change condition,
\r
1731 /// the result of this call is undefined.
\r
1735 /// <param name="cutFunction">The cut function <code>K</code> to <code>int</code>, given
\r
1736 /// by the <code>CompareTo</code> method of an object implementing
\r
1737 /// <code>IComparable<K></code>.</param>
\r
1738 /// <param name="lowEntry">Returns the largest item in the collection, where the
\r
1739 /// cut function is positive (if any).</param>
\r
1740 /// <param name="lowIsValid">Returns true if the cut function is positive somewhere
\r
1741 /// on this collection.</param>
\r
1742 /// <param name="highEntry">Returns the least item in the collection, where the
\r
1743 /// cut function is negative (if any).</param>
\r
1744 /// <param name="highIsValid">Returns true if the cut function is negative somewhere
\r
1745 /// on this collection.</param>
\r
1746 /// <returns>True if the cut function is zero somewhere
\r
1747 /// on this collection.</returns>
\r
1748 bool Cut(IComparable<K> cutFunction, out KeyValuePair<K, V> lowEntry, out bool lowIsValid, out KeyValuePair<K, V> highEntry, out bool highIsValid);
\r
1751 /// Query this sorted collection for items greater than or equal to a supplied value.
\r
1752 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1753 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1754 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1756 /// <param name="bot">The lower bound (inclusive).</param>
\r
1757 /// <returns>The result directed collection.</returns>
\r
1758 IDirectedEnumerable<KeyValuePair<K, V>> RangeFrom(K bot);
\r
1762 /// Query this sorted collection for items between two supplied values.
\r
1763 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1764 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1765 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1767 /// <param name="lowerBound">The lower bound (inclusive).</param>
\r
1768 /// <param name="upperBound">The upper bound (exclusive).</param>
\r
1769 /// <returns>The result directed collection.</returns>
\r
1770 IDirectedEnumerable<KeyValuePair<K, V>> RangeFromTo(K lowerBound, K upperBound);
\r
1774 /// Query this sorted collection for items less than a supplied value.
\r
1775 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1776 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1777 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1779 /// <param name="top">The upper bound (exclusive).</param>
\r
1780 /// <returns>The result directed collection.</returns>
\r
1781 IDirectedEnumerable<KeyValuePair<K, V>> RangeTo(K top);
\r
1785 /// Create a directed collection with the same items as this collection.
\r
1786 /// <para>The returned collection is not a copy but a view into the collection.</para>
\r
1787 /// <para>The view is fragile in the sense that changes to the underlying collection will
\r
1788 /// invalidate the view so that further operations on the view throws InvalidView exceptions.</para>
\r
1790 /// <returns>The result directed collection.</returns>
\r
1791 IDirectedCollectionValue<KeyValuePair<K, V>> RangeAll();
\r
1794 //TODO: remove now that we assume that we can check the sorting order?
\r
1796 /// Add all the items from another collection with an enumeration order that
\r
1797 /// is increasing in the items.
\r
1799 /// <exception cref="ArgumentException"> if the enumerated items turns out
\r
1800 /// not to be in increasing order.</exception>
\r
1801 /// <param name="items">The collection to add.</param>
\r
1802 void AddSorted(SCG.IEnumerable<KeyValuePair<K, V>> items);
\r
1806 /// Remove all items of this collection above or at a supplied threshold.
\r
1808 /// <param name="low">The lower threshold (inclusive).</param>
\r
1809 void RemoveRangeFrom(K low);
\r
1813 /// Remove all items of this collection between two supplied thresholds.
\r
1815 /// <param name="low">The lower threshold (inclusive).</param>
\r
1816 /// <param name="hi">The upper threshold (exclusive).</param>
\r
1817 void RemoveRangeFromTo(K low, K hi);
\r
1821 /// Remove all items of this collection below a supplied threshold.
\r
1823 /// <param name="hi">The upper threshold (exclusive).</param>
\r
1824 void RemoveRangeTo(K hi);
\r
1829 /*******************************************************************/
\r
1831 /// The type of an item comparer
\r
1832 /// <i>Implementations of this interface must asure that the method is self-consistent
\r
1833 /// and defines a sorting order on items, or state precise conditions under which this is true.</i>
\r
1834 /// <i>Implementations <b>must</b> assure that repeated calls of
\r
1835 /// the method to the same (in reference or binary identity sense) arguments
\r
1836 /// will return values with the same sign (-1, 0 or +1), or state precise conditions
\r
1837 /// under which the user
\r
1838 /// can be assured repeated calls will return the same sign.</i>
\r
1839 /// <i>Implementations of this interface must always return values from the method
\r
1840 /// and never throw exceptions.</i>
\r
1841 /// <i>This interface is identical to System.Collections.Generic.IComparer<T></i>
\r
1843 public interface IComparer<T>
\r
1846 /// Compare two items with respect to this item comparer
\r
1848 /// <param name="item1">First item</param>
\r
1849 /// <param name="item2">Second item</param>
\r
1850 /// <returns>Positive if item1 is greater than item2, 0 if they are equal, negative if item1 is less than item2</returns>
\r
1851 int Compare(T item1, T item2);
\r
1855 /// The type of an item equalityComparer.
\r
1856 /// <i>Implementations of this interface <b>must</b> assure that the methods are
\r
1857 /// consistent, that is, that whenever two items i1 and i2 satisfies that Equals(i1,i2)
\r
1858 /// returns true, then GetHashCode returns the same value for i1 and i2.</i>
\r
1859 /// <i>Implementations of this interface <b>must</b> assure that repeated calls of
\r
1860 /// the methods to the same (in reference or binary identity sense) arguments
\r
1861 /// will return the same values, or state precise conditions under which the user
\r
1862 /// can be assured repeated calls will return the same values.</i>
\r
1863 /// <i>Implementations of this interface must always return values from the methods
\r
1864 /// and never throw exceptions.</i>
\r
1865 /// <i>This interface is similar in function to System.IKeyComparer<T></i>
\r
1867 public interface SCG.IEqualityComparer<T>
\r
1870 /// Get the hash code with respect to this item equalityComparer
\r
1872 /// <param name="item">The item</param>
\r
1873 /// <returns>The hash code</returns>
\r
1874 int GetHashCode(T item);
\r
1878 /// Check if two items are equal with respect to this item equalityComparer
\r
1880 /// <param name="item1">first item</param>
\r
1881 /// <param name="item2">second item</param>
\r
1882 /// <returns>True if equal</returns>
\r
1883 bool Equals(T item1, T item2);
\r