#if NET_2_0 /* Copyright (c) 2003-2004 Niels Kokholm and Peter Sestoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Diagnostics; using MSG = System.Collections.Generic; using SC = System.Collections; namespace C5 { /// /// A read-only wrapper class for a generic enumerator /// public class GuardedEnumerator: MSG.IEnumerator { #region Fields MSG.IEnumerator enumerator; #endregion #region Constructor /// /// Create a wrapper around a generic enumerator /// /// The enumerator to wrap public GuardedEnumerator(MSG.IEnumerator enumerator) { this.enumerator = enumerator; } #endregion #region IEnumerator Members /// /// Move wrapped enumerator to next item, or the first item if /// this is the first call to MoveNext. /// /// True if enumerator is valid now public bool MoveNext() { return enumerator.MoveNext(); } /// /// Undefined if enumerator is not valid (MoveNext hash been called returning true) /// /// The current item of the wrapped enumerator. public T Current { get { return enumerator.Current; } } void SC.IEnumerator.Reset () { enumerator.Reset (); } object SC.IEnumerator.Current { get { return enumerator.Current; } } #endregion #region IDisposable Members /// /// Dispose wrapped enumerator /// public void Dispose() { enumerator.Dispose(); } #endregion } /// /// A read-only wrapper class for a generic enumerable /// ///

This is mainly interesting as a base of other guard classes

///
public class GuardedEnumerable: MSG.IEnumerable { #region Fields MSG.IEnumerable enumerable; #endregion #region Constructor /// /// Wrap an enumerable in a read-only wrapper /// /// The enumerable to wrap public GuardedEnumerable(MSG.IEnumerable enumerable) { this.enumerable = enumerable; } #endregion #region MSG.IEnumerable Members /// /// Get an enumerator from the wrapped enumerable /// /// The enumerator (itself wrapped) public MSG.IEnumerator GetEnumerator() { return new GuardedEnumerator(enumerable.GetEnumerator()); } #endregion SC.IEnumerator SC.IEnumerable.GetEnumerator() { return GetEnumerator (); } } /// /// A read-only wrapper for a generic directed enumerable /// ///

This is mainly interesting as a base of other guard classes

///
public class GuardedDirectedEnumerable: GuardedEnumerable, IDirectedEnumerable { #region Fields IDirectedEnumerable directedenumerable; #endregion #region Constructor /// /// Wrap a directed enumerable in a read-only wrapper /// /// the collection to wrap public GuardedDirectedEnumerable(IDirectedEnumerable directedenumerable) : base(directedenumerable) { this.directedenumerable = directedenumerable; } #endregion #region IDirectedEnumerable Members /// /// Get a enumerable that enumerates the wrapped collection in the opposite direction /// /// The mirrored enumerable public IDirectedEnumerable Backwards() { return new GuardedDirectedEnumerable(directedenumerable.Backwards()); } /// /// Forwards if same, else Backwards /// /// The enumeration direction relative to the original collection. public EnumerationDirection Direction { get { return directedenumerable.Direction; } } #endregion } /// /// A read-only wrapper for an ICollectionValue<T> /// ///

This is mainly interesting as a base of other guard classes

///
public class GuardedCollectionValue: GuardedEnumerable, ICollectionValue { #region Fields ICollectionValue collection; #endregion #region Constructor /// /// Wrap a ICollectionValue<T> in a read-only wrapper /// /// the collection to wrap public GuardedCollectionValue(ICollectionValue collection) : base(collection) { this.collection = collection; } #endregion #region ICollection Members /// /// Get the size of the wrapped collection /// /// The size public int Count { get { return collection.Count; } } /// /// The value is symbolic indicating the type of asymptotic complexity /// in terms of the size of this collection (worst-case or amortized as /// relevant). /// /// A characterization of the speed of the /// Count property in this collection. public Speed CountSpeed { get { return collection.CountSpeed; } } /// /// Copy the items of the wrapped collection to an array /// /// The array /// Starting offset public void CopyTo(T[] a, int i) { collection.CopyTo(a, i); } /// /// Create an array from the items of the wrapped collection /// /// The array public T[] ToArray() { return collection.ToArray(); } /// /// Apply a delegate to all items of the wrapped enumerable. /// /// The delegate to apply //TODO: change this to throw an exception? public void Apply(Applier a) { collection.Apply(a); } /// /// Check if there exists an item that satisfies a /// specific predicate in the wrapped enumerable. /// /// A filter delegate /// () defining the predicate /// True is such an item exists public bool Exists(Filter filter) { return collection.Exists(filter); } /// /// Check if all items in the wrapped enumerable satisfies a specific predicate. /// /// A filter delegate /// () defining the predicate /// True if all items satisfies the predicate public bool All(Filter filter) { return collection.All(filter); } #endregion } /// /// A read-only wrapper for a directed collection /// ///

This is mainly interesting as a base of other guard classes

///
public class GuardedDirectedCollectionValue: GuardedCollectionValue, IDirectedCollectionValue { #region Fields IDirectedCollectionValue directedcollection; #endregion #region Constructor /// /// Wrap a directed collection in a read-only wrapper /// /// the collection to wrap public GuardedDirectedCollectionValue(IDirectedCollectionValue directedcollection) : base(directedcollection) { this.directedcollection = directedcollection; } #endregion #region IDirectedCollection Members /// /// Get a collection that enumerates the wrapped collection in the opposite direction /// /// The mirrored collection public IDirectedCollectionValue Backwards() { return new GuardedDirectedCollectionValue(directedcollection.Backwards()); } #endregion #region IDirectedEnumerable Members IDirectedEnumerable IDirectedEnumerable.Backwards() { return Backwards(); } /// /// Forwards if same, else Backwards /// /// The enumeration direction relative to the original collection. public EnumerationDirection Direction { get { return directedcollection.Direction; } } #endregion } /// /// A read-only wrapper for an ICollection<T>. /// /// ///

Suitable for wrapping hash tables, /// and

///
public class GuardedCollection: GuardedCollectionValue, ICollection { #region Fields ICollection collection; #endregion #region Constructor /// /// Wrap an ICollection<T> in a read-only wrapper /// /// the collection to wrap public GuardedCollection(ICollection collection) :base(collection) { this.collection = collection; } #endregion #region ICollection Members /// /// (This is a read-only wrapper) /// /// True public bool IsReadOnly { get { return true; } } /// /// Speed of wrapped collection public Speed ContainsSpeed { get { return collection.ContainsSpeed; } } int ICollection.GetHashCode() { return ((ICollection)collection).GetHashCode(); } bool ICollection.Equals(ICollection that) { return ((ICollection)collection).Equals(that); } /// /// Check if an item is in the wrapped collection /// /// The item /// True if found public bool Contains(T item) { return collection.Contains(item); } /// /// Count the number of times an item appears in the wrapped collection /// /// The item /// The number of copies public int ContainsCount(T item) { return collection.ContainsCount(item); } /// /// Check if all items in the argument is in the wrapped collection /// /// The items /// True if so public bool ContainsAll(MSG.IEnumerable items) { return collection.ContainsAll(items); } /// /// Search for an item in the wrapped collection /// /// On entry the item to look for, on exit the equivalent item found (if any) /// public bool Find(ref T item) { return collection.Find(ref item); } /// /// since this is a read-only wrappper /// /// /// public bool FindOrAdd(ref T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// /// public bool Update(T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// /// public bool UpdateOrAdd(T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// /// public bool Remove(T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// /// public bool RemoveWithReturn(ref T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// public void RemoveAllCopies(T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// public void RemoveAll(MSG.IEnumerable items) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// public void Clear() { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// public void RetainAll(MSG.IEnumerable items) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// Check wrapped collection for internal consistency /// /// True if check passed public bool Check() { return collection.Check(); } #endregion #region ISink Members /// /// False if wrapped collection has set semantics public bool AllowsDuplicates { get { return collection.AllowsDuplicates; } } /// /// The sync root of the wrapped collection public object SyncRoot { get { return collection.SyncRoot; } } /// /// True if wrapped collection is empty public bool IsEmpty { get { return collection.IsEmpty; } } /// /// since this is a read-only wrappper /// /// /// public bool Add(T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// public void AddAll(MSG.IEnumerable items) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// /*public*/ void C5.IExtensible.AddAll(MSG.IEnumerable items) //where U : T { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } #endregion } /// /// A read-only wrapper for a sequenced collection /// ///

This is mainly interesting as a base of other guard classes

///
public class GuardedSequenced: GuardedCollection, ISequenced { #region Fields ISequenced sequenced; #endregion #region Constructor /// /// Wrap a sequenced collection in a read-only wrapper /// /// public GuardedSequenced(ISequenced sorted):base(sorted) { this.sequenced = sorted; } #endregion #region ISequenced Members int ISequenced.GetHashCode() { return ((ISequenced)sequenced).GetHashCode(); } bool ISequenced.Equals(ISequenced that) { return ((ISequenced)sequenced).Equals(that); } #endregion #region IEditableCollection Members int ICollection.GetHashCode() { return ((ICollection)sequenced).GetHashCode(); } bool ICollection.Equals(ICollection that) { return ((ICollection)sequenced).Equals(that); } #endregion #region IDirectedCollection Members /// /// Get a collection that enumerates the wrapped collection in the opposite direction /// /// The mirrored collection public IDirectedCollectionValue Backwards() { return new GuardedDirectedCollectionValue(sequenced.Backwards()); } #endregion #region IDirectedEnumerable Members IDirectedEnumerable IDirectedEnumerable.Backwards() { return Backwards(); } /// /// Forwards if same, else Backwards /// /// The enumeration direction relative to the original collection. public EnumerationDirection Direction { get { return EnumerationDirection.Forwards; } } #endregion } /// /// A read-only wrapper for a sorted collection /// ///

This is mainly interesting as a base of other guard classes

///
public class GuardedSorted: GuardedSequenced, ISorted { #region Fields ISorted sorted; #endregion #region Constructor /// /// Wrap a sorted collection in a read-only wrapper /// /// public GuardedSorted(ISorted sorted):base(sorted) { this.sorted = sorted; } #endregion #region IEditableCollection Members int ICollection.GetHashCode() { return ((ICollection)sorted).GetHashCode(); } bool ICollection.Equals(ICollection that) { return ((ICollection)sorted).Equals(that); } #endregion #region ISequenced Members int ISequenced.GetHashCode() { return ((ISequenced)sorted).GetHashCode(); } bool ISequenced.Equals(ISequenced that) { return ((ISequenced)sorted).Equals(that); } #endregion #region ISorted Members /// /// Find the predecessor of the item in the wrapped sorted collection /// /// The item /// The predecessor public T Predecessor(T item) { return sorted.Predecessor(item); } /// /// Find the Successor of the item in the wrapped sorted collection /// /// The item /// The Successor public T Successor(T item) { return sorted.Successor(item); } /// /// Find the weak predecessor of the item in the wrapped sorted collection /// /// The item /// The weak predecessor public T WeakPredecessor(T item) { return sorted.WeakPredecessor(item); } /// /// Find the weak Successor of the item in the wrapped sorted collection /// /// The item /// The weak Successor public T WeakSuccessor(T item) { return sorted.WeakSuccessor(item); } /// /// Run Cut on the wrapped sorted collection /// /// /// /// /// /// /// public bool Cut(IComparable c, out T low, out bool lval, out T high, out bool hval) { return sorted.Cut(c, out low, out lval, out high, out hval); } /// /// Get the specified range from the wrapped collection. /// (The current implementation erroneously does not wrap the result.) /// /// /// public IDirectedEnumerable RangeFrom(T bot) { return sorted.RangeFrom(bot); } /// /// Get the specified range from the wrapped collection. /// (The current implementation erroneously does not wrap the result.) /// /// /// /// public IDirectedEnumerable RangeFromTo(T bot, T top) { return sorted.RangeFromTo(bot, top); } /// /// Get the specified range from the wrapped collection. /// (The current implementation erroneously does not wrap the result.) /// /// /// public IDirectedEnumerable RangeTo(T top) { return sorted.RangeTo(top); } /// /// Get the specified range from the wrapped collection. /// (The current implementation erroneously does not wrap the result.) /// /// public IDirectedCollectionValue RangeAll() { return sorted.RangeAll(); } /// /// since this is a read-only wrappper /// /// public void AddSorted(MSG.IEnumerable items) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// public void RemoveRangeFrom(T low) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// /// public void RemoveRangeFromTo(T low, T hi) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// public void RemoveRangeTo(T hi) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } #endregion #region IPriorityQueue Members /// /// Find the minimum of the wrapped collection /// /// The minimum public T FindMin() { return sorted.FindMin(); } /// /// since this is a read-only wrappper /// /// public T DeleteMin() { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// Find the maximum of the wrapped collection /// /// The maximum public T FindMax() { return sorted.FindMax(); } /// /// since this is a read-only wrappper /// /// public T DeleteMax() { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// The comparer object supplied at creation time for the underlying collection /// /// The comparer public IComparer Comparer { get { return sorted.Comparer; } } #endregion #region IDirectedEnumerable Members IDirectedEnumerable IDirectedEnumerable.Backwards() { return Backwards(); } #endregion } /// /// Read-only wrapper for indexed sorted collections /// ///

Suitable for wrapping TreeSet, TreeBag and SortedArray

///
public class GuardedIndexedSorted: GuardedSorted, IIndexedSorted { #region Fields IIndexedSorted indexedsorted; #endregion #region Constructor /// /// Wrap an indexed sorted collection in a read-only wrapper /// /// the indexed sorted collection public GuardedIndexedSorted(IIndexedSorted list):base(list) { this.indexedsorted = list; } #endregion #region IIndexedSorted Members /// /// Get the specified range from the wrapped collection. /// (The current implementation erroneously does not wrap the result.) /// /// /// public new IDirectedCollectionValue RangeFrom(T bot) { return indexedsorted.RangeFrom(bot); } /// /// Get the specified range from the wrapped collection. /// (The current implementation erroneously does not wrap the result.) /// /// /// /// public new IDirectedCollectionValue RangeFromTo(T bot, T top) { return indexedsorted.RangeFromTo(bot, top); } /// /// Get the specified range from the wrapped collection. /// (The current implementation erroneously does not wrap the result.) /// /// /// public new IDirectedCollectionValue RangeTo(T top) { return indexedsorted.RangeTo(top); } /// /// Report the number of items in the specified range of the wrapped collection /// /// /// public int CountFrom(T bot) { return indexedsorted.CountFrom(bot); } /// /// Report the number of items in the specified range of the wrapped collection /// /// /// /// public int CountFromTo(T bot, T top) { return indexedsorted.CountFromTo(bot, top); } /// /// Report the number of items in the specified range of the wrapped collection /// /// /// public int CountTo(T top) { return indexedsorted.CountTo(top); } /// /// Run FindAll on the wrapped collection with the indicated filter. /// The result will not be read-only. /// /// /// public IIndexedSorted FindAll(Filter f) { return indexedsorted.FindAll(f); } /// /// Run Map on the wrapped collection with the indicated mapper. /// The result will not be read-only. /// /// /// The comparer to use in the result /// public IIndexedSorted Map(Mapper m, IComparer c) { return indexedsorted.Map(m, c); } #endregion #region IIndexed Members /// /// /// /// The i'th item of the wrapped sorted collection public T this[int i] { get { return indexedsorted[i]; } } /// /// A directed collection of the items in the indicated interval of the wrapped collection public IDirectedCollectionValue this[int start, int end] { get { return new GuardedDirectedCollectionValue(indexedsorted[start, end]); } } /// /// Find the (first) index of an item in the wrapped collection /// /// /// public int IndexOf(T item) { return indexedsorted.IndexOf(item); } /// /// Find the last index of an item in the wrapped collection /// /// /// public int LastIndexOf(T item) { return indexedsorted.LastIndexOf(item); } /// /// since this is a read-only wrappper /// /// /// public T RemoveAt(int i) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// since this is a read-only wrappper /// /// /// public void RemoveInterval(int start, int count) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } #endregion #region ISequenced Members int ISequenced.GetHashCode() { return ((ISequenced)indexedsorted).GetHashCode(); } bool ISequenced.Equals(ISequenced that) { return ((ISequenced)indexedsorted).Equals(that); } #endregion #region IEditableCollection Members int ICollection.GetHashCode() { return ((ICollection)indexedsorted).GetHashCode(); } bool ICollection.Equals(ICollection that) { return ((ICollection)indexedsorted).Equals(that); } #endregion #region IDirectedEnumerable Members IDirectedEnumerable IDirectedEnumerable.Backwards() { return Backwards(); } #endregion } /// /// A read-only wrapper for a generic list collection ///

Suitable as a wrapper for LinkedList, HashedLinkedList, ArrayList and HashedArray. /// , /// , /// or /// . ///

///
public class GuardedList: GuardedSequenced, IList { #region Fields IList list; #endregion #region Constructor /// /// Wrap a list in a read-only wrapper /// /// The list public GuardedList(IList list) : base (list) { this.list = list; } #endregion #region IList Members /// /// /// /// The first item of the wrapped list public T First { get { return list.First; } } /// /// /// /// The last item of the wrapped list public T Last { get { return list.Last; } } /// /// if used as setter /// /// True if wrapped list has FIFO semantics for the Add(T item) and Remove() methods public bool FIFO { get { return list.FIFO; } set { throw new InvalidOperationException("List is read only"); } } /// /// if used as setter /// /// The i'th item of the wrapped list public T this[int i] { get { return list[i]; } set { throw new InvalidOperationException("List is read only"); } } /// /// since this is a read-only wrappper /// /// /// public void Insert(int i, T item) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// public void InsertFirst(T item) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// public void InsertLast(T item) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// /// public void InsertBefore(T item, T target) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// /// public void InsertAfter(T item, T target) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// /// public void InsertAll(int i, MSG.IEnumerable items) { throw new InvalidOperationException("List is read only"); } /// /// Perform FindAll on the wrapped list. The result is not necessarily read-only. /// /// The filter to use /// public IList FindAll(Filter filter) { return list.FindAll(filter); } /// /// Perform Map on the wrapped list. The result is not necessarily read-only. /// /// The type of items of the new list /// The mapper to use. /// The mapped list public IList Map(Mapper mapper) { return list.Map(mapper); } /// /// Perform Map on the wrapped list. The result is not necessarily read-only. /// /// The type of items of the new list /// The delegate defining the map. /// The hasher to use for the new list /// The new list. public IList Map(Mapper mapper, IHasher hasher) { return list.Map(mapper, hasher); } /// /// since this is a read-only wrappper /// /// public T Remove() { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// public T RemoveFirst() { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// public T RemoveLast() { throw new InvalidOperationException("List is read only"); } /// /// Create the indicated view on the wrapped list and wrap it read-only. /// /// /// /// public IList View(int start, int count) { return new GuardedList(list.View(start, count)); } //TODO: This is wrong! /// /// (This is wrong functionality) /// /// The wrapped underlying list of the wrapped view public IList Underlying { get { return new GuardedList(list.Underlying); } } /// /// /// /// The offset of the wrapped list as a view. public int Offset { get { return list.Offset; } } /// /// since this is a read-only wrappper /// /// public void Slide(int offset) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// /// public void Slide(int offset, int size) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// public void Reverse() { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// /// public void Reverse(int start, int count) { throw new InvalidOperationException("List is read only"); } /// /// Check if wrapped list is sorted /// /// The sorting order to use /// True if sorted public bool IsSorted(IComparer c) { return list.IsSorted(c); } /// /// since this is a read-only wrappper /// /// public void Sort(IComparer c) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// public void Shuffle() { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// public void Shuffle(Random rnd) { throw new InvalidOperationException("List is read only"); } #endregion #region IIndexed Members /// /// A directed collection of the items in the indicated interval of the wrapped collection public IDirectedCollectionValue this[int start, int end] { get { return new GuardedDirectedCollectionValue(list[start, end]); } } /// /// Find the (first) index of an item in the wrapped collection /// /// /// public int IndexOf(T item) { return list.IndexOf(item); } /// /// Find the last index of an item in the wrapped collection /// /// /// public int LastIndexOf(T item) { return list.LastIndexOf(item); } /// /// since this is a read-only wrappper /// /// /// public T RemoveAt(int i) { throw new InvalidOperationException("List is read only"); } /// /// since this is a read-only wrappper /// /// /// public void RemoveInterval(int start, int count) { throw new InvalidOperationException("List is read only"); } #endregion #region ISequenced Members int ISequenced.GetHashCode() { return ((ISequenced)list).GetHashCode(); } bool ISequenced.Equals(ISequenced that) { return ((ISequenced)list).Equals(that); } #endregion #region IEditableCollection Members int ICollection.GetHashCode() { return ((ICollection)list).GetHashCode(); } bool ICollection.Equals(ICollection that) { return ((ICollection)list).Equals(that); } #endregion #region IDirectedEnumerable Members IDirectedEnumerable IDirectedEnumerable.Backwards() { return Backwards(); } #endregion #region IStack Members /// /// /// /// since this is a read-only wrappper /// - public void Push(T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// /// /// since this is a read-only wrappper /// - public T Pop() { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } #endregion #region IQueue Members /// /// /// /// since this is a read-only wrappper /// - public void EnQueue(T item) { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } /// /// /// /// since this is a read-only wrappper /// - public T DeQueue() { throw new InvalidOperationException("Collection cannot be modified through this guard object"); } #endregion } /// /// A read-only wrapper for a dictionary. /// ///

Suitable for wrapping a HashDictionary.

///
public class GuardedDictionary: GuardedEnumerable>, IDictionary { #region Fields IDictionary dict; #endregion #region Constructor /// /// Wrap a dictionary in a read-only wrapper /// /// the dictionary public GuardedDictionary(IDictionary dict) : base(dict) { this.dict = dict; } #endregion #region IDictionary Members /// /// since this is a /// read-only wrappper if used as a setter /// /// Get the value corresponding to a key in the wrapped dictionary public V this[K key] { get { return dict[key]; } set { throw new InvalidOperationException("Dictionary is read only"); } } /// /// The size of the wrapped dictionary public int Count { get { return dict.Count; } } /// /// (This is a read-only wrapper) /// /// True public bool IsReadOnly { get { return true; } } /// /// The sync root of the wrapped dictionary public object SyncRoot { get { return dict.SyncRoot; } } //TODO: guard with a read-only wrapper? Probably so! /// /// The collection of keys of the wrapped dictionary public ICollectionValue Keys { get { return dict.Keys; } } /// /// The collection of values of the wrapped dictionary public ICollectionValue Values { get { return dict.Values; } } /// /// since this is a read-only wrappper /// /// /// public void Add(K key, V val) { throw new InvalidOperationException("Dictionary is read only"); } /// /// since this is a read-only wrappper /// /// /// public bool Remove(K key) { throw new InvalidOperationException("Dictionary is read only"); } /// /// since this is a read-only wrappper /// /// /// /// public bool Remove(K key, out V val) { throw new InvalidOperationException("Dictionary is read only"); } /// /// since this is a read-only wrappper /// public void Clear() { throw new InvalidOperationException("Dictionary is read only"); } /// /// Check if the wrapped dictionary contains a specific key /// /// The key /// True if it does public bool Contains(K key) { return dict.Contains(key); } /// /// Search for a key in the wrapped dictionary, reporting the value if found /// /// The key /// On exit: the value if found /// True if found public bool Find(K key, out V val) { return dict.Find(key, out val); } /// /// since this is a read-only wrappper /// /// /// /// public bool Update(K key, V val) { throw new InvalidOperationException("Dictionary is read only"); } /// /// since this is a read-only wrappper /// /// /// /// public bool FindOrAdd(K key, ref V val) { throw new InvalidOperationException("Dictionary is read only"); } /// /// since this is a read-only wrappper /// /// /// /// public bool UpdateOrAdd(K key, V val) { throw new InvalidOperationException("Dictionary is read only"); } /// /// Check the internal consistency of the wrapped dictionary /// /// True if check passed public bool Check() { return dict.Check(); } #endregion } /// /// A read-only wrapper for a sorted dictionary. /// ///

Suitable for wrapping a Dictionary.

///
public class GuardedSortedDictionary: GuardedDictionary, ISortedDictionary { #region Fields ISortedDictionary sorteddict; #endregion #region Constructor /// /// Wrap a sorted dictionary in a read-only wrapper /// /// the dictionary public GuardedSortedDictionary(ISortedDictionary sorteddict) :base(sorteddict) { this.sorteddict = sorteddict; } #endregion #region ISortedDictionary Members /// /// Get the entry in the wrapped dictionary whose key is the /// predecessor of a specified key. /// /// The key /// The entry public KeyValuePair Predecessor(K key) { return sorteddict.Predecessor(key); } /// /// Get the entry in the wrapped dictionary whose key is the /// successor of a specified key. /// /// The key /// The entry public KeyValuePair Successor(K key) { return sorteddict.Successor(key); } /// /// Get the entry in the wrapped dictionary whose key is the /// weak predecessor of a specified key. /// /// The key /// The entry public KeyValuePair WeakPredecessor(K key) { return sorteddict.WeakPredecessor(key); } /// /// Get the entry in the wrapped dictionary whose key is the /// weak successor of a specified key. /// /// The key /// The entry public KeyValuePair WeakSuccessor(K key) { return sorteddict.WeakSuccessor(key); } #endregion } } #endif