#ifndef CC_BASE_RTREE_H_
#define CC_BASE_RTREE_H_
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <optional>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/debug/crash_logging.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/notreached.h"
#include "base/numerics/clamped_math.h"
#include "ui/gfx/geometry/rect.h"
namespace cc {
template <typename T>
class RTree {
public:
RTree();
RTree(const RTree&) = delete;
~RTree();
RTree& operator=(const RTree&) = delete;
template <typename Container>
void Build(const Container& items);
template <typename BoundsFunctor, typename PayloadFunctor>
void Build(size_t item_count,
const BoundsFunctor& bounds_getter,
const PayloadFunctor& payload_getter);
bool has_valid_bounds() const { return has_valid_bounds_; }
template <typename ResultFunctor>
void Search(const gfx::Rect& query,
const ResultFunctor& result_handler) const;
void Search(const gfx::Rect& query,
std::vector<T>* results,
std::vector<gfx::Rect>* rects = nullptr) const;
void SearchRefs(const gfx::Rect& query, std::vector<const T*>* results) const;
std::optional<gfx::Rect> bounds() const;
std::map<T, gfx::Rect> GetAllBoundsForTracing() const;
private:
static constexpr size_t kMinChildren = 6;
static constexpr size_t kMaxChildren = 11;
template <typename U>
struct Node;
template <typename U>
struct Branch {
RAW_PTR_EXCLUSION Node<U>* subtree = nullptr;
U payload;
gfx::Rect bounds;
Branch() = default;
Branch(U payload, const gfx::Rect& bounds)
: payload(std::move(payload)), bounds(bounds) {}
};
template <typename U>
struct Node {
uint16_t num_children = 0;
uint16_t level = 0;
Branch<U> children[kMaxChildren];
explicit Node(uint16_t level) : level(level) {}
};
template <typename ResultFunctor>
void SearchRecursive(const Node<T>& node,
const gfx::Rect& query,
const ResultFunctor& result_handler) const;
template <typename ResultFunctor>
void SearchRecursiveFallback(const Node<T>& node,
const gfx::Rect& query,
const ResultFunctor& result_handler) const;
Branch<T> BuildRecursive(std::vector<Branch<T>>* branches, uint16_t level);
Node<T>* AllocateNodeAtLevel(uint16_t level);
void GetAllBoundsRecursive(const Node<T>& node,
std::map<T, gfx::Rect>* results) const;
NOINLINE void AddCrashKeysForTreeSizeAndCrash(size_t node_count) const;
size_t num_data_elements_ = 0u;
std::vector<Node<T>> nodes_;
Branch<T> root_;
bool has_valid_bounds_ = true;
};
template <typename T>
RTree<T>::RTree() = default;
template <typename T>
RTree<T>::~RTree() = default;
template <typename T>
template <typename Container>
void RTree<T>::Build(const Container& items) {
Build(
items.size(), [&items](size_t index) { return items[index]; },
[](size_t index) { return index; });
}
template <typename T>
template <typename BoundsFunctor, typename PayloadFunctor>
void RTree<T>::Build(size_t item_count,
const BoundsFunctor& bounds_getter,
const PayloadFunctor& payload_getter) {
DCHECK_EQ(0u, num_data_elements_);
std::vector<Branch<T>> branches;
branches.reserve(item_count);
for (size_t i = 0; i < item_count; i++) {
const gfx::Rect& bounds = bounds_getter(i);
if (bounds.IsEmpty()) {
continue;
}
branches.emplace_back(payload_getter(i), bounds);
}
num_data_elements_ = branches.size();
if (num_data_elements_ == 1u) {
nodes_.reserve(1);
Node<T>* node = AllocateNodeAtLevel(0);
root_.subtree = node;
root_.bounds = branches[0].bounds;
node->num_children = 1;
node->children[0] = std::move(branches[0]);
} else if (num_data_elements_ > 1u) {
size_t branch_count = kMaxChildren;
double depth = log(branches.size()) / log(branch_count);
size_t node_count =
static_cast<size_t>((std::pow(branch_count, depth) - 1) /
(branch_count - 1)) +
kMinChildren;
if (node_count > nodes_.max_size()) [[unlikely]] {
AddCrashKeysForTreeSizeAndCrash(node_count);
NOTREACHED();
}
nodes_.reserve(node_count);
root_ = BuildRecursive(&branches, 0);
}
DCHECK_LE(nodes_.capacity() - nodes_.size(), kMinChildren);
}
template <typename T>
auto RTree<T>::AllocateNodeAtLevel(uint16_t level) -> Node<T>* {
DCHECK_GT(nodes_.capacity(), nodes_.size());
nodes_.emplace_back(level);
return &nodes_.back();
}
template <typename T>
auto RTree<T>::BuildRecursive(std::vector<Branch<T>>* branches, uint16_t level)
-> Branch<T> {
if (branches->size() == 1) {
return std::move((*branches)[0]);
}
size_t remainder = branches->size() % kMaxChildren;
if (remainder > 0) {
if (remainder >= kMinChildren) {
remainder = 0;
} else {
remainder = kMinChildren - remainder;
}
}
size_t current_branch = 0;
size_t new_branch_index = 0;
while (current_branch < branches->size()) {
size_t increment_by = kMaxChildren;
if (remainder != 0) {
if (remainder <= kMaxChildren - kMinChildren) {
increment_by -= remainder;
remainder = 0;
} else {
increment_by = kMinChildren;
remainder -= kMaxChildren - kMinChildren;
}
}
Node<T>* node = AllocateNodeAtLevel(level);
node->num_children = 1;
node->children[0] = (*branches)[current_branch];
Branch<T> branch;
branch.bounds = (*branches)[current_branch].bounds;
branch.subtree = node;
++current_branch;
int x = branch.bounds.x();
int y = branch.bounds.y();
int right = branch.bounds.right();
int bottom = branch.bounds.bottom();
for (size_t k = 1; k < increment_by && current_branch < branches->size();
++k) {
auto& bounds = (*branches)[current_branch].bounds;
x = std::min(x, bounds.x());
y = std::min(y, bounds.y());
right = std::max(right, bounds.right());
bottom = std::max(bottom, bounds.bottom());
UNSAFE_TODO(node->children[k]) = (*branches)[current_branch];
++node->num_children;
++current_branch;
}
branch.bounds.SetRect(x, y, base::ClampSub(right, x),
base::ClampSub(bottom, y));
bool overflow =
branch.bounds.right() != right || branch.bounds.bottom() != bottom;
has_valid_bounds_ &= !overflow;
DCHECK_LT(new_branch_index, current_branch);
(*branches)[new_branch_index] = std::move(branch);
++new_branch_index;
}
branches->resize(new_branch_index);
return BuildRecursive(branches, level + 1);
}
template <typename T>
template <typename ResultFunctor>
void RTree<T>::Search(const gfx::Rect& query,
const ResultFunctor& result_handler) const {
if (num_data_elements_ == 0) {
return;
}
CHECK(root_.subtree);
if (!has_valid_bounds_) {
SearchRecursiveFallback(*root_.subtree, query, result_handler);
} else if (query.Intersects(root_.bounds)) {
SearchRecursive(*root_.subtree, query, result_handler);
}
}
template <typename T>
void RTree<T>::Search(const gfx::Rect& query,
std::vector<T>* results,
std::vector<gfx::Rect>* rects) const {
results->clear();
if (rects) {
rects->clear();
}
Search(query, [results, rects](const T& payload, const gfx::Rect& rect) {
results->push_back(payload);
if (rects) {
rects->push_back(rect);
}
});
}
template <typename T>
void RTree<T>::SearchRefs(const gfx::Rect& query,
std::vector<const T*>* results) const {
results->clear();
Search(query, [results](const T& payload, const gfx::Rect&) {
results->push_back(&payload);
});
}
template <typename T>
template <typename ResultFunctor>
void RTree<T>::SearchRecursive(const Node<T>& node,
const gfx::Rect& query,
const ResultFunctor& result_handler) const {
for (uint16_t i = 0; i < node.num_children; ++i) {
const auto& child = UNSAFE_TODO(node.children[i]);
if (query.Intersects(child.bounds)) {
if (node.level == 0) {
result_handler(child.payload, child.bounds);
} else {
CHECK(child.subtree);
SearchRecursive(*child.subtree, query, result_handler);
}
}
}
}
template <typename T>
template <typename ResultFunctor>
void RTree<T>::SearchRecursiveFallback(
const Node<T>& node,
const gfx::Rect& query,
const ResultFunctor& result_handler) const {
for (uint16_t i = 0; i < node.num_children; ++i) {
const auto& child = UNSAFE_TODO(node.children[i]);
if (node.level == 0) {
if (query.Intersects(child.bounds)) {
result_handler(child.payload, child.bounds);
}
} else {
CHECK(child.subtree);
SearchRecursive(*child.subtree, query, result_handler);
}
}
}
template <typename T>
std::optional<gfx::Rect> RTree<T>::bounds() const {
if (has_valid_bounds_) {
return root_.bounds;
}
return std::nullopt;
}
template <typename T>
std::map<T, gfx::Rect> RTree<T>::GetAllBoundsForTracing() const {
std::map<T, gfx::Rect> results;
if (num_data_elements_ > 0) {
CHECK(root_.subtree);
GetAllBoundsRecursive(*root_.subtree, &results);
}
return results;
}
template <typename T>
void RTree<T>::GetAllBoundsRecursive(const Node<T>& node,
std::map<T, gfx::Rect>* results) const {
for (uint16_t i = 0; i < node.num_children; ++i) {
const auto& child = UNSAFE_TODO(node.children[i]);
if (node.level == 0) {
(*results)[child.payload] = child.bounds;
} else {
CHECK(child.subtree);
GetAllBoundsRecursive(*child.subtree, results);
}
}
}
template <typename T>
void RTree<T>::AddCrashKeysForTreeSizeAndCrash(size_t node_count) const {
double branches_log = log(num_data_elements_);
double depth = branches_log / log(kMaxChildren);
double branch_pow = std::pow(kMaxChildren, depth);
size_t node_count_recalculated =
static_cast<size_t>((branch_pow - 1) / (kMaxChildren - 1)) + kMinChildren;
SCOPED_CRASH_KEY_STRING32("Bug447555058", "initial_calcd_node_count",
base::NumberToString(node_count));
SCOPED_CRASH_KEY_STRING32("Bug447555058", "recalc_ln_data_elements",
base::NumberToString(branches_log));
SCOPED_CRASH_KEY_STRING32("Bug447555058", "recalculated_depth",
base::NumberToString(depth));
SCOPED_CRASH_KEY_STRING32("Bug447555058", "recalculated_branch_pow",
base::NumberToString(branch_pow));
SCOPED_CRASH_KEY_STRING32("Bug447555058", "recalculated_node_count",
base::NumberToString(node_count_recalculated));
NOTREACHED();
}
}
#endif