1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
22
namespace ICSharpCode.NRefactory.Utils
24
public sealed class ProjectedList<TInput, TOutput> : IList<TOutput> where TOutput : class
26
readonly IList<TInput> input;
27
readonly Func<TInput, TOutput> projection;
28
readonly TOutput[] items;
30
public ProjectedList(IList<TInput> input, Func<TInput, TOutput> projection)
33
throw new ArgumentNullException("input");
34
if (projection == null)
35
throw new ArgumentNullException("projection");
37
this.projection = projection;
38
this.items = new TOutput[input.Count];
41
public TOutput this[int index] {
43
TOutput output = LazyInit.VolatileRead(ref items[index]);
47
return LazyInit.GetOrSet(ref items[index], projection(input[index]));
51
TOutput IList<TOutput>.this[int index] {
52
get { return this[index]; }
54
throw new NotSupportedException();
59
get { return items.Length; }
62
bool ICollection<TOutput>.IsReadOnly {
66
int IList<TOutput>.IndexOf(TOutput item)
68
var comparer = EqualityComparer<TOutput>.Default;
69
for (int i = 0; i < this.Count; i++) {
70
if (comparer.Equals(this[i], item))
76
void IList<TOutput>.Insert(int index, TOutput item)
78
throw new NotSupportedException();
81
void IList<TOutput>.RemoveAt(int index)
83
throw new NotSupportedException();
86
void ICollection<TOutput>.Add(TOutput item)
88
throw new NotSupportedException();
91
void ICollection<TOutput>.Clear()
93
throw new NotSupportedException();
96
bool ICollection<TOutput>.Contains(TOutput item)
98
var comparer = EqualityComparer<TOutput>.Default;
99
for (int i = 0; i < this.Count; i++) {
100
if (comparer.Equals(this[i], item))
106
void ICollection<TOutput>.CopyTo(TOutput[] array, int arrayIndex)
108
for (int i = 0; i < items.Length; i++) {
109
array[arrayIndex + i] = this[i];
113
bool ICollection<TOutput>.Remove(TOutput item)
115
throw new NotSupportedException();
118
public IEnumerator<TOutput> GetEnumerator()
120
for (int i = 0; i < this.Count; i++) {
121
yield return this[i];
125
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
127
return GetEnumerator();
131
public sealed class ProjectedList<TContext, TInput, TOutput> : IList<TOutput> where TOutput : class
133
readonly IList<TInput> input;
134
readonly TContext context;
135
readonly Func<TContext, TInput, TOutput> projection;
136
readonly TOutput[] items;
138
public ProjectedList(TContext context, IList<TInput> input, Func<TContext, TInput, TOutput> projection)
141
throw new ArgumentNullException("input");
142
if (projection == null)
143
throw new ArgumentNullException("projection");
145
this.context = context;
146
this.projection = projection;
147
this.items = new TOutput[input.Count];
150
public TOutput this[int index] {
152
TOutput output = LazyInit.VolatileRead(ref items[index]);
153
if (output != null) {
156
return LazyInit.GetOrSet(ref items[index], projection(context, input[index]));
160
TOutput IList<TOutput>.this[int index] {
161
get { return this[index]; }
163
throw new NotSupportedException();
168
get { return items.Length; }
171
bool ICollection<TOutput>.IsReadOnly {
175
int IList<TOutput>.IndexOf(TOutput item)
177
var comparer = EqualityComparer<TOutput>.Default;
178
for (int i = 0; i < this.Count; i++) {
179
if (comparer.Equals(this[i], item))
185
void IList<TOutput>.Insert(int index, TOutput item)
187
throw new NotSupportedException();
190
void IList<TOutput>.RemoveAt(int index)
192
throw new NotSupportedException();
195
void ICollection<TOutput>.Add(TOutput item)
197
throw new NotSupportedException();
200
void ICollection<TOutput>.Clear()
202
throw new NotSupportedException();
205
bool ICollection<TOutput>.Contains(TOutput item)
207
var comparer = EqualityComparer<TOutput>.Default;
208
for (int i = 0; i < this.Count; i++) {
209
if (comparer.Equals(this[i], item))
215
void ICollection<TOutput>.CopyTo(TOutput[] array, int arrayIndex)
217
for (int i = 0; i < items.Length; i++) {
218
array[arrayIndex + i] = this[i];
222
bool ICollection<TOutput>.Remove(TOutput item)
224
throw new NotSupportedException();
227
public IEnumerator<TOutput> GetEnumerator()
229
for (int i = 0; i < this.Count; i++) {
230
yield return this[i];
234
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
236
return GetEnumerator();