From cb32001998c8097dd04b97e11f2ef7ab6dc4e0e4 Mon Sep 17 00:00:00 2001 From: Jarl Holta Date: Sat, 28 Dec 2024 05:15:18 +0100 Subject: [PATCH] Tweak docgen - support nested comments --- DocGen/docgen.py | 36 +++++++++++++++++++++++++++---- Source/simba.container_kdtree.pas | 6 +++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/DocGen/docgen.py b/DocGen/docgen.py index 2a7f1546a..7b8eac12a 100644 --- a/DocGen/docgen.py +++ b/DocGen/docgen.py @@ -9,6 +9,30 @@ SRC_DIR = '../Source/script/imports/' BUILD_DIR = 'source/api/' +# between that handles nested comments, regex did not handle this! +def between(str, start, stop): + result = [] + init = 0 + while True: + first = str.find(start, init) + if first == -1: break + + first += len(start) + last = str.find(stop, first) + + if last == -1: break + + nested_start = str.find(start, first) + while nested_start != -1 and nested_start < last: + last = str.find(stop, last + 1) + if last == -1: break + nested_start = str.find(start, nested_start + len(start)) + + result.append(str[first:last]) + init = last + len(stop) + return result + + # remove old build files, all but index for file in glob.glob(f"{BUILD_DIR}*.md"): os.remove(file) @@ -17,20 +41,24 @@ comments = [] for source in glob.glob(f"{SRC_DIR}*.pas"): with open(source, 'r') as f: - comments.extend(re.findall(r'\(\*(.*?)\*\)', f.read(), re.DOTALL)) - + comments.extend(between(f.read(), '(*', '*)')) + + # write comments, changing the file when a header is found # header being a === line rather than --- currentfile = None for comment in comments: comment = comment.strip() - if (comment.splitlines()[1][0] == '='): + lines = comment.splitlines(); + + if (len(lines[1]) > 0) and (lines[1][0] == '='): if currentfile: currentfile.close() - currentfile = open(f"{BUILD_DIR}/{comment.splitlines()[0]}.md", 'a') + currentfile = open(f"{BUILD_DIR}/{lines[0]}.md", 'a') currentfile.write(comment) else: currentfile.write('\n\n' + '-----' + '\n\n' + comment) + if currentfile: currentfile.close() diff --git a/Source/simba.container_kdtree.pas b/Source/simba.container_kdtree.pas index 9d5d0e971..a333901e7 100644 --- a/Source/simba.container_kdtree.pas +++ b/Source/simba.container_kdtree.pas @@ -663,8 +663,12 @@ function TKDTree.WeightedKNearestClassify(Vector: TSingleArray; K: Int32): Int32 Implements an efficient n-dimensional spatial clustering algorithm using a KD-Tree. It supports label-based filtering to cluster points within specific categories. + Best case: O(n) Average Time Complexity: O(n log n) - Worst Case Time Complexity: O(n log n) + Worst Case Time Complexity: O(n^2) in theory .. Maybe still just O(n log n) or thereabout. + + Best case of O(n) can happen when we prune all branches in the first pass, + this requires a radii that covers all points from anywhere or luck point selection. TODO: Add a version that returns T2DIntegerArray where each version represents an index in KDTree.Data.