26
26
template <class Item>
29
static const long default_init_size = 5;
30
static const long default_increment = 5;
33
Array (long is = default_init_size, long incr = default_increment);
34
Array (const Array<Item>& array);
35
Array<Item>& operator =(const Array<Item>&);
37
void append (const Item& item);
38
void insert (long index, const Item& item);
39
void prepend (const Item& item);
40
void remove (long index);
42
Item& get (long index);
43
Item& operator[] (long index);
44
Item fetch (long index) const;
45
Item& lookup (long index) const;
29
static const long default_init_size = 5;
30
static const long default_increment = 5;
33
Array (long is = default_init_size, long incr = default_increment);
34
Array (const Array<Item>& array);
35
Array<Item>& operator =(const Array<Item>&);
37
void append (const Item& item);
38
void insert (long index, const Item& item);
39
void prepend (const Item& item);
40
void remove (long index);
42
Item& get (long index);
43
Item& operator[] (long index);
44
Item fetch (long index) const;
45
Item& lookup (long index) const;
54
void check (long wanted);
58
inline Array<Item>::Array (long is, long incr)
63
data = new Item[size];
67
Array<Item>::Array (const Array<Item>& array)
71
data = new Item[size];
72
increment = array.increment;
74
for (int pos = 0; pos < array.length (); pos++)
75
append (array.lookup (pos));
79
Array<Item>& Array<Item>::operator =(const Array<Item>& array)
85
data = new Item[size];
86
increment = array.increment;
88
for (int pos = 0; pos < array.length (); pos++)
89
append (array.lookup (pos));
94
Array<Item>::~Array ()
100
template <class Item>
101
inline void Array<Item>::check (long wanted)
107
while (wanted >= size)
112
new_data = new Item[size];
113
for (int pos = 0; pos < count; pos++)
114
new_data[pos] = data[pos];
54
void grow (long wanted);
58
inline Array<Item>::Array (long is, long incr) {
66
Array<Item>::Array (const Array<Item>& array) {
69
increment = array.increment;
71
if (size && array.data) {
72
data = new Item[size];
73
for (; count < array.count; count++) {
74
data[count] = array.data[count];
81
Array<Item>& Array<Item>::operator =(const Array<Item>& array) {
87
increment = array.increment;
89
if (size && array.data) {
90
data = new Item[size];
91
for (; count < array.count; count++) {
92
data[count] = array.data[count];
100
template <class Item>
101
Array<Item>::~Array () {
106
template <class Item>
107
inline void Array<Item>::grow (long wanted) {
111
} while (wanted >= size);
114
Item* new_data = new Item[size];
115
for (long i = 0; i < count; i++) {
116
new_data[i] = data[i];
121
data = new Item[size];
120
124
template <class Item>
121
inline void Array<Item>::append (const Item& item)
124
data[count++] = item;
127
template <class Item>
128
void Array<Item>::prepend (const Item& item)
133
template <class Item>
134
void Array<Item>::insert (long index, const Item& item)
137
for (int pos = count; pos > index; pos--)
138
data[pos] = data[pos - 1];
143
template <class Item>
144
inline Item& Array<Item>::get (long index)
152
template <class Item>
153
inline Item& Array<Item>::operator[] (long index)
158
template <class Item>
159
inline Item Array<Item>::fetch (long index) const
161
assert(index < count);
165
template <class Item>
166
inline long Array<Item>::length () const
171
template <class Item>
172
inline void Array<Item>::remove (long index)
174
if (index < count && count > 0)
176
for (int pos = index; pos < count - 1; pos++)
177
data[pos] = data[pos + 1];
125
inline void Array<Item>::append (const Item& item) {
126
if (count >= size || ! data) {
129
data[count++] = item;
132
template <class Item>
133
void Array<Item>::prepend (const Item& item) {
137
template <class Item>
138
void Array<Item>::insert (long index, const Item& item) {
139
if (count >= size || ! data) {
142
for (long i = count; i > index; i--)
143
data[i] = data[i - 1];
148
template <class Item>
149
inline Item& Array<Item>::get (long index) {
150
if (index >= size || ! data) {
158
template <class Item>
159
inline Item& Array<Item>::operator[] (long index) {
163
template <class Item>
164
inline Item Array<Item>::fetch (long index) const {
165
assert(index < count && data);
169
template <class Item>
170
inline long Array<Item>::length () const {
174
template <class Item>
175
inline void Array<Item>::remove (long index) {
176
if (index < count && count > 0) {
177
for (long i = index; i < count - 1; i++)
178
data[i] = data[i + 1];
184
185
inline void Array<int>::reset () {
189
190
template <class Item>
190
void Array<Item>::reset ()
195
size = default_init_size;
196
increment = default_increment;
197
data = new Item[size];
191
void Array<Item>::reset () {
196
size = default_init_size;
197
increment = default_increment;
198
data = new Item[size];
200
201
template <class Item>
201
inline Item& Array<Item>::lookup (long index) const
203
assert(index >= 0 && index < count);
204
// if (index >= count) index = count - 1;
205
// if (index < 0) index = 0;
202
inline Item& Array<Item>::lookup (long index) const {
203
assert(index >= 0 && index < count);
210
208
} // namespace Puma