Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / C5 / MappedEnumerators.cs @ 3

1
/*
2
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4
 of this software and associated documentation files (the "Software"), to deal
5
 in the Software without restriction, including without limitation the rights
6
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
 copies of the Software, and to permit persons to whom the Software is
8
 furnished to do so, subject to the following conditions:
9
 
10
 The above copyright notice and this permission notice shall be included in
11
 all copies or substantial portions of the Software.
12
 
13
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
 SOFTWARE.
20
*/
21

    
22
using System;
23
using System.Diagnostics;
24
using SCG = System.Collections.Generic;
25
namespace C5
26
{
27
  abstract class MappedDirectedCollectionValue<T, V> : DirectedCollectionValueBase<V>, IDirectedCollectionValue<V>
28
  {
29
    IDirectedCollectionValue<T> directedcollectionvalue;
30

    
31
    abstract public V Map(T item);
32

    
33
    public MappedDirectedCollectionValue(IDirectedCollectionValue<T> directedcollectionvalue)
34
    {
35
      this.directedcollectionvalue = directedcollectionvalue;
36
    }
37

    
38
    public override V Choose() { return Map(directedcollectionvalue.Choose()); }
39

    
40
    public override bool IsEmpty { get { return directedcollectionvalue.IsEmpty; } }
41

    
42
    public override int Count { get { return directedcollectionvalue.Count; } }
43

    
44
    public override Speed CountSpeed { get { return directedcollectionvalue.CountSpeed; } }
45

    
46
    public override IDirectedCollectionValue<V> Backwards()
47
    {
48
      MappedDirectedCollectionValue<T, V> retval = (MappedDirectedCollectionValue<T, V>)MemberwiseClone();
49
      retval.directedcollectionvalue = directedcollectionvalue.Backwards();
50
      return retval;
51
      //If we made this classs non-abstract we could do
52
      //return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;
53
    }
54

    
55

    
56
    public override SCG.IEnumerator<V> GetEnumerator()
57
    {
58
      foreach (T item in directedcollectionvalue)
59
        yield return Map(item);
60
    }
61

    
62
    public override EnumerationDirection Direction
63
    {
64
      get { return directedcollectionvalue.Direction; }
65
    }
66

    
67
    IDirectedEnumerable<V> IDirectedEnumerable<V>.Backwards()
68
    {
69
      return Backwards();
70
    }
71

    
72

    
73
  }
74

    
75
  abstract class MappedCollectionValue<T, V> : CollectionValueBase<V>, ICollectionValue<V>
76
  {
77
    ICollectionValue<T> collectionvalue;
78

    
79
    abstract public V Map(T item);
80

    
81
    public MappedCollectionValue(ICollectionValue<T> collectionvalue)
82
    {
83
      this.collectionvalue = collectionvalue;
84
    }
85

    
86
    public override V Choose() { return Map(collectionvalue.Choose()); }
87

    
88
    public override bool IsEmpty { get { return collectionvalue.IsEmpty; } }
89

    
90
    public override int Count { get { return collectionvalue.Count; } }
91

    
92
    public override Speed CountSpeed { get { return collectionvalue.CountSpeed; } }
93

    
94
    public override SCG.IEnumerator<V> GetEnumerator()
95
    {
96
      foreach (T item in collectionvalue)
97
        yield return Map(item);
98
    }
99
  }
100
  
101
  class MultiplicityOne<K> : MappedCollectionValue<K, KeyValuePair<K, int>>
102
  {
103
    public MultiplicityOne(ICollectionValue<K> coll):base(coll) { }
104
    public override KeyValuePair<K, int> Map(K k) { return new KeyValuePair<K, int>(k, 1); }
105
  }
106

    
107
  class DropMultiplicity<K> : MappedCollectionValue<KeyValuePair<K, int>, K>
108
  {
109
    public DropMultiplicity(ICollectionValue<KeyValuePair<K, int>> coll):base(coll) { }
110
    public override K Map(KeyValuePair<K, int> kvp) { return kvp.Key; }
111
  }
112
  
113
  abstract class MappedDirectedEnumerable<T, V> : EnumerableBase<V>, IDirectedEnumerable<V>
114
  {
115
    IDirectedEnumerable<T> directedenumerable;
116

    
117
    abstract public V Map(T item);
118

    
119
    public MappedDirectedEnumerable(IDirectedEnumerable<T> directedenumerable)
120
    {
121
      this.directedenumerable = directedenumerable;
122
    }
123

    
124
    public IDirectedEnumerable<V> Backwards()
125
    {
126
      MappedDirectedEnumerable<T, V> retval = (MappedDirectedEnumerable<T, V>)MemberwiseClone();
127
      retval.directedenumerable = directedenumerable.Backwards();
128
      return retval;
129
      //If we made this classs non-abstract we could do
130
      //return new MappedDirectedCollectionValue<T,V>(directedcollectionvalue.Backwards());;
131
    }
132

    
133

    
134
    public override SCG.IEnumerator<V> GetEnumerator()
135
    {
136
      foreach (T item in directedenumerable)
137
        yield return Map(item);
138
    }
139

    
140
    public EnumerationDirection Direction
141
    {
142
      get { return directedenumerable.Direction; }
143
    }
144
  }
145
}