14 #ifndef STXXL_MNG_ADAPTOR_HEADER
15 #define STXXL_MNG_ADAPTOR_HEADER
19 #include <stxxl/bits/common/types.h>
22 __STXXL_BEGIN_NAMESPACE
29 template <
unsigned_type modulo>
38 void set(unsigned_type pos)
42 offset = pos % modulo;
51 blocked_index(unsigned_type pos)
56 blocked_index(unsigned_type block, unsigned_type offset)
59 this->offset = offset;
60 pos = block * modulo + offset;
63 void operator = (unsigned_type pos)
69 blocked_index & operator ++ ()
82 blocked_index operator ++ (
int)
84 blocked_index former(*
this);
90 blocked_index & operator -- ()
103 blocked_index operator -- (
int)
105 blocked_index former(*
this);
110 blocked_index & operator += (unsigned_type addend)
116 blocked_index & operator >>= (unsigned_type shift)
122 operator unsigned_type ()
const
127 const unsigned_type & get_block()
const
132 const unsigned_type & get_offset()
const
138 #define STXXL_ADAPTOR_ARITHMETICS(pos) \
139 bool operator == (const _Self & a) const \
141 return (a.pos == pos); \
143 bool operator != (const _Self & a) const \
145 return (a.pos != pos); \
147 bool operator < (const _Self & a) const \
149 return (pos < a.pos); \
151 bool operator > (const _Self & a) const \
153 return (pos > a.pos); \
155 bool operator <= (const _Self & a) const \
157 return (pos <= a.pos); \
159 bool operator >= (const _Self & a) const \
161 return (pos >= a.pos); \
163 _Self operator + (pos_type off) const \
165 return _Self(array, pos + off); \
167 _Self operator - (pos_type off) const \
169 return _Self(array, pos - off); \
171 _Self & operator ++ () \
176 _Self operator ++ (int) \
182 _Self & operator -- () \
187 _Self operator -- (int) \
193 pos_type operator - (const _Self & a) const \
195 return pos - a.pos; \
197 _Self & operator -= (pos_type off) \
202 _Self & operator += (pos_type off) \
208 template <
class one_dim_array_type,
class data_type,
class pos_type>
209 struct TwoToOneDimArrayAdaptorBase
210 :
public std::iterator<std::random_access_iterator_tag, data_type, unsigned_type>
212 one_dim_array_type * array;
214 typedef pos_type _pos_type;
215 typedef TwoToOneDimArrayAdaptorBase<one_dim_array_type,
216 data_type, pos_type> _Self;
219 TwoToOneDimArrayAdaptorBase()
222 TwoToOneDimArrayAdaptorBase(one_dim_array_type * a, pos_type p)
225 TwoToOneDimArrayAdaptorBase(
const TwoToOneDimArrayAdaptorBase & a)
226 : array(a.array), pos(a.pos)
229 STXXL_ADAPTOR_ARITHMETICS(pos)
235 #define BLOCK_ADAPTOR_OPERATORS(two_to_one_dim_array_adaptor_base) \
237 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
238 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator ++ ( \
239 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a) \
245 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
246 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator ++ ( \
247 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, int) \
249 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> tmp = a; \
254 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
255 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator -- ( \
256 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a) \
262 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
263 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator -- ( \
264 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, int) \
266 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> tmp = a; \
271 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
272 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator -= ( \
273 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
274 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
280 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
281 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator += ( \
282 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
283 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
289 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
290 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator + ( \
291 const two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
292 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
294 return two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>(a.array, a.pos + off); \
297 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
298 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator + ( \
299 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off, \
300 const two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a) \
302 return two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>(a.array, a.pos + off); \
305 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
306 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator - ( \
307 const two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
308 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
310 return two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>(a.array, a.pos - off); \
316 template <
class one_dim_array_type,
class data_type,
317 unsigned dim_size,
class pos_type = blocked_index<dim_size> >
318 struct TwoToOneDimArrayRowAdaptor :
public
319 TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>
321 typedef TwoToOneDimArrayRowAdaptor<one_dim_array_type,
322 data_type, dim_size, pos_type> _Self;
324 typedef TwoToOneDimArrayAdaptorBase<one_dim_array_type,
325 data_type, pos_type> _Parent;
326 using _Parent::array;
329 TwoToOneDimArrayRowAdaptor()
331 TwoToOneDimArrayRowAdaptor(one_dim_array_type * a, pos_type p)
332 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a, p)
334 TwoToOneDimArrayRowAdaptor(
const TwoToOneDimArrayRowAdaptor & a)
335 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a)
338 data_type & operator * ()
340 return array[(pos).get_block()][(pos).get_offset()];
343 data_type * operator -> ()
const
345 return &(array[(pos).get_block()][(pos).get_offset()]);
348 data_type & operator [] (pos_type n)
351 return array[(n) / dim_size][(n) % dim_size];
354 const data_type & operator [] (pos_type n)
const
357 return array[(n) / dim_size][(n) % dim_size];
359 STXXL_ADAPTOR_ARITHMETICS(pos)
362 template <
class one_dim_array_type,
class data_type,
363 unsigned dim_size,
class pos_type = blocked_index<dim_size> >
364 struct TwoToOneDimArrayColumnAdaptor
365 :
public TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>
367 typedef TwoToOneDimArrayColumnAdaptor<one_dim_array_type,
368 data_type, dim_size, pos_type> _Self;
370 using TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>::pos;
371 using TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>::array;
373 TwoToOneDimArrayColumnAdaptor(one_dim_array_type * a, pos_type p)
374 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a, p)
376 TwoToOneDimArrayColumnAdaptor(
const _Self & a)
377 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a)
380 data_type & operator * ()
382 return array[(pos).get_offset()][(pos).get_block()];
385 data_type * operator -> ()
const
387 return &(array[(pos).get_offset()][(pos).get_block()]);
390 const data_type & operator [] (pos_type n)
const
393 return array[(n) % dim_size][(n) / dim_size];
396 data_type & operator [] (pos_type n)
399 return array[(n) % dim_size][(n) / dim_size];
401 STXXL_ADAPTOR_ARITHMETICS(pos)
406 template <
typename array_type,
typename value_type,
unsigned_type modulo>
407 class ArrayOfSequencesIterator :
public std::iterator<std::random_access_iterator_tag, value_type, unsigned_type>
410 unsigned_type offset;
413 value_type * base_element;
417 void set(unsigned_type pos)
420 offset = pos % modulo;
421 base = arrays + pos / modulo;
422 base_element = base->elem;
426 ArrayOfSequencesIterator()
432 ArrayOfSequencesIterator(array_type * arrays)
434 this->arrays = arrays;
438 ArrayOfSequencesIterator(array_type * arrays, unsigned_type pos)
440 this->arrays = arrays;
444 void operator = (unsigned_type pos)
450 ArrayOfSequencesIterator & operator ++ ()
454 if (offset == modulo)
458 base_element = base->elem;
464 ArrayOfSequencesIterator operator ++ (
int)
466 ArrayOfSequencesIterator former(*
this);
472 ArrayOfSequencesIterator & operator -- ()
479 base_element = base->elem;
486 ArrayOfSequencesIterator operator -- (
int)
488 ArrayOfSequencesIterator former(*
this);
493 ArrayOfSequencesIterator & operator += (unsigned_type addend)
499 ArrayOfSequencesIterator & operator -= (unsigned_type addend)
505 ArrayOfSequencesIterator operator + (unsigned_type addend)
const
507 return ArrayOfSequencesIterator(arrays, pos + addend);
510 ArrayOfSequencesIterator operator - (unsigned_type subtrahend)
const
512 return ArrayOfSequencesIterator(arrays, pos - subtrahend);
515 unsigned_type operator - (
const ArrayOfSequencesIterator & subtrahend)
const
517 return pos - subtrahend.pos;
520 bool operator == (
const ArrayOfSequencesIterator & aoai)
const
522 return pos == aoai.pos;
525 bool operator != (
const ArrayOfSequencesIterator & aoai)
const
527 return pos != aoai.pos;
530 bool operator < (
const ArrayOfSequencesIterator & aoai)
const
532 return pos < aoai.pos;
535 bool operator <= (
const ArrayOfSequencesIterator & aoai)
const
537 return pos <= aoai.pos;
540 bool operator > (
const ArrayOfSequencesIterator & aoai)
const
542 return pos > aoai.pos;
545 bool operator >= (
const ArrayOfSequencesIterator & aoai)
const
547 return pos >= aoai.pos;
550 const value_type & operator * ()
const
552 return base_element[offset];
555 value_type & operator * ()
557 return base_element[offset];
560 const value_type & operator -> ()
const
562 return &(base_element[offset]);
565 value_type & operator -> ()
567 return &(base_element[offset]);
570 const value_type & operator [] (unsigned_type index)
const
572 return arrays[index / modulo][index % modulo];
575 value_type & operator [] (unsigned_type index)
577 return arrays[index / modulo][index % modulo];
584 __STXXL_END_NAMESPACE
586 #endif // !STXXL_MNG_ADAPTOR_HEADER