2020-12-26 10:22:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-26 10:22:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-14 13:19:53 +00:00
|
|
|
#include <AK/Concepts.h>
|
2020-12-26 10:22:44 +00:00
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <AK/HashMap.h>
|
2021-05-19 12:35:34 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-12-26 10:22:44 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
namespace Detail {
|
|
|
|
|
|
|
|
template<typename TypeA, typename Default>
|
|
|
|
struct SubstituteIfVoid {
|
|
|
|
using Type = TypeA;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Default>
|
|
|
|
struct SubstituteIfVoid<void, Default> {
|
|
|
|
using Type = Default;
|
|
|
|
};
|
|
|
|
|
2023-07-28 17:22:34 +00:00
|
|
|
template<typename DeclaredBaseType, typename DefaultBaseType, typename ValueType, typename MetadataT, typename ValueTraits, template<typename, typename, typename> typename MapType>
|
2020-12-26 10:22:44 +00:00
|
|
|
class Trie {
|
|
|
|
using BaseType = typename SubstituteIfVoid<DeclaredBaseType, DefaultBaseType>::Type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using MetadataType = MetadataT;
|
|
|
|
|
|
|
|
Trie(ValueType value, Optional<MetadataType> metadata)
|
|
|
|
: m_value(move(value))
|
|
|
|
, m_metadata(move(metadata))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
2022-04-01 17:58:27 +00:00
|
|
|
BaseType& traverse_until_last_accessible_node(It& it, It const& end)
|
2020-12-26 10:22:44 +00:00
|
|
|
{
|
|
|
|
Trie* node = this;
|
|
|
|
for (; it < end; ++it) {
|
|
|
|
auto next_it = node->m_children.find(*it);
|
|
|
|
if (next_it == node->m_children.end())
|
|
|
|
return static_cast<BaseType&>(*node);
|
|
|
|
node = &*(*next_it).value;
|
|
|
|
}
|
|
|
|
return static_cast<BaseType&>(*node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
2022-04-01 17:58:27 +00:00
|
|
|
BaseType const& traverse_until_last_accessible_node(It& it, It const& end) const { return const_cast<Trie*>(this)->traverse_until_last_accessible_node(it, end); }
|
2020-12-26 10:22:44 +00:00
|
|
|
|
|
|
|
template<typename It>
|
2022-04-01 17:58:27 +00:00
|
|
|
BaseType& traverse_until_last_accessible_node(It const& begin, It const& end)
|
2020-12-26 10:22:44 +00:00
|
|
|
{
|
|
|
|
auto it = begin;
|
|
|
|
return const_cast<Trie*>(this)->traverse_until_last_accessible_node(it, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
2022-04-01 17:58:27 +00:00
|
|
|
BaseType const& traverse_until_last_accessible_node(It const& begin, It const& end) const
|
2020-12-26 10:22:44 +00:00
|
|
|
{
|
|
|
|
auto it = begin;
|
|
|
|
return const_cast<Trie*>(this)->traverse_until_last_accessible_node(it, end);
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:22:34 +00:00
|
|
|
bool has_metadata() const { return m_metadata.has_value(); }
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Optional<MetadataType> metadata() const
|
|
|
|
requires(!IsNullPointer<MetadataType>)
|
|
|
|
{
|
|
|
|
return m_metadata;
|
|
|
|
}
|
|
|
|
void set_metadata(MetadataType metadata)
|
|
|
|
requires(!IsNullPointer<MetadataType>)
|
|
|
|
{
|
|
|
|
m_metadata = move(metadata);
|
|
|
|
}
|
|
|
|
MetadataType const& metadata_value() const
|
|
|
|
requires(!IsNullPointer<MetadataType>)
|
|
|
|
{
|
|
|
|
return m_metadata.value();
|
|
|
|
}
|
|
|
|
MetadataType& metadata_value()
|
|
|
|
requires(!IsNullPointer<MetadataType>)
|
|
|
|
{
|
|
|
|
return m_metadata.value();
|
|
|
|
}
|
2020-12-26 10:22:44 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ValueType const& value() const { return m_value; }
|
2020-12-26 10:22:44 +00:00
|
|
|
ValueType& value() { return m_value; }
|
|
|
|
|
2022-02-14 13:19:53 +00:00
|
|
|
ErrorOr<Trie*> ensure_child(ValueType value, Optional<MetadataType> metadata = {})
|
2020-12-26 10:22:44 +00:00
|
|
|
{
|
|
|
|
auto it = m_children.find(value);
|
|
|
|
if (it == m_children.end()) {
|
2022-02-15 18:59:25 +00:00
|
|
|
OwnPtr<Trie> node;
|
|
|
|
if constexpr (requires { { value->try_clone() } -> SpecializationOf<ErrorOr>; })
|
|
|
|
node = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Trie(TRY(value->try_clone()), move(metadata))));
|
|
|
|
else
|
|
|
|
node = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Trie(value, move(metadata))));
|
2020-12-26 10:22:44 +00:00
|
|
|
auto& node_ref = *node;
|
2022-02-15 18:59:25 +00:00
|
|
|
TRY(m_children.try_set(move(value), node.release_nonnull()));
|
2022-02-14 13:19:53 +00:00
|
|
|
return &static_cast<BaseType&>(node_ref);
|
2020-12-26 10:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto& node_ref = *it->value;
|
|
|
|
if (metadata.has_value())
|
|
|
|
node_ref.m_metadata = move(metadata);
|
2022-02-14 13:19:53 +00:00
|
|
|
return &static_cast<BaseType&>(node_ref);
|
2020-12-26 10:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It, typename ProvideMetadataFunction>
|
2022-02-14 13:19:53 +00:00
|
|
|
ErrorOr<BaseType*> insert(
|
2022-10-16 22:06:11 +00:00
|
|
|
It& it, It const& end, MetadataType metadata, ProvideMetadataFunction provide_missing_metadata)
|
|
|
|
requires(!IsNullPointer<MetadataType>)
|
2020-12-26 10:22:44 +00:00
|
|
|
{
|
|
|
|
Trie* last_root_node = &traverse_until_last_accessible_node(it, end);
|
2022-04-01 17:58:27 +00:00
|
|
|
auto invoke_provide_missing_metadata = [&]<typename... Ts>(Ts&&... args) -> ErrorOr<Optional<MetadataType>> {
|
2022-02-14 13:19:53 +00:00
|
|
|
if constexpr (SameAs<MetadataType, decltype(provide_missing_metadata(forward<Ts>(args)...))>)
|
|
|
|
return Optional<MetadataType>(provide_missing_metadata(forward<Ts>(args)...));
|
|
|
|
else
|
|
|
|
return provide_missing_metadata(forward<Ts>(args)...);
|
|
|
|
};
|
2022-02-15 18:59:25 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
if constexpr (requires { { ValueType::ElementType::try_create(*it) } -> SpecializationOf<ErrorOr>; })
|
|
|
|
last_root_node = static_cast<Trie*>(TRY(last_root_node->ensure_child(TRY(ValueType::ElementType::try_create(*it)), TRY(invoke_provide_missing_metadata(static_cast<BaseType&>(*last_root_node), it)))));
|
|
|
|
else
|
|
|
|
last_root_node = static_cast<Trie*>(TRY(last_root_node->ensure_child(*it, TRY(invoke_provide_missing_metadata(static_cast<BaseType&>(*last_root_node), it)))));
|
|
|
|
}
|
2020-12-26 10:22:44 +00:00
|
|
|
last_root_node->set_metadata(move(metadata));
|
2022-02-14 13:19:53 +00:00
|
|
|
return static_cast<BaseType*>(last_root_node);
|
2020-12-26 10:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<BaseType*> insert(It& it, It const& end)
|
|
|
|
requires(IsNullPointer<MetadataType>)
|
2020-12-26 10:22:44 +00:00
|
|
|
{
|
|
|
|
Trie* last_root_node = &traverse_until_last_accessible_node(it, end);
|
2022-02-15 18:59:25 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
if constexpr (requires { { ValueType::ElementType::try_create(*it) } -> SpecializationOf<ErrorOr>; })
|
|
|
|
last_root_node = static_cast<Trie*>(TRY(last_root_node->ensure_child(TRY(ValueType::ElementType::try_create(*it)), {})));
|
|
|
|
else
|
|
|
|
last_root_node = static_cast<Trie*>(TRY(last_root_node->ensure_child(*it, {})));
|
|
|
|
}
|
2022-02-14 13:19:53 +00:00
|
|
|
return static_cast<BaseType*>(last_root_node);
|
2020-12-26 10:22:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 11:13:27 +00:00
|
|
|
template<typename It, typename ProvideMetadataFunction>
|
2022-02-14 13:19:53 +00:00
|
|
|
ErrorOr<BaseType*> insert(
|
2022-10-16 22:06:11 +00:00
|
|
|
It const& begin, It const& end, MetadataType metadata, ProvideMetadataFunction provide_missing_metadata)
|
|
|
|
requires(!IsNullPointer<MetadataType>)
|
2020-12-26 11:13:27 +00:00
|
|
|
{
|
|
|
|
auto it = begin;
|
|
|
|
return insert(it, end, move(metadata), move(provide_missing_metadata));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename It>
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<BaseType*> insert(It const& begin, It const& end)
|
|
|
|
requires(IsNullPointer<MetadataType>)
|
2020-12-26 11:13:27 +00:00
|
|
|
{
|
|
|
|
auto it = begin;
|
|
|
|
return insert(it, end);
|
|
|
|
}
|
|
|
|
|
2023-07-28 17:22:34 +00:00
|
|
|
MapType<ValueType, NonnullOwnPtr<Trie>, ValueTraits>& children() { return m_children; }
|
|
|
|
MapType<ValueType, NonnullOwnPtr<Trie>, ValueTraits> const& children() const { return m_children; }
|
2021-06-06 17:24:26 +00:00
|
|
|
|
2022-02-14 13:19:53 +00:00
|
|
|
template<typename Fn>
|
|
|
|
ErrorOr<void> for_each_node_in_tree_order(Fn callback) const
|
|
|
|
{
|
|
|
|
struct State {
|
|
|
|
bool did_generate_root { false };
|
2023-07-28 17:22:34 +00:00
|
|
|
typename MapType<ValueType, NonnullOwnPtr<Trie>, ValueTraits>::ConstIteratorType it;
|
|
|
|
typename MapType<ValueType, NonnullOwnPtr<Trie>, ValueTraits>::ConstIteratorType end;
|
2022-02-14 13:19:53 +00:00
|
|
|
};
|
|
|
|
Vector<State> state;
|
|
|
|
TRY(state.try_empend(false, m_children.begin(), m_children.end()));
|
|
|
|
|
|
|
|
auto invoke = [&](auto& current_node) -> ErrorOr<IterationDecision> {
|
2024-04-18 19:32:56 +00:00
|
|
|
if constexpr (VoidFunction<Fn, BaseType const&>) {
|
|
|
|
callback(static_cast<BaseType const&>(current_node));
|
2022-02-14 13:19:53 +00:00
|
|
|
return IterationDecision::Continue;
|
2024-04-18 19:32:56 +00:00
|
|
|
} else if constexpr (IsSpecializationOf<decltype(callback(declval<BaseType const&>())), ErrorOr>) {
|
|
|
|
return callback(static_cast<BaseType const&>(current_node));
|
|
|
|
} else if constexpr (IteratorFunction<Fn, BaseType const&>) {
|
|
|
|
return callback(static_cast<BaseType const&>(current_node));
|
2022-02-14 13:19:53 +00:00
|
|
|
} else {
|
|
|
|
static_assert(DependentFalse<Fn>, "Invalid iterator function type signature");
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto* current_node = this; current_node != nullptr;) {
|
|
|
|
if (TRY(invoke(*current_node)) == IterationDecision::Break)
|
|
|
|
break;
|
|
|
|
TRY(skip_to_next_iterator(state, current_node));
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2020-12-26 10:22:44 +00:00
|
|
|
|
2021-04-11 08:31:50 +00:00
|
|
|
[[nodiscard]] bool is_empty() const { return m_children.is_empty(); }
|
2020-12-26 10:22:44 +00:00
|
|
|
void clear() { m_children.clear(); }
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
ErrorOr<BaseType> deep_copy()
|
|
|
|
requires(requires(ValueType value) { { value->try_clone() } -> SpecializationOf<ErrorOr>; })
|
2022-02-15 18:59:25 +00:00
|
|
|
{
|
|
|
|
Trie root(TRY(m_value->try_clone()), TRY(copy_metadata(m_metadata)));
|
|
|
|
for (auto& it : m_children)
|
|
|
|
TRY(root.m_children.try_set(TRY(it.key->try_clone()), TRY(adopt_nonnull_own_or_enomem(new (nothrow) Trie(TRY(it.value->deep_copy()))))));
|
|
|
|
return static_cast<BaseType&&>(move(root));
|
|
|
|
}
|
|
|
|
|
2022-02-14 13:19:53 +00:00
|
|
|
ErrorOr<BaseType> deep_copy()
|
2020-12-26 10:22:44 +00:00
|
|
|
{
|
2022-02-14 13:19:53 +00:00
|
|
|
Trie root(m_value, TRY(copy_metadata(m_metadata)));
|
2020-12-26 10:22:44 +00:00
|
|
|
for (auto& it : m_children)
|
2022-02-14 13:19:53 +00:00
|
|
|
TRY(root.m_children.try_set(it.key, TRY(adopt_nonnull_own_or_enomem(new (nothrow) Trie(TRY(it.value->deep_copy()))))));
|
2020-12-26 10:22:44 +00:00
|
|
|
return static_cast<BaseType&&>(move(root));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-02-14 13:19:53 +00:00
|
|
|
static ErrorOr<Optional<MetadataType>> copy_metadata(Optional<MetadataType> const& metadata)
|
|
|
|
{
|
|
|
|
if (!metadata.has_value())
|
|
|
|
return Optional<MetadataType> {};
|
|
|
|
|
|
|
|
if constexpr (requires(MetadataType t) { { t.copy() } -> SpecializationOf<ErrorOr>; })
|
|
|
|
return Optional<MetadataType> { TRY(metadata->copy()) };
|
|
|
|
else
|
|
|
|
return Optional<MetadataType> { MetadataType(metadata.value()) };
|
|
|
|
}
|
|
|
|
|
|
|
|
static ErrorOr<void> skip_to_next_iterator(auto& state, auto& current_node)
|
|
|
|
{
|
|
|
|
auto& current_state = state.last();
|
|
|
|
if (current_state.did_generate_root)
|
|
|
|
++current_state.it;
|
|
|
|
else
|
|
|
|
current_state.did_generate_root = true;
|
|
|
|
|
|
|
|
if (current_state.it == current_state.end)
|
|
|
|
return pop_and_get_next_iterator(state, current_node);
|
|
|
|
|
|
|
|
current_node = &*(*current_state.it).value;
|
|
|
|
|
|
|
|
TRY(state.try_empend(false, current_node->m_children.begin(), current_node->m_children.end()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static ErrorOr<void> pop_and_get_next_iterator(auto& state, auto& current_node)
|
|
|
|
{
|
|
|
|
state.take_last();
|
|
|
|
if (state.is_empty()) {
|
|
|
|
current_node = nullptr;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return skip_to_next_iterator(state, current_node);
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:22:44 +00:00
|
|
|
ValueType m_value;
|
|
|
|
Optional<MetadataType> m_metadata;
|
2023-07-28 17:22:34 +00:00
|
|
|
MapType<ValueType, NonnullOwnPtr<Trie>, ValueTraits> m_children;
|
2020-12-26 10:22:44 +00:00
|
|
|
};
|
|
|
|
|
2023-07-28 17:22:34 +00:00
|
|
|
template<typename BaseType, typename DefaultBaseType, typename ValueType, typename ValueTraits, template<typename, typename, typename> typename MapType>
|
|
|
|
class Trie<BaseType, DefaultBaseType, ValueType, void, ValueTraits, MapType> : public Trie<BaseType, DefaultBaseType, ValueType, decltype(nullptr), ValueTraits, MapType> {
|
|
|
|
using Trie<BaseType, DefaultBaseType, ValueType, decltype(nullptr), ValueTraits, MapType>::Trie;
|
2020-12-26 10:22:44 +00:00
|
|
|
};
|
|
|
|
|
2023-07-28 17:22:34 +00:00
|
|
|
template<typename K, typename V, typename T>
|
|
|
|
using HashMapForTrie = HashMap<K, V, T>;
|
|
|
|
|
2020-12-26 10:22:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 17:22:34 +00:00
|
|
|
template<typename ValueType, typename MetadataT = void, typename ValueTraits = Traits<ValueType>, typename BaseT = void, template<typename, typename, typename> typename MapType = Detail::HashMapForTrie>
|
|
|
|
class Trie : public Detail::Trie<BaseT, Trie<ValueType, MetadataT, ValueTraits, void, MapType>, ValueType, MetadataT, ValueTraits, MapType> {
|
2020-12-26 10:22:44 +00:00
|
|
|
public:
|
2023-07-28 17:22:34 +00:00
|
|
|
using DetailTrie = Detail::Trie<BaseT, Trie<ValueType, MetadataT, ValueTraits, void, MapType>, ValueType, MetadataT, ValueTraits, MapType>;
|
2020-12-26 10:22:44 +00:00
|
|
|
using MetadataType = typename DetailTrie::MetadataType;
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
Trie(ValueType value, MetadataType metadata)
|
|
|
|
requires(!IsVoid<MetadataType> && !IsNullPointer<MetadataType>)
|
2020-12-26 10:22:44 +00:00
|
|
|
: DetailTrie(move(value), move(metadata))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit Trie(ValueType value)
|
|
|
|
: DetailTrie(move(value), Optional<MetadataType> {})
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-12-26 10:22:44 +00:00
|
|
|
using AK::Trie;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|