Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.13/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/Utils/ProjectedList.cs @ 15164

Last change on this file since 15164 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 6.0 KB
Line 
1// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21
22namespace ICSharpCode.NRefactory.Utils
23{
24  public sealed class ProjectedList<TInput, TOutput> : IList<TOutput> where TOutput : class
25  {
26    readonly IList<TInput> input;
27    readonly Func<TInput, TOutput> projection;
28    readonly TOutput[] items;
29   
30    public ProjectedList(IList<TInput> input, Func<TInput, TOutput> projection)
31    {
32      if (input == null)
33        throw new ArgumentNullException("input");
34      if (projection == null)
35        throw new ArgumentNullException("projection");
36      this.input = input;
37      this.projection = projection;
38      this.items = new TOutput[input.Count];
39    }
40   
41    public TOutput this[int index] {
42      get {
43        TOutput output = LazyInit.VolatileRead(ref items[index]);
44        if (output != null) {
45          return output;
46        }
47        return LazyInit.GetOrSet(ref items[index], projection(input[index]));
48      }
49    }
50   
51    TOutput IList<TOutput>.this[int index] {
52      get { return this[index]; }
53      set {
54        throw new NotSupportedException();
55      }
56    }
57   
58    public int Count {
59      get { return items.Length; }
60    }
61   
62    bool ICollection<TOutput>.IsReadOnly {
63      get { return true; }
64    }
65   
66    int IList<TOutput>.IndexOf(TOutput item)
67    {
68      var comparer = EqualityComparer<TOutput>.Default;
69      for (int i = 0; i < this.Count; i++) {
70        if (comparer.Equals(this[i], item))
71          return i;
72      }
73      return -1;
74    }
75   
76    void IList<TOutput>.Insert(int index, TOutput item)
77    {
78      throw new NotSupportedException();
79    }
80   
81    void IList<TOutput>.RemoveAt(int index)
82    {
83      throw new NotSupportedException();
84    }
85   
86    void ICollection<TOutput>.Add(TOutput item)
87    {
88      throw new NotSupportedException();
89    }
90   
91    void ICollection<TOutput>.Clear()
92    {
93      throw new NotSupportedException();
94    }
95   
96    bool ICollection<TOutput>.Contains(TOutput item)
97    {
98      var comparer = EqualityComparer<TOutput>.Default;
99      for (int i = 0; i < this.Count; i++) {
100        if (comparer.Equals(this[i], item))
101          return true;
102      }
103      return false;
104    }
105   
106    void ICollection<TOutput>.CopyTo(TOutput[] array, int arrayIndex)
107    {
108      for (int i = 0; i < items.Length; i++) {
109        array[arrayIndex + i] = this[i];
110      }
111    }
112   
113    bool ICollection<TOutput>.Remove(TOutput item)
114    {
115      throw new NotSupportedException();
116    }
117   
118    public IEnumerator<TOutput> GetEnumerator()
119    {
120      for (int i = 0; i < this.Count; i++) {
121        yield return this[i];
122      }
123    }
124   
125    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
126    {
127      return GetEnumerator();
128    }
129  }
130 
131  public sealed class ProjectedList<TContext, TInput, TOutput> : IList<TOutput> where TOutput : class
132  {
133    readonly IList<TInput> input;
134    readonly TContext context;
135    readonly Func<TContext, TInput, TOutput> projection;
136    readonly TOutput[] items;
137   
138    public ProjectedList(TContext context, IList<TInput> input, Func<TContext, TInput, TOutput> projection)
139    {
140      if (input == null)
141        throw new ArgumentNullException("input");
142      if (projection == null)
143        throw new ArgumentNullException("projection");
144      this.input = input;
145      this.context = context;
146      this.projection = projection;
147      this.items = new TOutput[input.Count];
148    }
149   
150    public TOutput this[int index] {
151      get {
152        TOutput output = LazyInit.VolatileRead(ref items[index]);
153        if (output != null) {
154          return output;
155        }
156        return LazyInit.GetOrSet(ref items[index], projection(context, input[index]));
157      }
158    }
159   
160    TOutput IList<TOutput>.this[int index] {
161      get { return this[index]; }
162      set {
163        throw new NotSupportedException();
164      }
165    }
166   
167    public int Count {
168      get { return items.Length; }
169    }
170   
171    bool ICollection<TOutput>.IsReadOnly {
172      get { return true; }
173    }
174   
175    int IList<TOutput>.IndexOf(TOutput item)
176    {
177      var comparer = EqualityComparer<TOutput>.Default;
178      for (int i = 0; i < this.Count; i++) {
179        if (comparer.Equals(this[i], item))
180          return i;
181      }
182      return -1;
183    }
184   
185    void IList<TOutput>.Insert(int index, TOutput item)
186    {
187      throw new NotSupportedException();
188    }
189   
190    void IList<TOutput>.RemoveAt(int index)
191    {
192      throw new NotSupportedException();
193    }
194   
195    void ICollection<TOutput>.Add(TOutput item)
196    {
197      throw new NotSupportedException();
198    }
199   
200    void ICollection<TOutput>.Clear()
201    {
202      throw new NotSupportedException();
203    }
204   
205    bool ICollection<TOutput>.Contains(TOutput item)
206    {
207      var comparer = EqualityComparer<TOutput>.Default;
208      for (int i = 0; i < this.Count; i++) {
209        if (comparer.Equals(this[i], item))
210          return true;
211      }
212      return false;
213    }
214   
215    void ICollection<TOutput>.CopyTo(TOutput[] array, int arrayIndex)
216    {
217      for (int i = 0; i < items.Length; i++) {
218        array[arrayIndex + i] = this[i];
219      }
220    }
221   
222    bool ICollection<TOutput>.Remove(TOutput item)
223    {
224      throw new NotSupportedException();
225    }
226   
227    public IEnumerator<TOutput> GetEnumerator()
228    {
229      for (int i = 0; i < this.Count; i++) {
230        yield return this[i];
231      }
232    }
233   
234    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
235    {
236      return GetEnumerator();
237    }
238  }
239}
Note: See TracBrowser for help on using the repository browser.