2004-08-26 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Mono.C5 / Wrappers.cs
1 #if NET_2_0\r
2 /*\r
3  Copyright (c) 2003-2004 Niels Kokholm <kokholm@itu.dk> and Peter Sestoft <sestoft@dina.kvl.dk>\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
10  \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
13  \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
20  SOFTWARE.\r
21 */\r
22 \r
23 using System;\r
24 using System.Diagnostics;\r
25 using MSG = System.Collections.Generic;\r
26 namespace C5\r
27 {\r
28         /// <summary>\r
29         /// A read-only wrapper class for a generic enumerator\r
30         /// </summary>\r
31         public class GuardedEnumerator<T>: MSG.IEnumerator<T>\r
32         {\r
33                 #region Fields\r
34 \r
35                 MSG.IEnumerator<T> enumerator;\r
36 \r
37                 #endregion\r
38 \r
39                 #region Constructor\r
40 \r
41                 /// <summary>\r
42                 /// Create a wrapper around a generic enumerator\r
43                 /// </summary>\r
44                 /// <param name="enumerator">The enumerator to wrap</param>\r
45                 public GuardedEnumerator(MSG.IEnumerator<T> enumerator)\r
46                 { this.enumerator = enumerator; }\r
47 \r
48                 #endregion\r
49 \r
50                 #region IEnumerator<T> Members\r
51 \r
52                 /// <summary>\r
53                 /// Move wrapped enumerator to next item, or the first item if\r
54                 /// this is the first call to MoveNext. \r
55                 /// </summary>\r
56                 /// <returns>True if enumerator is valid now</returns>\r
57                 public bool MoveNext() { return enumerator.MoveNext(); }\r
58 \r
59 \r
60                 /// <summary>\r
61                 /// Undefined if enumerator is not valid (MoveNext hash been called returning true)\r
62                 /// </summary>\r
63                 /// <value>The current item of the wrapped enumerator.</value>\r
64                 public T Current { get { return enumerator.Current; } }\r
65 \r
66                 #endregion\r
67 \r
68                 #region IDisposable Members\r
69 \r
70                 /// <summary>\r
71                 /// Dispose wrapped enumerator\r
72                 /// </summary>\r
73                 public void Dispose() { enumerator.Dispose(); }\r
74 \r
75                 #endregion\r
76         }\r
77 \r
78 \r
79 \r
80         /// <summary>\r
81         /// A read-only wrapper class for a generic enumerable\r
82         ///\r
83         /// <p>This is mainly interesting as a base of other guard classes</p>\r
84         /// </summary>\r
85         public class GuardedEnumerable<T>: MSG.IEnumerable<T>\r
86         {\r
87                 #region Fields\r
88 \r
89                 MSG.IEnumerable<T> enumerable;\r
90 \r
91                 #endregion\r
92 \r
93                 #region Constructor\r
94 \r
95                 /// <summary>\r
96                 /// Wrap an enumerable in a read-only wrapper\r
97                 /// </summary>\r
98                 /// <param name="enumerable">The enumerable to wrap</param>\r
99                 public GuardedEnumerable(MSG.IEnumerable<T> enumerable)\r
100                 { this.enumerable = enumerable; }\r
101 \r
102                 #endregion\r
103 \r
104                 #region MSG.IEnumerable<T> Members\r
105 \r
106                 /// <summary>\r
107                 /// Get an enumerator from the wrapped enumerable\r
108                 /// </summary>\r
109                 /// <returns>The enumerator (itself wrapped)</returns>\r
110                 public MSG.IEnumerator<T> GetEnumerator()\r
111                 { return new GuardedEnumerator<T>(enumerable.GetEnumerator()); }\r
112 \r
113                 #endregion\r
114         }\r
115 \r
116 \r
117 \r
118         /// <summary>\r
119         /// A read-only wrapper for a generic directed enumerable\r
120         ///\r
121         /// <p>This is mainly interesting as a base of other guard classes</p>\r
122         /// </summary>\r
123         public class GuardedDirectedEnumerable<T>: GuardedEnumerable<T>, IDirectedEnumerable<T>\r
124         {\r
125                 #region Fields\r
126 \r
127                 IDirectedEnumerable<T> directedenumerable;\r
128 \r
129                 #endregion\r
130 \r
131                 #region Constructor\r
132 \r
133                 /// <summary>\r
134                 /// Wrap a directed enumerable in a read-only wrapper\r
135                 /// </summary>\r
136                 /// <param name="directedenumerable">the collection to wrap</param>\r
137                 public GuardedDirectedEnumerable(IDirectedEnumerable<T> directedenumerable)\r
138                         : base(directedenumerable)\r
139                 { this.directedenumerable = directedenumerable; }\r
140 \r
141                 #endregion\r
142 \r
143                 #region IDirectedEnumerable<T> Members\r
144 \r
145                 /// <summary>\r
146                 /// Get a enumerable that enumerates the wrapped collection in the opposite direction\r
147                 /// </summary>\r
148                 /// <returns>The mirrored enumerable</returns>\r
149                 public IDirectedEnumerable<T> Backwards()\r
150                 { return new GuardedDirectedEnumerable<T>(directedenumerable.Backwards()); }\r
151 \r
152 \r
153                 /// <summary>\r
154                 /// <code>Forwards</code> if same, else <code>Backwards</code>\r
155                 /// </summary>\r
156                 /// <value>The enumeration direction relative to the original collection.</value>\r
157                 public EnumerationDirection Direction\r
158                 { get { return directedenumerable.Direction; } }\r
159 \r
160                 #endregion\r
161         }\r
162 \r
163 \r
164 \r
165         /// <summary>\r
166         /// A read-only wrapper for an ICollectionValue&lt;T&gt;\r
167         ///\r
168         /// <p>This is mainly interesting as a base of other guard classes</p>\r
169         /// </summary>\r
170         public class GuardedCollectionValue<T>: GuardedEnumerable<T>, ICollectionValue<T>\r
171         {\r
172                 #region Fields\r
173 \r
174                 ICollectionValue<T> collection;\r
175 \r
176                 #endregion\r
177 \r
178                 #region Constructor\r
179 \r
180                 /// <summary>\r
181                 /// Wrap a ICollectionValue&lt;T&gt; in a read-only wrapper\r
182                 /// </summary>\r
183                 /// <param name="collection">the collection to wrap</param>\r
184                 public GuardedCollectionValue(ICollectionValue<T> collection) : base(collection)\r
185                 { this.collection = collection; }\r
186 \r
187                 #endregion\r
188 \r
189                 #region ICollection<T> Members\r
190 \r
191                 /// <summary>\r
192                 /// Get the size of the wrapped collection\r
193                 /// </summary>\r
194                 /// <value>The size</value>\r
195                 public int Count { get { return collection.Count; } }\r
196 \r
197         /// <summary>\r
198         /// The value is symbolic indicating the type of asymptotic complexity\r
199         /// in terms of the size of this collection (worst-case or amortized as\r
200         /// relevant).\r
201         /// </summary>\r
202         /// <value>A characterization of the speed of the \r
203         /// <code>Count</code> property in this collection.</value>\r
204         public Speed CountSpeed { get { return collection.CountSpeed; } }\r
205 \r
206         /// <summary>\r
207                 /// Copy the items of the wrapped collection to an array\r
208                 /// </summary>\r
209                 /// <param name="a">The array</param>\r
210                 /// <param name="i">Starting offset</param>\r
211                 public void CopyTo(T[] a, int i) { collection.CopyTo(a, i); }\r
212 \r
213         /// <summary>\r
214         /// Create an array from the items of the wrapped collection\r
215         /// </summary>\r
216         /// <returns>The array</returns>\r
217         public T[] ToArray() { return collection.ToArray(); }\r
218 \r
219         /// <summary>\r
220         /// Apply a delegate to all items of the wrapped enumerable.\r
221         /// </summary>\r
222         /// <param name="a">The delegate to apply</param>\r
223         //TODO: change this to throw an exception?\r
224         public void Apply(Applier<T> a) { collection.Apply(a); }\r
225 \r
226 \r
227         /// <summary>\r
228         /// Check if there exists an item  that satisfies a\r
229         /// specific predicate in the wrapped enumerable.\r
230         /// </summary>\r
231         /// <param name="filter">A filter delegate \r
232         /// (<see cref="T:C5.Filter!1"/>) defining the predicate</param>\r
233         /// <returns>True is such an item exists</returns>\r
234         public bool Exists(Filter<T> filter) { return collection.Exists(filter); }\r
235 \r
236 \r
237         /// <summary>\r
238         /// Check if all items in the wrapped enumerable satisfies a specific predicate.\r
239         /// </summary>\r
240         /// <param name="filter">A filter delegate \r
241         /// (<see cref="T:C5.Filter!1"/>) defining the predicate</param>\r
242         /// <returns>True if all items satisfies the predicate</returns>\r
243         public bool All(Filter<T> filter) { return collection.All(filter); }\r
244         #endregion\r
245     }\r
246 \r
247 \r
248 \r
249         /// <summary>\r
250         /// A read-only wrapper for a directed collection\r
251         ///\r
252         /// <p>This is mainly interesting as a base of other guard classes</p>\r
253         /// </summary>\r
254         public class GuardedDirectedCollectionValue<T>: GuardedCollectionValue<T>, IDirectedCollectionValue<T>\r
255         {\r
256                 #region Fields\r
257 \r
258                 IDirectedCollectionValue<T> directedcollection;\r
259 \r
260                 #endregion\r
261 \r
262                 #region Constructor\r
263 \r
264                 /// <summary>\r
265                 /// Wrap a directed collection in a read-only wrapper\r
266                 /// </summary>\r
267                 /// <param name="directedcollection">the collection to wrap</param>\r
268                 public GuardedDirectedCollectionValue(IDirectedCollectionValue<T> directedcollection) : \r
269                         base(directedcollection)\r
270                 { this.directedcollection = directedcollection; }\r
271 \r
272                 #endregion\r
273 \r
274                 #region IDirectedCollection<T> Members\r
275 \r
276                 /// <summary>\r
277                 /// Get a collection that enumerates the wrapped collection in the opposite direction\r
278                 /// </summary>\r
279                 /// <returns>The mirrored collection</returns>\r
280                 public IDirectedCollectionValue<T> Backwards()\r
281                 { return new GuardedDirectedCollectionValue<T>(directedcollection.Backwards()); }\r
282 \r
283                 #endregion\r
284 \r
285                 #region IDirectedEnumerable<T> Members\r
286 \r
287                 IDirectedEnumerable<T> IDirectedEnumerable<T>.Backwards()\r
288                 { return Backwards(); }\r
289 \r
290 \r
291                 /// <summary>\r
292                 /// <code>Forwards</code> if same, else <code>Backwards</code>\r
293                 /// </summary>\r
294                 /// <value>The enumeration direction relative to the original collection.</value>\r
295                 public EnumerationDirection Direction\r
296                 { get { return directedcollection.Direction; } }\r
297 \r
298                 #endregion\r
299         }\r
300 \r
301 \r
302 \r
303         /// <summary>\r
304         /// A read-only wrapper for an ICollection&lt;T&gt;.\r
305         /// <see cref="T:C5.ICollection!1"/>\r
306         ///\r
307         /// <p>Suitable for wrapping hash tables, <see cref="T:C5.HashSet!1"/>\r
308         /// and <see cref="T:C5.HashBag!1"/>  </p>\r
309         /// </summary>\r
310         public class GuardedCollection<T>: GuardedCollectionValue<T>, ICollection<T>\r
311         {\r
312                 #region Fields\r
313 \r
314                 ICollection<T> collection;\r
315 \r
316                 #endregion\r
317 \r
318                 #region Constructor\r
319 \r
320                 /// <summary>\r
321                 /// Wrap an ICollection&lt;T&gt; in a read-only wrapper\r
322                 /// </summary>\r
323                 /// <param name="collection">the collection to wrap</param>\r
324                 public GuardedCollection(ICollection<T> collection)\r
325                         :base(collection)\r
326                 { this.collection = collection; }\r
327 \r
328                 #endregion\r
329 \r
330                 #region ICollection<T> Members\r
331 \r
332                 /// <summary>\r
333                 /// (This is a read-only wrapper)\r
334                 /// </summary>\r
335                 /// <value>True</value>\r
336                 public bool IsReadOnly { get { return true; } }\r
337 \r
338 \r
339                 /// <summary> </summary>\r
340                 /// <value>Speed of wrapped collection</value>\r
341                 public Speed ContainsSpeed { get { return collection.ContainsSpeed; } }\r
342 \r
343 \r
344                 int ICollection<T>.GetHashCode()\r
345                 { return ((ICollection<T>)collection).GetHashCode(); }\r
346 \r
347 \r
348                 bool ICollection<T>.Equals(ICollection<T> that)\r
349                 { return ((ICollection<T>)collection).Equals(that); }\r
350 \r
351 \r
352                 /// <summary>\r
353                 /// Check if an item is in the wrapped collection\r
354                 /// </summary>\r
355                 /// <param name="item">The item</param>\r
356                 /// <returns>True if found</returns>\r
357                 public bool Contains(T item) { return collection.Contains(item); }\r
358 \r
359 \r
360                 /// <summary>\r
361                 /// Count the number of times an item appears in the wrapped collection\r
362                 /// </summary>\r
363                 /// <param name="item">The item</param>\r
364                 /// <returns>The number of copies</returns>\r
365                 public int ContainsCount(T item) { return collection.ContainsCount(item); }\r
366 \r
367 \r
368                 /// <summary>\r
369                 /// Check if all items in the argument is in the wrapped collection\r
370                 /// </summary>\r
371                 /// <param name="items">The items</param>\r
372                 /// <returns>True if so</returns>\r
373                 public bool ContainsAll(MSG.IEnumerable<T> items) { return collection.ContainsAll(items); }\r
374 \r
375 \r
376                 /// <summary>\r
377                 /// Search for an item in the wrapped collection\r
378                 /// </summary>\r
379                 /// <param name="item">On entry the item to look for, on exit the equivalent item found (if any)</param>\r
380                 /// <returns></returns>\r
381                 public bool Find(ref T item) { return collection.Find(ref item); }\r
382 \r
383 \r
384                 /// <summary>\r
385                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
386                 /// </summary>\r
387                 /// <param name="item"></param>\r
388                 /// <returns></returns>\r
389                 public bool FindOrAdd(ref T item)\r
390                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
391 \r
392 \r
393                 /// <summary>\r
394                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
395                 /// </summary>\r
396                 /// <param name="item"></param>\r
397                 /// <returns></returns>\r
398                 public bool Update(T item)\r
399                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
400 \r
401 \r
402                 /// <summary>\r
403                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
404                 /// </summary>\r
405                 /// <param name="item"></param>\r
406                 /// <returns></returns>\r
407                 public bool UpdateOrAdd(T item)\r
408                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
409 \r
410 \r
411                 /// <summary>\r
412                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
413                 /// </summary>\r
414                 /// <param name="item"></param>\r
415                 /// <returns></returns>\r
416                 public bool Remove(T item)\r
417                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
418 \r
419 \r
420                 /// <summary>\r
421                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
422                 /// </summary>\r
423                 /// <param name="item"></param>\r
424                 /// <returns></returns>\r
425                 public bool RemoveWithReturn(ref T item)\r
426                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
427 \r
428 \r
429                 /// <summary>\r
430                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
431                 /// </summary>\r
432                 /// <param name="item"></param>\r
433                 public void RemoveAllCopies(T item)\r
434                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
435 \r
436 \r
437                 /// <summary>\r
438                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
439                 /// </summary>\r
440                 /// <param name="items"></param>\r
441                 public void RemoveAll(MSG.IEnumerable<T> items)\r
442                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
443 \r
444 \r
445                 /// <summary>\r
446                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
447                 /// </summary>\r
448                 public void Clear()\r
449                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
450 \r
451 \r
452                 /// <summary>\r
453                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
454                 /// </summary>\r
455                 /// <param name="items"></param>\r
456                 public void RetainAll(MSG.IEnumerable<T> items)\r
457                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
458 \r
459 \r
460                 /// <summary>\r
461                 /// Check  wrapped collection for internal consistency\r
462                 /// </summary>\r
463                 /// <returns>True if check passed</returns>\r
464                 public bool Check() { return collection.Check(); }\r
465 \r
466                 #endregion\r
467 \r
468                 #region ISink<T> Members\r
469 \r
470                 /// <summary> </summary>\r
471                 /// <value>False if wrapped collection has set semantics</value>\r
472         public bool AllowsDuplicates { get { return collection.AllowsDuplicates; } }\r
473 \r
474 \r
475         /// <summary> </summary>\r
476                 /// <value>The sync root of the wrapped collection</value>\r
477                 public object SyncRoot { get { return collection.SyncRoot; } }\r
478 \r
479 \r
480                 /// <summary> </summary>\r
481                 /// <value>True if wrapped collection is empty</value>\r
482                 public bool IsEmpty { get { return collection.IsEmpty; } }\r
483 \r
484 \r
485                 /// <summary>\r
486                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
487                 /// </summary>\r
488                 /// <param name="item"></param>\r
489                 /// <returns></returns>\r
490                 public bool Add(T item)\r
491                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
492 \r
493 \r
494         /// <summary>\r
495         /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
496         /// </summary>\r
497         /// <param name="items"></param>\r
498         public void AddAll(MSG.IEnumerable<T> items)\r
499         { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
500 \r
501         /// <summary>\r
502         /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
503         /// </summary>\r
504         /// <param name="items"></param>\r
505         /*public*/ void C5.IExtensible<T>.AddAll<U>(MSG.IEnumerable<U> items) //where U : T\r
506         { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
507 \r
508         #endregion\r
509     }\r
510 \r
511 \r
512         /// <summary>\r
513         /// A read-only wrapper for a sequenced collection\r
514         ///\r
515         /// <p>This is mainly interesting as a base of other guard classes</p>\r
516         /// </summary>\r
517         public class GuardedSequenced<T>: GuardedCollection<T>, ISequenced<T>\r
518         {\r
519                 #region Fields\r
520 \r
521                 ISequenced<T> sequenced;\r
522 \r
523                 #endregion\r
524 \r
525                 #region Constructor\r
526 \r
527                 /// <summary>\r
528                 /// Wrap a sequenced collection in a read-only wrapper\r
529                 /// </summary>\r
530                 /// <param name="sorted"></param>\r
531                 public GuardedSequenced(ISequenced<T> sorted):base(sorted) { this.sequenced = sorted; }\r
532 \r
533                 #endregion\r
534 \r
535                 #region ISequenced<T> Members\r
536 \r
537                 int ISequenced<T>.GetHashCode()\r
538                 { return ((ISequenced<T>)sequenced).GetHashCode(); }\r
539 \r
540 \r
541                 bool ISequenced<T>.Equals(ISequenced<T> that)\r
542                 { return ((ISequenced<T>)sequenced).Equals(that); }\r
543 \r
544                 #endregion\r
545 \r
546                 #region IEditableCollection<T> Members\r
547 \r
548                 int ICollection<T>.GetHashCode()\r
549                 { return ((ICollection<T>)sequenced).GetHashCode(); }\r
550 \r
551 \r
552                 bool ICollection<T>.Equals(ICollection<T> that)\r
553                 { return ((ICollection<T>)sequenced).Equals(that); }\r
554 \r
555                 #endregion\r
556 \r
557                 #region IDirectedCollection<T> Members\r
558 \r
559                 /// <summary>\r
560                 /// Get a collection that enumerates the wrapped collection in the opposite direction\r
561                 /// </summary>\r
562                 /// <returns>The mirrored collection</returns>\r
563                 public IDirectedCollectionValue<T> Backwards()\r
564                 { return new GuardedDirectedCollectionValue<T>(sequenced.Backwards()); }\r
565 \r
566                 #endregion\r
567 \r
568                 #region IDirectedEnumerable<T> Members\r
569 \r
570                 IDirectedEnumerable<T> IDirectedEnumerable<T>.Backwards()\r
571                 { return Backwards(); }\r
572 \r
573 \r
574 \r
575                 /// <summary>\r
576                 /// <code>Forwards</code> if same, else <code>Backwards</code>\r
577                 /// </summary>\r
578                 /// <value>The enumeration direction relative to the original collection.</value>\r
579                 public EnumerationDirection Direction\r
580                 { get { return EnumerationDirection.Forwards; } }\r
581 \r
582                 #endregion\r
583         }\r
584 \r
585 \r
586 \r
587         /// <summary>\r
588         /// A read-only wrapper for a sorted collection\r
589         ///\r
590         /// <p>This is mainly interesting as a base of other guard classes</p>\r
591         /// </summary>\r
592         public class GuardedSorted<T>: GuardedSequenced<T>, ISorted<T>\r
593         {\r
594                 #region Fields\r
595 \r
596                 ISorted<T> sorted;\r
597 \r
598                 #endregion\r
599 \r
600                 #region Constructor\r
601 \r
602                 /// <summary>\r
603                 /// Wrap a sorted collection in a read-only wrapper\r
604                 /// </summary>\r
605                 /// <param name="sorted"></param>\r
606                 public GuardedSorted(ISorted<T> sorted):base(sorted) { this.sorted = sorted; }\r
607 \r
608                 #endregion\r
609 \r
610         #region IEditableCollection Members\r
611 \r
612                 int ICollection<T>.GetHashCode()\r
613                 { return ((ICollection<T>)sorted).GetHashCode(); }\r
614 \r
615 \r
616                 bool ICollection<T>.Equals(ICollection<T> that)\r
617                 { return ((ICollection<T>)sorted).Equals(that); }\r
618 \r
619                 #endregion\r
620 \r
621                 #region ISequenced<T> Members\r
622 \r
623                 int ISequenced<T>.GetHashCode()\r
624                 { return ((ISequenced<T>)sorted).GetHashCode(); }\r
625 \r
626 \r
627                 bool ISequenced<T>.Equals(ISequenced<T> that)\r
628                 { return ((ISequenced<T>)sorted).Equals(that); }\r
629 \r
630 \r
631                 #endregion\r
632 \r
633                 #region ISorted<T> Members\r
634 \r
635                 /// <summary>\r
636                 /// Find the predecessor of the item in the wrapped sorted collection\r
637                 /// </summary>\r
638                 /// <param name="item">The item</param>\r
639                 /// <returns>The predecessor</returns>\r
640                 public T Predecessor(T item) { return sorted.Predecessor(item); }\r
641 \r
642 \r
643                 /// <summary>\r
644                 /// Find the Successor of the item in the wrapped sorted collection\r
645                 /// </summary>\r
646                 /// <param name="item">The item</param>\r
647                 /// <returns>The Successor</returns>\r
648                 public T Successor(T item) { return sorted.Successor(item); }\r
649 \r
650 \r
651                 /// <summary>\r
652                 /// Find the weak predecessor of the item in the wrapped sorted collection\r
653                 /// </summary>\r
654                 /// <param name="item">The item</param>\r
655                 /// <returns>The weak predecessor</returns>\r
656                 public T WeakPredecessor(T item) { return sorted.WeakPredecessor(item); }\r
657 \r
658 \r
659                 /// <summary>\r
660                 /// Find the weak Successor of the item in the wrapped sorted collection\r
661                 /// </summary>\r
662                 /// <param name="item">The item</param>\r
663                 /// <returns>The weak Successor</returns>\r
664                 public T WeakSuccessor(T item) { return sorted.WeakSuccessor(item); }\r
665 \r
666 \r
667                 /// <summary>\r
668                 /// Run Cut on the wrapped sorted collection\r
669                 /// </summary>\r
670                 /// <param name="c"></param>\r
671                 /// <param name="low"></param>\r
672                 /// <param name="lval"></param>\r
673                 /// <param name="high"></param>\r
674                 /// <param name="hval"></param>\r
675                 /// <returns></returns>\r
676                 public bool Cut(IComparable<T> c, out T low, out bool lval, out T high, out bool hval)\r
677                 { return sorted.Cut(c, out low, out lval, out high, out hval); }\r
678 \r
679 \r
680                 /// <summary>\r
681                 /// Get the specified range from the wrapped collection. \r
682                 /// (The current implementation erroneously does not wrap the result.)\r
683                 /// </summary>\r
684                 /// <param name="bot"></param>\r
685                 /// <returns></returns>\r
686                 public IDirectedEnumerable<T> RangeFrom(T bot) { return sorted.RangeFrom(bot); }\r
687 \r
688 \r
689                 /// <summary>\r
690                 /// Get the specified range from the wrapped collection. \r
691                 /// (The current implementation erroneously does not wrap the result.)\r
692                 /// </summary>\r
693                 /// <param name="bot"></param>\r
694                 /// <param name="top"></param>\r
695                 /// <returns></returns>\r
696                 public IDirectedEnumerable<T> RangeFromTo(T bot, T top)\r
697                 { return sorted.RangeFromTo(bot, top); }\r
698 \r
699 \r
700                 /// <summary>\r
701                 /// Get the specified range from the wrapped collection. \r
702                 /// (The current implementation erroneously does not wrap the result.)\r
703                 /// </summary>\r
704                 /// <param name="top"></param>\r
705                 /// <returns></returns>\r
706                 public IDirectedEnumerable<T> RangeTo(T top) { return sorted.RangeTo(top); }\r
707 \r
708 \r
709                 /// <summary>\r
710                 /// Get the specified range from the wrapped collection. \r
711                 /// (The current implementation erroneously does not wrap the result.)\r
712                 /// </summary>\r
713                 /// <returns></returns>\r
714                 public IDirectedCollectionValue<T> RangeAll() { return sorted.RangeAll(); }\r
715 \r
716 \r
717                 /// <summary>\r
718                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
719                 /// </summary>\r
720                 /// <param name="items"></param>\r
721                 public void AddSorted(MSG.IEnumerable<T> items)\r
722                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
723 \r
724 \r
725                 /// <summary>\r
726                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
727                 /// </summary>\r
728                 /// <param name="low"></param>\r
729                 public void RemoveRangeFrom(T low)\r
730                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
731 \r
732 \r
733                 /// <summary>\r
734                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
735                 /// </summary>\r
736                 /// <param name="low"></param>\r
737                 /// <param name="hi"></param>\r
738                 public void RemoveRangeFromTo(T low, T hi)\r
739                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
740 \r
741 \r
742                 /// <summary>\r
743                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
744                 /// </summary>\r
745                 /// <param name="hi"></param>\r
746                 public void RemoveRangeTo(T hi)\r
747                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
748 \r
749                 #endregion\r
750 \r
751                 #region IPriorityQueue<T> Members\r
752 \r
753                 /// <summary>\r
754                 /// Find the minimum of the wrapped collection\r
755                 /// </summary>\r
756                 /// <returns>The minimum</returns>\r
757                 public T FindMin() { return sorted.FindMin(); }\r
758 \r
759 \r
760                 /// <summary>\r
761                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
762                 /// </summary>\r
763                 /// <returns></returns>\r
764                 public T DeleteMin()\r
765                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
766 \r
767 \r
768                 /// <summary>\r
769                 /// Find the maximum of the wrapped collection\r
770                 /// </summary>\r
771                 /// <returns>The maximum</returns>\r
772                 public T FindMax() { return sorted.FindMax(); }\r
773 \r
774 \r
775                 /// <summary>\r
776                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
777                 /// </summary>\r
778                 /// <returns></returns>\r
779                 public T DeleteMax()\r
780                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
781 \r
782         /// <summary>\r
783         /// The comparer object supplied at creation time for the underlying collection\r
784         /// </summary>\r
785         /// <value>The comparer</value>\r
786         public IComparer<T> Comparer { get { return sorted.Comparer; } }\r
787         #endregion\r
788 \r
789 \r
790                 #region IDirectedEnumerable<T> Members\r
791 \r
792                 IDirectedEnumerable<T> IDirectedEnumerable<T>.Backwards()\r
793                 { return Backwards(); }\r
794 \r
795                 #endregion\r
796         }\r
797 \r
798 \r
799 \r
800         /// <summary>\r
801         /// Read-only wrapper for indexed sorted collections\r
802         ///\r
803         /// <p>Suitable for wrapping TreeSet, TreeBag and SortedArray</p>\r
804         /// </summary>\r
805         public class GuardedIndexedSorted<T>: GuardedSorted<T>, IIndexedSorted<T>\r
806         {\r
807                 #region Fields\r
808 \r
809                 IIndexedSorted<T> indexedsorted;\r
810 \r
811                 #endregion\r
812 \r
813                 #region Constructor\r
814 \r
815                 /// <summary>\r
816                 /// Wrap an indexed sorted collection in a read-only wrapper\r
817                 /// </summary>\r
818                 /// <param name="list">the indexed sorted collection</param>\r
819                 public GuardedIndexedSorted(IIndexedSorted<T> list):base(list)\r
820                 { this.indexedsorted = list; }\r
821 \r
822                 #endregion\r
823 \r
824                 #region IIndexedSorted<T> Members\r
825 \r
826                 /// <summary>\r
827                 /// Get the specified range from the wrapped collection. \r
828                 /// (The current implementation erroneously does not wrap the result.)\r
829                 /// </summary>\r
830                 /// <param name="bot"></param>\r
831                 /// <returns></returns>\r
832                 public new IDirectedCollectionValue<T> RangeFrom(T bot)\r
833                 { return indexedsorted.RangeFrom(bot); }\r
834 \r
835 \r
836                 /// <summary>\r
837                 /// Get the specified range from the wrapped collection. \r
838                 /// (The current implementation erroneously does not wrap the result.)\r
839                 /// </summary>\r
840                 /// <param name="bot"></param>\r
841                 /// <param name="top"></param>\r
842                 /// <returns></returns>\r
843                 public new IDirectedCollectionValue<T> RangeFromTo(T bot, T top)\r
844                 { return indexedsorted.RangeFromTo(bot, top); }\r
845 \r
846 \r
847                 /// <summary>\r
848                 /// Get the specified range from the wrapped collection. \r
849                 /// (The current implementation erroneously does not wrap the result.)\r
850                 /// </summary>\r
851                 /// <param name="top"></param>\r
852                 /// <returns></returns>\r
853                 public new IDirectedCollectionValue<T> RangeTo(T top)\r
854                 { return indexedsorted.RangeTo(top); }\r
855 \r
856 \r
857                 /// <summary>\r
858                 /// Report the number of items in the specified range of the wrapped collection\r
859                 /// </summary>\r
860                 /// <param name="bot"></param>\r
861                 /// <returns></returns>\r
862                 public int CountFrom(T bot) { return indexedsorted.CountFrom(bot); }\r
863 \r
864 \r
865                 /// <summary>\r
866                 /// Report the number of items in the specified range of the wrapped collection\r
867                 /// </summary>\r
868                 /// <param name="bot"></param>\r
869                 /// <param name="top"></param>\r
870                 /// <returns></returns>\r
871                 public int CountFromTo(T bot, T top) { return indexedsorted.CountFromTo(bot, top); }\r
872 \r
873 \r
874                 /// <summary>\r
875                 /// Report the number of items in the specified range of the wrapped collection\r
876                 /// </summary>\r
877                 /// <param name="top"></param>\r
878                 /// <returns></returns>\r
879                 public int CountTo(T top) { return indexedsorted.CountTo(top); }\r
880 \r
881 \r
882                 /// <summary>\r
883                 /// Run FindAll on the wrapped collection with the indicated filter.\r
884                 /// The result will <b>not</b> be read-only.\r
885                 /// </summary>\r
886                 /// <param name="f"></param>\r
887                 /// <returns></returns>\r
888                 public IIndexedSorted<T> FindAll(Filter<T> f)\r
889                 { return indexedsorted.FindAll(f); }\r
890 \r
891 \r
892                 /// <summary>\r
893                 /// Run Map on the wrapped collection with the indicated mapper.\r
894                 /// The result will <b>not</b> be read-only.\r
895                 /// </summary>\r
896                 /// <param name="m"></param>\r
897                 /// <param name="c">The comparer to use in the result</param>\r
898                 /// <returns></returns>\r
899                 public IIndexedSorted<V> Map<V>(Mapper<T,V> m, IComparer<V> c)\r
900                 { return indexedsorted.Map(m, c); }\r
901 \r
902                 #endregion\r
903 \r
904                 #region IIndexed<T> Members\r
905 \r
906                 /// <summary>\r
907                 /// \r
908                 /// </summary>\r
909                 /// <value>The i'th item of the wrapped sorted collection</value>\r
910                 public T this[int i] { get { return indexedsorted[i]; } }\r
911 \r
912 \r
913                 /// <summary> </summary>\r
914                 /// <value>A directed collection of the items in the indicated interval of the wrapped collection</value>\r
915                 public IDirectedCollectionValue<T> this[int start, int end]\r
916                 { get { return new GuardedDirectedCollectionValue<T>(indexedsorted[start, end]); } }\r
917 \r
918 \r
919                 /// <summary>\r
920                 /// Find the (first) index of an item in the wrapped collection\r
921                 /// </summary>\r
922                 /// <param name="item"></param>\r
923                 /// <returns></returns>\r
924                 public int IndexOf(T item) { return indexedsorted.IndexOf(item); }\r
925 \r
926 \r
927                 /// <summary>\r
928                 /// Find the last index of an item in the wrapped collection\r
929                 /// </summary>\r
930                 /// <param name="item"></param>\r
931                 /// <returns></returns>\r
932                 public int LastIndexOf(T item) { return indexedsorted.LastIndexOf(item); }\r
933 \r
934 \r
935                 /// <summary>\r
936                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
937                 /// </summary>\r
938                 /// <param name="i"></param>\r
939                 /// <returns></returns>\r
940                 public T RemoveAt(int i)\r
941                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
942 \r
943 \r
944                 /// <summary>\r
945                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
946                 /// </summary>\r
947                 /// <param name="start"></param>\r
948                 /// <param name="count"></param>\r
949                 public void RemoveInterval(int start, int count)\r
950                 { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
951 \r
952                 #endregion\r
953 \r
954                 #region ISequenced<T> Members\r
955 \r
956                 int ISequenced<T>.GetHashCode()\r
957                 { return ((ISequenced<T>)indexedsorted).GetHashCode(); }\r
958 \r
959 \r
960                 bool ISequenced<T>.Equals(ISequenced<T> that)\r
961                 { return ((ISequenced<T>)indexedsorted).Equals(that); }\r
962 \r
963                 #endregion\r
964 \r
965                 #region IEditableCollection<T> Members\r
966 \r
967                 int ICollection<T>.GetHashCode()\r
968                 { return ((ICollection<T>)indexedsorted).GetHashCode(); }\r
969 \r
970 \r
971                 bool ICollection<T>.Equals(ICollection<T> that)\r
972                 { return ((ICollection<T>)indexedsorted).Equals(that); }\r
973 \r
974                 #endregion\r
975 \r
976                 #region IDirectedEnumerable<T> Members\r
977 \r
978                 IDirectedEnumerable<T> IDirectedEnumerable<T>.Backwards()\r
979                 { return Backwards(); }\r
980 \r
981                 #endregion\r
982         }\r
983 \r
984 \r
985 \r
986         /// <summary>\r
987         /// A read-only wrapper for a generic list collection\r
988         /// <p>Suitable as a wrapper for LinkedList, HashedLinkedList, ArrayList and HashedArray.\r
989         /// <see cref="T:C5.LinkedList!1"/>, \r
990         /// <see cref="T:C5.HashedLinkedList!1"/>, \r
991         /// <see cref="T:C5.ArrayList!1"/> or\r
992         /// <see cref="T:C5.HashedArray!1"/>.\r
993         /// </p>\r
994         /// </summary>\r
995         public class GuardedList<T>: GuardedSequenced<T>, IList<T>\r
996         {\r
997                 #region Fields\r
998 \r
999                 IList<T> list;\r
1000 \r
1001                 #endregion\r
1002 \r
1003                 #region Constructor\r
1004 \r
1005                 /// <summary>\r
1006                 /// Wrap a list in a read-only wrapper\r
1007                 /// </summary>\r
1008                 /// <param name="list">The list</param>\r
1009                 public GuardedList(IList<T> list) : base (list) { this.list = list; }\r
1010 \r
1011                 #endregion\r
1012 \r
1013                 #region IList<T> Members\r
1014 \r
1015                 /// <summary>\r
1016                 /// \r
1017                 /// </summary>\r
1018                 /// <value>The first item of the wrapped list</value>\r
1019                 public T First { get { return list.First; } }\r
1020 \r
1021 \r
1022                 /// <summary>\r
1023                 /// \r
1024                 /// </summary>\r
1025                 /// <value>The last item of the wrapped list</value>\r
1026                 public T Last { get { return list.Last; } }\r
1027 \r
1028 \r
1029                 /// <summary>\r
1030                 /// <exception cref="InvalidOperationException"/> if used as setter\r
1031                 /// </summary>\r
1032                 /// <value>True if wrapped list has FIFO semantics for the Add(T item) and Remove() methods</value>\r
1033                 public bool FIFO\r
1034                 {\r
1035                         get { return list.FIFO; }\r
1036                         set { throw new InvalidOperationException("List is read only"); }\r
1037                 }\r
1038 \r
1039 \r
1040                 /// <summary>\r
1041                 /// <exception cref="InvalidOperationException"/> if used as setter\r
1042                 /// </summary>\r
1043                 /// <value>The i'th item of the wrapped list</value>\r
1044                 public T this[int i]\r
1045                 {\r
1046                         get { return list[i]; }\r
1047                         set { throw new InvalidOperationException("List is read only"); }\r
1048                 }\r
1049 \r
1050 \r
1051                 /// <summary>\r
1052                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1053                 /// </summary>\r
1054                 /// <param name="i"></param>\r
1055                 /// <param name="item"></param>\r
1056                 public void Insert(int i, T item)\r
1057                 { throw new InvalidOperationException("List is read only"); }\r
1058 \r
1059 \r
1060                 /// <summary>\r
1061                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1062                 /// </summary>\r
1063                 /// <param name="item"></param>\r
1064                 public void InsertFirst(T item)\r
1065                 { throw new InvalidOperationException("List is read only"); }\r
1066 \r
1067                 /// <summary>\r
1068                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1069                 /// </summary>\r
1070                 /// <param name="item"></param>\r
1071                 public void InsertLast(T item)\r
1072                 { throw new InvalidOperationException("List is read only"); }\r
1073 \r
1074                 /// <summary>\r
1075                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1076                 /// </summary>\r
1077                 /// <param name="item"></param>\r
1078                 /// <param name="target"></param>\r
1079                 public void InsertBefore(T item, T target)\r
1080                 { throw new InvalidOperationException("List is read only"); }\r
1081 \r
1082 \r
1083                 /// <summary>\r
1084                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1085                 /// </summary>\r
1086                 /// <param name="item"></param>\r
1087                 /// <param name="target"></param>\r
1088                 public void InsertAfter(T item, T target)\r
1089                 { throw new InvalidOperationException("List is read only"); }\r
1090 \r
1091 \r
1092                 /// <summary>\r
1093                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1094                 /// </summary>\r
1095                 /// <param name="i"></param>\r
1096                 /// <param name="items"></param>\r
1097                 public void InsertAll(int i, MSG.IEnumerable<T> items)\r
1098                 { throw new InvalidOperationException("List is read only"); }\r
1099 \r
1100 \r
1101                 /// <summary>\r
1102                 /// Perform FindAll on the wrapped list. The result is <b>not</b> necessarily read-only.\r
1103                 /// </summary>\r
1104                 /// <param name="filter">The filter to use</param>\r
1105                 /// <returns></returns>\r
1106                 public IList<T> FindAll(Filter<T> filter) { return list.FindAll(filter); }\r
1107 \r
1108 \r
1109                 /// <summary>\r
1110                 /// Perform Map on the wrapped list. The result is <b>not</b> necessarily read-only.\r
1111                 /// </summary>\r
1112         /// <typeparam name="V">The type of items of the new list</typeparam>\r
1113         /// <param name="mapper">The mapper to use.</param>\r
1114         /// <returns>The mapped list</returns>\r
1115         public IList<V> Map<V>(Mapper<T, V> mapper) { return list.Map(mapper); }\r
1116 \r
1117         /// <summary>\r
1118         /// Perform Map on the wrapped list. The result is <b>not</b> necessarily read-only.\r
1119         /// </summary>\r
1120         /// <typeparam name="V">The type of items of the new list</typeparam>\r
1121         /// <param name="mapper">The delegate defining the map.</param>\r
1122         /// <param name="hasher">The hasher to use for the new list</param>\r
1123         /// <returns>The new list.</returns>\r
1124         public IList<V> Map<V>(Mapper<T, V> mapper, IHasher<V> hasher) { return list.Map(mapper, hasher); }\r
1125 \r
1126         /// <summary>\r
1127         /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1128                 /// </summary>\r
1129                 /// <returns></returns>\r
1130                 public T Remove() { throw new InvalidOperationException("List is read only"); }\r
1131 \r
1132 \r
1133                 /// <summary>\r
1134                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1135                 /// </summary>\r
1136                 /// <returns></returns>\r
1137                 public T RemoveFirst() { throw new InvalidOperationException("List is read only"); }\r
1138 \r
1139 \r
1140                 /// <summary>\r
1141                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1142                 /// </summary>\r
1143                 /// <returns></returns>\r
1144                 public T RemoveLast() { throw new InvalidOperationException("List is read only"); }\r
1145 \r
1146 \r
1147                 /// <summary>\r
1148                 /// Create the indicated view on the wrapped list and wrap it read-only.\r
1149                 /// </summary>\r
1150                 /// <param name="start"></param>\r
1151                 /// <param name="count"></param>\r
1152                 /// <returns></returns>\r
1153                 public IList<T> View(int start, int count)\r
1154                 {\r
1155                         return new GuardedList<T>(list.View(start, count));\r
1156                 }\r
1157 \r
1158 \r
1159                 //TODO: This is wrong!\r
1160                 /// <summary>\r
1161                 /// (This is wrong functionality)\r
1162                 /// </summary>\r
1163         /// <value>The wrapped underlying list of the wrapped view </value>\r
1164         public IList<T> Underlying { get { return new GuardedList<T>(list.Underlying); } }\r
1165 \r
1166 \r
1167         /// <summary>\r
1168                 /// \r
1169                 /// </summary>\r
1170                 /// <value>The offset of the wrapped list as a view.</value>\r
1171                 public int Offset { get { return list.Offset; } }\r
1172 \r
1173 \r
1174                 /// <summary>\r
1175                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1176                 /// </summary>\r
1177                 /// <param name="offset"></param>\r
1178                 public void Slide(int offset) { throw new InvalidOperationException("List is read only"); }\r
1179 \r
1180 \r
1181                 /// <summary>\r
1182                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1183                 /// </summary>\r
1184                 /// <param name="offset"></param>\r
1185                 /// <param name="size"></param>\r
1186                 public void Slide(int offset, int size) { throw new InvalidOperationException("List is read only"); }\r
1187 \r
1188 \r
1189                 /// <summary>\r
1190                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1191                 /// </summary>\r
1192                 public void Reverse() { throw new InvalidOperationException("List is read only"); }\r
1193 \r
1194 \r
1195                 /// <summary>\r
1196                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1197                 /// </summary>\r
1198                 /// <param name="start"></param>\r
1199                 /// <param name="count"></param>\r
1200                 public void Reverse(int start, int count)\r
1201                 { throw new InvalidOperationException("List is read only"); }\r
1202 \r
1203 \r
1204                 /// <summary>\r
1205                 /// Check if wrapped list is sorted\r
1206                 /// </summary>\r
1207                 /// <param name="c">The sorting order to use</param>\r
1208                 /// <returns>True if sorted</returns>\r
1209                 public bool IsSorted(IComparer<T> c) { return list.IsSorted(c); }\r
1210 \r
1211 \r
1212                 /// <summary>\r
1213                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1214                 /// </summary>\r
1215                 /// <param name="c"></param>\r
1216                 public void Sort(IComparer<T> c)\r
1217                 { throw new InvalidOperationException("List is read only"); }\r
1218 \r
1219 \r
1220                 /// <summary>\r
1221                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1222                 /// </summary>\r
1223                 public void Shuffle()\r
1224                 { throw new InvalidOperationException("List is read only"); }\r
1225 \r
1226 \r
1227                 /// <summary>\r
1228                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1229                 /// </summary>\r
1230                 /// <param name="rnd"></param>\r
1231                 public void Shuffle(Random rnd)\r
1232                 { throw new InvalidOperationException("List is read only"); }\r
1233 \r
1234                 #endregion\r
1235 \r
1236                 #region IIndexed<T> Members\r
1237 \r
1238                 /// <summary> </summary>\r
1239                 /// <value>A directed collection of the items in the indicated interval of the wrapped collection</value>\r
1240                 public IDirectedCollectionValue<T> this[int start, int end]\r
1241                 { get { return new GuardedDirectedCollectionValue<T>(list[start, end]); } }\r
1242 \r
1243 \r
1244                 /// <summary>\r
1245                 /// Find the (first) index of an item in the wrapped collection\r
1246                 /// </summary>\r
1247                 /// <param name="item"></param>\r
1248                 /// <returns></returns>\r
1249                 public int IndexOf(T item) { return list.IndexOf(item); }\r
1250 \r
1251 \r
1252                 /// <summary>\r
1253                 /// Find the last index of an item in the wrapped collection\r
1254                 /// </summary>\r
1255                 /// <param name="item"></param>\r
1256                 /// <returns></returns>\r
1257                 public int LastIndexOf(T item) { return list.LastIndexOf(item); }\r
1258 \r
1259 \r
1260                 /// <summary>\r
1261                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1262                 /// </summary>\r
1263                 /// <param name="i"></param>\r
1264                 /// <returns></returns>\r
1265                 public T RemoveAt(int i)\r
1266                 { throw new InvalidOperationException("List is read only"); }\r
1267 \r
1268 \r
1269                 /// <summary>\r
1270                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1271                 /// </summary>\r
1272                 /// <param name="start"></param>\r
1273                 /// <param name="count"></param>\r
1274                 public void RemoveInterval(int start, int count)\r
1275                 { throw new InvalidOperationException("List is read only"); }\r
1276 \r
1277                 #endregion\r
1278 \r
1279                 #region ISequenced<T> Members\r
1280 \r
1281                 int ISequenced<T>.GetHashCode()\r
1282                 { return ((ISequenced<T>)list).GetHashCode(); }\r
1283 \r
1284 \r
1285                 bool ISequenced<T>.Equals(ISequenced<T> that)\r
1286                 { return ((ISequenced<T>)list).Equals(that); }\r
1287 \r
1288                 #endregion\r
1289 \r
1290                 #region IEditableCollection<T> Members\r
1291 \r
1292                 int ICollection<T>.GetHashCode()\r
1293                 { return ((ICollection<T>)list).GetHashCode(); }\r
1294 \r
1295 \r
1296                 bool ICollection<T>.Equals(ICollection<T> that)\r
1297                 { return ((ICollection<T>)list).Equals(that); }\r
1298 \r
1299                 #endregion\r
1300 \r
1301                 #region IDirectedEnumerable<T> Members\r
1302 \r
1303                 IDirectedEnumerable<T> IDirectedEnumerable<T>.Backwards()\r
1304                 { return Backwards(); }\r
1305 \r
1306                 #endregion\r
1307 \r
1308         #region IStack<T> Members\r
1309 \r
1310 \r
1311         /// <summary>\r
1312         /// \r
1313         /// </summary>\r
1314         /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1315         /// <returns>-</returns>\r
1316         public void Push(T item)\r
1317         { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
1318 \r
1319         /// <summary>\r
1320         /// \r
1321         /// </summary>\r
1322         /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1323         /// <returns>-</returns>\r
1324         public T Pop()\r
1325         { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
1326 \r
1327                 #endregion\r
1328 \r
1329         #region IQueue<T> Members\r
1330 \r
1331         /// <summary>\r
1332         /// \r
1333         /// </summary>\r
1334         /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1335         /// <returns>-</returns>\r
1336         public void EnQueue(T item)\r
1337         { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
1338 \r
1339         /// <summary>\r
1340         /// \r
1341         /// </summary>\r
1342         /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1343         /// <returns>-</returns>\r
1344         public T DeQueue()\r
1345         { throw new InvalidOperationException("Collection cannot be modified through this guard object"); }\r
1346 \r
1347                 #endregion\r
1348 \r
1349     }\r
1350 \r
1351 \r
1352 \r
1353         /// <summary>\r
1354         /// A read-only wrapper for a dictionary.\r
1355         ///\r
1356         /// <p>Suitable for wrapping a HashDictionary. <see cref="T:C5.HashDictionary!2"/></p>\r
1357         /// </summary>\r
1358         public class GuardedDictionary<K,V>: GuardedEnumerable<KeyValuePair<K,V>>, IDictionary<K,V>\r
1359         {\r
1360                 #region Fields\r
1361 \r
1362                 IDictionary<K,V> dict;\r
1363 \r
1364                 #endregion\r
1365 \r
1366                 #region Constructor\r
1367 \r
1368                 /// <summary>\r
1369                 /// Wrap a dictionary in a read-only wrapper\r
1370                 /// </summary>\r
1371                 /// <param name="dict">the dictionary</param>\r
1372                 public GuardedDictionary(IDictionary<K,V> dict) : base(dict) { this.dict = dict; }\r
1373 \r
1374                 #endregion\r
1375 \r
1376                 #region IDictionary<K,V> Members\r
1377 \r
1378                 /// <summary>\r
1379                 /// <exception cref="InvalidOperationException"/> since this is a\r
1380                 /// read-only wrappper if used as a setter\r
1381                 /// </summary>\r
1382                 /// <value>Get the value corresponding to a key in the wrapped dictionary</value>\r
1383                 public V this[K key]\r
1384                 {\r
1385                         get { return dict[key]; }\r
1386                         set { throw new InvalidOperationException("Dictionary is read only"); }\r
1387                 }\r
1388 \r
1389 \r
1390                 /// <summary> </summary>\r
1391                 /// <value>The size of the wrapped dictionary</value>\r
1392                 public int Count { get { return dict.Count; } }\r
1393 \r
1394 \r
1395                 /// <summary>\r
1396                 /// (This is a read-only wrapper)\r
1397                 /// </summary>\r
1398                 /// <value>True</value>\r
1399                 public bool IsReadOnly { get { return true; } }\r
1400 \r
1401 \r
1402                 /// <summary> </summary>\r
1403                 /// <value>The sync root of the wrapped dictionary</value>\r
1404                 public object SyncRoot { get { return dict.SyncRoot; } }\r
1405 \r
1406 \r
1407                 //TODO: guard with a read-only wrapper? Probably so!\r
1408                 /// <summary> </summary>\r
1409                 /// <value>The collection of keys of the wrapped dictionary</value>\r
1410                 public ICollectionValue<K> Keys\r
1411                 { get { return dict.Keys; } }\r
1412 \r
1413 \r
1414                 /// <summary> </summary>\r
1415                 /// <value>The collection of values of the wrapped dictionary</value>\r
1416                 public ICollectionValue<V> Values { get { return dict.Values; } }\r
1417 \r
1418 \r
1419                 /// <summary>\r
1420                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1421                 /// </summary>\r
1422                 /// <param name="key"></param>\r
1423                 /// <param name="val"></param>\r
1424                 public void Add(K key, V val)\r
1425                 { throw new InvalidOperationException("Dictionary is read only"); }\r
1426 \r
1427 \r
1428                 /// <summary>\r
1429                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1430                 /// </summary>\r
1431                 /// <param name="key"></param>\r
1432                 /// <returns></returns>\r
1433                 public bool Remove(K key)\r
1434                 { throw new InvalidOperationException("Dictionary is read only"); }\r
1435 \r
1436 \r
1437                 /// <summary>\r
1438                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1439                 /// </summary>\r
1440                 /// <param name="key"></param>\r
1441                 /// <param name="val"></param>\r
1442                 /// <returns></returns>\r
1443                 public bool Remove(K key, out V val)\r
1444                 { throw new InvalidOperationException("Dictionary is read only"); }\r
1445 \r
1446 \r
1447                 /// <summary>\r
1448                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1449                 /// </summary>\r
1450                 public void Clear()\r
1451                 { throw new InvalidOperationException("Dictionary is read only"); }\r
1452 \r
1453 \r
1454                 /// <summary>\r
1455                 /// Check if the wrapped dictionary contains a specific key\r
1456                 /// </summary>\r
1457                 /// <param name="key">The key</param>\r
1458                 /// <returns>True if it does</returns>\r
1459                 public bool Contains(K key) { return dict.Contains(key); }\r
1460 \r
1461 \r
1462                 /// <summary>\r
1463                 /// Search for a key in the wrapped dictionary, reporting the value if found\r
1464                 /// </summary>\r
1465                 /// <param name="key">The key</param>\r
1466                 /// <param name="val">On exit: the value if found</param>\r
1467                 /// <returns>True if found</returns>\r
1468                 public bool Find(K key, out V val) { return dict.Find(key, out val); }\r
1469 \r
1470 \r
1471                 /// <summary>\r
1472                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1473                 /// </summary>\r
1474                 /// <param name="key"></param>\r
1475                 /// <param name="val"></param>\r
1476                 /// <returns></returns>\r
1477                 public bool Update(K key, V val)\r
1478                 { throw new InvalidOperationException("Dictionary is read only"); }\r
1479 \r
1480 \r
1481                 /// <summary>\r
1482                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1483                 /// </summary>\r
1484                 /// <param name="key"></param>\r
1485                 /// <param name="val"></param>\r
1486                 /// <returns></returns>\r
1487                 public bool FindOrAdd(K key, ref V val)\r
1488                 { throw new InvalidOperationException("Dictionary is read only"); }\r
1489 \r
1490 \r
1491                 /// <summary>\r
1492                 /// <exception cref="InvalidOperationException"/> since this is a read-only wrappper\r
1493                 /// </summary>\r
1494                 /// <param name="key"></param>\r
1495                 /// <param name="val"></param>\r
1496                 /// <returns></returns>\r
1497                 public bool UpdateOrAdd(K key, V val)\r
1498                 { throw new InvalidOperationException("Dictionary is read only"); }\r
1499 \r
1500 \r
1501                 /// <summary>\r
1502                 /// Check the internal consistency of the wrapped dictionary\r
1503                 /// </summary>\r
1504                 /// <returns>True if check passed</returns>\r
1505                 public bool Check() { return dict.Check(); }\r
1506 \r
1507                 #endregion\r
1508         }\r
1509 \r
1510 \r
1511 \r
1512         /// <summary>\r
1513         /// A read-only wrapper for a sorted dictionary.\r
1514         ///\r
1515         /// <p>Suitable for wrapping a Dictionary. <see cref="T:C5.Dictionary!2"/></p>\r
1516         /// </summary>\r
1517         public class GuardedSortedDictionary<K,V>: GuardedDictionary<K,V>, ISortedDictionary<K,V>\r
1518         {\r
1519                 #region Fields\r
1520 \r
1521                 ISortedDictionary<K,V> sorteddict;\r
1522 \r
1523                 #endregion\r
1524 \r
1525                 #region Constructor\r
1526 \r
1527                 /// <summary>\r
1528                 /// Wrap a sorted dictionary in a read-only wrapper\r
1529                 /// </summary>\r
1530                 /// <param name="sorteddict">the dictionary</param>\r
1531                 public GuardedSortedDictionary(ISortedDictionary<K,V> sorteddict) :base(sorteddict)\r
1532                 { this.sorteddict = sorteddict; }\r
1533 \r
1534                 #endregion\r
1535 \r
1536                 #region ISortedDictionary<K,V> Members\r
1537 \r
1538                 /// <summary>\r
1539                 /// Get the entry in the wrapped dictionary whose key is the\r
1540                 /// predecessor of a specified key.\r
1541                 /// </summary>\r
1542                 /// <param name="key">The key</param>\r
1543                 /// <returns>The entry</returns>\r
1544                 public KeyValuePair<K,V> Predecessor(K key)\r
1545                 { return sorteddict.Predecessor(key); }\r
1546 \r
1547 \r
1548                 /// <summary>\r
1549                 /// Get the entry in the wrapped dictionary whose key is the\r
1550                 /// successor of a specified key.\r
1551                 /// </summary>\r
1552                 /// <param name="key">The key</param>\r
1553                 /// <returns>The entry</returns>\r
1554                 public KeyValuePair<K,V> Successor(K key)\r
1555                 { return sorteddict.Successor(key); }\r
1556 \r
1557 \r
1558                 /// <summary>\r
1559                 /// Get the entry in the wrapped dictionary whose key is the\r
1560                 /// weak predecessor of a specified key.\r
1561                 /// </summary>\r
1562                 /// <param name="key">The key</param>\r
1563                 /// <returns>The entry</returns>\r
1564                 public KeyValuePair<K,V> WeakPredecessor(K key)\r
1565                 { return sorteddict.WeakPredecessor(key); }\r
1566 \r
1567 \r
1568                 /// <summary>\r
1569                 /// Get the entry in the wrapped dictionary whose key is the\r
1570                 /// weak successor of a specified key.\r
1571                 /// </summary>\r
1572                 /// <param name="key">The key</param>\r
1573                 /// <returns>The entry</returns>\r
1574                 public KeyValuePair<K,V> WeakSuccessor(K key)\r
1575                 { return sorteddict.WeakSuccessor(key); }\r
1576 \r
1577                 #endregion\r
1578         }\r
1579 \r
1580 }\r
1581 #endif\r