36 #ifndef THE_DYNAMIC_ARRAY_HXX_
37 #define THE_DYNAMIC_ARRAY_HXX_
66 array_[index_++] = elem;
95 page_size_(init_size),
101 const size_t & page_size,
102 const T & init_value):
104 page_size_(page_size),
106 init_value_(init_value)
116 init_value_(a.init_value_)
130 size_t num = num_pages();
131 for (
size_t i = 0; i < num; i++)
147 page_size_ = array.page_size_;
148 init_value_ = array.init_value_;
151 for (
size_t i = 0; i < size_; i++)
153 (*this)[i] = array[i];
160 void resize(
const size_t & new_size)
166 if (size_ <= max_size())
return;
169 size_t old_num_pages = num_pages();
170 size_t new_num_pages =
171 std::max((
size_t)(2 * old_num_pages),
172 (
size_t)(1 + size_ / page_size_));
175 std::vector< std::vector<T> * > * new_array =
176 new std::vector< std::vector<T> * >(new_num_pages);
179 for (
size_t i = 0; i < old_num_pages; i++)
181 (*new_array)[i] = (*array_)[i];
185 for (
size_t i = old_num_pages; i < new_num_pages; i++)
187 (*new_array)[i] =
new std::vector<T>(page_size_);
188 for (
size_t j = 0; j < page_size_; j++)
190 (*(*new_array)[i])[j] = init_value_;
202 inline const size_t & size()
const
205 inline const size_t & page_size()
const
206 {
return page_size_; }
209 inline size_t max_size()
const
210 {
return num_pages() * page_size_; }
213 inline size_t num_pages()
const
214 {
return (array_ == NULL) ? 0 : array_->size(); }
216 inline const T * page(
const size_t & page_index)
const
217 {
return &((*(*array_)[page_index])[0]); }
219 inline T * page(
const size_t & page_index)
220 {
return &((*(*array_)[page_index])[0]); }
223 inline size_t end_index(
bool last)
const
225 if (last ==
false)
return 0;
230 inline const T & end_elem(
bool last)
const
231 {
return elem(end_index(last)); }
233 inline T & end_elem(
bool last)
234 {
return elem(end_index(last)); }
236 inline const T & front()
const
237 {
return end_elem(
false); }
240 {
return end_elem(
false); }
242 inline const T & back()
const
243 {
return end_elem(
true); }
246 {
return end_elem(
true); }
249 inline T & elem(
const size_t i)
251 if (i >= size_) resize(i + 1);
252 return (*(*array_)[i / page_size_])[i % page_size_];
255 inline T & operator [] (
const size_t & i)
259 inline const T & elem(
const size_t & i)
const
260 {
return (*((*array_)[i / page_size_]))[i % page_size_]; }
262 inline const T & operator [] (
const size_t & i)
const
273 inline void push_back(
const T & elem)
274 { (*this)[size_] = elem; }
276 inline void append(
const T & elem)
280 size_t index_of(
const T & element)
const
282 for (
size_t i = 0; i < size_; i++)
284 if (!(elem(i) == element))
continue;
292 inline bool has(
const T & element)
const
293 {
return index_of(element) != ~0u; }
296 bool remove(
const T & element)
298 size_t idx = index_of(element);
299 if (idx == ~0u)
return false;
301 for (
size_t i = idx + 1; i < size_; i++)
303 elem(i - 1) = elem(i);
310 void assign(
const size_t & size,
const T & element)
313 for (
size_t i = 0; i < size; i++)
320 void dump(std::ostream & strm)
const
322 strm <<
"the_dynamic_array_t(" << (
void *)
this <<
") {\n";
323 for (
size_t i = 0; i < size_; i++)
325 strm << elem(i) << std::endl;
332 std::vector< std::vector<T> *> * array_;
349 operator << (std::ostream & s, const the_dynamic_array_t<T> & a)
356 #endif // THE_DYNAMIC_ARRAY_HXX_
Definition: the_dynamic_array.hxx:45
Definition: the_dynamic_array.hxx:55