poaflocParser Of Arguments For Lines Of Commands |
git clone git://git.dimitrijedobrota.com/poafloc.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
trie.cpp (1182B)
0 #include <algorithm>
1 #include <cstdint>
3 #include "poafloc/poafloc.hpp"
5 namespace poafloc {
7 bool Parser::trie_t::insert(const std::string& option, int key)
8 {
9 trie_t* crnt = this;
11 if (!is_valid(option)) return false;
13 for (const char chr : option)
14 {
15 if (!crnt->m_terminal) crnt->m_key = key;
16 crnt->m_count++;
18 const size_t idx = static_cast<unsigned>(chr) - 'a';
19 if (!crnt->m_children.at(idx))
20 crnt->m_children.at(idx) = std::make_unique<trie_t>();
22 crnt = crnt->m_children.at(idx).get();
23 }
25 crnt->m_terminal = true;
26 crnt->m_key = key;
28 return true;
29 }
31 int Parser::trie_t::get(const std::string& option) const
32 {
33 const trie_t* crnt = this;
35 if (!is_valid(option)) return 0;
37 for (const char chr : option)
38 {
39 const size_t idx = static_cast<unsigned>(chr) - 'a';
40 if (!crnt->m_children.at(idx)) return 0;
42 crnt = crnt->m_children.at(idx).get();
43 }
45 if (!crnt->m_terminal && crnt->m_count > 1) return 0;
46 return crnt->m_key;
47 }
49 bool Parser::trie_t::is_valid(const std::string& option)
50 {
51 return std::all_of(
52 begin(option), end(option), [](char chr) { return std::islower(chr); });
53 }
55 } // namespace poafloc