@@ -864,7 +864,7 @@ class PooledAllocator
864864 /* We maintain memory alignment to word boundaries by requiring that all
865865 allocations be in multiples of the machine wordsize. */
866866 /* Size of machine word in bytes. Must be power of 2. */
867- /* Minimum number of bytes requested at a time from the system. Must be
867+ /* Minimum number of bytes requested at a time from the system. Must be
868868 * multiple of WORDSIZE. */
869869
870870 using Size = size_t ;
@@ -1102,7 +1102,7 @@ class KDTreeBaseClass
11021102 Size size (const Derived& obj) const { return obj.size_ ; }
11031103
11041104 /* * Returns the length of each point in the dataset */
1105- Size veclen (const Derived& obj) { return DIM > 0 ? DIM : obj.dim ; }
1105+ Size veclen (const Derived& obj) const { return DIM > 0 ? DIM : obj.dim ; }
11061106
11071107 // / Helper accessor to the dataset points:
11081108 ElementType dataset_get (
@@ -1112,19 +1112,22 @@ class KDTreeBaseClass
11121112 }
11131113
11141114 /* *
1115- * Computes the inde memory usage
1115+ * Computes the index memory usage
11161116 * Returns: memory used by the index
11171117 */
1118- Size usedMemory (Derived& obj)
1118+ Size usedMemory (const Derived& obj) const
11191119 {
11201120 return obj.pool_ .usedMemory + obj.pool_ .wastedMemory +
11211121 obj.dataset_ .kdtree_get_point_count () *
11221122 sizeof (IndexType); // pool memory and vind array memory
11231123 }
11241124
1125+ /* *
1126+ * Compute the minimum and maximum element values in the specified dimension
1127+ */
11251128 void computeMinMax (
11261129 const Derived& obj, Offset ind, Size count, Dimension element,
1127- ElementType& min_elem, ElementType& max_elem)
1130+ ElementType& min_elem, ElementType& max_elem) const
11281131 {
11291132 min_elem = dataset_get (obj, vAcc_[ind], element);
11301133 max_elem = min_elem;
@@ -1142,6 +1145,7 @@ class KDTreeBaseClass
11421145 *
11431146 * @param left index of the first vector
11441147 * @param right index of the last vector
1148+ * @param bbox bounding box used as input for splitting and output for parent node
11451149 */
11461150 NodePtr divideTree (
11471151 Derived& obj, const Offset left, const Offset right, BoundingBox& bbox)
@@ -1176,17 +1180,20 @@ class KDTreeBaseClass
11761180 }
11771181 else
11781182 {
1183+ /* Determine the index, dimension and value for split plane */
11791184 Offset idx;
11801185 Dimension cutfeat;
11811186 DistanceType cutval;
11821187 middleSplit_ (obj, left, right - left, idx, cutfeat, cutval, bbox);
11831188
11841189 node->node_type .sub .divfeat = cutfeat;
11851190
1191+ /* Recurse on left */
11861192 BoundingBox left_bbox (bbox);
11871193 left_bbox[cutfeat].high = cutval;
11881194 node->child1 = this ->divideTree (obj, left, left + idx, left_bbox);
11891195
1196+ /* Recurse on right */
11901197 BoundingBox right_bbox (bbox);
11911198 right_bbox[cutfeat].low = cutval;
11921199 node->child2 = this ->divideTree (obj, left + idx, right, right_bbox);
@@ -1211,6 +1218,7 @@ class KDTreeBaseClass
12111218 *
12121219 * @param left index of the first vector
12131220 * @param right index of the last vector
1221+ * @param bbox bounding box used as input for splitting and output for parent node
12141222 * @param thread_count count of std::async threads
12151223 * @param mutex mutex for mempool allocation
12161224 */
@@ -1249,6 +1257,7 @@ class KDTreeBaseClass
12491257 }
12501258 else
12511259 {
1260+ /* Determine the index, dimension and value for split plane */
12521261 Offset idx;
12531262 Dimension cutfeat;
12541263 DistanceType cutval;
@@ -1258,11 +1267,14 @@ class KDTreeBaseClass
12581267
12591268 std::future<NodePtr> right_future;
12601269
1270+ /* Recurse on right concurrently, if possible */
1271+
12611272 BoundingBox right_bbox (bbox);
12621273 right_bbox[cutfeat].low = cutval;
12631274 if (++thread_count < n_thread_build_)
12641275 {
1265- // Concurrent right sub-tree
1276+ /* Concurrent thread for right recursion */
1277+
12661278 right_future = std::async (
12671279 std::launch::async, &KDTreeBaseClass::divideTreeConcurrent,
12681280 this , std::ref (obj), left + idx, right,
@@ -1271,19 +1283,24 @@ class KDTreeBaseClass
12711283 }
12721284 else { --thread_count; }
12731285
1286+ /* Recurse on left in this thread */
1287+
12741288 BoundingBox left_bbox (bbox);
12751289 left_bbox[cutfeat].high = cutval;
12761290 node->child1 = this ->divideTreeConcurrent (
12771291 obj, left, left + idx, left_bbox, thread_count, mutex);
12781292
12791293 if (right_future.valid ())
12801294 {
1281- // Block and wait for concurrent right sub-tree
1295+ /* Block and wait for concurrent right from above */
1296+
12821297 node->child2 = right_future.get ();
12831298 --thread_count;
12841299 }
12851300 else
12861301 {
1302+ /* Otherwise, recurse on right in this thread */
1303+
12871304 node->child2 = this ->divideTreeConcurrent (
12881305 obj, left + idx, right, right_bbox, thread_count, mutex);
12891306 }
@@ -1359,16 +1376,18 @@ class KDTreeBaseClass
13591376 * corresponding to the 'cutfeat' dimension at 'cutval' position.
13601377 *
13611378 * On return:
1362- * dataset[ind[0..lim1-1]][cutfeat]< cutval
1363- * dataset[ind[lim1..lim2-1]][cutfeat]== cutval
1364- * dataset[ind[lim2..count]][cutfeat]> cutval
1379+ * dataset[ind[0..lim1-1]][cutfeat] < cutval
1380+ * dataset[ind[lim1..lim2-1]][cutfeat] == cutval
1381+ * dataset[ind[lim2..count]][cutfeat] > cutval
13651382 */
13661383 void planeSplit (
13671384 const Derived& obj, const Offset ind, const Size count,
13681385 const Dimension cutfeat, const DistanceType& cutval, Offset& lim1,
13691386 Offset& lim2)
13701387 {
1371- /* Move vector indices for left subtree to front of list. */
1388+ /* First pass.
1389+ * Determine lim1 with all values less than cutval to the left.
1390+ */
13721391 Offset left = 0 ;
13731392 Offset right = count - 1 ;
13741393 for (;;)
@@ -1385,8 +1404,9 @@ class KDTreeBaseClass
13851404 ++left;
13861405 --right;
13871406 }
1388- /* If either list is empty, it means that all remaining features
1389- * are identical. Split in the middle to maintain a balanced tree.
1407+ /* Second pass
1408+ * Determine lim2 with all values greater than cutval to the right
1409+ * The middle is used for balancing the tree
13901410 */
13911411 lim1 = left;
13921412 right = count - 1 ;
0 commit comments