* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.C5 / C5 / MappedEnumerators.cs
1 #if NET_2_0
2 /*\r
3  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
4  Permission is hereby granted, free of charge, to any person obtaining a copy\r
5  of this software and associated documentation files (the "Software"), to deal\r
6  in the Software without restriction, including without limitation the rights\r
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
8  copies of the Software, and to permit persons to whom the Software is\r
9  furnished to do so, subject to the following conditions:\r
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 SCG = System.Collections.Generic;\r
26 namespace C5\r
27 {\r
28   abstract class MappedDirectedCollectionValue<T, V> : DirectedCollectionValueBase<V>, IDirectedCollectionValue<V>\r
29   {\r
30     IDirectedCollectionValue<T> directedcollectionvalue;\r
31 \r
32     abstract public V Map(T item);\r
33 \r
34     public MappedDirectedCollectionValue(IDirectedCollectionValue<T> directedcollectionvalue)\r
35     {\r
36       this.directedcollectionvalue = directedcollectionvalue;\r
37     }\r
38 \r
39     public override V Choose() { return Map(directedcollectionvalue.Choose()); }\r
40 \r
41     public override bool IsEmpty { get { return directedcollectionvalue.IsEmpty; } }\r
42 \r
43     public override int Count { get { return directedcollectionvalue.Count; } }\r
44 \r
45     public override Speed CountSpeed { get { return directedcollectionvalue.CountSpeed; } }\r
46 \r
47     public override IDirectedCollectionValue<V> Backwards()\r
48     {\r
49       MappedDirectedCollectionValue<T, V> retval = (MappedDirectedCollectionValue<T, V>)MemberwiseClone();\r
50       retval.directedcollectionvalue = directedcollectionvalue.Backwards();\r
51       return retval;\r
52       //If we made this classs non-abstract we could do\r
53       //return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;\r
54     }\r
55 \r
56 \r
57     public override SCG.IEnumerator<V> GetEnumerator()\r
58     {\r
59       foreach (T item in directedcollectionvalue)\r
60         yield return Map(item);\r
61     }\r
62 \r
63     public override EnumerationDirection Direction\r
64     {\r
65       get { return directedcollectionvalue.Direction; }\r
66     }\r
67 \r
68     IDirectedEnumerable<V> IDirectedEnumerable<V>.Backwards()\r
69     {\r
70       return Backwards();\r
71     }\r
72 \r
73 \r
74   }\r
75 \r
76   abstract class MappedCollectionValue<T, V> : CollectionValueBase<V>, ICollectionValue<V>\r
77   {\r
78     ICollectionValue<T> collectionvalue;\r
79 \r
80     abstract public V Map(T item);\r
81 \r
82     public MappedCollectionValue(ICollectionValue<T> collectionvalue)\r
83     {\r
84       this.collectionvalue = collectionvalue;\r
85     }\r
86 \r
87     public override V Choose() { return Map(collectionvalue.Choose()); }\r
88 \r
89     public override bool IsEmpty { get { return collectionvalue.IsEmpty; } }\r
90 \r
91     public override int Count { get { return collectionvalue.Count; } }\r
92 \r
93     public override Speed CountSpeed { get { return collectionvalue.CountSpeed; } }\r
94 \r
95     public override SCG.IEnumerator<V> GetEnumerator()\r
96     {\r
97       foreach (T item in collectionvalue)\r
98         yield return Map(item);\r
99     }\r
100   }\r
101   \r
102   class MultiplicityOne<K> : MappedCollectionValue<K, KeyValuePair<K, int>>\r
103   {\r
104     public MultiplicityOne(ICollectionValue<K> coll):base(coll) { }\r
105     public override KeyValuePair<K, int> Map(K k) { return new KeyValuePair<K, int>(k, 1); }\r
106   }\r
107 \r
108   class DropMultiplicity<K> : MappedCollectionValue<KeyValuePair<K, int>, K>\r
109   {\r
110     public DropMultiplicity(ICollectionValue<KeyValuePair<K, int>> coll):base(coll) { }\r
111     public override K Map(KeyValuePair<K, int> kvp) { return kvp.Key; }\r
112   }\r
113   \r
114   abstract class MappedDirectedEnumerable<T, V> : EnumerableBase<V>, IDirectedEnumerable<V>\r
115   {\r
116     IDirectedEnumerable<T> directedenumerable;\r
117 \r
118     abstract public V Map(T item);\r
119 \r
120     public MappedDirectedEnumerable(IDirectedEnumerable<T> directedenumerable)\r
121     {\r
122       this.directedenumerable = directedenumerable;\r
123     }\r
124 \r
125     public IDirectedEnumerable<V> Backwards()\r
126     {\r
127       MappedDirectedEnumerable<T, V> retval = (MappedDirectedEnumerable<T, V>)MemberwiseClone();\r
128       retval.directedenumerable = directedenumerable.Backwards();\r
129       return retval;\r
130       //If we made this classs non-abstract we could do\r
131       //return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;\r
132     }\r
133 \r
134 \r
135     public override SCG.IEnumerator<V> GetEnumerator()\r
136     {\r
137       foreach (T item in directedenumerable)\r
138         yield return Map(item);\r
139     }\r
140 \r
141     public EnumerationDirection Direction\r
142     {\r
143       get { return directedenumerable.Direction; }\r
144     }\r
145   }\r
146 }
147 #endif