47 template<
class T,
unsigned char pks = 5>
class dynamic_array;
52 typedef value_type* pointer;
53 typedef value_type& reference;
54 typedef size_t size_type;
55 typedef ptrdiff_t difference_type;
56 typedef std::random_access_iterator_tag iterator_category;
58 # define DNAMPKS__ ((size_type(1) << pks) - 1)
65 { p = &da; in = ii; pT = p->pt_to(in); }
67 inline size_type index(
void)
const {
return in; }
71 if ((++in)&DNAMPKS__) pT++;
else pT=p->pt_to(in);
return tmp;
76 if ((in--)&DNAMPKS__) pT--;
else pT=p->pt_to(in);
return tmp;
80 {
if ((++in)&DNAMPKS__) pT++;
else pT=p->pt_to(in);
return *
this; }
83 {
if ((in--)&DNAMPKS__) pT--;
else pT=p->pt_to(in);
return *
this; }
86 { in += i; pT=p->pt_to(in);
return *
this; }
89 { in -= i; pT=p->pt_to(in);
return *
this; }
98 {
return difference_type(in - i.in); }
100 reference operator *()
const {
return (*pT); }
101 pointer operator ->()
const {
return pT; }
102 reference operator [](size_type ii)
const {
return (*p)[in+ii]; }
104 bool operator ==(
const dna_iterator &i)
const {
return (i.in==in);}
105 bool operator !=(
const dna_iterator &i)
const {
return (i.in!=in);}
106 bool operator < (
const dna_iterator &i)
const {
return ( in<i.in);}
107 bool operator >=(
const dna_iterator &i)
const {
return ( in>=i.in);}
108 bool operator > (
const dna_iterator &i)
const {
return ( in>i.in);}
113 typedef T value_type;
114 typedef const value_type* pointer;
115 typedef const value_type& reference;
116 typedef size_t size_type;
117 typedef ptrdiff_t difference_type;
118 typedef std::random_access_iterator_tag iterator_category;
120 # define DNAMPKS__ ((size_type(1) << pks) - 1)
127 { p = &da; in = ii; pT = p->pt_to(in); }
129 : p(it.p), in(it.in), pT(it.pT) {}
131 inline size_type index(
void)
const {
return in; }
134 if ((++in)&DNAMPKS__) pT++;
else pT=p->pt_to(in);
return tmp;
138 if ((in--)&DNAMPKS__) pT--;
else pT=p->pt_to(in);
return tmp;
141 {
if ((++in)&DNAMPKS__) pT++;
else pT=p->pt_to(in);
return *
this; }
143 {
if ((in--)&DNAMPKS__) pT--;
else pT=p->pt_to(in);
return *
this; }
145 { in += i; pT=p->pt_to(in);
return *
this; }
147 { in -= i; pT=p->pt_to(in);
return *
this; }
153 {
return difference_type(in - i.in); }
155 reference operator *()
const {
return (*pT); }
156 pointer operator ->()
const {
return pT; }
157 reference operator [](size_type ii)
const {
return (*p)[in+ii]; }
160 {
return (i.in == in); }
162 {
return (i.in != in); }
164 {
return (in < i.in); }
166 {
return (i.in >= in); }
168 {
return (in > i.in); }
199 typedef T value_type;
200 typedef value_type* pointer;
201 typedef const value_type* const_pointer;
202 typedef value_type& reference;
203 typedef const value_type& const_reference;
204 typedef size_t size_type;
205 typedef ptrdiff_t difference_type;
206 typedef unsigned char pack_size_type;
207 typedef std::vector<std::unique_ptr<T[]>> pointer_array;
210 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
211 typedef std::reverse_iterator<iterator> reverse_iterator;
215 # define DNAMPKS__ ((size_type(1) << pks) - 1)
220 size_type last_accessed;
225 size_type
size(
void)
const {
return last_accessed; }
226 size_type capacity(
void)
const {
return last_ind; }
227 size_type max_size(
void)
const {
return (size_type(-1)) / 2; }
229 bool empty(
void)
const {
return last_accessed == 0; }
239 reverse_iterator rbegin(
void) {
return reverse_iterator(
end()); }
240 const_reverse_iterator rbegin(
void)
const
241 {
return const_reverse_iterator(
end()); }
242 reverse_iterator rend(
void) {
return reverse_iterator(
begin()); }
243 const_reverse_iterator rend(
void)
const
244 {
return const_reverse_iterator(
begin()); }
246 reference front(
void) {
return *
begin(); }
247 const_reference front(
void)
const {
return *
begin(); }
248 reference back(
void) {
return *(
end() - 1); }
249 const_reference back(
void)
const {
return *(
end() - 1); }
251 void swap(dynamic_array<T,pks> &da);
259 { last_accessed = last_ind = 0; array.resize(8); ppks = 3; m_ppks = 7; }
265 dynamic_array(
void) { init(); }
267 inline pointer pt_to(size_type ii)
268 {
return (ii >=last_ind) ? NULL : &((array[ii>>pks])[ii&DNAMPKS__]); }
269 inline const_pointer pt_to(size_type ii)
const
270 {
return (ii >=last_ind) ? NULL : &((array[ii>>pks])[ii&DNAMPKS__]); }
276 void resize(size_type i) { (*this)[i-1]; }
280 return sizeof(pointer) * array.capacity()
285 void swap(size_type i1, size_type i2)
286 { std::swap((*
this)[i1], (*
this)[i2]); }
295 template<
class T,
unsigned char pks>
296 void dynamic_array<T,pks>::swap(dynamic_array<T,pks> &da) {
297 array.swap(da.array);
298 std::swap(last_ind, da.last_ind);
299 std::swap(last_accessed, da.last_accessed);
300 std::swap(ppks, da.ppks); std::swap(m_ppks, da.m_ppks);
303 template<
class T,
unsigned char pks>
308 array.clear(); init();
313 array.resize(da.array.size());
314 last_ind = da.last_ind;
315 last_accessed = da.last_accessed;
316 ppks = da.ppks; m_ppks = da.m_ppks;
317 typename pointer_array::iterator it = array.begin();
318 typename pointer_array::const_iterator ita = da.array.begin();
319 typename pointer_array::iterator ite = it+ ((last_ind + DNAMPKS__) >> pks);
321 *it = std::unique_ptr<T[]>(
new T[DNAMPKS__+1]);
322 pointer p = it->get(); ++it;
323 pointer pe = p + (DNAMPKS__+1);
324 const_pointer pa = (ita++)->get();
325 while (p != pe) *p++ = *pa++;
330 template<
class T,
unsigned char pks>
331 typename dynamic_array<T,pks>::const_reference
333 THREAD_SAFE_STATIC T f;
334 return (ii<last_ind) ? (array[ii>>pks])[ii&DNAMPKS__] : f;
337 template<
class T,
unsigned char pks>
typename dynamic_array<T,pks>::reference
339 if (ii >= last_accessed) {
340 GMM_ASSERT2(ii < INT_MAX,
"out of range");
342 last_accessed = ii + 1;
343 if (ii >= last_ind) {
344 if ((ii >> (pks+ppks)) > 0) {
345 while ((ii >> (pks+ppks)) > 0) ppks++;
346 array.resize(m_ppks = (
size_type(1) << ppks)); m_ppks--;
348 for (size_type jj = (last_ind >> pks); ii >= last_ind;
349 jj++, last_ind += (DNAMPKS__ + 1)){
350 array[jj] = std::unique_ptr<T[]>(
new T[DNAMPKS__+1]);
354 return (array[ii >> pks])[ii & DNAMPKS__];
362 template<
class T,
unsigned char pks>
367 typename dynamic_array<T,pks>::size_type d = std::min(itxe-itxb,itye-ityb);
370 if (!std::equal(itxb, itxc, ityb))
return false;
371 for (; itxc != itxe; itxc++)
if (*itxc != T())
return false;
372 for (; ityc != itye; ityc++)
if (*ityc != T())
return false;
376 template<
class T,
unsigned char pks>
377 bool operator < (
const dynamic_array<T,pks> &x,
378 const dynamic_array<T,pks> &y)
379 {
return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end()); }
381 template<
class T,
unsigned char pks>
inline
382 void swap(
const dynamic_array<T,pks> &x,
const dynamic_array<T,pks> &y)
size_type memsize(void) const
Gives the total memory occupied by the array.
const_iterator end(void) const
Constant iterator on the last + 1 element.
iterator begin(void)
Iterator on the first element.
bool empty(void) const
True if no space is allocated.
size_type size(void) const
Number of allocated elements.
void clear(void)
Clear and desallocate all the elements.
iterator end(void)
Iterator on the last + 1 element.
const_iterator begin(void) const
Constant iterator on the first element.
void swap(size_type i1, size_type i2)
Swap element i1 and i2.
const_reference operator[](size_type ii) const
Gives a constant reference on element ii.
defines and typedefs for namespace dal
Tools for multithreaded, OpenMP and Boost based parallelization.
size_t size_type
used as the common size type in the library
Constant iterator class for dynamic array.
Iterator class for dynamic array.
dna_iterator & operator++()
next element.
dna_iterator & operator+=(difference_type i)
go i elements forward.
dna_iterator operator-(difference_type i) const
gives an iterator pointing i elements backward.
dna_iterator & operator--()
previous element.
dna_iterator operator+(difference_type i) const
gives an iterator pointing i elements forward.
dna_iterator & operator-=(difference_type i)
go i elements backward.