pub struct RawWordSelector<T> { /* private fields */ }
Expand description

Efficient selector of words from a word list.

The word list is given by a BufReader.

§Assumptions

The word list is assumed to:

  • Have a list of words separated by newline.
  • Use only English alphabet and ASCII.
  • Be sorted alphabetically.
    • In case-insensitive manner.
    • For example, both “Apple” and “apple” must appear before words started with “b”.
  • Be a file that is not modified while the object is alive.
  • Have no empty lines except at the end of the file.

Note: only words between length 2 and 8, inclusive, are considered. Having no words matching the criteria may lead to an infinite loop.

§Algorithm

During initialization, the RawWordSelector iterates through all the words in the list and builds an index mapping each letter (of the alphabet) to its byte position in the file and the cumulative number of words present starting with it.

To select a (pesudo-)random word, a random number between 0 (inclusive) and number of lines (exclusive) is generated. Using binary search, the index of where this number lies in the cumulative no. of words list is found. Using this index, the byte offset of the corresponding letter is found. The file is then read starting from this byte offset and read line-by-line until the correct word (at line number - cumulative num. words from the starting of this letter).

§Time complexity

Initialization: O(n)

Selecting a word: O(1) (best case) or O(n) (worst case)

§Space complexity

O(1) (only needs fixed length arrays).

Implementations§

source§

impl<T: Seek + Read> RawWordSelector<T>

source

pub fn new(reader: BufReader<T>) -> Result<Self, Error>

Create from any arbitrary BufReader.

Please ensure that assumptions defined at RawWordSelector are valid for the contents.

source§

impl RawWordSelector<File>

source

pub fn from_path(word_list_path: PathBuf) -> Result<Self, Error>

Create from a file at a path given by a PathBuf.

Please ensure that assumptions defined at RawWordSelector are valid for this file.

source§

impl RawWordSelector<Cursor<String>>

source

pub fn from_string(word_list: String) -> Result<Self, Error>

Create from a String representing the word list file.

Please ensure that assumptions defined at RawWordSelector are valid for the contents.

Trait Implementations§

source§

impl<T: Debug> Debug for RawWordSelector<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Seek + Read> WordSelector for RawWordSelector<T>

source§

fn new_word(&mut self) -> Result<String, Error>

Returns a new word.
source§

fn new_words(&mut self, num_words: usize) -> Result<Vec<String>, Error>

Returns a Vec containing num_words words.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for RawWordSelector<T>
where T: RefUnwindSafe,

§

impl<T> Send for RawWordSelector<T>
where T: Send,

§

impl<T> Sync for RawWordSelector<T>
where T: Sync,

§

impl<T> Unpin for RawWordSelector<T>
where T: Unpin,

§

impl<T> UnwindSafe for RawWordSelector<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V