LeechCraft
0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
Toggle main menu visibility
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
18
namespace
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>
24
class
StringPathTrie
25
{
26
std::optional<V> Value_;
27
28
// TODO C++20 use transparent hashes and unordered_map
29
QHash<QString, StringPathTrie> Children_;
30
public
:
31
const
std::optional<V>&
GetValue
()
const
32
{
33
return
Value_;
34
}
35
36
const
StringPathTrie
*
GetChild
(QStringView view)
const
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
67
struct
FindResult
68
{
69
std::optional<V>
Value_
;
70
std::ptrdiff_t
Remaining_
= 0;
71
72
inline
const
static
StringPathTrie
NullTrie
{};
73
const
StringPathTrie
*
Rest_
= &
NullTrie
;
74
75
bool
operator==
(
const
FindResult
& other)
const
76
{
77
return
Value_
== other.
Value_
&&
Remaining_
== other.
Remaining_
;
78
}
79
};
80
81
FindResult
Find
(QStringView single)
const
82
{
83
std::initializer_list<QStringView> dummy { single };
84
return
Find
(dummy.begin (), dummy.end ());
85
}
86
87
template
<StringViewContainer Cont>
88
FindResult
Find
(Cont&& path)
const
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
}
LC::Util::StringPathTrie
Definition
stringpathtrie.h:25
LC::Util::StringPathTrie::Add
void Add(It begin, End end, V value)
Definition
stringpathtrie.h:52
LC::Util::StringPathTrie::GetValue
const std::optional< V > & GetValue() const
Definition
stringpathtrie.h:31
LC::Util::StringPathTrie::Add
void Add(Cont &&path, V value)
Definition
stringpathtrie.h:46
LC::Util::StringPathTrie::Find
FindResult Find(Cont &&path) const
Definition
stringpathtrie.h:88
LC::Util::StringPathTrie::GetChild
const StringPathTrie * GetChild(QStringView view) const
Definition
stringpathtrie.h:36
LC::Util::StringPathTrie::Find
FindResult Find(QStringView single) const
Definition
stringpathtrie.h:81
LC::Util::StringPathTrie::Find
FindResult Find(It begin, End end) const
Definition
stringpathtrie.h:94
LC::Util::StringViewContainer
Definition
stringpathtrie.h:21
LC::Util
Definition
icoreproxy.h:34
LC::Util::Find
typename detail::Find< Name, Def, Args... >::type Find
Definition
typelist.h:176
LC::Util::FindResult
IntTrie::FindResult FindResult
Definition
stringpathtrietest.cpp:20
LC::Util::StringPathTrie::FindResult
Definition
stringpathtrie.h:68
LC::Util::StringPathTrie::FindResult::operator==
bool operator==(const FindResult &other) const
Definition
stringpathtrie.h:75
LC::Util::StringPathTrie::FindResult::NullTrie
static const StringPathTrie NullTrie
Definition
stringpathtrie.h:72
LC::Util::StringPathTrie::FindResult::Value_
std::optional< V > Value_
Definition
stringpathtrie.h:69
LC::Util::StringPathTrie::FindResult::Rest_
const StringPathTrie * Rest_
Definition
stringpathtrie.h:73
LC::Util::StringPathTrie::FindResult::Remaining_
std::ptrdiff_t Remaining_
Definition
stringpathtrie.h:70
src
util
sll
stringpathtrie.h
Generated by
1.17.0