LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
stringpathtrie.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <optional>
12#include <type_traits>
13#include <QHash>
14#include <QString>
15#include <QStringView>
16#include <QtDebug>
17
18namespace LC::Util
19{
20 template<typename Cont>
21 concept StringViewContainer = std::is_same_v<typename std::decay_t<Cont>::value_type, QStringView>;
22
23 template<typename V>
25 {
26 std::optional<V> Value_;
27
28 QHash<QString, StringPathTrie> Children_;
29 public:
30 const std::optional<V>& GetValue () const
31 {
32 return Value_;
33 }
34
35 const StringPathTrie* GetChild (QStringView view) const
36 {
37 const auto pos = Children_.find (view);
38 if (pos == Children_.end ())
39 return nullptr;
40
41 return &*pos;
42 }
43
44 template<StringViewContainer Cont>
45 void Add (Cont&& path, V value)
46 {
47 Add (path.begin (), path.end (), std::move (value));
48 }
49
50 template<typename It, typename End>
51 void Add (It begin, End end, V value)
52 {
53 if (begin == end)
54 {
55 Value_ = std::move (value);
56 return;
57 }
58
59 auto pos = Children_.find (*begin);
60 if (pos == Children_.end ())
61 pos = Children_.insert ((*begin).toString (), {});
62 pos->Add (std::next (begin), end, std::move (value));
63 }
64
66 {
67 std::optional<V> Value_;
68 std::ptrdiff_t Remaining_ = 0;
69
70 inline const static StringPathTrie NullTrie {};
72
73 bool operator== (const FindResult& other) const
74 {
75 return Value_ == other.Value_ && Remaining_ == other.Remaining_;
76 }
77 };
78
79 FindResult Find (QStringView single) const
80 {
81 std::initializer_list<QStringView> dummy { single };
82 return Find (dummy.begin (), dummy.end ());
83 }
84
85 template<StringViewContainer Cont>
86 FindResult Find (Cont&& path) const
87 {
88 return Find (path.begin (), path.end ());
89 }
90
91 template<typename It, typename End>
92 FindResult Find (It begin, End end) const
93 {
94 return Find (begin, end, { Value_, end - begin, this });
95 }
96 private:
97 template<typename It, typename End>
98 FindResult Find (It begin, End end, FindResult lastGood) const
99 {
100 if (Value_)
101 lastGood = { Value_, end - begin, this };
102
103 if (begin == end)
104 return lastGood;
105
106 const auto pos = Children_.find (*begin);
107 if (pos == Children_.end ())
108 return lastGood;
109
110 return pos->Find (std::next (begin), end, lastGood);
111 }
112 };
113}
void Add(It begin, End end, V value)
const std::optional< V > & GetValue() const
void Add(Cont &&path, V value)
FindResult Find(Cont &&path) const
const StringPathTrie * GetChild(QStringView view) const
FindResult Find(QStringView single) const
FindResult Find(It begin, End end) const
typename detail::Find< Name, Def, Args... >::type Find
Definition typelist.h:176
IntTrie::FindResult FindResult
bool operator==(const FindResult &other) const
static const StringPathTrie NullTrie