Wordsearch Solver
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
utility::FlatCharValueMap< Value > Class Template Reference

A small cache for each character of a word and an associated value, such as iterator into a trie for each character. More...

#include <flat_char_value_map.hpp>

Public Types

using NumbElementsConsumed = std::size_t
 

Public Member Functions

 FlatCharValueMap (const FlatCharValueMap &)
 
FlatCharValueMapoperator= (const FlatCharValueMap &)
 
const Value * lookup (const std::string_view &word, std::size_t &consumed)
 Retrieve the cached value for a word. More...
 
void append (const char key, const Value &value)
 Add a mapping from a char key to value. More...
 
void append (const char key, Value &&value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void clear ()
 Clears the cache.
 

Private Member Functions

template<class Str >
NumbElementsConsumed lookup_impl (const Str &word) const
 Lookup word and return how many letters are found in the cache. More...
 

Private Attributes

std::string keys_
 
std::vector< Value > values_
 
std::size_t hits = 0
 
std::size_t misses = 0
 

Detailed Description

template<class Value>
class utility::FlatCharValueMap< Value >

A small cache for each character of a word and an associated value, such as iterator into a trie for each character.

Really this exists to overcome a potential performance limitation of the interface by which the dictionary solvers plug into the solver algorithm, where they would otherwise have to find their position again every iteration from scratch. However, for some iterations, the stem of the word is still the same, as the solver performs a breadth first search.

A different interface for the solver dictionaries to implement may help this, or simply storing more state on the solvers. What I'd really like is c++20 coroutines, allowing the solver dictionaries to write a tradional for loop style solver that would keep all the appropriate state in scope managed by the coroutine.

Member Function Documentation

◆ append()

template<class Value >
void utility::FlatCharValueMap< Value >::append ( const char  key,
const Value &  value 
)
inline

Add a mapping from a char key to value.

Parameters
[in]key
[in]value

◆ lookup()

template<class Value >
const Value* utility::FlatCharValueMap< Value >::lookup ( const std::string_view &  word,
std::size_t &  consumed 
)
inline

Retrieve the cached value for a word.

Parameters
[in]wordThe word to lookup
[in,out]consumedNumber that will be incremented by how many letters are matched
Returns
Pointer to the Value, or nullptr if no letters matched
Note
gcc seems to take a perf hit when passed a string param and then a string_view must be created, clang not so. Both compilers seem unable to completely remove the overhead of constructing a string_view by value when passed one.

◆ lookup_impl()

template<class Value >
template<class Str >
NumbElementsConsumed utility::FlatCharValueMap< Value >::lookup_impl ( const Str &  word) const
inlineprivate

Lookup word and return how many letters are found in the cache.

Parameters
[in]wordTemplated (unnecessarily now) for use with string or string_view
Returns
0 if none found

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