AIToolbox
A library that offers tools for AI problem solving.
|
This class is a generally faster implementation of a Trie. More...
#include <AIToolbox/Factored/Utils/FasterTrie.hpp>
Public Types | |
using | Entry = std::pair< size_t, PartialFactors > |
using | Entries = std::vector< Entry > |
Public Member Functions | |
FasterTrie (Factors f) | |
Basic constructor. More... | |
size_t | insert (PartialFactors pf) |
This function inserts a new id using the input as a key. More... | |
void | erase (size_t id, const PartialFactors &pf) |
This function erases the id with the input key. More... | |
std::vector< size_t > | filter (const Factors &f) const |
This function returns all ids of the keys that match the input Factors. More... | |
std::tuple< Entries, Factors > | reconstruct (const PartialFactors &pf, bool remove=false) |
This function returns a set of Entries which match the input and each other. More... | |
size_t | size () const |
This function returns the number of keys in the FasterTrie. More... | |
const Factors & | getF () const |
This function returns a reference of the internal Factors space. More... | |
This class is a generally faster implementation of a Trie.
This class stores keys in a different way from Trie, which allows it to be much faster when retrieving. On the other hand, it is slightly less flexible in what it can do.
using AIToolbox::Factored::FasterTrie::Entries = std::vector<Entry> |
using AIToolbox::Factored::FasterTrie::Entry = std::pair<size_t, PartialFactors> |
AIToolbox::Factored::FasterTrie::FasterTrie | ( | Factors | f | ) |
Basic constructor.
This constructor simply copies the input state space and uses it as bound to construct its internal data structures.
f | The factored space. |
void AIToolbox::Factored::FasterTrie::erase | ( | size_t | id, |
const PartialFactors & | pf | ||
) |
This function erases the id with the input key.
This operation takes an amount of time proportional to the number of keys with the same first element (key,value) of the input PartialFactors.
id | The id to remove. |
pf | The PartialFactors used as key for the insertion. |
std::vector<size_t> AIToolbox::Factored::FasterTrie::filter | ( | const Factors & | f | ) | const |
This function returns all ids of the keys that match the input Factors.
The output is not sorted.
The input can have fewer elements than the space; the output will be matched on those elements. Differently from Trie, it's not possible to provide an offset.
f | The Factors to match against. |
const Factors& AIToolbox::Factored::FasterTrie::getF | ( | ) | const |
This function returns a reference of the internal Factors space.
size_t AIToolbox::Factored::FasterTrie::insert | ( | PartialFactors | pf | ) |
This function inserts a new id using the input as a key.
Differently from Trie, we don't store the keys in an ordered way, so this operation takes constant time (bar reallocations).
pf | The PartialFactors used as key for the insertion. |
std::tuple<Entries, Factors> AIToolbox::Factored::FasterTrie::reconstruct | ( | const PartialFactors & | pf, |
bool | remove = false |
||
) |
This function returns a set of Entries which match the input and each other.
The output set is constructed randomly to avoid bias. The output of this function is thus randomized and not deterministic.
We additionally return the Factors constructed by merging all matches together. Any elements who couldn't be filled will be set as the value of their space.
pf | The PartialFactors to match against. |
remove | Whether the matches should be removed from the FasterTrie. |
size_t AIToolbox::Factored::FasterTrie::size | ( | ) | const |
This function returns the number of keys in the FasterTrie.