|
OpenTTD Source 20260218-master-g2123fca5ea
|
K-dimensional tree, specialised for 2-dimensional space. More...
#include <kdtree.hpp>
Data Structures | |
| struct | node |
| Type of a node in the tree. More... | |
Public Member Functions | |
| Kdtree () | |
| Construct a new Kdtree with the given xyfunc. | |
| template<typename It> | |
| void | Build (It begin, It end) |
| Clear and rebuild the tree from a new sequence of elements,. | |
| void | Clear () |
| Clear the tree. | |
| void | Rebuild () |
| Reconstruct the tree with the same elements, letting it be fully balanced. | |
| void | Insert (const T &element) |
| Insert a single element in the tree. | |
| void | Remove (const T &element) |
| Remove a single element from the tree, if it exists. | |
| size_t | Count () const |
| Get number of elements stored in tree. | |
| T | FindNearest (CoordT x, CoordT y) const |
| Find the element closest to given coordinate, in Manhattan distance. | |
| template<typename Outputter> | |
| void | FindContained (CoordT x1, CoordT y1, CoordT x2, CoordT y2, const Outputter &outputter) const |
| Find all items contained within the given rectangle. | |
| std::vector< T > | FindContained (CoordT x1, CoordT y1, CoordT x2, CoordT y2) const |
| Find all items contained within the given rectangle. | |
Private Types | |
| using | node_distance = std::pair<T, DistT> |
| A data element and its distance to a searched-for point. | |
Private Member Functions | |
| size_t | AddNode (const T &element) |
| Create one new node in the tree. | |
| template<typename It> | |
| CoordT | SelectSplitCoord (It begin, It end, int level) |
| Find a coordinate value to split a range of elements at. | |
| template<typename It> | |
| size_t | BuildSubtree (It begin, It end, int level) |
| Construct a subtree from elements between begin and end iterators. | |
| bool | Rebuild (const T *include_element, const T *exclude_element) |
| Rebuild the tree with all existing elements, optionally adding or removing one more. | |
| void | InsertRecursive (const T &element, size_t node_idx, int level) |
| Insert one element in the tree somewhere below node_idx. | |
| std::vector< T > | FreeSubtree (size_t node_idx) |
| Free all children of the given node. | |
| size_t | RemoveRecursive (const T &element, size_t node_idx, int level) |
| Find and remove one element from the tree. | |
| DistT | ManhattanDistance (const T &element, CoordT x, CoordT y) const |
| node_distance | FindNearestRecursive (CoordT xy[2], size_t node_idx, int level, DistT limit=std::numeric_limits< DistT >::max()) const |
| Search a sub-tree for the element nearest to a given point. | |
| template<typename Outputter> | |
| void | FindContainedRecursive (CoordT p1[2], CoordT p2[2], size_t node_idx, int level, const Outputter &outputter) const |
| size_t | CountValue (const T &element, size_t node_idx) const |
| Debugging function, counts number of occurrences of an element regardless of its correct position in the tree. | |
| void | IncrementUnbalanced (size_t amount=1) |
| bool | IsUnbalanced () const |
| Check if the entire tree is in need of rebuilding. | |
| void | CheckInvariant (size_t node_idx, int level, CoordT min_x, CoordT max_x, CoordT min_y, CoordT max_y) const |
| Verify that the invariant is true for a sub-tree, assert if not. | |
| void | CheckInvariant () const |
| Verify the invariant for the entire tree, does nothing unless KDTREE_DEBUG is defined. | |
Static Private Member Functions | |
| static node_distance | SelectNearestNodeDistance (const node_distance &a, const node_distance &b) |
| Ordering function for node_distance objects, elements with equal distance are ordered by less-than comparison. | |
Private Attributes | |
| std::vector< node > | nodes |
| Pool of all nodes in the tree. | |
| std::vector< size_t > | free_list |
| List of dead indices in the nodes vector. | |
| size_t | root |
| Index of root node. | |
| size_t | unbalanced |
| Number approximating how unbalanced the tree might be. | |
Static Private Attributes | |
| static const size_t | INVALID_NODE = SIZE_MAX |
| Index value indicating no-such-node. | |
| static const size_t | MIN_REBALANCE_THRESHOLD = 8 |
| Arbitrary value for "not worth rebalancing". | |
K-dimensional tree, specialised for 2-dimensional space.
This is not intended as a primary storage of data, but as an index into existing data. Usually the type stored by this tree should be an index into an existing array.
This implementation assumes Manhattan distances are used.
Be careful when using this in game code, depending on usage pattern, the tree shape may end up different for different clients in multiplayer, causing iteration order to differ and possibly having elements returned in different order. The using code should be designed to produce the same result regardless of iteration order.
The element type T must be less-than comparable for FindNearest to work.
| T | Type stored in the tree, should be cheap to copy. |
| TxyFunc | Functor type to extract coordinate from a T value and dimension index (0 or 1). |
| CoordT | Type of coordinate values extracted via TxyFunc. |
| DistT | Type to use for representing distance values. |
Definition at line 33 of file kdtree.hpp.
|
private |
A data element and its distance to a searched-for point.
Definition at line 254 of file kdtree.hpp.
|
inline |
Construct a new Kdtree with the given xyfunc.
Definition at line 406 of file kdtree.hpp.
|
inlineprivate |
Create one new node in the tree.
| element | The element to add. |
Definition at line 56 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::BuildSubtree(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Insert(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::InsertRecursive().
|
inline |
Clear and rebuild the tree from a new sequence of elements,.
| It | Iterator type for element sequence. |
| begin | First element in sequence. |
| end | One past last element in sequence. |
Definition at line 415 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Rebuild().
|
inlineprivate |
Construct a subtree from elements between begin and end iterators.
| begin | The begin of the range to consider. |
| end | The end of the range to consider. |
| level | The depth into the tree. |
Definition at line 92 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Build(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::BuildSubtree(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::RemoveRecursive().
|
inlineprivate |
Verify the invariant for the entire tree, does nothing unless KDTREE_DEBUG is defined.
Definition at line 397 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Build(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::CheckInvariant(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::CheckInvariant(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Insert(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Remove().
|
inlineprivate |
Verify that the invariant is true for a sub-tree, assert if not.
| node_idx | The root of the sub-tree. |
| level | The current level in the tree. |
| min_x | The expected minimum X-coordinate. |
| max_x | The expected maximum X-coordinate. |
| min_y | The expected minimum Y-coordinate. |
| max_y | The expected maximum Y-coordinate. |
Definition at line 372 of file kdtree.hpp.
|
inline |
Clear the tree.
Definition at line 430 of file kdtree.hpp.
|
inline |
Get number of elements stored in tree.
Definition at line 488 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::FindContained(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::FindNearest(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Insert(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::IsUnbalanced(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Rebuild(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Remove().
|
inlineprivate |
Debugging function, counts number of occurrences of an element regardless of its correct position in the tree.
| element | The element to look for. |
| node_idx | The root to start searching from. |
Definition at line 340 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::CountValue().
|
inline |
Find all items contained within the given rectangle.
| x1 | Start first coordinate, points found are greater or equals to this. |
| y1 | Start second coordinate, points found are greater or equals to this. |
| x2 | End first coordinate, points found are less than this. |
| y2 | End second coordinate, points found are less than this. |
Definition at line 541 of file kdtree.hpp.
|
inline |
Find all items contained within the given rectangle.
| x1 | Start first coordinate, points found are greater or equals to this. |
| y1 | Start second coordinate, points found are greater or equals to this. |
| x2 | End first coordinate, points found are less than this. |
| y2 | End second coordinate, points found are less than this. |
| outputter | Callback used to return values from the search. |
Definition at line 520 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::FindContained(), and ForAllStationsRadius().
|
inlineprivate |
Definition at line 312 of file kdtree.hpp.
|
inline |
Find the element closest to given coordinate, in Manhattan distance.
For multiple elements with the same distance, the one comparing smaller with a less-than comparison is chosen.
| x | The X-coordinate. |
| y | The Y-coordinate. |
Definition at line 502 of file kdtree.hpp.
|
inlineprivate |
Search a sub-tree for the element nearest to a given point.
| xy | The coordinate to start from. |
| node_idx | The root node to start from. |
| level | The current search level. |
| limit | Distance to limit searching at. |
Definition at line 277 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::FindNearest(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::FindNearestRecursive().
|
inlineprivate |
Free all children of the given node.
| node_idx | The root node. |
Definition at line 178 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Rebuild(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::RemoveRecursive().
|
inlineprivate |
Definition at line 347 of file kdtree.hpp.
|
inline |
Insert a single element in the tree.
Repeatedly inserting single elements may cause the tree to become unbalanced. Undefined behaviour if the element already exists in the tree.
| element | The elemnt to add. |
Definition at line 452 of file kdtree.hpp.
|
inlineprivate |
Insert one element in the tree somewhere below node_idx.
| element | The element to insert. |
| node_idx | The root of the sub-tree to try to insert to. |
| level | The current depth in the tree. |
Definition at line 148 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Insert(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::InsertRecursive().
|
inlineprivate |
Check if the entire tree is in need of rebuilding.
true iff the tree should be rebalanced. Definition at line 356 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Insert(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Remove().
|
inlineprivate |
Definition at line 248 of file kdtree.hpp.
|
inline |
Reconstruct the tree with the same elements, letting it be fully balanced.
Definition at line 441 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Insert(), Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Rebuild(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Remove().
|
inlineprivate |
Rebuild the tree with all existing elements, optionally adding or removing one more.
| include_element | Element to add to the tree, if not nullptr. |
| exclude_element | Element to remove from the tree, if not nullptr. |
true iff the tree is still considered balanced. Definition at line 118 of file kdtree.hpp.
|
inline |
Remove a single element from the tree, if it exists.
Since elements are stored in interior nodes as well as leaf nodes, removing one may require a larger sub-tree to be re-built. Because of this, worst case run time is as bad as a full tree rebuild.
| element | The element to remove. |
Definition at line 472 of file kdtree.hpp.
|
inlineprivate |
Find and remove one element from the tree.
| element | The element to search for |
| node_idx | Sub-tree to search in |
| level | Current depth in the tree |
Definition at line 209 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::Remove(), and Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::RemoveRecursive().
|
inlinestaticprivate |
Ordering function for node_distance objects, elements with equal distance are ordered by less-than comparison.
| a | The first distance to compare. |
| b | The second distance to compare. |
Definition at line 261 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::FindNearestRecursive().
|
inlineprivate |
Find a coordinate value to split a range of elements at.
| begin | The begin of the range to consider. |
| end | The end of the range to consider. |
| level | The depth into the tree. |
Definition at line 77 of file kdtree.hpp.
Referenced by Kdtree< StationID, Kdtree_StationXYFunc, uint16_t, int >::BuildSubtree().
|
private |
List of dead indices in the nodes vector.
Definition at line 47 of file kdtree.hpp.
|
staticprivate |
Index value indicating no-such-node.
Definition at line 43 of file kdtree.hpp.
|
staticprivate |
Arbitrary value for "not worth rebalancing".
Definition at line 44 of file kdtree.hpp.
|
private |
Pool of all nodes in the tree.
Definition at line 46 of file kdtree.hpp.
|
private |
Index of root node.
Definition at line 48 of file kdtree.hpp.
|
private |
Number approximating how unbalanced the tree might be.
Definition at line 49 of file kdtree.hpp.