15 #ifndef STXXL_MNG_ADAPTOR_HEADER
16 #define STXXL_MNG_ADAPTOR_HEADER
20 #include <stxxl/bits/common/types.h>
23 __STXXL_BEGIN_NAMESPACE
30 template <
unsigned_type modulo>
39 void set(unsigned_type pos)
43 offset = pos % modulo;
52 blocked_index(unsigned_type pos)
57 blocked_index(unsigned_type block, unsigned_type offset)
60 this->offset = offset;
61 pos = block * modulo + offset;
64 void operator = (unsigned_type pos)
70 blocked_index & operator ++ ()
83 blocked_index operator ++ (
int)
85 blocked_index former(*
this);
91 blocked_index & operator -- ()
104 blocked_index operator -- (
int)
106 blocked_index former(*
this);
111 blocked_index & operator += (unsigned_type addend)
117 blocked_index & operator >>= (unsigned_type shift)
123 operator unsigned_type ()
const
128 const unsigned_type & get_block()
const
133 const unsigned_type & get_offset()
const
139 #define STXXL_ADAPTOR_ARITHMETICS(pos) \
140 bool operator == (const _Self & a) const \
142 return (a.pos == pos); \
144 bool operator != (const _Self & a) const \
146 return (a.pos != pos); \
148 bool operator < (const _Self & a) const \
150 return (pos < a.pos); \
152 bool operator > (const _Self & a) const \
154 return (pos > a.pos); \
156 bool operator <= (const _Self & a) const \
158 return (pos <= a.pos); \
160 bool operator >= (const _Self & a) const \
162 return (pos >= a.pos); \
164 _Self operator + (pos_type off) const \
166 return _Self(array, pos + off); \
168 _Self operator - (pos_type off) const \
170 return _Self(array, pos - off); \
172 _Self & operator ++ () \
177 _Self operator ++ (int) \
183 _Self & operator -- () \
188 _Self operator -- (int) \
194 pos_type operator - (const _Self & a) const \
196 return pos - a.pos; \
198 _Self & operator -= (pos_type off) \
203 _Self & operator += (pos_type off) \
209 template <
class one_dim_array_type,
class data_type,
class pos_type>
210 struct TwoToOneDimArrayAdaptorBase
211 :
public std::iterator<std::random_access_iterator_tag, data_type, unsigned_type>
213 one_dim_array_type * array;
215 typedef pos_type _pos_type;
216 typedef TwoToOneDimArrayAdaptorBase<one_dim_array_type,
217 data_type, pos_type> _Self;
220 TwoToOneDimArrayAdaptorBase()
223 TwoToOneDimArrayAdaptorBase(one_dim_array_type * a, pos_type p)
226 TwoToOneDimArrayAdaptorBase(
const TwoToOneDimArrayAdaptorBase & a)
227 : array(a.array), pos(a.pos)
230 STXXL_ADAPTOR_ARITHMETICS(pos)
236 #define BLOCK_ADAPTOR_OPERATORS(two_to_one_dim_array_adaptor_base) \
238 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
239 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator ++ ( \
240 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a) \
246 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
247 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator ++ ( \
248 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, int) \
250 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> tmp = a; \
255 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
256 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator -- ( \
257 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a) \
263 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
264 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator -- ( \
265 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, int) \
267 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> tmp = a; \
272 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
273 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator -= ( \
274 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
275 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
281 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
282 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & operator += ( \
283 two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
284 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
290 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
291 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator + ( \
292 const two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
293 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
295 return two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>(a.array, a.pos + off); \
298 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
299 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator + ( \
300 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off, \
301 const two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a) \
303 return two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>(a.array, a.pos + off); \
306 template <unsigned _blk_sz, typename _run_type, class __pos_type> \
307 inline two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> operator - ( \
308 const two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type> & a, \
309 typename two_to_one_dim_array_adaptor_base<_blk_sz, _run_type, __pos_type>::_pos_type off) \
311 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,
318 unsigned dim_size,
class pos_type = blocked_index<dim_size> >
319 struct TwoToOneDimArrayRowAdaptor :
320 public TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>
322 typedef TwoToOneDimArrayRowAdaptor<one_dim_array_type,
323 data_type, dim_size, pos_type> _Self;
325 typedef TwoToOneDimArrayAdaptorBase<one_dim_array_type,
326 data_type, pos_type> _Parent;
327 using _Parent::array;
330 TwoToOneDimArrayRowAdaptor()
332 TwoToOneDimArrayRowAdaptor(one_dim_array_type * a, pos_type p)
333 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a, p)
335 TwoToOneDimArrayRowAdaptor(
const TwoToOneDimArrayRowAdaptor & a)
336 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a)
339 data_type & operator * ()
341 return array[(pos).get_block()][(pos).get_offset()];
344 data_type * operator -> ()
const
346 return &(array[(pos).get_block()][(pos).get_offset()]);
349 data_type & operator [] (pos_type n)
352 return array[(n) / dim_size][(n) % dim_size];
355 const data_type & operator [] (pos_type n)
const
358 return array[(n) / dim_size][(n) % dim_size];
360 STXXL_ADAPTOR_ARITHMETICS(pos)
363 template <
class one_dim_array_type,
class data_type,
364 unsigned dim_size,
class pos_type = blocked_index<dim_size> >
365 struct TwoToOneDimArrayColumnAdaptor
366 :
public TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>
368 typedef TwoToOneDimArrayColumnAdaptor<one_dim_array_type,
369 data_type, dim_size, pos_type> _Self;
371 using TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>::pos;
372 using TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>::array;
374 TwoToOneDimArrayColumnAdaptor(one_dim_array_type * a, pos_type p)
375 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a, p)
377 TwoToOneDimArrayColumnAdaptor(
const _Self & a)
378 : TwoToOneDimArrayAdaptorBase<one_dim_array_type, data_type, pos_type>(a)
381 data_type & operator * ()
383 return array[(pos).get_offset()][(pos).get_block()];
386 data_type * operator -> ()
const
388 return &(array[(pos).get_offset()][(pos).get_block()]);
391 const data_type & operator [] (pos_type n)
const
394 return array[(n) % dim_size][(n) / dim_size];
397 data_type & operator [] (pos_type n)
400 return array[(n) % dim_size][(n) / dim_size];
402 STXXL_ADAPTOR_ARITHMETICS(pos)
407 template <
typename array_type,
typename value_type,
unsigned_type modulo>
408 class ArrayOfSequencesIterator :
public std::iterator<std::random_access_iterator_tag, value_type, unsigned_type>
411 unsigned_type offset;
414 value_type * base_element;
418 void set(unsigned_type pos)
421 offset = pos % modulo;
422 base = arrays + pos / modulo;
423 base_element = base->elem;
427 ArrayOfSequencesIterator()
433 ArrayOfSequencesIterator(array_type * arrays)
435 this->arrays = arrays;
439 ArrayOfSequencesIterator(array_type * arrays, unsigned_type pos)
441 this->arrays = arrays;
445 void operator = (unsigned_type pos)
451 ArrayOfSequencesIterator & operator ++ ()
455 if (offset == modulo)
459 base_element = base->elem;
465 ArrayOfSequencesIterator operator ++ (
int)
467 ArrayOfSequencesIterator former(*
this);
473 ArrayOfSequencesIterator & operator -- ()
480 base_element = base->elem;
487 ArrayOfSequencesIterator operator -- (
int)
489 ArrayOfSequencesIterator former(*
this);
494 ArrayOfSequencesIterator & operator += (unsigned_type addend)
500 ArrayOfSequencesIterator & operator -= (unsigned_type addend)
506 ArrayOfSequencesIterator operator + (unsigned_type addend)
const
508 return ArrayOfSequencesIterator(arrays, pos + addend);
511 ArrayOfSequencesIterator operator - (unsigned_type subtrahend)
const
513 return ArrayOfSequencesIterator(arrays, pos - subtrahend);
516 unsigned_type operator - (
const ArrayOfSequencesIterator & subtrahend)
const
518 return pos - subtrahend.pos;
521 bool operator == (
const ArrayOfSequencesIterator & aoai)
const
523 return pos == aoai.pos;
526 bool operator != (
const ArrayOfSequencesIterator & aoai)
const
528 return pos != aoai.pos;
531 bool operator < (
const ArrayOfSequencesIterator & aoai)
const
533 return pos < aoai.pos;
536 bool operator <= (
const ArrayOfSequencesIterator & aoai)
const
538 return pos <= aoai.pos;
541 bool operator > (
const ArrayOfSequencesIterator & aoai)
const
543 return pos > aoai.pos;
546 bool operator >= (
const ArrayOfSequencesIterator & aoai)
const
548 return pos >= aoai.pos;
551 const value_type & operator * ()
const
553 return base_element[offset];
556 value_type & operator * ()
558 return base_element[offset];
561 const value_type & operator -> ()
const
563 return &(base_element[offset]);
566 value_type & operator -> ()
568 return &(base_element[offset]);
571 const value_type & operator [] (unsigned_type index)
const
573 return arrays[index / modulo][index % modulo];
576 value_type & operator [] (unsigned_type index)
578 return arrays[index / modulo][index % modulo];
584 template <
typename BlockType,
bool can_use_trivial_po
inter>
585 class element_iterator_generator
589 template <
typename BlockType>
590 class element_iterator_generator<BlockType, false>
592 typedef BlockType block_type;
593 typedef typename block_type::value_type value_type;
596 typedef ArrayOfSequencesIterator<block_type, value_type, block_type::size> iterator;
598 iterator operator () (block_type * blocks, unsigned_type offset)
const
600 return iterator(blocks, offset);
605 template <
typename BlockType>
606 class element_iterator_generator<BlockType, true>
608 typedef BlockType block_type;
609 typedef typename block_type::value_type value_type;
612 typedef value_type * iterator;
614 iterator operator () (block_type * blocks, unsigned_type offset)
const
616 return blocks[0].elem + offset;
621 template <
typename BlockType>
622 struct element_iterator_traits
624 typedef typename helper::element_iterator_generator<BlockType, BlockType::has_only_data>::iterator element_iterator;
627 template <
typename BlockType>
629 typename element_iterator_traits<BlockType>::element_iterator
630 make_element_iterator(BlockType * blocks, unsigned_type offset)
632 helper::element_iterator_generator<BlockType, BlockType::has_only_data> iter_gen;
633 return iter_gen(blocks, offset);
638 __STXXL_END_NAMESPACE
640 #endif // !STXXL_MNG_ADAPTOR_HEADER