-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsli_array.h
242 lines (188 loc) · 4.36 KB
/
sli_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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* tarrayobj.h
*
* This file is part of NEST
*
* Copyright (C) 2004 by
* The NEST Initiative
*
* See the file AUTHORS for details.
*
* Permission is granted to compile and modify
* this file for non-commercial use.
* See the file LICENSE for details.
*
*/
#ifndef TOKENARRAY_H
#define TOKENARRAY_H
/*
Array of Token
*/
#include <typeinfo>
#include <cstddef>
#include "sli_token.h"
#include "sli_allocator.h"
namespace sli3
{
#define ARRAY_ALLOC_SIZE 64
class Token;
class TokenArray
{
private:
static sli3::pool memory;
Token* p;
Token* begin_of_free_storage;
Token* end_of_free_storage;
unsigned int alloc_block_size;
unsigned int refs_;
void allocate(size_t, size_t, size_t, const Token & = Token());
public:
static size_t allocations;
TokenArray(void)
:p(NULL),begin_of_free_storage(NULL),
end_of_free_storage(NULL),
alloc_block_size(ARRAY_ALLOC_SIZE),
refs_(1)
{
++allocations;
}
TokenArray(size_t , const Token & = Token(), size_t = 0);
TokenArray(const TokenArray&);
virtual ~TokenArray();
/**
* This function returns a pointer to a copy of the token array with
* just one reference.
*/
TokenArray *detach()
{
if( refs_ ==1)
return this;
--refs_;
return new TokenArray(*this);
}
Token * begin() const
{
return p;
}
Token * end() const
{
return begin_of_free_storage;
}
size_t size(void) const
{
return (size_t)(begin_of_free_storage-p);
}
size_t capacity(void) const
{
return (size_t)(end_of_free_storage - p);
}
Token & operator[](size_t i) { return p[i]; }
const Token & operator[](size_t i) const
{ return p[i]; }
const Token & get(long i) const
{
return *(p+i);
// return p[i];
}
bool index_is_valid(size_t i) const
{
return ((p+i) < begin_of_free_storage);
}
void rotate(Token *, Token *, Token *);
// Memory allocation
bool shrink(void);
bool reserve(size_t);
unsigned int references(void)
{
return refs_;
}
unsigned int remove_reference()
{
--refs_;
if(refs_==0)
{
delete this;
return 0;
}
return refs_;
}
unsigned int add_reference()
{
return ++refs_;
}
void resize(size_t, size_t, const Token & = Token());
void resize(size_t, const Token & = Token());
void reserve_token(int n)
{
if((end_of_free_storage-begin_of_free_storage) < n)
reserve(size()+n);
}
// Insertion, deletion
void push_back(const Token &t)
{
reserve_token(1);
(begin_of_free_storage++)->init(t);
}
void pop_back(void)
{
(--begin_of_free_storage)->clear();
}
// Erase the range given by the iterators.
void erase(size_t , size_t);
void erase(Token *, Token *);
void erase(Token *tp)
{
erase(tp,tp+1);
}
// Reduce the array to the range given by the iterators
void reduce(Token *, Token *);
void reduce(size_t, size_t);
/**
Insert n tokens, starting at position i.
*/
void insert(size_t i, size_t n= 1, const Token& = Token());
void insert(size_t i, const Token& t)
{
insert(i,1,t);
}
void insert(size_t, TokenArray const &);
/**
* Assign sub-array.
*/
void assign(const TokenArray &, size_t, size_t);
void append(TokenArray const&);
/**
* Erade contents of array and free memory.
*/
void clear(void);
const TokenArray & operator=(const TokenArray &);
bool operator==(const TokenArray &) const;
bool empty(void) const
{
return size()==0;
}
void info(std::ostream &) const;
static size_t getallocations(void)
{ return allocations;}
bool valid(void) const; // check integrity
static void * operator new(size_t size)
{
if(size != memory.size_of())
return ::operator new(size);
return memory.alloc();
}
static void operator delete(void *p, size_t size)
{
if(p == NULL)
return;
if(size != memory.size_of())
{
::operator delete(p);
return;
}
memory.free(p);
}
};
std::ostream & operator<<(std::ostream& , const TokenArray&);
}
#endif