00001
00002
00013
00014
00015 #ifndef polybori_iterators_COrderedIter_h_
00016 #define polybori_iterators_COrderedIter_h_
00017
00018
00019 #include <polybori/pbori_defs.h>
00020 #include <polybori/routines/pbori_algo.h>
00021
00022
00023 #include <polybori/BoolePolynomial.h>
00024 #include "CBidirectTermIter.h"
00025 #include <algorithm>
00026
00027 #include "CStackSelector.h"
00028 #include "CTermGenerator.h"
00029
00030
00031 BEGIN_NAMESPACE_PBORI
00032
00033 template <class NavigatorType>
00034 class CAbstractStackBase {
00035 public:
00036 typedef NavigatorType navigator;
00037
00038 typedef CAbstractStackBase<NavigatorType> self;
00039 typedef CTermStackBase<NavigatorType, self> iterator_core;
00040 typedef boost::shared_ptr<iterator_core> core_pointer;
00041
00042 virtual void increment() = 0;
00043 virtual core_pointer copy() const = 0;
00044
00045 virtual ~CAbstractStackBase() {}
00046 };
00047
00048
00049
00050 template <class StackType>
00051 class CWrappedStack:
00052 public StackType {
00053 public:
00054 typedef StackType base;
00055 typedef CWrappedStack<StackType> self;
00056
00057 typedef typename base::navigator navigator;
00058
00059 typedef typename base::iterator_core iterator_core;
00060 typedef boost::shared_ptr<iterator_core> core_pointer;
00061
00062 template <class MgrType>
00063 CWrappedStack(navigator navi, const MgrType& mgr):
00064 base(navi, mgr) {
00065 base::init();
00066 }
00067
00068 CWrappedStack(const self& rhs): base(rhs) {}
00069
00070
00071 core_pointer copy() const {
00072 return core_pointer(new self(*this));
00073 }
00074
00075 };
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 template <class NavigatorType, class MonomType>
00112 class COrderedIter:
00113 public boost::iterator_facade<
00114 COrderedIter<NavigatorType, MonomType>,
00115 MonomType, std::forward_iterator_tag, MonomType
00116 > {
00117
00118 public:
00119
00120 typedef COrderedIter<NavigatorType, MonomType> self;
00121 typedef CAbstractStackBase<NavigatorType> stack_base;
00122 typedef CTermStackBase<NavigatorType, stack_base> iterator_core;
00123
00125 typedef CTermGenerator<MonomType> term_generator;
00126
00127 typedef typename iterator_core::const_iterator const_iterator;
00128 typedef typename iterator_core::const_reverse_iterator
00129 const_reverse_iterator;
00130 typedef typename iterator_core::size_type size_type;
00131 typedef typename iterator_core::deg_type deg_type;
00132 typedef typename iterator_core::idx_type idx_type;
00133
00134
00136 typedef NavigatorType navigator;
00137
00138
00139 typedef boost::shared_ptr<iterator_core> core_pointer;
00140
00142 typedef bool bool_type;
00143
00144
00145 COrderedIter(core_pointer rhs,
00146 const term_generator & getTerm):
00147 m_getTerm(getTerm), p_iter(rhs) {}
00148
00149
00150 ~COrderedIter() {}
00151
00152 bool equal(const self& rhs) const {
00153 return p_iter->equal(*rhs.p_iter); }
00154
00156 void increment() {
00157 if (!p_iter.unique()) {
00158 core_pointer tmp(p_iter->copy());
00159 p_iter = tmp;
00160 }
00161
00162 p_iter->increment();
00163 }
00164
00166 bool_type isOne() const { return p_iter->isOne(); }
00167
00169 bool_type isZero() const { return p_iter->isZero(); }
00170
00172 bool_type isEnd() const { return isZero(); }
00173
00175 MonomType dereference() const {
00176
00177 return m_getTerm(*p_iter);
00178 }
00179
00180 const_iterator begin() const { return p_iter->begin(); }
00181 const_iterator end() const { return p_iter->end(); }
00182 const_reverse_iterator rbegin() const { return p_iter->rbegin(); }
00183 const_reverse_iterator rend() const { return p_iter->rend(); }
00184
00185 deg_type deg() const { return p_iter->deg(); }
00186 idx_type firstIndex() const { return *begin(); }
00187
00189 navigator navigation() const {
00190 return p_iter->navigation();
00191 }
00192
00193 protected:
00195 term_generator m_getTerm;
00196
00198 core_pointer p_iter;
00199 };
00200
00201
00202 template <class OrderType, class NavigatorType, class MonomType>
00203 class CGenericOrderedIter:
00204 public COrderedIter<NavigatorType, MonomType> {
00205 public:
00206 typedef CAbstractStackBase<NavigatorType> stack_base;
00207 typedef typename CStackSelector<OrderType, NavigatorType, stack_base>::type
00208 ordered_iter_base;
00209 typedef CWrappedStack<ordered_iter_base> ordered_iter_type;
00210
00211 typedef COrderedIter<NavigatorType, MonomType> base;
00212 typedef typename base::iterator_core iterator_core;
00213 typedef typename base::core_pointer core_pointer;
00214
00215 typedef typename base::term_generator term_generator;
00216
00217 template <class MgrType>
00218 CGenericOrderedIter(NavigatorType navi, const MgrType& gen):
00219 base( core_pointer(new ordered_iter_type(navi, gen) ), gen) {}
00220
00221
00222
00223 CGenericOrderedIter(const CGenericOrderedIter& rhs): base(rhs) {}
00224 };
00225
00226 template <class OrderType, class NavigatorType>
00227 class CGenericOrderedIter<OrderType, NavigatorType, BooleExponent> :
00228 public COrderedIter<NavigatorType, BooleExponent> {
00229 public:
00230 typedef CAbstractStackBase<NavigatorType> stack_base;
00231 typedef typename CStackSelector<OrderType, NavigatorType, stack_base>::type
00232 ordered_iter_base;
00233 typedef CWrappedStack<ordered_iter_base> ordered_iter_type;
00234
00235 typedef COrderedIter<NavigatorType, BooleExponent> base;
00236 typedef typename base::iterator_core iterator_core;
00237 typedef typename base::core_pointer core_pointer;
00238
00239 typedef typename base::term_generator term_generator;
00240
00241 template <class MgrType>
00242 CGenericOrderedIter(NavigatorType navi, const MgrType& mgr):
00243 base( core_pointer(new ordered_iter_type(navi, mgr)),
00244 term_generator() ) {}
00245
00246
00247
00248
00249 CGenericOrderedIter(const CGenericOrderedIter& rhs): base(rhs) {}
00250 };
00251
00252 END_NAMESPACE_PBORI
00253
00254 #endif