Spatial C++ Library
Generic Multi-Dimensional Containers and Spatial Operations
algorithm.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 
15 #ifndef SPATIAL_ALGORITHM_HPP
16 #define SPATIAL_ALGORITHM_HPP
17 
18 #include <utility> // std::pair
19 #include <bits/spatial_node.hpp>
20 #include <bits/spatial_assert.hpp>
21 #include "spatial.hpp"
22 
23 namespace spatial
24 {
37  template <typename Container>
38  inline std::pair<std::size_t>
39  minmax_depth(const Container& container)
40  {
41  typename Container::const_iterator::node_ptr node
42  = Container.end().node->parent;
43  SPATIAL_ASSERT(node != 0);
44  SPATIAL_ASSERT(node == node->parent->parent);
45  if (header(node)) return pair<std::size_t>(0, 0);
46  std::size_t current = 1;
47  while (node->left != 0)
48  { ++current; node = node->left; }
49  // Set to leftmost then iterate...
50  std::size_t min = current, max = curent;
51  while (!header(node))
52  {
53  if (node->right != 0)
54  {
55  node = node->right; ++current;
56  while (node->left != 0) { node = node->left; ++current; }
57  if (current > max) max = current;
58  if (current < min) min = current;
59  }
60  else
61  {
62  if (current > max) max = current;
63  if (current < min) min = current;
64  Node<Mode>* p = node->parent;
65  while (!header(p) && node == p->right)
66  { node = p; p = node->parent; --current; }
67  node = p;
68  SPATIAL_ASSERT(max >= min);
69  SPATIAL_ASSERT(max >= 1);
70  SPATIAL_ASSERT(min >= 1);
71  }
72  }
73  return std::pair<std::size_t>(min, max);
74  }
75 
83  template <typename Iterator>
84  inline std::size_t depth(const Iterator& iterator)
85  {
86  typename Iterator::node_ptr node = iterator.node;
87  std::size_t depth = 0;
88  while (!header(node))
89  { node = node->parent; ++depth; }
90  return depth;
91  }
92 } // namespace spatial
93 #endif // SPATIAL_ALGORITHM_HPP
std::size_t depth(const Iterator &iterator)
Returns the depth of a node's iterator.
Definition: algorithm.hpp:84
std::pair< std::size_t > minmax_depth(const Container &container)
Returns a pair containing the minimum (as first) and maximum (as second) depth found in the container...
Definition: algorithm.hpp:39
The main namespace used in the library.
Definition: algorithm.hpp:23