-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIgushArray.cs
227 lines (206 loc) · 4.83 KB
/
IgushArray.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
/*
--------------------------------------------------------------------------------
Declarations of original IgushArray realization
--------------------------------------------------------------------------------
http://igushev.ru/papers/igusharray/
Эдуард Игушев
IgushArray (быстрый массив)
--------------------------------------------------------------------------------
Загрузка. Гарантия и лицензия
Реализация поставляется “как есть”.
Любое некоммерческое и коммерческое использование разрешено.
Сохранение оригинального имени и ссылку на источник обязательно.
Любой отзыв желателен :-)
------------------------------------------------------------------------------
*/
public class IgushArray<T>
{
public class Block
{
public int count = 0;
private readonly int size;
private readonly T[] array;
private int offset = 0;
public Block(int size)
{
this.size = size;
array = new T[size];
}
public T Insert(int index, T value)
{
if (count < size)
count++;
T removed = array[(size + offset - 1) % size];
int i = (index + offset) % size;
if (i < offset)
{
Array.Copy(array, i, array, i + 1, offset - i - 1);
}
else
{
T last = array[size - 1];
Array.Copy(array, i, array, i + 1, size - i - 1);
if (offset > 0)
{
Array.Copy(array, 0, array, 1, offset - 1);
array[0] = last;
}
}
array[i] = value;
return removed;
}
public T Insert(T value)
{
if (count < size)
count++;
offset = (size + offset - 1) % size;
T removed = array[offset];
array[offset] = value;
return removed;
}
public void Remove(int index)
{
count--;
int i = (index + offset) % size;
if (i < offset)
{
Array.Copy(array, i + 1, array, i, offset - i - 1);
}
else
{
T last = array[0];
Array.Copy(array, i + 1, array, i, size - i - 1);
if (offset > 0)
{
Array.Copy(array, 1, array, 0, offset - 1);
array[size - 1] = last;
}
}
array[(size + offset - 1) % size] = default(T);
}
public void Remove()
{
count--;
array[offset] = default(T);
offset = (size + offset + 1) % size;
}
public T this[int index]
{
get { return array[(index + offset) % size]; }
set { array[(index + offset) % size] = value; }
}
public T[] ToArray()
{
T[] array = new T[count];
for (int i = 0; i < count; i++)
{
array[i] = this[i];
}
return array;
}
}
private int blockSize;
private int blocksCount;
private Block[] blocks;
public IgushArray(int blockSize)
{
this.blockSize = blockSize;
blocksCount = 0;
blocks = new Block[4];
}
private int count = 0;
public int Count { get { return count; } }
public T this[int index]
{
get
{
int i = index / blockSize;
return blocks[i][index - i * blockSize];
}
set
{
int i = index / blockSize;
blocks[i][index - i * blockSize] = value;
}
}
public void Insert(int index, T value)
{
count++;
int i = index / blockSize;
int j = index - i * blockSize;
if ((count - 1) / blockSize >= blocksCount)
{
AllocateBlocks(blocksCount + 1);
Block block = new Block(blockSize);
blocks[blocksCount - 1] = block;
}
T last = blocks[i].Insert(j, value);
for (; i < blocksCount - 1; i++)
{
Block blockI = blocks[i + 1];
last = blockI.Insert(last);
}
}
public void RemoveAt(int index)
{
count--;
int i = index / blockSize;
int j = index - i * blockSize;
blocks[i].Remove(j);
for (int ii = i + 1; ii < blocksCount; ii++)
{
blocks[ii - 1][blockSize - 1] = blocks[ii][0];
blocks[ii].Remove();
}
if (count % blockSize == 0)
{
blocksCount--;
if (blocksCount <= blocks.Length / 4 && blocks.Length > 4)
{
Block[] newBlocks = new Block[blocks.Length / 2];
Array.Copy(blocks, 0, newBlocks, 0, newBlocks.Length);
blocks = newBlocks;
}
}
}
public void Add(T value)
{
int i = count / blockSize;
int j = count - i * blockSize;
count++;
Block block;
if (i >= blocksCount)
{
AllocateBlocks(blocksCount + 1);
block = new Block(blockSize);
blocks[blocksCount - 1] = block;
}
else
{
block = blocks[i];
}
block[j] = value;
}
private void AllocateBlocks(int blocksCount)
{
this.blocksCount = blocksCount;
if (blocksCount > blocks.Length)
{
Block[] newBlocks = new Block[blocks.Length * 2];
Array.Copy(blocks, 0, newBlocks, 0, blocks.Length);
blocks = newBlocks;
}
}
public T[] ToArray()
{
T[] array = new T[count];
for (int i = 0; i < count; i++)
{
array[i] = this[i];
}
return array;
}
public int BlocksCapacity { get { return blocks.Length; } }
public int BlocksCount { get { return blocksCount; } }
}