C++QEDCore  v2 Milestone 10
a framework for simulating open quantum dynamics – core
MultiIndexIterator.h
Go to the documentation of this file.
1 // Copyright András Vukics 2006–2014. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt)
2 // -*- C++ -*-
4 #ifndef CPPQEDCORE_UTILS_MULTIINDEXITERATOR_H_INCLUDED
5 #define CPPQEDCORE_UTILS_MULTIINDEXITERATOR_H_INCLUDED
6 
7 #include "MultiIndexIteratorFwd.h"
8 
9 #include "BlitzTiny.h"
10 
11 #include <boost/operators.hpp>
12 
13 #include <boost/mpl/identity.hpp>
14 
15 
17 namespace cpputils {
18 
19 
21 namespace mii {
22 
23 typedef boost::mpl::false_ Begin;
24 typedef boost::mpl::true_ End ;
25 
26 const Begin begin=Begin();
27 const End end =End ();
28 
29 }
30 
31 
32 #define FORWARD_IteratorHelper boost::forward_iterator_helper<MultiIndexIterator<RANK>,IdxTiny<RANK> >
33 
35 
41 template<int RANK>
42 class MultiIndexIterator : public FORWARD_IteratorHelper
43 {
44 private:
45  typedef FORWARD_IteratorHelper Base;
46 
47 #undef FORWARD_IteratorHelper
48 
49 public:
50  typedef typename Base::value_type MultiIndex;
51  typedef typename MultiIndex::T_numtype SingleIndex;
52 
54 
55  MultiIndexIterator(const MultiIndex& lbound,
57  const MultiIndex& ubound,
58  mii::Begin)
59  : lbound_(lbound), ubound_(ubound), idx_(lbound_) {}
60 
61  MultiIndexIterator(const MultiIndex& lbound, const MultiIndex& ubound, mii::End )
62  : lbound_(lbound), ubound_(ubound), idx_(ubound_) {operator++();}
63 
65 
66  MultiIndexIterator(const MultiIndex& ubound,
67  mii::Begin b)
68  : MultiIndexIterator(MultiIndex(SingleIndex(0)),ubound,b) {}
69 
71 
72  MultiIndexIterator(const MultiIndex& ubound, mii::End e)
73  : MultiIndexIterator(MultiIndex(SingleIndex(0)),ubound,e) {}
75 
76  MultiIndexIterator& operator=(const MultiIndexIterator& other) {idx_=other.idx_; return *this;}
77 
79 
80  MultiIndexIterator& operator++() {doIt(boost::mpl::int_<RANK-1>()); return *this;}
81 
82  const MultiIndex& operator*() const {return idx_;}
83  MultiIndex& operator*() {return const_cast<MultiIndex&>(static_cast<const MultiIndexIterator*>(this)->operator*());}
85 
87  friend bool operator==(const MultiIndexIterator& i1, const MultiIndexIterator& i2) {return all(i1.idx_==i2.idx_);}
88 
90 
91  const MultiIndexIterator getBegin() const {return MultiIndexIterator(lbound_,ubound_,mii::begin);}
92  const MultiIndexIterator getEnd () const {return MultiIndexIterator(lbound_,ubound_,mii::end );}
93 
94  MultiIndexIterator& setToBegin() {idx_=lbound_; return *this ;}
95  MultiIndexIterator& setToEnd () {idx_=ubound_; return ++(*this);}
97 
98 private:
99  void doIt(boost::mpl::int_<0>);
100 
101  template<int N>
102  void doIt(boost::mpl::int_<N>);
103 
104  const MultiIndex lbound_, ubound_;
105 
106  MultiIndex idx_;
107 
108 };
109 
110 
111 } // cpputils
112 
113 
114 
115 #endif // CPPQEDCORE_UTILS_MULTIINDEXITERATOR_H_INCLUDED
Namespace comprising otherwise hard-to-classify generic utilities.
Definition: Algorithm.h:10
MultiIndexIterator(const MultiIndex &lbound, const MultiIndex &ubound, mii::Begin)
Initialization to the beginning of the sequence.
MultiIndexIterator(const MultiIndex &ubound, mii::End e)
friend bool operator==(const MultiIndexIterator &i1, const MultiIndexIterator &i2)
Comparison only for the actual values referred to. The user has to take care that the bounds are actu...
An iterator over all possible combinations of indices (a number of integers) between certain bounds...
MultiIndexIterator(const MultiIndex &lbound, const MultiIndex &ubound, mii::End)
” to the end of the sequence (which is in fact beyond the end by one)
Defines template aliases for blitz::TinyVectors used for characterising the size of multi-arrays and ...
MultiIndexIterator(const MultiIndex &ubound, mii::Begin b)