LeechCraft 0.6.70-16373-g319c272718
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 // TODO C++20 use transparent hashes and unordered_map
30 public:
31 const std::optional<V>& GetValue () const
32 {
33 return Value_;
34 }
35
37 {
38 const auto pos = Children_.find (view.toString ());
39 if (pos == Children_.end ())
40 return nullptr;
41
42 return &*pos;
43 }
44
45 template<StringViewContainer Cont>
46 void Add (Cont&& path, V value)
47 {
48 Add (path.begin (), path.end (), std::move (value));
49 }
50
51 template<typename It, typename End>
52 void Add (It begin, End end, V value)
53 {
54 if (begin == end)
55 {
56 Value_ = std::move (value);
57 return;
58 }
59
60 const auto& strRef = (*begin).toString ();
61 auto pos = Children_.find (strRef);
62 if (pos == Children_.end ())
63 pos = Children_.insert (strRef, {});
64 pos->Add (std::next (begin), end, std::move (value));
65 }
66
68 {
69 std::optional<V> Value_;
70 std::ptrdiff_t Remaining_ = 0;
71
72 inline const static StringPathTrie NullTrie {};
74
75 bool operator== (const FindResult& other) const
76 {
77 return Value_ == other.Value_ && Remaining_ == other.Remaining_;
78 }
79 };
80
82 {
83 std::initializer_list<QStringView> dummy { single };
84 return Find (dummy.begin (), dummy.end ());
85 }
86
87 template<StringViewContainer Cont>
89 {
90 return Find (path.begin (), path.end ());
91 }
92
93 template<typename It, typename End>
94 FindResult Find (It begin, End end) const
95 {
96 return Find (begin, end, { Value_, end - begin, this });
97 }
98 private:
99 template<typename It, typename End>
100 FindResult Find (It begin, End end, FindResult lastGood) const
101 {
102 if (Value_)
103 lastGood = { Value_, end - begin, this };
104
105 if (begin == end)
106 return lastGood;
107
108 const auto& strRef = (*begin).toString ();
109 const auto pos = Children_.find (strRef);
110 if (pos == Children_.end ())
111 return lastGood;
112
113 return pos->Find (std::next (begin), end, lastGood);
114 }
115 };
116}
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
Container< T > Filter(const Container< T > &c, F f)
Definition prelude.h:118
typename detail::Find< Name, Def, Args... >::type Find
Definition typelist.h:176
const QVariant Value_
Definition plotitem.cpp:74
bool operator==(const FindResult &other) const
static const StringPathTrie NullTrie