PolyBoRi
CBidirectTermIter.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //*****************************************************************************
42 //*****************************************************************************
43 
44 // get standard header
45 #include <stack>
46 #include <utility>
47 
48 // include basic definitions
49 #include "pbori_defs.h"
50 
51 // Get forward term iterator
52 #include "CTermIter.h"
53 
54 
55 #ifndef CBidirectTermIter_h_
56 #define CBidirectTermIter_h_
57 
59 
60 template<class NavigatorType>
61 class handle_else :
62  public std::deque<NavigatorType> {
63 public:
64 
65  typedef NavigatorType navigator_type;
66  typedef std::deque<NavigatorType> base;
67 
68  void operator()(const navigator_type& navi) {
69 
70  while(!base::empty() && (*top() >= *navi) )
71  base::pop_back();
72 
73  base::push_back(navi);
74  }
75  void push(const navigator_type& navi) { base::push_back(navi); }
76  void pop() { base::pop_back(); }
77 
78  const navigator_type& top() const { return base::back(); };
79 
80  void append(const handle_else& rhs) {
81  assert(base::empty() || rhs.empty() || ((**rhs.begin()) > (*top())) );
82  base::insert(base::end(), rhs.begin(), rhs.end());
83  }
84 };
85 
86 #if 0
87 
93 template <class TermType, class NavigatorType,
94  class ForwardOp, class BackwardOp,
95  class TerminalValueOp = project_ith<2> >
96 class CBidirectTermIter:
97  public CTermIter<TermType, NavigatorType,
98  ForwardOp, BackwardOp,
99  TerminalValueOp,
100  handle_else<NavigatorType> >{
101 
102 public:
103 
105  typedef TermType term_type;
106 
108  typedef NavigatorType navigator_type;
109 
111  typedef ForwardOp forwardop_type;
112 
114  typedef BackwardOp backwardop_type;
115 
117  typedef TerminalValueOp termvalop_type;
118 
120  typedef handle_else<navigator_type> elsehandle_type;
121 
123  typedef CBidirectTermIter<term_type, navigator_type,
124  forwardop_type, backwardop_type, termvalop_type> self;
125 
127  typedef CTermIter<term_type, navigator_type,
128  forwardop_type, backwardop_type, termvalop_type,
129  elsehandle_type> base;
130 
132 
133  typedef std::bidirectional_iterator_tag iterator_category;
134  typedef typename base::difference_type difference_type;
135  typedef typename base::pointer pointer;
136  typedef typename base::reference reference;
138 
140  using base::handleElse;
141 
143  CBidirectTermIter():
144  base() {}
145 
147  CBidirectTermIter(navigator_type navi,
148  forwardop_type fop_ = forwardop_type(),
149  backwardop_type bop_ = backwardop_type(),
150  termvalop_type tvop_ = termvalop_type() ):
151  base(navi, fop_, bop_, tvop_) {}
152 
154  CBidirectTermIter(navigator_type navi, dummy_iterator):
155  base() {
156  if(navi.isValid()) {
157  followElse(navi);
158  terminate(navi);
159  }
160  }
161 
163  CBidirectTermIter(const self& rhs):
164  base(rhs) {};
165 
167  ~CBidirectTermIter() {};
168 
170  self& operator++() {
171  base::operator++();
172  return *this;
173  }
174 
176  self operator++(int dummy) {
177  return base::operator++(dummy);
178  };
179 
181  self& operator--() {
182 
183  if (!handleElse.empty()){
184  navigator_type navi = handleElse.top();
185  base::popToIndex(*navi);
186 
187 
188  handleElse.pop();
189  base::nextThen(navi);
190 
191  followElse(navi);
192  }
193  else
194  base::clear();
195  return *this;
196  }
197 
199  self operator--(int) {
200  self tmp(*this);
201  operator--();
202  return tmp;
203  };
204 
205 protected:
206 
207 
208  void followElse(navigator_type& navi) {
209  while( !navi.isConstant() ) { // if still in interior of a path
210  if(!navi.elseBranch().isEmpty()) {
211  handleElse.push(navi);
212  navi.incrementElse(); // go in direction of last term, if possible
213  }
214  else
215  base::nextThen(navi);
216  }
217  }
218 
219 };
220 
221 #endif
222 
224 
225 #endif