-
Notifications
You must be signed in to change notification settings - Fork 0
/
arx_array.h
148 lines (124 loc) · 2.15 KB
/
arx_array.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
#ifndef ARX_ARRAY_H
#define ARX_ARRAY_H
#include <stdlib.h>
#include <new>
#include <stdexcept>
namespace arx {
template <typename T>
class Array {
T* d;
int dsize;
int msize;
protected:
int AlignedSize( int sz){
int ret = 1;
while( ret < sz ){
ret *= 2;
}
return ret;
}
void BufferCheck( int sz ){
int szq = sizeof(T) * sz;
if( d ){
if( szq > msize){
szq = this->AlignedSize( szq );
T* t = static_cast< T*>( malloc( szq ));
for( T* i=d, *j=t, *e=d+dsize; i<e; i++,j++){
new ((void*)j) T(*i);
i->~T();
}
free( d );
d = t;
msize = szq;
}
} else {
msize = this->AlignedSize( szq );
d = static_cast<T*>( malloc( msize ) );
}
}
void Construct( T* a, T* b ){
while( a < b ){
new ((void*)a) T();
a++;
}
}
void Construct( T* a, T* b, const T& t ){
while( a < b ){
new ((void*)a) T(t);
a++;
}
}
void Destruct( T* a, T* b ){
while( a < b ){
a->~T();
a++;
}
}
void Move( T* src, T* dst, int size ){
}
public:
typedef T* iterator;
typedef const T* const_iterator;
Array(){
d = 0;
dsize = msize = 0;
}
~Array(){
if( d ) {
this->Destruct( d, d + dsize );
free( d);
d = 0;
dsize = msize = 0;
}
}
void push_back( const T& dt ){
this->BufferCheck( dsize + 1 );
new ( (void*)(d + dsize) ) T( dt);
dsize++;
}
T& back() const {
return d[ dsize - 1];
}
T& operator[]( int i ) const {
return d[ i];
}
T& at( int i ) const {
if( i > dsize ) throw std::out_of_range("index out of range.");
return d[ i];
}
bool empty() const {
return dsize - 1;
}
void clear(){
T* i = d, *e = d + dsize;
while( i < e ) {
i->~T();
i++;
}
dsize = 0;
}
int size() const {
return dsize;
}
iterator begin() const {
return d;
}
iterator end() const {
return d + dsize;
}
int capacity() const {
return msize / sizeof(T);
}
void resize( const T& a, int size ){
if( size > dsize ){
this->BufferCheck( size );
this->Construct( d + dsize, d + size, a);
dsize = size;
} else if( size > 0 && size != dsize ){
this->Destruct( d + size, d + dsize );
dsize = size;
}
}
};
}
#endif