134template <
typename T = Empty_Class>
220 other.root_ =
nullptr;
234 other.root_ =
nullptr;
373 if (
root_ ==
nullptr)
396 if (
root->y() == p.get_y())
398 if (
root->x() == p.get_x())
404 if (p.get_y() <
root->y())
415 if (
root->x() == p.get_x())
417 if (
root->y() == p.get_y())
423 if (p.get_x() <
root->x())
450 if (
const Point & point =
root->point_;
rect.contains(point))
494 if (
root->rect_.distance_squared_to(p) > st.dist2)
497 if (
const Geom_Number d2 =
root->point_.distance_squared_to(p); d2 < st.dist2)
503 if (p.get_x() <
root->x())
527 if (
root->rect_.distance_squared_to(p) > st.dist2)
530 if (
const Geom_Number d2 =
root->point_.distance_squared_to(p); d2 < st.dist2)
536 if (p.get_y() <
root->y())
568 return st.
node !=
nullptr ? std::optional<Point>(st.
node->
point_) : std::nullopt;
576 template <
typename Op>
604 if (points.
size() > 1)
614 for (
size_t r = 1;
r < points.
size(); ++
r)
615 if (points(
r) != points(
w - 1))
616 points(
w++) = points(
r);
618 while (points.
size() >
w)
640 const size_t lo,
const size_t hi,
647 const size_t mid = lo + (hi - lo) / 2;
668 for (
size_t i = lo; i <
mid; ++i)
714 template <
typename Op>
Core definitions, constants, and utility macros for Aleph-w.
High-level sorting functions for Aleph containers.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Represents a point with rectangular coordinates in a 2D plane.
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Geom_Number distance_squared_to(const Point &that) const
Calculates the squared Euclidean distance to another point.
An axis-aligned rectangle.
const Geom_Number & get_xmin() const
Gets the minimum x-coordinate.
void set_rect(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax)
Sets the coordinates of the rectangle.
const Geom_Number & get_ymax() const
Gets the maximum y-coordinate.
const Geom_Number & get_ymin() const
Gets the minimum y-coordinate.
const Geom_Number & get_xmax() const
Gets the maximum x-coordinate.
2D k-d tree for efficient spatial point operations.
bool insert(const Point &p)
Insert a point into the tree.
static Node * lr_search(Node *root, const Point &p) noexcept
Search with left-right (X) split pattern.
Point pmax_
Upper-right corner of reference rectangle.
K2Tree()
Default constructor - creates an empty tree with zero-sized region.
constexpr size_t size() const noexcept
Get the number of points in the tree.
K2Tree(Point pmin, Point pmax)
Construct a k-d tree with specified bounding region.
Node * root_
Root of the tree.
static void lr_nearest(Node *root, const Point &p, NearestState &st) noexcept
Recursively find the nearest neighbor (X-split level).
void range(const Rectangle &rect, DynList< Point > *l) const
Find all points within a rectangle.
static void range(Node *root, const Rectangle &rect, DynList< Point > *q)
Recursively find points within a rectangle.
K2Tree(const K2Tree &)=delete
Point pmin_
Lower-left corner of reference rectangle.
K2Tree(K2Tree &&other) noexcept
Move constructor — transfers ownership of all nodes.
void for_each(Op &&op) const
Apply an operation to every point in the tree (inorder).
static K2Tree build(Array< Point > points, const Point &pmin, const Point &pmax)
Build a balanced k-d tree from an array of points.
static Node * build_balanced(Array< Point > &pts, const size_t lo, const size_t hi, const bool split_x, const Point &rmin, const Point &rmax)
Recursively build a balanced subtree selecting medians.
K2Tree & operator=(const K2Tree &)=delete
Node * bu_insert(Node *root, const Point &p)
Insert into tree with bottom-up (Y) split.
bool contains(const Point &p) const noexcept
Check if the tree contains a point.
static void bu_nearest(Node *root, const Point &p, NearestState &st) noexcept
Recursively find the nearest neighbor (Y-split level).
std::optional< Point > nearest(const Point &p) const noexcept
Find the nearest point to a query point.
Node * lr_insert(Node *root, const Point &p)
Insert into tree with left-right (X) split.
static void for_each_node(const Node *node, Op &&op)
Recursive inorder traversal helper for for_each.
constexpr bool is_empty() const noexcept
Check if the tree is empty.
K2Tree(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax)
Construct a k-d tree with specified coordinate bounds.
~K2Tree()
Destructor - frees all nodes.
void destroy_tree(Node *node) noexcept
Recursively delete all nodes.
static Node * bu_search(Node *root, const Point &p) noexcept
Search with bottom-up (Y) split pattern.
size_t N_
Number of points in the tree.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Singly linked list implementations with head-tail access.
auto pmin(ThreadPool &pool, const Container &c, size_t chunk_size=0)
Parallel minimum element.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
auto pmax(ThreadPool &pool, const Container &c, size_t chunk_size=0)
Parallel maximum element.
2D point and geometric utilities.
Empty placeholder class with no data members.
Thread-local state for nearest-neighbor recursion.
Internal node structure for the k-d tree.
Point point_
The point stored at this node.
const Geom_Number & ymin() const noexcept
const Geom_Number & xmin() const noexcept
const Geom_Number & x() const noexcept
Node * rt_
Right/top subtree.
void set_rect(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax) noexcept
Set the rectangular region for this node.
const Geom_Number & y() const noexcept
Node(Point p) noexcept
Construct a node with a given point.
const Geom_Number & ymax() const noexcept
void set_rect(const Point &pmin, const Point &pmax) noexcept
Set the rectangular region using corner points.
const Geom_Number & xmax() const noexcept
Node * lb_
Left/bottom subtree.
Rectangle rect_
The axis-aligned rectangle for this node's region.
Dynamic array container with automatic resizing.