libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
z::util::dictionary Class Reference

A class for performing searches on a dictionary of words. More...

#include <dictionary.hpp>

Inheritance diagram for z::util::dictionary:
z::core::sortedRefArray< zstring * > z::core::sortedArray< T > z::core::array< T > z::core::sizable z::core::arrayLike< const T &, T * > z::core::indexable< T > z::core::iterable< ITER >

Public Member Functions

 dictionary (bool caseSensitive=false) noexcept
 Constructor.
 
 dictionary (const dictionary &other) noexcept
 Copy constructor.
 
 ~dictionary () noexcept
 Destructor.
 
void clear () noexcept
 Empty the word list.
 
bool read (const zstring &filename, const core::timeout &time=-1, bool assumePresorted=false)
 Read this dictionary's word list as text from a file.
 
void write (const zstring &filename)
 Write this dictionary's word list to a file.
 
bool isWord (const zstring &word) const noexcept
 Check if the given string is a valid word in the dictionary (case is ignored).
 
void addWord (const zstring &word) noexcept
 Add a word to the dictionary.
 
bool isCaseSensitive () const noexcept
 Check whether this dictionary is case sensitive.
 
void setCaseSensitive (bool caseSensitive) noexcept
 Set the case sensitivity of this dictionary.
 
dictRange range () const noexcept
 Create a new range of words encompassing the whole dictionary.
 
bool narrow (dictRange *wordRange, uint32_t nextChar) const noexcept
 Narrow the results of a range given the next character.
 
int maxWordLength () const noexcept
 Get the length of the longest word in the dictionary.
 
dictionary filter (std::function< bool(const zstring &)> lambda) const
 Filters the dictionary based on a predicate and returns a new dictionary containing the words that satisfy the predicate.
 
dictionaryoperator= (dictionary &&other) noexcept
 Dictionary move operator.
 
dictionaryoperator= (const dictionary &other) noexcept
 Dictionary assignment operator.
 
z::core::array< zstringfindPermutations (const zstring &scrambled) const noexcept
 Find all permutations of a given scrambled word that are valid words in the dictionary.
 
template<typename archive >
void save (archive &ar) const
 Serialization output.
 
template<class archive >
void load (archive &ar)
 Serialization input.
 
- Public Member Functions inherited from z::core::sortedRefArray< zstring * >
 sortedRefArray ()
 Default constructor.
 
 sortedRefArray (const zstring * &arg1, const Args &...args)
 List-initialized constructor.
 
virtual bool operator() (const zstring * &arg1, const zstring * &arg2) const override
 Callable operator.
 
- Public Member Functions inherited from z::core::sortedArray< T >
 sortedArray ()
 Default constructor.
 
template<typename... Args>
 sortedArray (const T &arg1, const Args &...args)
 List-initialized constructor.
 
virtual int add (const T &object) override
 Add an object to the array.
 
virtual int find (const T &object) const override
 Check if a given object is in the array.
 
virtual int findInsert (const T &object, bool allowDuplicates=true) const
 Find an index where the given object can be inserted while keeping the array sorted.
 
virtual void shuffle () noexcept override
 Shuffle the elements of the array into a random order.
 
virtual void reverse () noexcept override
 Reverse the order of all elements in the array.
 
- Public Member Functions inherited from z::core::array< T >
 array ()
 Default constructor.
 
 array (const array &other)
 Copy constructor.
 
 array (const std::vector< T > &other)
 Copy from std::vector.
 
template<typename... Args>
 array (const T &arg1, const Args &...args)
 List-initialized constructor.
 
 array (const std::initializer_list< T > &other)
 Construct from a generic initializer list.
 
virtual ~array ()
 Destructor.
 
void clear ()
 Clear the data in the array.
 
void increase (int newSize) noexcept
 Increase the space allocated for this array.
 
void add (const array &other) noexcept
 Add another array to this array.
 
int push (const T &object) noexcept
 Add an object to the array.
 
void push (const array &other) noexcept
 Add another array to this array.
 
T pop ()
 Remove the last element from this array.
 
arrayinsert (const T &, int)
 Insert an object into the array.
 
void append (const T &)
 Append an object to the end of the array.
 
arrayremove (int)
 Remove an object from the array.
 
arrayremove (int, int)
 Remove all elements in a subset of the array.
 
arrayreplace (int, int, const T &)
 Replace all objects in the given range with an object.
 
arrayreplace (int, int, const array< T > &)
 Replace all objects in the given range with an array of objects.
 
array subset (int index, int count) const
 Get a contiguous subset of the elements in the array.
 
size_t size () const noexcept override
 Get the size of the array.
 
int length () const noexcept override
 Get the length of the array.
 
Tat (int)
 Function to get the object at the given index.
 
const Tat (int) const override
 Const function to get the object at the given index.
 
const Toperator[] (int index) const override
 Function to get the object at the given index.
 
Toperator[] (int index)
 Function to get the object at the given index.
 
bool contains (const T &object) const noexcept
 Check if a given object is in the array.
 
void sort () noexcept
 Sort the array based on default comparison operator.
 
void sort (std::function< bool(const T &, const T &)> lambda) noexcept
 Sort the array based on an arbitrary function.
 
array sorted () const noexcept
 Sort the array based on default comparison operator.
 
array sorted (std::function< bool(const T &, const T &)> lambda) const noexcept
 Sort the array based on an arbitrary function.
 
array shuffled () const noexcept
 Shuffle the elements of the array into a random order.
 
array reversed () const noexcept
 Reverse the order of all elements in the array.
 
arrayoperator= (const array &other)
 Array assignment operator.
 
arrayoperator= (const std::initializer_list< T > &other)
 Initializer list assignment operator.
 
bool operator== (const array &other) const
 Check whether two arrays' contents are the same.
 
bool operator> (const array &other) const
 Array greater-than operator.
 
bool operator< (const array &other) const
 Array less-than operator.
 
bool operator>= (const array &other) const
 Array greater-than-or-equal operator.
 
bool operator<= (const array &other) const
 Array less-than-or-equal operator.
 
virtual bool operator() (const T &arg1, const T &arg2) const
 Callable operator.
 
bool isValid (int index) const
 Check if an index is within the bounds of the array.
 
arrayswap (int index1, int index2)
 Swap two elements in an array.
 
template<typename U >
array< Umap (std::function< U(const T &)> lambda) const
 Applies a transformation function to each element of the array and returns a new array with the results.
 
array filter (std::function< bool(const T &)> lambda) const
 Filters the array based on a predicate and returns a new array containing the elements that satisfy the predicate.
 
T reduce (const T &defaultValue, std::function< T(const T &, const T &)> lambda) const
 Reduces the array to a single value by applying a binary operation cumulatively to the elements.
 
T randomElement () const
 Get a random element from the array.
 
array randomElements (int count) const noexcept
 Get N random elements from the array.
 
Tbegin () const noexcept override
 Get pointer to the beginning of the array.
 
Tend () const noexcept override
 Get pointer to the end of the array.
 
void save (cereal::JSONOutputArchive &ar) const
 JSON specialization of serialization output.
 
void save (cereal::XMLOutputArchive &ar) const
 XML specialization of serialization output.
 
template<typename archive >
void save (archive &ar) const
 Binary specialization of serialization output.
 
void load (cereal::JSONInputArchive &ar)
 JSON specialization of serialization input.
 
void load (cereal::XMLInputArchive &ar)
 XML specialization of serialization input.
 
template<class archive >
void load (archive &ar)
 Binary specialization of serialization input.
 
- Public Member Functions inherited from z::core::sizable
virtual ~sizable () noexcept
 Virtual destructor.
 
- Public Member Functions inherited from z::core::indexable< T >
virtual ~indexable () noexcept
 Virtual destructor.
 
- Public Member Functions inherited from z::core::iterable< ITER >
virtual ~iterable () noexcept
 Virtual destructor.
 

Additional Inherited Members

- Protected Member Functions inherited from z::core::sortedRefArray< zstring * >
virtual bool eq (const zstring * &arg1, const zstring * &arg2) const override
 Check if two objects are equal.
 
virtual bool gt (const zstring * &arg1, const zstring * &arg2) const override
 Check if one object is greater than another.
 
virtual bool lt (const zstring * &arg1, const zstring * &arg2) const override
 Check if one object is less than another.
 
- Protected Member Functions inherited from z::core::array< T >
void init (const T &arg1)
 Helper function for single object initialization.
 
template<typename... Args>
void init (const T &arg1, const Args &...args)
 Helper function for brace-enclosed list initialization.
 
virtual bool eq (const T &arg1, const T &arg2) const
 Check if two objects are equal.
 
virtual bool gt (const T &arg1, const T &arg2) const
 Check if one object is greater than another.
 
virtual bool lt (const T &arg1, const T &arg2) const
 Check if one object is less than another.
 
- Protected Attributes inherited from z::core::array< T >
std::vector< Tarray_data
 The data in the array.
 

Detailed Description

A class for performing searches on a dictionary of words.

Examples
dictionary.cpp, and word_unscramble.cpp.

Constructor & Destructor Documentation

◆ dictionary() [1/2]

z::util::dictionary::dictionary ( bool  caseSensitive = false)
noexcept

Constructor.

Parameters
caseSensitiveWhether searches on this dictionary are case sensitive.

◆ dictionary() [2/2]

z::util::dictionary::dictionary ( const dictionary other)
noexcept

Copy constructor.

Parameters
otherThe dictionary to copy from.

Member Function Documentation

◆ addWord()

void z::util::dictionary::addWord ( const zstring word)
noexcept

Add a word to the dictionary.

If the word already exists, does nothing. Otherwise, creates a new word.

Parameters
wordThe word to add or update.
Re-Entry: Simultaneous calls to this member function on the same object can cause data races.

◆ filter()

dictionary z::util::dictionary::filter ( std::function< bool(const zstring &)>  lambda) const

Filters the dictionary based on a predicate and returns a new dictionary containing the words that satisfy the predicate.

This function iterates through the dictionary, applies the given lambda function as a predicate to each element, and adds words that satisfy the predicate to the resulting dictionary.

Parameters
lambdaA function that takes a constant reference to a zstring and returns a boolean indicating whether the element should be included.
Returns
A new dictionary containing the words that satisfy the predicate.
Examples
dictionary.cpp.

◆ findPermutations()

z::core::array< zstring > z::util::dictionary::findPermutations ( const zstring scrambled) const
noexcept

Find all permutations of a given scrambled word that are valid words in the dictionary.

This function generates all possible permutations of the input scrambled word and checks each permutation against the dictionary to determine if it is a valid word. It returns an array of valid words found.

Parameters
scrambledThe scrambled word to find permutations for.
Returns
An array of valid words that are permutations of the input scrambled word.
Warning
This function may be computationally intensive for longer words due to the factorial growth of permutations.
Examples
word_unscramble.cpp.

◆ isCaseSensitive()

bool z::util::dictionary::isCaseSensitive ( ) const
noexcept

Check whether this dictionary is case sensitive.

Returns
true if case sensitive, false otherwise.
Re-Entry: This member function is thread-safe. Simultaneous calls will not cause data races, and any other thread-safe member may be called concurrently without interfering.

◆ isWord()

bool z::util::dictionary::isWord ( const zstring word) const
noexcept

Check if the given string is a valid word in the dictionary (case is ignored).

Parameters
wordThe word to search for.
Returns
True if it is a valid word, false otherwise.
Re-Entry: This member function is thread-safe. Simultaneous calls will not cause data races, and any other thread-safe member may be called concurrently without interfering.
Examples
dictionary.cpp.

◆ load()

template<class archive >
void z::util::dictionary::load ( archive ar)
inline

Serialization input.

Parameters
arThe input archive.

◆ maxWordLength()

int z::util::dictionary::maxWordLength ( ) const
noexcept

Get the length of the longest word in the dictionary.

This function is very fast, as the value is calculated when the dictionary is loaded or changed, not when this function is called. Prefer this function over manually scanning the dictionary for the longest word.

Returns
The length of the longest word.

◆ narrow()

bool z::util::dictionary::narrow ( dictRange wordRange,
uint32_t  nextChar 
) const
noexcept

Narrow the results of a range given the next character.

Parameters
wordRangeA reference to the range we're narrowing.
nextCharThe next character all words in the range must contain.
Returns
true if the range can be narrowed further, false otherwise.

◆ operator=() [1/2]

Dictionary assignment operator.

Clear the contents of this dictionary and create a copy of another dictionary's contents into this one.

Parameters
otherthe dictionary to copy from.
Returns
This dictionary after the operation (for a=b=c type expressions).

◆ operator=() [2/2]

Dictionary move operator.

Move a dictionary's contents to this dictionary.

Parameters
otherthe dictionary to copy from.
Returns
This dictionary after the operation (for a=b=c type expressions).

◆ range()

dictRange z::util::dictionary::range ( ) const
noexcept

Create a new range of words encompassing the whole dictionary.

This is meant to generate the initial object, then narrow() should be called to narrow down the possible results.

Returns
An empty dictRange object.

◆ read()

bool z::util::dictionary::read ( const zstring filename,
const core::timeout time = -1,
bool  assumePresorted = false 
)

Read this dictionary's word list as text from a file.

Note that for long dictionaries, it may take several seconds for this operation to complete! Hence the timeout method.

Parameters
filenameThe file to read from.
timeOptional param to force return after time out.
assumePresortedIf false, the word list is sorted while it is read. If true, no sorting is done.
Note
Operating on an un-sorted dictionary may have undefined behavior! Only set assumePresorted to true if you know for sure your data is sorted already!
Returns
false if finished reading successfully, true if not finished reading,
Exceptions
z::file::unreadableif reading failed.
Re-Entry: Simultaneous calls to this member function on the same object can cause data races.
Examples
dictionary.cpp, and word_unscramble.cpp.

◆ save()

template<typename archive >
void z::util::dictionary::save ( archive ar) const
inline

Serialization output.

Parameters
arThe output archive.

◆ setCaseSensitive()

void z::util::dictionary::setCaseSensitive ( bool  caseSensitive)
noexcept

Set the case sensitivity of this dictionary.

Parameters
caseSensitiveWhether this dictionary is case sensitive.

◆ write()

void z::util::dictionary::write ( const zstring filename)

Write this dictionary's word list to a file.

Parameters
filenameThe file to write to.
Exceptions
z::file::unwritableif writing failed.
Re-Entry: Simultaneous calls to this member function on the same object can cause data races.

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