| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
namespace System.Reactive |
| 4 |
{ |
| 5 |
class ImmutableList<T> |
| 6 |
{ |
| 7 |
T[] data; |
| 8 |
|
| 9 |
public ImmutableList() |
| 10 |
{ |
| 11 |
data = new T[0]; |
| 12 |
} |
| 13 |
|
| 14 |
public ImmutableList(T[] data) |
| 15 |
{ |
| 16 |
this.data = data; |
| 17 |
} |
| 18 |
|
| 19 |
public ImmutableList<T> Add(T value) |
| 20 |
{ |
| 21 |
var newData = new T[data.Length + 1]; |
| 22 |
Array.Copy(data, newData, data.Length); |
| 23 |
newData[data.Length] = value; |
| 24 |
return new ImmutableList<T>(newData); |
| 25 |
} |
| 26 |
|
| 27 |
public ImmutableList<T> Remove(T value) |
| 28 |
{ |
| 29 |
var i = IndexOf(value); |
| 30 |
if (i < 0) |
| 31 |
return this; |
| 32 |
var newData = new T[data.Length - 1]; |
| 33 |
Array.Copy(data, 0, newData, 0, i); |
| 34 |
Array.Copy(data, i + 1, newData, i, data.Length - i - 1); |
| 35 |
return new ImmutableList<T>(newData); |
| 36 |
} |
| 37 |
|
| 38 |
public int IndexOf(T value) |
| 39 |
{ |
| 40 |
for (var i = 0; i < data.Length; ++i) |
| 41 |
if (data[i].Equals(value)) |
| 42 |
return i; |
| 43 |
return -1; |
| 44 |
} |
| 45 |
|
| 46 |
public T[] Data |
| 47 |
{ |
| 48 |
get { return data; } |
| 49 |
} |
| 50 |
} |
| 51 |
} |