forked from tslab-hub/handlers
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConstList.cs
More file actions
88 lines (71 loc) · 2.02 KB
/
ConstList.cs
File metadata and controls
88 lines (71 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections;
using System.Collections.Generic;
namespace TSLab.Script.Handlers
{
internal sealed class ConstList<T> : IList<T>, IReadOnlyList<T>
{
private static readonly IEqualityComparer<T> s_equalityComparer = EqualityComparer<T>.Default;
private readonly T m_value;
public ConstList(int count, T value)
{
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count));
Count = count;
m_value = value;
}
public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i < Count; i++)
yield return m_value;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(T item)
{
return Count > 0 && s_equalityComparer.Equals(m_value, item);
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotSupportedException();
}
public bool Remove(T item)
{
throw new NotSupportedException();
}
public int Count { get; }
public bool IsReadOnly => true;
public int IndexOf(T item)
{
return Contains(item) ? 0 : -1;
}
public void Insert(int index, T item)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
public T this[int index]
{
get
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException(nameof(index));
return m_value;
}
set { throw new NotSupportedException(); }
}
}
}