-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoarray.h
183 lines (164 loc) · 2.83 KB
/
autoarray.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
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <new>
//Note: T must be a pointer type, or stuff will screw up. To make a pointer last longer than this object, assign NULL to it and it won't free the old one.
template<typename T> class autoptr {
T ptr;
public:
operator T() const
{
return ptr;
}
autoptr& operator=(T ptr_)
{
ptr=ptr_;
return *this;
}
autoptr()
{
ptr=NULL;
}
autoptr(T ptr_)
{
ptr=ptr_;
}
~autoptr()
{
if (ptr) free((void*)ptr);
}
};
template<typename T> class autoarray {
public:
int count;
private:
T* ptr;
int bufferlen;
T dummy;
T& getconst(int id)
{
if (id<0) return dummy;
if (id>=count) return dummy;
return ptr[id];
}
T& get(int id)
{
if (id<0) return dummy;
if (id>=bufferlen-4)
{
int oldlen=bufferlen;
while (bufferlen<id+4) bufferlen*=2;
ptr=(T*)realloc(ptr, sizeof(T)*bufferlen);
memset(ptr+oldlen, 0, (bufferlen-oldlen)*sizeof(T));
}
if (id>=count)
{
for (int i=count;i<=id;i++) new(ptr+i) T;
count=id+1;
}
return ptr[id];
}
public:
void reset(int keep=0)
{
if (keep>=count) return;
for (int i=keep;i<count;i++) ptr[i].~T();
memset(ptr+keep, 0, (count-keep)*sizeof(T));
if (keep<bufferlen/2)
{
while (keep<bufferlen/2 && bufferlen>4) bufferlen/=2;
ptr=(T*)realloc(ptr, sizeof(T)*bufferlen);
}
count=keep;
}
T& operator[](int id)
{
return get(id);
}
T& operator[](int id) const
{
return getconst(id);
}
operator T*()
{
return ptr;
}
operator const T*() const
{
return ptr;
}
void append(const T& item)
{
get(count)=item;
}
void insert(int pos)
{
if (pos<0 || pos>count) return;
if (count>=bufferlen-4)
{
int oldlen=bufferlen;
while (bufferlen<count+4) bufferlen*=2;
ptr=(T*)realloc(ptr, sizeof(T)*bufferlen);
memset(ptr+oldlen, 0, (bufferlen-oldlen)*sizeof(T));
}
memmove(ptr+pos+1, ptr+pos, sizeof(T)*(count-pos));
memset(ptr+pos, 0, sizeof(T));
new(ptr+pos) T;
count++;
}
void insert(int pos, const T& item)
{
if (pos<0 || pos>count) return;
if (count>=bufferlen-4)
{
int oldlen=bufferlen;
while (bufferlen<count+4) bufferlen*=2;
ptr=(T*)realloc(ptr, sizeof(T)*bufferlen);
memset(ptr+oldlen, 0, (bufferlen-oldlen)*sizeof(T));
}
memmove(ptr+pos+1, ptr+pos, sizeof(T)*(count-pos));
memset(ptr+pos, 0, sizeof(T));
new(ptr+pos) T;
ptr[pos]=item;
count++;
}
void remove(int id)
{
if (id<0 || id>=count) return;
count--;
ptr[id].~T();
memmove(ptr+id, ptr+id+1, sizeof(T)*(count-id));
if (count<bufferlen/2)
{
bufferlen/=2;
ptr=(T*)realloc(ptr, sizeof(T)*bufferlen);
}
}
autoarray()
{
ptr=(T*)malloc(sizeof(T)*1);
memset(ptr, 0, sizeof(T));
bufferlen=1;
count=0;
}
~autoarray()
{
for (int i=0;i<count;i++) ptr[i].~T();
free(ptr);
}
#ifdef SERIALIZER
void serialize(serializer& s)
{
if (s.serializing) s(count);
else
{
int i;
s(i);
get(i-1);
}
for (int i=0;i<count;i++) s(ptr[i]);
}
#endif
#define SERIALIZER_BANNED
};