Wordsearch Solver
Classes | Functions
utility Namespace Reference

Utility functions. More...

Classes

class  FlatCharValueMap
 A small cache for each character of a word and an associated value, such as iterator into a trie for each character. More...
 
struct  ValueIteratorPair
 std::pair neater replacement with appropriate names More...
 
class  LruCache
 Quick and dirty attempt at implementing an LRU cache. More...
 

Functions

template<class K , class V >
std::ostream & operator<< (std::ostream &os, const LruCache< K, V > &cache)
 
template<class Rng >
auto words_grouped_by_prefix_suffix (const Rng &words)
 This rather hefty function produces a range of ranges from the input of a range of strings (or a container holding a container of chars). More...
 
std::vector< std::string > read_file_as_lines (const char *filepath)
 Read a file at path filepath and return it split by newlines into a std::vector<std::string> More...
 
std::vector< std::string > read_file_as_lines (const std::string &filepath)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
std::string read_file_as_string (const char *filepath)
 Read a file at path filepath into a std::string More...
 
std::string read_file_as_string (const std::string &filepath)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
std::vector< std::string > read_file_as_lines_keep_lowercase_ascii_only (const char *filepath)
 Just like read_file_as_lines() except only keeps lines where all characters are lowercase ascii. More...
 
std::vector< std::string > read_file_as_lines_keep_lowercase_ascii_only (const std::string &filepath)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<class String >
void throw_if_not_lowercase_ascii (const String &word)
 
template<class Range >
auto make_adjacent_view (const Range &rng)
 Given a range [1, 2, 3, 4], returns [{1, 2}, {2, 3}, {3, 4}] More...
 
template<class DataView , class RowIndexes >
auto make_row_view (DataView &&data_view, const RowIndexes &row_indexes)
 Outputs a row view onto data_view for each difference between indexes in row_indexes. More...
 

Detailed Description

Utility functions.

Function Documentation

◆ make_adjacent_view()

template<class Range >
auto utility::make_adjacent_view ( const Range &  rng)

Given a range [1, 2, 3, 4], returns [{1, 2}, {2, 3}, {3, 4}]

Parameters
[in]rngA forward range or better

◆ make_row_view()

template<class DataView , class RowIndexes >
auto utility::make_row_view ( DataView &&  data_view,
const RowIndexes &  row_indexes 
)

Outputs a row view onto data_view for each difference between indexes in row_indexes.

Parameters
[in]data_viewA view onto data
[in]row_indexesIndexes splitting the data view into rows

Example: make_row_view("abcdef", {0, 2, 5, 6}) => ("ab", "cde", "f")

◆ read_file_as_lines()

std::vector< std::string > utility::read_file_as_lines ( const char *  filepath)

Read a file at path filepath and return it split by newlines into a std::vector<std::string>

Parameters
[in]filepath
Returns
std::vector<std::string> Each line as a string
Exceptions
std::runtime_errorIf there is an error with file opening (file doesn't exist, etc)

◆ read_file_as_lines_keep_lowercase_ascii_only()

std::vector< std::string > utility::read_file_as_lines_keep_lowercase_ascii_only ( const char *  filepath)

Just like read_file_as_lines() except only keeps lines where all characters are lowercase ascii.

Read a file at path filepath and return it split by newlines into a std::vector<std::string>

Parameters
[in]filepath
Returns
std::vector<std::string> Each line as a string
Exceptions
std::runtime_errorIf there is an error with file opening (file doesn't exist, etc)

◆ read_file_as_string()

std::string utility::read_file_as_string ( const char *  filepath)

Read a file at path filepath into a std::string

Parameters
[in]filepath
Returns
std::string The file in a string
Exceptions
std::runtime_errorIf there is an error with file opening (file doesn't exist, etc)

◆ throw_if_not_lowercase_ascii()

template<class String >
void utility::throw_if_not_lowercase_ascii ( const String &  word)
Exceptions
std::runtime_error

◆ words_grouped_by_prefix_suffix()

template<class Rng >
auto utility::words_grouped_by_prefix_suffix ( const Rng &  words)

This rather hefty function produces a range of ranges from the input of a range of strings (or a container holding a container of chars).

Parameters
[in]wordsA lexicographically sorted container or view of strings
Returns
See example below.
Note
The input range to this function MUST be sorted, else throws.
Exceptions
std::runtime_errorIf the input range is not sorted

It is used for construction of tries. It returns a range of ranges that may be used as such:

std::vector<std::string> lines = {
"woods", "woody", "wood", "a", "ask",
};
std::sort(lines.begin(), lines.end());
for (auto words_by_length : utility::words_grouped_by_prefix_suffix(lines)) {
for (auto [prefix, suffix, end_of_word] : words_by_length) {
fmt::print("Word: \"{}\", suffixes: [{}], end_of_word: {}\n", prefix,
suffix, end_of_word);
}
fmt::print("\n");
}
auto words_grouped_by_prefix_suffix(const Rng &words)
This rather hefty function produces a range of ranges from the input of a range of strings (or a cont...
Definition: utility.tpp:133
Output:
Word: "", suffixes: [aw], end_of_word: false
Word: "a", suffixes: [s], end_of_word: true
Word: "w", suffixes: [o], end_of_word: false
Word: "as", suffixes: [k], end_of_word: false
Word: "wo", suffixes: [o], end_of_word: false
Word: "ask", suffixes: [], end_of_word: true
Word: "woo", suffixes: [d], end_of_word: false
Word: "wood", suffixes: [sy], end_of_word: true
Word: "woods", suffixes: [], end_of_word: true
Word: "woody", suffixes: [], end_of_word: true