Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
spatial_bidirectional.hpp
1 // -*- C++ -*-
2 //
3 // Copyright Sylvain Bougerel 2009 - 2013.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file COPYING or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 
13 #ifndef SPATIAL_BIDIRECTIONAL_HPP
14 #define SPATIAL_BIDIRECTIONAL_HPP
15 
16 #include "spatial_node.hpp"
17 
18 namespace spatial
19 {
20  namespace details
21  {
32  template <typename Link, typename Rank>
33  class Bidirectional_iterator : private Rank
34  {
35  public:
40  typedef typename Link::value_type& reference;
42  typedef typename Link::value_type* pointer;
44  typedef std::ptrdiff_t difference_type;
46  typedef std::bidirectional_iterator_tag iterator_category;
48  typedef typename Link::node_ptr node_ptr;
50  typedef Rank rank_type;
52  typedef typename Link::invariant_category invariant_category;
53 
56 
58  Bidirectional_iterator(const Rank& rank_, node_ptr node_,
59  dimension_type node_dim_)
60  : Rank(rank_), node(node_), node_dim(node_dim_) { }
61 
63  reference operator*()
64  { return value(node); }
65 
67  pointer operator->()
68  { return &value(node); }
69 
77  { return node == x.node; }
78 
86  { return node != x.node; }
87 
89 
97  operator Node_iterator<Link>() const
98  { return Node_iterator<Link>(node); }
99 
100  operator Const_node_iterator<Link>() const
101  { return Const_node_iterator<Link>(node); }
103 
107  const rank_type& rank() const { return static_cast<const Rank&>(*this); }
108 
113  { return static_cast<const Rank&>(*this)(); }
114 
122  node_ptr node;
123 
132  };
133 
144  template <typename Link, typename Rank>
145  class Const_bidirectional_iterator : private Rank
146  {
147  public:
152  typedef const typename Link::value_type& reference;
154  typedef const typename Link::value_type* pointer;
156  typedef std::ptrdiff_t difference_type;
158  typedef std::bidirectional_iterator_tag iterator_category;
160  typedef typename Link::const_node_ptr node_ptr;
162  typedef Rank rank_type;
164  typedef typename Link::invariant_category invariant_category;
165 
168 
170  Const_bidirectional_iterator(const Rank& rank_, node_ptr node_,
171  dimension_type node_dim_)
172  : Rank(rank_), node(node_), node_dim(node_dim_) { }
173 
175  reference operator*()
176  { return const_value(node); }
177 
179  pointer operator->()
180  { return &const_value(node); }
181 
189  { return node == x.node; }
190 
199  { return node != x.node; }
200 
206  operator Const_node_iterator<Link>() const
207  { return Const_node_iterator<Link>(node); }
208 
212  rank_type rank() const { return static_cast<const Rank>(*this); }
213 
219  { return static_cast<const Rank&>(*this)(); }
220 
228  node_ptr node;
229 
238  };
239 
240  } // namespace details
241 } // namespace spatial
242 
243 #endif // SPATIAL_BIDIRECTIONAL_HPP
Bidirectional_iterator(const Rank &rank_, node_ptr node_, dimension_type node_dim_)
Initialize the node at construction time.
reference operator*()
Returns the reference to the value pointed to by the iterator.
A bidirectional iterator traversing all node in the tree in inorder traversal.
A bidirectional iterator traversing all node in the tree in inorder traversal.
dimension_type dimension() const
Return the current number of dimensions given by the Rank of the iterator.
Link::const_node_ptr node_ptr
The type for the node pointed to by the iterator.
bool operator!=(const Const_node_iterator< Link > &x) const
A bidirectional iterator can be compared for inequality with a node iterator if they work on identica...
node_ptr node
The node pointed to by the iterator.
const Link::value_type * pointer
The pointer type of the object pointed to by the iterator.
rank_type rank() const
Return the current Rank type used by the iterator.
const rank_type & rank() const
Return the current Rank type used by the iterator.
Bidirectional_iterator()
Build an uninitialized iterator.
node_ptr node
The pointer to the current node.
Link::value_type * pointer
The pointer type of the object pointed to by the iterator.
dimension_type dimension() const
Return the number of dimensions stored by the Rank of the iterator.
std::ptrdiff_t difference_type
The difference_type returned by the distance between 2 iterators.
node_ptr node
The pointer to the current node.
Link::invariant_category invariant_category
The invariant category of the the iterator.
Rank rank_type
The type of rank used by the iterator.
const Kdtree_link< Key, Value >::value_type & const_value(const Node< Kdtree_link< Key, Value > > *node)
This function converts a pointer on a node into a value for a Kdtree_link type.
reference operator*()
Returns the reference to the value pointed to by the iterator.
dimension_type node_dim
The dimension of the current node.
const Link::value_type & reference
The reference type of the object pointed to by the iterator.
Rank rank_type
The type of rank used by the iterator.
mutate< typename Link::value_type >::type value_type
The value_type can receive a copy of the reference pointed to be the iterator.
Link::value_type & reference
The reference type of the object pointed to by the iterator.
Link::node_ptr node_ptr
The type for the node pointed to by the iterator.
std::size_t dimension_type
Defines the type for the dimension as being a size.
Definition: spatial.hpp:71
mutate< typename Link::value_type >::type value_type
The value_type can receive a copy of the reference pointed to be the iterator.
bool operator==(const Const_node_iterator< Link > &x) const
A bidirectional iterator can be compared with a node iterator if they work on identical linking modes...
Link::invariant_category invariant_category
The invariant category of the the iterator.
Const_bidirectional_iterator()
Build an uninitialized iterator.
Const_bidirectional_iterator(const Rank &rank_, node_ptr node_, dimension_type node_dim_)
Initialize the node at construction time.
The main namespace used in the library.
Definition: algorithm.hpp:23
A common template for constant bidirectional iterators that work on identical modes of linking...
std::ptrdiff_t difference_type
The difference_type returned by the distance between 2 iterators.
A common template for bidirectional iterators that work on identical modes of linking.
bool operator==(const Const_node_iterator< Link > &x) const
A bidirectional iterator can be compared with a node iterator if they work on identical linking modes...
std::bidirectional_iterator_tag iterator_category
The iterator category that is always Bidirectional_iterator_tag.
bool operator!=(const Const_node_iterator< Link > &x) const
A bidirectional iterator can be compared for inequality with a node iterator if they work on identica...
Kdtree_link< Key, Value >::value_type & value(Node< Kdtree_link< Key, Value > > *node)
This function converts a pointer on a node into a value for a Kdtree_link type.
dimension_type node_dim
The dimension of the current node.
pointer operator->()
Returns a pointer to the value pointed to by the iterator.
std::bidirectional_iterator_tag iterator_category
The iterator category that is always Bidirectional_iterator_tag.
pointer operator->()
Returns a pointer to the value pointed to by the iterator.