Stride Reference Manual  - generated for commit 9643b11
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KdNode.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, 2019, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #pragma once
17 
18 #include <cstddef>
19 #include <memory>
20 #include <utility>
21 
22 namespace geopop {
23 
24 template <typename P>
25 class KdTree;
26 
27 namespace kd {
28 
32 template <typename P>
33 class BaseNode
34 {
35 public:
36  virtual ~BaseNode() = default;
37 
39  virtual BaseNode<P>* BorrowLeft() const = 0;
40 
42  virtual BaseNode<P>* BorrowRight() const = 0;
43 
45  virtual BaseNode<P>* BorrowSplitChild(const P& point) const = 0;
46 
48  virtual void AddChild(P point) = 0;
49 
51  virtual P GetPoint() const = 0;
52 };
53 
57 template <typename P, std::size_t D>
58 class Node : public BaseNode<P>
59 {
60 public:
61  explicit Node(P pt) : m_point(pt), m_left(nullptr), m_right(nullptr) {}
62 
63  BaseNode<P>* BorrowLeft() const override { return m_left.get(); }
64 
65  BaseNode<P>* BorrowRight() const override { return m_right.get(); }
66 
67  BaseNode<P>* BorrowSplitChild(const P& point) const override
68  {
69  auto refval = m_point.template Get<D>();
70  auto testval = point.template Get<D>();
71  if (testval <= refval) {
72  return m_left.get();
73  } else {
74  return m_right.get();
75  }
76  }
77 
78  void AddChild(P point) override
79  {
80  auto refval = m_point.template Get<D>();
81  auto testval = point.template Get<D>();
82  if (testval <= refval) {
83  m_left = std::make_unique<Child>(point);
84  } else {
85  m_right = std::make_unique<Child>(point);
86  }
87  }
88 
89  P GetPoint() const override { return m_point; }
90 
91 private:
93 
95  std::unique_ptr<Child> m_left, m_right;
96 
97  friend class KdTree<P>;
98 };
99 
100 } // namespace kd
101 } // namespace geopop
void AddChild(P point) override
Add a new child in the right place, according to split.
Definition: KdNode.h:78
P GetPoint() const override
Gets the point for this node.
Definition: KdNode.h:89
BaseNode< P > * BorrowSplitChild(const P &point) const override
Get a non-owning pointer to the child corresponding to the correct split for point.
Definition: KdNode.h:67
Node(P pt)
Definition: KdNode.h:61
A k-d tree: a k-dimensional generalization of binary search trees This data structure allows for effi...
Definition: KdNode.h:25
virtual BaseNode< P > * BorrowRight() const =0
Get a non-owning pointer to the right child (nullptr if no such child).
virtual BaseNode< P > * BorrowSplitChild(const P &point) const =0
Get a non-owning pointer to the child corresponding to the correct split for point.
A node in the KdTree (parameter P: the type of point, parameter D: dimension this node splits on)...
Definition: KdNode.h:58
virtual ~BaseNode()=default
std::unique_ptr< Child > m_right
Definition: KdNode.h:95
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
virtual P GetPoint() const =0
Gets the point for this node.
virtual void AddChild(P point)=0
Add a new child in the right place, according to split.
virtual BaseNode< P > * BorrowLeft() const =0
Get a non-owning pointer to the left child (nullptr if no such child).
BaseNode< P > * BorrowLeft() const override
Get a non-owning pointer to the left child (nullptr if no such child).
Definition: KdNode.h:63
BaseNode< P > * BorrowRight() const override
Get a non-owning pointer to the right child (nullptr if no such child).
Definition: KdNode.h:65
A base class for all instantiations of a Node with D.
Definition: KdNode.h:33