-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFastSorter.cs
More file actions
179 lines (151 loc) · 3.5 KB
/
FastSorter.cs
File metadata and controls
179 lines (151 loc) · 3.5 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
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
using UnityEngine;
using System.Collections;
using System;
namespace FastCollections
{
/// <summary>
/// List that sorts its elements, adds from the optimal end, and has O(1) popping.
/// </summary>
public class FastSorter<T>
{
public delegate int SortCompare<CompareT> (CompareT source, CompareT other);
private const int defaultCapacity = 4;
public int Count { get; private set; }
private int Capacity;
private T[] innerArray;
private int _offset;
private int Offset {
get {
return _offset;
}
set {
_offset = value;
}
}
public SortCompare<T> Comparer;
public FastSorter () {
_Init (CompareWithIComparable, defaultCapacity);
}
public FastSorter (SortCompare<T> comparer)
{
_Init (comparer, defaultCapacity);
}
private void _Init (SortCompare<T> comparer, int startCapacity)
{
Comparer = comparer;
Capacity = startCapacity;
innerArray = new T[startCapacity];
Offset = 0;
}
public void Add (T item)
{
if (Count > 0) {
int min = 0;
int max = Count;
int split = GetSplit (min, max);
while (min < max) {
int compare = Comparer (item, innerArray [GetIndex (split)]);
if (compare == 0) {
min = split + 1;
break;
} else if (compare > 0) {
min = split + 1;
} else {
max = split;
}
split = GetSplit (min, max);
}
Insert (item, min);
} else {
Insert (item, 0);
}
Count++;
}
public T PopMin ()
{
int index = GetIndex (0);
T ret = innerArray [index];
innerArray[index] = default(T);
Offset++;
Count--;
if (Count == 0)
Offset = 0;
return ret;
}
public T PeekMin () {
return innerArray[GetIndex(0)];
}
public T PopMax ()
{
int index = GetIndex (--Count);
T ret = innerArray [index];
innerArray[index] = default(T);
if (Count == 0)
Offset = 0;
return ret;
}
public T PeekMax () {
return innerArray[GetIndex (Count - 1)];
}
public void Clear (bool fast = true)
{
if (fast == false) {
Array.Clear (innerArray, GetIndex(0), Count);
}
Count = 0;
Offset = 0;
}
private int GetSplit (int min, int max)
{
return min + ((max - min) / 2);
}
private int GetIndex (int place)
{
return place + Offset;
}
private void Insert (T item, int place)
{
int index = GetIndex (place);
bool capped = Count + Offset >= Capacity;
bool forceLeftShift = false;
if (capped) {
if (forceLeftShift = Offset != 0) {
} else {
CheckCapacity (Count + Offset + 1);
}
}
if (Count > 0) {
int distanceToHead = Count - place;
int distanceToTail = place + 1;
if (forceLeftShift || Offset != 0 && (distanceToHead >= distanceToTail)) {
Shift (innerArray, Offset--, index, -1);
} else {
Shift (innerArray, index, Count + Offset, 1);
}
if (Offset == 0 || (!capped && distanceToHead < distanceToTail)) {
if (distanceToHead > 0) {
}
} else {
}
}
innerArray [GetIndex (place)] = item;
}
private void CheckCapacity (int min)
{
if (Capacity < min) {
Capacity *= 2;
if (Capacity < min)
Capacity = min;
Array.Resize (ref innerArray, Capacity);
}
}
private static void Shift (Array array, int min, int max, int shiftAmount) {
if (shiftAmount == 0) return;
Array.Copy (array, min, array, min + shiftAmount, max - min);
}
#region Default SortCompares
public static SortCompare<T> CompareWithIComparable =
(obj, other) => {return ((IComparable)obj).CompareTo (other);};
#endregion
}
}