1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Provides utilities for working with alphabets.
//!
//! Note: This package re-exports the alphabet!() macro defined by the crate [alphabet_macro](https://crates.io/crates/alphabet-macro).

mod counter;
mod util;
mod word;

#[doc(inline)]
pub use alphabet_macro::alphabet;

use util::VectorAccess;

/// Represents an alphabet.
pub trait Alphabet<'a> {
    /// The iterator type used by iter_words().
    type IterWord: Iterator<Item = String>;

    /// The iterator type used by iter_words_counting().
    type IterWordCounting: Iterator<Item = String>;

    /// Allows to iterate over words of this alphabet (i.e. Self\* in the mathematical sense).
    /// This returns an infinite iterator in most cases.
    ///
    /// The words returned by the iterator are sorted lexicographically if the alphabet imposes any particular order on its symbols.
    ///
    /// All alphabets generated by the alphabet!() macro impose an order on their symbols, namely the order in which the symbols are listed.
    /// `Vec<char>` uses the order of its contents. For other implementors, refer to the respective documentation.
    ///
    /// The iterator is not infinite for empty alphabets. In that case, the only word is the empty word.
    ///
    /// # Examples
    ///
    /// Basic usage:
    /// ```
    /// use alphabet::*;
    ///
    /// alphabet!(SCREAM = "A");
    /// let mut words = SCREAM.iter_words();
    /// assert_eq!(words.next().unwrap(), "");
    /// assert_eq!(words.next().unwrap(), "A");
    /// assert_eq!(words.next().unwrap(), "AA");
    /// assert_eq!(words.next().unwrap(), "AAA");
    /// ```
    ///
    /// Note that this can not be used for 'counting':
    /// ```
    /// use alphabet::*;
    ///
    /// alphabet!(BINARY = "01");
    /// let mut words = BINARY.iter_words();
    /// assert_eq!(words.next().unwrap(), "");
    /// assert_eq!(words.next().unwrap(), "0");
    /// assert_eq!(words.next().unwrap(), "1");
    ///
    /// // Note that this does not 'count' in binary, it operates on symbols:
    /// assert_eq!(words.next().unwrap(), "00");
    /// assert_eq!(words.next().unwrap(), "01");
    /// assert_eq!(words.next().unwrap(), "10");
    /// assert_eq!(words.next().unwrap(), "11");
    /// assert_eq!(words.next().unwrap(), "000");
    /// ```
    ///
    /// Special case for empty alphabets:
    /// ```
    /// use alphabet::*;
    ///
    /// alphabet!(SILENCE = "");
    /// let mut words = SILENCE.iter_words();
    /// assert_eq!(words.next().unwrap(), "");
    /// assert!(words.next().is_none());
    /// ```
    fn iter_words(&'a self) -> Self::IterWord;

    /// Allows to iterate over 'count words' of this alphabet. This returns an infinite iterator in most cases.
    ///
    /// 'Count words' are words that are not empty and that do not start in the first symbol of the alphabet, unless they have length 1.
    /// Note that this requires the alphabet imposing an order on its symbols.
    ///
    /// Intuitively speaking, 'count words' are numbers using the alphabet as digits.
    ///
    /// All alphabets generated by the alphabet!() macro impose an order on their symbols, namely the order in which the symbols are listed.
    /// `Vec<char>` uses the order of its contents. For other implementors, refer to the respective documentation.
    ///
    /// The iterator is not infinite for empty alphabets and singleton alphabets.
    /// In both cases, there are no valid 'count words' and the iterator is empty.
    ///
    /// # Panics
    ///
    /// When `Self` imposes no order on its symbols.
    ///
    /// # Examples
    ///
    /// Basic usage:
    /// ```
    /// use alphabet::*;
    ///
    /// alphabet!(BINARY = "01");
    /// let mut words = BINARY.iter_words_counting();
    /// assert_eq!(words.next().unwrap(), "0");
    /// assert_eq!(words.next().unwrap(), "1");
    /// assert_eq!(words.next().unwrap(), "10");
    /// assert_eq!(words.next().unwrap(), "11");
    /// assert_eq!(words.next().unwrap(), "100");
    /// ```
    ///
    /// Special case empty and singleton alphabets:
    /// ```
    /// use alphabet::*;
    ///
    /// alphabet!(SILENCE = "");
    /// let mut words = SILENCE.iter_words_counting();
    /// assert!(words.next().is_none());
    /// ```
    /// ```
    /// use alphabet::*;
    ///
    /// alphabet!(SCREAM = "A");
    /// let mut words = SCREAM.iter_words_counting();
    /// assert!(words.next().is_none());
    /// ```
    fn iter_words_counting(&'a self) -> Self::IterWordCounting;
}

impl<'a> Alphabet<'a> for [char] {
    type IterWord = word::WordIterator<'a>;
    type IterWordCounting = word::CountingIterator<'a>;

    fn iter_words(&self) -> Self::IterWord {
        word::WordIterator::new(VectorAccess::Direct(self.to_vec()))
    }

    fn iter_words_counting(&self) -> Self::IterWordCounting {
        word::CountingIterator::new(VectorAccess::Direct(self.to_vec()))
    }
}

impl<'a> Alphabet<'a> for Vec<char> {
    type IterWord = word::WordIterator<'a>;
    type IterWordCounting = word::CountingIterator<'a>;

    fn iter_words(&'a self) -> Self::IterWord {
        word::WordIterator::new(VectorAccess::Indirect(&self))
    }

    fn iter_words_counting(&'a self) -> Self::IterWordCounting {
        word::CountingIterator::new(VectorAccess::Indirect(&self))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    alphabet!(EMPTY = "");
    alphabet!(SCREAM = "A");
    alphabet!(BINARY = "01");
    alphabet!(ENGLISH = "abcdefghijklmnopqrstuvwxyz");
    alphabet!(GERMAN = "aäbcdefghijklmnoöpqrstuüvwxyzß");

    #[test]
    fn test_many_words() {
        // Testing infinity is hard, so this has to be enough
        assert_eq!(10000, BINARY.iter_words().take(10000).count());
    }

    #[test]
    fn test_many_words_counting() {
        // Testing infinity is hard, so this has to be enough
        assert_eq!(10000, BINARY.iter_words_counting().take(10000).count());
    }

    #[test]
    fn test_binary_counting_works() {
        let mut iter = BINARY.iter_words_counting();
        for i in 0..10000 {
            let real_bin = format!("{:b}", i);
            assert_eq!(real_bin, iter.next().unwrap());
        }
    }

    #[test]
    fn test_empty_alphabet_has_only_empty_word() {
        let mut iter = EMPTY.iter_words();
        assert_eq!("", iter.next().unwrap());
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_empty_alphabet_has_no_count_words() {
        let mut iter = EMPTY.iter_words_counting();
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_singleton_alphabet_has_many_words() {
        // Testing infinity is hard, so this has to be enough
        assert_eq!(500, SCREAM.iter_words().take(500).count());
    }

    #[test]
    fn test_singleton_alphabet_has_no_count_words() {
        let mut iter = SCREAM.iter_words_counting();
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_some_english_words() {
        let mut iter = ENGLISH.iter_words();
        assert_eq!("i", iter.nth(9).unwrap());
        assert_eq!("cu", iter.nth(99 - 10).unwrap());
    }

    #[test]
    fn test_some_german_words() {
        let mut iter = GERMAN.iter_words();
        assert_eq!("ß", iter.nth(30).unwrap());
        assert_eq!("aä", iter.nth(32 - 31).unwrap());
    }

    #[test]
    fn test_iter_words_does_not_count() {
        let mut iter_a = BINARY.iter_words();
        let mut iter_b = BINARY.iter_words_counting();
        for _ in 0..10000 {
            assert_ne!(iter_a.next().unwrap(), iter_b.next().unwrap());
        }
    }
}