LeechCraft
0.6.70-19214-g121e5318a7
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
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
65
struct
FindResult
66
{
67
std::optional<V>
Value_
;
68
std::ptrdiff_t
Remaining_
= 0;
69
70
inline
const
static
StringPathTrie
NullTrie
{};
71
const
StringPathTrie
*
Rest_
= &
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
}
LC::Util::StringPathTrie
Definition
stringpathtrie.h:25
LC::Util::StringPathTrie::Add
void Add(It begin, End end, V value)
Definition
stringpathtrie.h:51
LC::Util::StringPathTrie::GetValue
const std::optional< V > & GetValue() const
Definition
stringpathtrie.h:30
LC::Util::StringPathTrie::Add
void Add(Cont &&path, V value)
Definition
stringpathtrie.h:45
LC::Util::StringPathTrie::Find
FindResult Find(Cont &&path) const
Definition
stringpathtrie.h:86
LC::Util::StringPathTrie::GetChild
const StringPathTrie * GetChild(QStringView view) const
Definition
stringpathtrie.h:35
LC::Util::StringPathTrie::Find
FindResult Find(QStringView single) const
Definition
stringpathtrie.h:79
LC::Util::StringPathTrie::Find
FindResult Find(It begin, End end) const
Definition
stringpathtrie.h:92
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:66
LC::Util::StringPathTrie::FindResult::operator==
bool operator==(const FindResult &other) const
Definition
stringpathtrie.h:73
LC::Util::StringPathTrie::FindResult::NullTrie
static const StringPathTrie NullTrie
Definition
stringpathtrie.h:70
LC::Util::StringPathTrie::FindResult::Value_
std::optional< V > Value_
Definition
stringpathtrie.h:67
LC::Util::StringPathTrie::FindResult::Rest_
const StringPathTrie * Rest_
Definition
stringpathtrie.h:71
LC::Util::StringPathTrie::FindResult::Remaining_
std::ptrdiff_t Remaining_
Definition
stringpathtrie.h:68
src
util
sll
stringpathtrie.h
Generated by
1.18.0