Wordsearch Solver
|
Solver implementation using a sorted std::vector
.
More...
#include <dictionary_std_vector.hpp>
Public Member Functions | |
DictionaryStdVector (DictionaryStdVector &&)=default | |
DictionaryStdVector & | operator= (DictionaryStdVector &&)=default |
DictionaryStdVector (const DictionaryStdVector &)=delete | |
DictionaryStdVector & | operator= (const DictionaryStdVector &)=delete |
DictionaryStdVector (const std::initializer_list< std::string_view > &words) | |
DictionaryStdVector (const std::initializer_list< std::string > &words) | |
DictionaryStdVector (const std::initializer_list< const char * > &words) | |
template<class Iterator1 , class Iterator2 > | |
DictionaryStdVector (Iterator1 first, const Iterator2 last) | |
Actual constructor that does the work. | |
template<class ForwardRange > | |
DictionaryStdVector (const ForwardRange &words) | |
std::size_t | size () const |
bool | empty () const |
template<class OutputIndexIterator > | |
void | contains_further (const std::string_view stem, const std::string_view suffixes, OutputIndexIterator contains_further_it) const |
For each char in suffix appended to stem, check whether this dictionary contains this word and if it may contain longer words with this prefix. More... | |
bool | contains (const std::string_view word) const |
Check if this dictionary contains word . More... | |
bool | further (const std::string_view word) const |
Check if this dictionary might contain words with word as a prefix. More... | |
Private Types | |
using | Iterator = std::vector< std::string >::const_iterator |
Private Member Functions | |
bool | further_impl (const std::string_view key, Iterator first, Iterator last) const |
Private Attributes | |
std::vector< std::string > | dict_ |
Friends | |
std::ostream & | operator<< (std::ostream &, const DictionaryStdVector &) |
Solver implementation using a sorted std::vector
.
contains() is O(log(n)) further() is more complicated, and suffers from being unable to definitively determine whether there may be further words. O(log(n))
Significantly faster than the std::set
based solver dictionary implementation due to an optimisation where we find the upper limit of a search, see calc_stop() and contains_further(). Still significantly slower than any of the actual tries.
bool dictionary_std_vector::DictionaryStdVector::contains | ( | const std::string_view | word | ) | const |
Check if this dictionary contains word
.
[in] | word | The word to check |
true
if word
is present, else false
void dictionary_std_vector::DictionaryStdVector::contains_further | ( | const std::string_view | stem, |
const std::string_view | suffixes, | ||
OutputIndexIterator | contains_further_it | ||
) | const |
For each char in suffix appended to stem, check whether this dictionary contains this word and if it may contain longer words with this prefix.
[in] | stem | |
[in] | suffixes | |
[out] | contains_further_it | This function is what the solver algorithm calls every iteration to ask the dictionary solver implementation to do its work. |
contains_further_it
should be assigned to and incremented like an output iterator. The value written should be a std::pair<bool, bool>.
Example contains_further implementation:
Each character's position in suffix
corresponds to the order that that suffix's
output should be written to contains_further_it
.
suffixes
is not guaranteed to be sorted.
bool dictionary_std_vector::DictionaryStdVector::further | ( | const std::string_view | word | ) | const |
Check if this dictionary might contain words with word
as a prefix.
[in] | word | The prefix to check |
false
if there are no more strings in the dictionary with word
as a prefix, true
if there might be.word
, there are more words that contain that prefix. This is acceptable, though sub-optimal and will result in wasted search time, as long as eventually this returns false, assuming for word
there are in fact no more words with that prefix.