SPSP
Simple publish-subscribe protocol. Connects low power IoT clients to MQTT.
All Classes Files Functions Variables Typedefs Enumerations
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
SPSP::WildcardTrie< TValue > Class Template Reference

String-based trie with wildcard support. More...

#include <wildcard_trie.hpp>

Public Types

using FindReturnT = std::unordered_map< std::string, const TValue & >
 

Public Member Functions

 WildcardTrie (const std::string &levelSeparator="/", const std::string &singleLevelWildcard="+", const std::string &multiLevelWildcard="#") noexcept
 Constructs a new object. More...
 
TValue & operator[] (const std::string &key)
 Gets/inserts current value of key More...
 
void insert (const std::string &key, const TValue &value)
 Inserts (or updates) key-value pair. More...
 
bool remove (const std::string &key)
 Removes key from trie. More...
 
const FindReturnT find (const std::string &key) const
 Finds key in trie. More...
 
void forEach (std::function< void(const std::string &key, const TValue &value)> f)
 Iterates through each item in trie and calls callback on each one. More...
 
bool empty () const
 Empty predicate. More...
 

Protected Member Functions

const std::vector< std::string > splitToLevels (const std::string &key) const
 Splits key to levels. More...
 

Detailed Description

template<typename TValue>
class SPSP::WildcardTrie< TValue >

String-based trie with wildcard support.

Made specifically for MQTT-like topics, but it's reusable.

Uses separators to distinguis "levels". Multi-level wildcard must be the last character in the key. There are no exceptions and no key validation. If key is semantically invalid, the item will just become inaccessible.

Template Parameters
TValueType of value

Definition at line 34 of file wildcard_trie.hpp.

Constructor & Destructor Documentation

◆ WildcardTrie()

template<typename TValue >
SPSP::WildcardTrie< TValue >::WildcardTrie ( const std::string &  levelSeparator = "/",
const std::string &  singleLevelWildcard = "+",
const std::string &  multiLevelWildcard = "#" 
)
inlinenoexcept

Constructs a new object.

Parameters
levelSeparatorLevel separator
singleLevelWildcardSingle-level wildcard token
multiLevelWildcardMulti-level wildcard token

Definition at line 64 of file wildcard_trie.hpp.

Member Function Documentation

◆ empty()

template<typename TValue >
bool SPSP::WildcardTrie< TValue >::empty ( ) const
inline

Empty predicate.

Returns
true Trie is empty
false Trie is not empty

Definition at line 250 of file wildcard_trie.hpp.

◆ find()

template<typename TValue >
const FindReturnT SPSP::WildcardTrie< TValue >::find ( const std::string &  key) const
inline

Finds key in trie.

Parameters
keyKey
Returns
Vector of values from matching keys (empty if not found)

Definition at line 169 of file wildcard_trie.hpp.

◆ forEach()

template<typename TValue >
void SPSP::WildcardTrie< TValue >::forEach ( std::function< void(const std::string &key, const TValue &value)>  f)
inline

Iterates through each item in trie and calls callback on each one.

Parameters
fFunction to call

Definition at line 218 of file wildcard_trie.hpp.

◆ insert()

template<typename TValue >
void SPSP::WildcardTrie< TValue >::insert ( const std::string &  key,
const TValue &  value 
)
inline

Inserts (or updates) key-value pair.

Parameters
keyKey
valueValue

Definition at line 107 of file wildcard_trie.hpp.

◆ operator[]()

template<typename TValue >
TValue& SPSP::WildcardTrie< TValue >::operator[] ( const std::string &  key)
inline

Gets/inserts current value of key

Parameters
keyKey
Returns
Current value reference

Definition at line 77 of file wildcard_trie.hpp.

◆ remove()

template<typename TValue >
bool SPSP::WildcardTrie< TValue >::remove ( const std::string &  key)
inline

Removes key from trie.

Parameters
keyKey
Returns
true Node removed successfully
false Node doesn't exist

Definition at line 119 of file wildcard_trie.hpp.

◆ splitToLevels()

template<typename TValue >
const std::vector<std::string> SPSP::WildcardTrie< TValue >::splitToLevels ( const std::string &  key) const
inlineprotected

Splits key to levels.

There's no validation of key.

Parameters
keyKey
Returns
Vector of levels

Definition at line 264 of file wildcard_trie.hpp.


The documentation for this class was generated from the following file: