Bringing C5 1.0 into the main branch.
[mono.git] / mcs / class / Mono.C5 / C5 / MappedEnumerators.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 \r
22 using System;\r
23 using System.Diagnostics;\r
24 using SCG = System.Collections.Generic;\r
25 namespace C5\r
26 {\r
27   abstract class MappedDirectedCollectionValue<T, V> : DirectedCollectionValueBase<V>, IDirectedCollectionValue<V>\r
28   {\r
29     IDirectedCollectionValue<T> directedcollectionvalue;\r
30 \r
31     abstract public V Map(T item);\r
32 \r
33     public MappedDirectedCollectionValue(IDirectedCollectionValue<T> directedcollectionvalue)\r
34     {\r
35       this.directedcollectionvalue = directedcollectionvalue;\r
36     }\r
37 \r
38     public override V Choose() { return Map(directedcollectionvalue.Choose()); }\r
39 \r
40     public override bool IsEmpty { get { return directedcollectionvalue.IsEmpty; } }\r
41 \r
42     public override int Count { get { return directedcollectionvalue.Count; } }\r
43 \r
44     public override Speed CountSpeed { get { return directedcollectionvalue.CountSpeed; } }\r
45 \r
46     public override IDirectedCollectionValue<V> Backwards()\r
47     {\r
48       MappedDirectedCollectionValue<T, V> retval = (MappedDirectedCollectionValue<T, V>)MemberwiseClone();\r
49       retval.directedcollectionvalue = directedcollectionvalue.Backwards();\r
50       return retval;\r
51       //If we made this classs non-abstract we could do\r
52       //return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;\r
53     }\r
54 \r
55 \r
56     public override SCG.IEnumerator<V> GetEnumerator()\r
57     {\r
58       foreach (T item in directedcollectionvalue)\r
59         yield return Map(item);\r
60     }\r
61 \r
62     public override EnumerationDirection Direction\r
63     {\r
64       get { return directedcollectionvalue.Direction; }\r
65     }\r
66 \r
67     IDirectedEnumerable<V> IDirectedEnumerable<V>.Backwards()\r
68     {\r
69       return Backwards();\r
70     }\r
71 \r
72 \r
73   }\r
74 \r
75   abstract class MappedCollectionValue<T, V> : CollectionValueBase<V>, ICollectionValue<V>\r
76   {\r
77     ICollectionValue<T> collectionvalue;\r
78 \r
79     abstract public V Map(T item);\r
80 \r
81     public MappedCollectionValue(ICollectionValue<T> collectionvalue)\r
82     {\r
83       this.collectionvalue = collectionvalue;\r
84     }\r
85 \r
86     public override V Choose() { return Map(collectionvalue.Choose()); }\r
87 \r
88     public override bool IsEmpty { get { return collectionvalue.IsEmpty; } }\r
89 \r
90     public override int Count { get { return collectionvalue.Count; } }\r
91 \r
92     public override Speed CountSpeed { get { return collectionvalue.CountSpeed; } }\r
93 \r
94     public override SCG.IEnumerator<V> GetEnumerator()\r
95     {\r
96       foreach (T item in collectionvalue)\r
97         yield return Map(item);\r
98     }\r
99   }\r
100   \r
101   class MultiplicityOne<K> : MappedCollectionValue<K, KeyValuePair<K, int>>\r
102   {\r
103     public MultiplicityOne(ICollectionValue<K> coll):base(coll) { }\r
104     public override KeyValuePair<K, int> Map(K k) { return new KeyValuePair<K, int>(k, 1); }\r
105   }\r
106 \r
107   class DropMultiplicity<K> : MappedCollectionValue<KeyValuePair<K, int>, K>\r
108   {\r
109     public DropMultiplicity(ICollectionValue<KeyValuePair<K, int>> coll):base(coll) { }\r
110     public override K Map(KeyValuePair<K, int> kvp) { return kvp.Key; }\r
111   }\r
112   \r
113   abstract class MappedDirectedEnumerable<T, V> : EnumerableBase<V>, IDirectedEnumerable<V>\r
114   {\r
115     IDirectedEnumerable<T> directedenumerable;\r
116 \r
117     abstract public V Map(T item);\r
118 \r
119     public MappedDirectedEnumerable(IDirectedEnumerable<T> directedenumerable)\r
120     {\r
121       this.directedenumerable = directedenumerable;\r
122     }\r
123 \r
124     public IDirectedEnumerable<V> Backwards()\r
125     {\r
126       MappedDirectedEnumerable<T, V> retval = (MappedDirectedEnumerable<T, V>)MemberwiseClone();\r
127       retval.directedenumerable = directedenumerable.Backwards();\r
128       return retval;\r
129       //If we made this classs non-abstract we could do\r
130       //return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;\r
131     }\r
132 \r
133 \r
134     public override SCG.IEnumerator<V> GetEnumerator()\r
135     {\r
136       foreach (T item in directedenumerable)\r
137         yield return Map(item);\r
138     }\r
139 \r
140     public EnumerationDirection Direction\r
141     {\r
142       get { return directedenumerable.Direction; }\r
143     }\r
144   }\r
145 }