-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathGrowingList.cs
More file actions
67 lines (52 loc) · 963 Bytes
/
GrowingList.cs
File metadata and controls
67 lines (52 loc) · 963 Bytes
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
using System.Collections.Generic;
using System.Diagnostics.Contracts;
namespace ReClassNET.Util
{
public class GrowingList<T>
{
private readonly List<T> list;
public T DefaultValue { get; set; }
public int Count => list.Count;
public GrowingList()
{
Contract.Ensures(list != null);
list = new List<T>();
}
public GrowingList(T defaultValue)
: this()
{
DefaultValue = defaultValue;
}
private void GrowToSize(int size)
{
list.Capacity = size;
for (var i = list.Count; i <= size; ++i)
{
list.Add(DefaultValue);
}
}
private void CheckIndex(int index)
{
Contract.Requires(index >= 0);
if (index >= list.Count)
{
GrowToSize(index);
}
}
public T this[int index]
{
get
{
Contract.Requires(index >= 0);
CheckIndex(index);
return list[index];
}
set
{
Contract.Requires(index >= 0);
CheckIndex(index);
list[index] = value;
}
}
}
}