From 7a471b7cf51f573936846eeb5789aab721514fbe Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 28 Jul 2023 20:52:34 +0330 Subject: [PATCH] AK: Allow customising Trie's underlying map type This makes it possible to use an ordered map and keep the insertion order intact. --- AK/Trie.h | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/AK/Trie.h b/AK/Trie.h index 62a451312dd..ca940f384a4 100644 --- a/AK/Trie.h +++ b/AK/Trie.h @@ -26,7 +26,7 @@ struct SubstituteIfVoid { using Type = Default; }; -template +template typename MapType> class Trie { using BaseType = typename SubstituteIfVoid::Type; @@ -69,6 +69,8 @@ public: return const_cast(this)->traverse_until_last_accessible_node(it, end); } + bool has_metadata() const { return m_metadata.has_value(); } + Optional metadata() const requires(!IsNullPointer) { @@ -166,16 +168,16 @@ public: return insert(it, end); } - HashMap, ValueTraits>& children() { return m_children; } - HashMap, ValueTraits> const& children() const { return m_children; } + MapType, ValueTraits>& children() { return m_children; } + MapType, ValueTraits> const& children() const { return m_children; } template ErrorOr for_each_node_in_tree_order(Fn callback) const { struct State { bool did_generate_root { false }; - typename HashMap, ValueTraits>::ConstIteratorType it; - typename HashMap, ValueTraits>::ConstIteratorType end; + typename MapType, ValueTraits>::ConstIteratorType it; + typename MapType, ValueTraits>::ConstIteratorType end; }; Vector state; TRY(state.try_empend(false, m_children.begin(), m_children.end())); @@ -265,20 +267,23 @@ private: ValueType m_value; Optional m_metadata; - HashMap, ValueTraits> m_children; + MapType, ValueTraits> m_children; }; -template -class Trie : public Trie { - using Trie::Trie; +template typename MapType> +class Trie : public Trie { + using Trie::Trie; }; +template +using HashMapForTrie = HashMap; + } -template, typename BaseT = void> -class Trie : public Detail::Trie, ValueType, MetadataT, ValueTraits> { +template, typename BaseT = void, template typename MapType = Detail::HashMapForTrie> +class Trie : public Detail::Trie, ValueType, MetadataT, ValueTraits, MapType> { public: - using DetailTrie = Detail::Trie, ValueType, MetadataT, ValueTraits>; + using DetailTrie = Detail::Trie, ValueType, MetadataT, ValueTraits, MapType>; using MetadataType = typename DetailTrie::MetadataType; Trie(ValueType value, MetadataType metadata)