Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::RangeTree2D Class Reference

Static 2D range tree for orthogonal range queries. More...

#include <geom_algorithms.H>

Collaboration diagram for Aleph::RangeTree2D:
[legend]

Classes

struct  DebugNode
 Represents a node in the range tree for debugging. More...
 
struct  DebugSnapshot
 A complete snapshot of the range tree for debugging. More...
 
struct  Node
 Internal node structure containing the y-sorted secondary array. More...
 

Public Member Functions

 RangeTree2D ()=default
 Build an empty range tree; build() fills it.
 
 RangeTree2D (const RangeTree2D &other)=default
 Copy the points and the tree of another range tree.
 
 RangeTree2D (RangeTree2D &&other) noexcept
 Take the points and the tree of another range tree, which is left empty.
 
RangeTree2D & operator= (const RangeTree2D &other)=default
 Copy the points and the tree of another range tree.
 
RangeTree2D & operator= (RangeTree2D &&other) noexcept
 Exchange this range tree with another, which is left with the previous points of this one.
 
void swap (RangeTree2D &other) noexcept
 Exchange in O(1) the points and the tree of this range tree and another.
 
void build (const DynList< Point > &points)
 Build the range tree from a point set.
 
DynList< Point > query (const Geom_Number &xmin, const Geom_Number &xmax, const Geom_Number &ymin, const Geom_Number &ymax) const
 Query: return all points inside [xmin,xmax] × [ymin,ymax].
 
size_t size () const noexcept
 
bool is_empty () const noexcept
 
DebugSnapshot debug_snapshot () const
 Return structural snapshot for visualization/debug.
 

Private Member Functions

size_t lower_bound_x (const Geom_Number &xval) const
 Binary search: first index i in pts_ where pts_(i).get_x() >= xval.
 
size_t upper_bound_x (const Geom_Number &xval) const
 Binary search: first index i in pts_ where pts_(i).get_x() > xval.
 
void build_node (const size_t node, const size_t lo, const size_t hi)
 
void query_range (const size_t node, const size_t lo, const size_t hi, const size_t qlo, const size_t qhi, const Geom_Number &ymin, const Geom_Number &ymax, DynList< Point > &out) const
 

Static Private Member Functions

static size_t lower_bound_y (const Array< Point > &arr, const Geom_Number &ymin)
 Binary search: first index i in arr where arr(i).get_y() >= ymin.
 
static size_t upper_bound_y (const Array< Point > &arr, const Geom_Number &ymax)
 Binary search: first index i in arr where arr(i).get_y() > ymax.
 

Private Attributes

Array< Point > pts_
 points sorted by x (primary key)
 
Array< Node > tree_
 implicit binary tree (1-indexed)
 
size_t n_ = 0
 
bool built_ = false
 

Detailed Description

Static 2D range tree for orthogonal range queries.

Supports axis-aligned rectangle queries: given [xmin,xmax] × [ymin,ymax], find all points inside.

Complexity

  • Build: O(n log n) time, O(n log n) space
  • Query: O(log² n + k) where k = output size
Example
pts.append(Point(0,0));
pts.append(Point(2,1));
pts.append(Point(3,3));
rt.build(pts);
rt.query(0, 0, 2, 2, out);
size_t size_t int32_t * out
Definition ca-c-api.h:120
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
Static 2D range tree for orthogonal range queries.
DynList< Point > query(const Geom_Number &xmin, const Geom_Number &xmax, const Geom_Number &ymin, const Geom_Number &ymax) const
Query: return all points inside [xmin,xmax] × [ymin,ymax].
void build(const DynList< Point > &points)
Build the range tree from a point set.

Definition at line 8180 of file geom_algorithms.H.

Constructor & Destructor Documentation

◆ RangeTree2D() [1/3]

Aleph::RangeTree2D::RangeTree2D ( )
default

Build an empty range tree; build() fills it.

◆ RangeTree2D() [2/3]

Aleph::RangeTree2D::RangeTree2D ( const RangeTree2D &  other)
default

Copy the points and the tree of another range tree.

Parameters
[in]otherrange tree to copy.
Exceptions
std::bad_allocif the arrays cannot be allocated.

◆ RangeTree2D() [3/3]

Aleph::RangeTree2D::RangeTree2D ( RangeTree2D &&  other)
inlinenoexcept

Take the points and the tree of another range tree, which is left empty.

The implicit move took the arrays but copied their size: the moved-from range tree then searched points that it no longer had.

Parameters
[in,out]otherrange tree to move from.
Note
Complexity: O(1).

Definition at line 8348 of file geom_algorithms.H.

Member Function Documentation

◆ build()

◆ build_node()

void Aleph::RangeTree2D::build_node ( const size_t  node,
const size_t  lo,
const size_t  hi 
)
inlineprivate

Definition at line 8271 of file geom_algorithms.H.

References Aleph::and, build_node(), pts_, and tree_.

Referenced by build(), and build_node().

◆ debug_snapshot()

DebugSnapshot Aleph::RangeTree2D::debug_snapshot ( ) const
inline

◆ is_empty()

bool Aleph::RangeTree2D::is_empty ( ) const
inlinenoexcept

Definition at line 8437 of file geom_algorithms.H.

References n_.

Referenced by Moved_From_Case< RangeTree2D >::is_empty(), and TEST_F().

◆ lower_bound_x()

size_t Aleph::RangeTree2D::lower_bound_x ( const Geom_Number &  xval) const
inlineprivate

Binary search: first index i in pts_ where pts_(i).get_x() >= xval.

Definition at line 8248 of file geom_algorithms.H.

References n_, and pts_.

Referenced by query().

◆ lower_bound_y()

static size_t Aleph::RangeTree2D::lower_bound_y ( const Array< Point > &  arr,
const Geom_Number &  ymin 
)
inlinestaticprivate

Binary search: first index i in arr where arr(i).get_y() >= ymin.

Definition at line 8224 of file geom_algorithms.H.

References Aleph::Array< T >::size().

Referenced by query_range().

◆ operator=() [1/2]

RangeTree2D & Aleph::RangeTree2D::operator= ( const RangeTree2D &  other)
default

Copy the points and the tree of another range tree.

Parameters
[in]otherrange tree to copy.
Returns
this range tree.
Exceptions
std::bad_allocif the arrays cannot be allocated.

◆ operator=() [2/2]

RangeTree2D & Aleph::RangeTree2D::operator= ( RangeTree2D &&  other)
inlinenoexcept

Exchange this range tree with another, which is left with the previous points of this one.

Parameters
[in,out]otherrange tree to move from.
Returns
this range tree.
Note
Complexity: O(1).

Definition at line 8366 of file geom_algorithms.H.

References swap().

◆ query()

DynList< Point > Aleph::RangeTree2D::query ( const Geom_Number &  xmin,
const Geom_Number &  xmax,
const Geom_Number &  ymin,
const Geom_Number &  ymax 
) const
inline

Query: return all points inside [xmin,xmax] × [ymin,ymax].

Definition at line 8414 of file geom_algorithms.H.

References built_, lower_bound_x(), n_, out, query_range(), and upper_bound_x().

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), Moved_From_Case< RangeTree2D >::use(), and Aleph::visualize_range_tree_query().

◆ query_range()

void Aleph::RangeTree2D::query_range ( const size_t  node,
const size_t  lo,
const size_t  hi,
const size_t  qlo,
const size_t  qhi,
const Geom_Number &  ymin,
const Geom_Number &  ymax,
DynList< Point > &  out 
) const
inlineprivate

Definition at line 8302 of file geom_algorithms.H.

References Aleph::and, lower_bound_y(), out, query_range(), tree_, and upper_bound_y().

Referenced by query(), and query_range().

◆ size()

size_t Aleph::RangeTree2D::size ( ) const
inlinenoexcept

◆ swap()

void Aleph::RangeTree2D::swap ( RangeTree2D &  other)
inlinenoexcept

Exchange in O(1) the points and the tree of this range tree and another.

Parameters
[in,out]otherrange tree to exchange with.

Definition at line 8376 of file geom_algorithms.H.

References built_, n_, pts_, and tree_.

Referenced by operator=().

◆ upper_bound_x()

size_t Aleph::RangeTree2D::upper_bound_x ( const Geom_Number &  xval) const
inlineprivate

Binary search: first index i in pts_ where pts_(i).get_x() > xval.

Definition at line 8260 of file geom_algorithms.H.

References n_, and pts_.

Referenced by query().

◆ upper_bound_y()

static size_t Aleph::RangeTree2D::upper_bound_y ( const Array< Point > &  arr,
const Geom_Number &  ymax 
)
inlinestaticprivate

Binary search: first index i in arr where arr(i).get_y() > ymax.

Definition at line 8236 of file geom_algorithms.H.

References Aleph::Array< T >::size().

Referenced by query_range().

Member Data Documentation

◆ built_

bool Aleph::RangeTree2D::built_ = false
private

Definition at line 8221 of file geom_algorithms.H.

Referenced by build(), debug_snapshot(), query(), and swap().

◆ n_

size_t Aleph::RangeTree2D::n_ = 0
private

◆ pts_

Array<Point> Aleph::RangeTree2D::pts_
private

points sorted by x (primary key)

Definition at line 8218 of file geom_algorithms.H.

Referenced by build(), build_node(), debug_snapshot(), lower_bound_x(), swap(), and upper_bound_x().

◆ tree_

Array<Node> Aleph::RangeTree2D::tree_
private

implicit binary tree (1-indexed)

Definition at line 8219 of file geom_algorithms.H.

Referenced by build(), build_node(), debug_snapshot(), query_range(), and swap().


The documentation for this class was generated from the following file: