yat  0.21pre
Iterator.h
1 #ifndef theplu_yat_utility_ranking_iterator
2 #define theplu_yat_utility_ranking_iterator
3 
4 // $Id: Iterator.h 4072 2021-08-20 05:40:18Z peter $
5 
6 /*
7  Copyright (C) 2021 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 // This is a private file used by yat/utility/Ranking.h
26 
27 #include "NodeBase.h"
28 #include "NodeValue.h"
29 
30 #include <yat/utility/yat_assert.h>
31 #include <boost/iterator/iterator_facade.hpp>
32 
33 #include <cstddef>
34 #include <iterator>
35 
36 #include <iostream> // debug
37 namespace theplu {
38 namespace yat {
39 namespace utility {
40 
42 
43  // namespace for internal classes used in class Ranking
44  namespace ranking {
45 
46  template<typename T>
47  class Iterator
48  : public boost::iterator_facade<
49  Iterator<T>, const T, std::bidirectional_iterator_tag
50  >
51  {
52  public:
53  Iterator(void);
54  explicit Iterator(const NodeBase* node);
55  const NodeBase* node_;
56  private:
57  friend class boost::iterator_core_access;
58  const T& dereference(void) const;
59  bool equal(Iterator other) const;
60  void increment(void);
61  void decrement(void);
62  };
63 
65  template<typename T>
66  Iterator<T>::Iterator(void)
67  : node_(nullptr)
68  {
69  }
70 
71 
72  template<typename T>
73  Iterator<T>::Iterator(const NodeBase* node)
74  : node_(node)
75  {
76  YAT_ASSERT(node);
77  }
78 
79 
80  template<typename T>
81  const T& Iterator<T>::dereference(void) const
82  {
83  YAT_ASSERT(node_);
84  YAT_ASSERT(node_->is_head_node()==false);
85  // All nodes are NodeValue except head which is pointee of end
86  // iterator and not dereferencable
87  return static_cast<const NodeValue<T>*>(node_)->value();
88  }
89 
90 
91  template<typename T>
92  bool Iterator<T>::equal(Iterator<T> other) const
93  {
94  return node_ == other.node_;
95  }
96 
97 
98  template<typename T>
99  void Iterator<T>::increment(void)
100  {
101  YAT_ASSERT(node_);
102  YAT_ASSERT(!node_->is_head_node());
103  YAT_ASSERT(node_->validate());
104 
105  // If we have a right branch, go to the leftmost leaf in it.
106  if (node_->right_) {
107  node_ = node_->right_->left_most();
108  YAT_ASSERT(node_);
109  return;
110  }
111 
112  // traverse up through ancestors until we are coming from left
113  const NodeBase* child = node_;
114  YAT_ASSERT(child->parent_);
115  while (child->is_right_node()) {
116  child = child->parent_; // traverse up
117  YAT_ASSERT(child->parent_);
118  YAT_ASSERT(child->validate());
119  }
120 
121  node_ = child->parent_;
122  YAT_ASSERT(node_);
123  }
124 
125 
126  template<typename T>
127  void Iterator<T>::decrement(void)
128  {
129  YAT_ASSERT(node_);
130 
131  if (node_->is_head_node()) {
132  node_ = node_->left_->right_most();
133  return;
134  }
135 
136  if (node_->left_) {
137  node_ = node_->left_->right_most();
138  YAT_ASSERT(node_);
139  return;
140  }
141 
142  // traverse up through ancestors until we are coming from right
143  const NodeBase* child = node_;
144  YAT_ASSERT(child->parent_);
145  while (child->is_left_node()) {
146  child = child->parent_;
147  YAT_ASSERT(child->parent_);
148  }
149 
150  node_ = child->parent_;
151  YAT_ASSERT(node_);
152  }
153  } // end of namespace ranking
154 
156 
157 }}} // of namespace utility, yat, and theplu
158 #endif
The Department of Theoretical Physics namespace as we define it.

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14