-
Notifications
You must be signed in to change notification settings - Fork 16
/
list.h
183 lines (140 loc) · 3.24 KB
/
list.h
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
#ifndef _SRC_SORTED_LIST_H_
#define _SRC_SORTED_LIST_H_
namespace candor {
namespace internal {
template <class T, class Policy, class Allocator>
class SortableList {
public:
SortableList(int size) : map_(NULL), size_(0), grow_(size), len_(0) {
Grow();
}
~SortableList() {
delete[] map_;
map_ = NULL;
}
inline T* At(int i) {
if (i < 0 || i >= len_) return NULL;
return map_[i];
}
inline void RemoveAt(int i) {
if (i < 0 || i >= len_) return;
// Shift left
len_--;
for (int j = i; j < len_; j++) {
map_[j] = map_[j + 1];
}
}
inline void Push(T* item) {
if (len_ == size_) Grow();
map_[len_++] = item;
}
inline void Unshift(T* item) {
if (len_ == size_) Grow();
// Shift right
len_++;
for (int i = len_ - 1; i > 0; i--) {
map_[i] = map_[i - 1];
}
map_[0] = item;
}
inline T* Pop() {
if (len_ == 0) return NULL;
return map_[--len_];
}
inline T* Shift() {
if (len_ == 0) return NULL;
T* res = map_[0];
// Shift left
len_--;
for (int i = 0; i < len_; i++) {
map_[i] = map_[i + 1];
}
return res;
}
template <class Shape>
inline void Sort() {
QuickSort<Shape>(0, length());
}
template <class Shape>
inline void InsertSorted(T* value) {
if (len_ == 0) {
Push(value);
return;
}
if (len_ == size_) Grow();
// Perform binary search for correct position
int middle_pos;
int cmp;
for (int i = 0, j = length() - 1; i <= j; ) {
middle_pos = (i + j) >> 1;
T* middle = map_[middle_pos];
cmp = Shape::Compare(value, middle);
if (cmp < 0) {
j = middle_pos - 1;
} else if (cmp > 0) {
i = middle_pos + 1;
} else {
break;
}
}
int insert_pos;
if (cmp <= 0) {
insert_pos = middle_pos;
} else {
insert_pos = middle_pos + 1;
}
len_++;
assert(insert_pos >= 0 && insert_pos < len_);
for (int i = len_ - 1; i > insert_pos; i--) {
map_[i] = map_[i - 1];
}
map_[insert_pos] = value;
}
inline T* head() { return At(0); }
inline T* tail() { return At(length() - 1); }
inline int length() { return len_; }
protected:
inline void Grow() {
// Allocate new map
int new_size = size_ + grow_;
T** new_map = new T*[new_size];
// Copy old entries
if (map_ != NULL) memcpy(new_map, map_, sizeof(*new_map) * size_);
// Replace map
delete[] map_;
map_ = new_map;
size_ = new_size;
}
template <class Shape>
inline void QuickSort(int start, int end) {
if (start + 1 >= end) return;
int marker_pos = (start + end) >> 1;
T* marker = map_[marker_pos];
int i = start;
int j = end - 1;
do {
while (Shape::Compare(map_[i], marker) < 0) {
i++;
}
while (Shape::Compare(marker, map_[j]) < 0) {
j--;
}
// Swap them out!
if (i < j) {
T* tmp = map_[i];
map_[i++] = map_[j];
map_[j--] = tmp;
}
} while (i < j);
// Sort lesser halves
QuickSort<Shape>(start, i);
QuickSort<Shape>(j, end);
}
T** map_;
int size_;
int grow_;
int len_;
};
} // namespace internal
} // namespace candor
#endif // _SRC_SORTED_LIST_H_