cheshirekow  v0.1.0
insert.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_KD2_INSERT_H_
28 #define MPBLOCKS_KD2_INSERT_H_
29 
30 #include <mpblocks/kd2/BinaryKey.h>
31 
32 namespace mpblocks {
33 namespace kd2 {
34 
35 
36 template<
37  class NodeRef,
38  class PointRef,
39  class IsLeafFn,
40  class LeafInsertFn,
41  class IdxFn,
42  class ValueFn,
43  class ChildFn,
44  class PointGetFn
45 >
46 void insert(NodeRef node,
47  PointRef point,
48  const IsLeafFn& isLeaf,
49  const LeafInsertFn& leafInsert,
50  const IdxFn& idx,
51  const ValueFn& value,
52  const ChildFn& child,
53  const PointGetFn& pointGet
54  )
55 {
56  NodeRef parent = node;
57  // walk down the tree until we reach a leaf
58  while( !isLeaf( node ) )
59  {
60  int i = idx( node );
61  BinaryKey which = ( pointGet(point,i) <= value(node) ) ? MIN : MAX;
62  node = child(node,which);
63  }
64 
65  // if we've reached a leaf node, then insert the point, if the leaf needs
66  // to split then this function also does the splitting
67  leafInsert(node,point);
68 }
69 
70 
71 template<
72  class HyperRect,
73  class NodeRef,
74  class PointRef,
75  class IsLeafFn,
76  class LeafInsertFn,
77  class IdxFn,
78  class ValueFn,
79  class ChildFn,
80  class PointGetFn,
81  class HyperSetFn
82 >
83 void insert(HyperRect& cell,
84  NodeRef node,
85  PointRef point,
86  const IsLeafFn& isLeaf,
87  const LeafInsertFn& leafInsert,
88  const IdxFn& idx,
89  const ValueFn& value,
90  const ChildFn& child,
91  const PointGetFn& pointGet,
92  const HyperSetFn& hyperSet
93  )
94 {
95  NodeRef parent = node;
96  // walk down the tree until we reach a leaf
97  while( !isLeaf( node ) )
98  {
99  int i = idx( node );
100  BinaryKey which = ( pointGet(point,i) <= value(node) ) ? MIN : MAX;
101  node = child(node,which);
102  hyperSet(cell,other(which),i,value(node));
103  }
104 
105  // if we've reached a leaf node, then insert the point, if the leaf needs
106  // to split then this function also does the splitting
107  leafInsert(cell,node,point);
108 }
109 
110 
111 
112 
113 
114 } //< namespace kd2
115 } //< namespace mpblocks
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 #endif // INSERT_H_
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
void insert(NodeRef node, PointRef point, const IsLeafFn &isLeaf, const LeafInsertFn &leafInsert, const IdxFn &idx, const ValueFn &value, const ChildFn &child, const PointGetFn &pointGet)
Definition: insert.h:46