-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayAmortizer.cs
92 lines (77 loc) · 2.06 KB
/
ArrayAmortizer.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mianen.DataStructures
{
public class ArrayAmortizer<T>
{
int LeftMemo;
int RightMemo;
int[] Lenghts;
public int Length { get => Data.Length * ChunkSize; }
public int ChunkSize { get; private set; }
public int ChunkCount { get => Data.Length; }
public bool IsReadOnly { get => false; }
T[][] Data;
public T this[int index]
{
get => this.Data[index / ChunkSize][index % ChunkSize];
set => this.Data[index / ChunkSize][index % ChunkSize] = value;
}
public ArrayAmortizer()
{
this.ChunkSize = 64;
this.Data = new T[2][];
this.Data[0] = new T[ChunkSize];
this.Data[1] = new T[ChunkSize];
}
private ArrayAmortizer(int ChunkCount, int ChunkSize)
{
this.ChunkSize = ChunkSize;
this.Data = new T[ChunkCount][];
for (int i = 0; i < ChunkCount; i++)
{
this.Data[i] = new T[ChunkSize];
}
}
public void Expand(int Begin, int End, out int newBegin, out int NewEnd)
{
newBegin = Begin + (ChunkCount / 2) * ChunkSize;
NewEnd = End + (ChunkCount / 2) * ChunkSize;
T[][] newData = new T[ChunkCount * 2][];
for (int i = 0; i < ChunkCount / 2; i++)
{
newData[i] = new T[ChunkSize];
}
for (int i = 0; i < ChunkCount; i++)
{
newData[i + ChunkCount / 2] = this.Data[i];
}
for (int i = ChunkCount + ChunkCount / 2; i < newData.Length; i++)
{
newData[i] = new T[ChunkSize];
}
this.Data = newData;
}
public bool Contains(T item)
{
for (int i = 0; i < ChunkCount; i++)
{
if (this.Data[i].Contains(item))
return true;
}
return false;
}
public static ArrayAmortizer<T> GetCopy(ArrayAmortizer<T> Sourse, int Index, int Length)
{
ArrayAmortizer<T> newMemo = new ArrayAmortizer<T>(Sourse.ChunkCount, Sourse.ChunkSize);
for (int i = 0; i < Length; i++)
{
newMemo[Index + i] = Sourse[Index + i];
}
return newMemo;
}
}
}