libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
dictionary.hpp
1#pragma once
2
3#include "../core/sortedRefArray.hpp"
4#include "../core/string.hpp"
5#include "../core/timeout.hpp"
6
7#include "../core/sortedRefArray.hpp"
8#include "dictionary/dictRange.hpp"
9
10namespace z {
11namespace util {
15class dictionary : public core::sortedRefArray<zstring *> {
16private:
17 bool caseSensitive;
18 bool readingStream;
19 int maxWordLen;
20
21public:
26 dictionary(bool caseSensitive = false) noexcept;
27
33
36
41
62 bool read(const zstring &filename, const core::timeout &time = -1, bool assumePresorted = false);
63
70 void write(const zstring &filename);
71
82
93
102
108 void setCaseSensitive(bool caseSensitive) noexcept;
109
119
129
140
151 dictionary filter(std::function<bool(const zstring &)> lambda) const;
152
164
177
189
190#ifdef __has_include
191#if __has_include(<cereal/cereal.hpp>)
196 template <typename archive>
197 void save(archive &ar) const {
199 for (int i = 0; i < length(); i++) {
200 ar(*at(i));
201 }
202 }
203
208 template <class archive>
209 void load(archive &ar) {
210 clear();
212 ar(sz);
213 increase(sz);
214
215 for (CEREAL_SIZE_TYPE i = 0; i < sz; i++) {
216 zstring *data = new zstring;
217 ar(*data);
218 append(data);
219 }
220 }
221#endif
222#endif
223};
224} // namespace util
225} // namespace z
void increase(int newSize) noexcept
Increase the space allocated for this array.
Definition array.hpp:189
array()
Default constructor.
Definition array.hpp:146
int length() const noexcept override
Get the length of the array.
Definition array.hpp:1011
T & at(int)
Function to get the object at the given index.
Definition array.hpp:1028
void append(const T &)
Append an object to the end of the array.
Definition array.hpp:931
An extension of the core::sortedArray class, specialized for pointers.
Definition sortedRefArray.hpp:26
A template class for character strings.
Definition string.hpp:62
A class to allow efficient, custom dictionary search functions.
Definition dictRange.hpp:13
A class for performing searches on a dictionary of words.
Definition dictionary.hpp:15
z::core::array< zstring > findPermutations(const zstring &scrambled) const noexcept
Find all permutations of a given scrambled word that are valid words in the dictionary.
int maxWordLength() const noexcept
Get the length of the longest word in the dictionary.
bool isWord(const zstring &word) const noexcept
Check if the given string is a valid word in the dictionary (case is ignored).
void load(archive &ar)
Serialization input.
Definition dictionary.hpp:209
void clear() noexcept
Empty the word list.
void setCaseSensitive(bool caseSensitive) noexcept
Set the case sensitivity of this 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 sa...
dictRange range() const noexcept
Create a new range of words encompassing the whole dictionary.
void write(const zstring &filename)
Write this dictionary's word list to a file.
void save(archive &ar) const
Serialization output.
Definition dictionary.hpp:197
bool narrow(dictRange *wordRange, uint32_t nextChar) const noexcept
Narrow the results of a range given the next character.
void addWord(const zstring &word) noexcept
Add a word to the dictionary.
bool isCaseSensitive() const noexcept
Check whether this dictionary is case sensitive.
bool read(const zstring &filename, const core::timeout &time=-1, bool assumePresorted=false)
Read this dictionary's word list as text from a file.
dictionary(bool caseSensitive=false) noexcept
Constructor.