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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use crate::DistanceMetric;
use std::cmp;

/// Represents a QGram metric where `q` is the length of a q-gram fragment.
///
/// The distance corresponds to
///
/// ```text
///     ||v(s1, q) - v(s2, q)||
/// ```
///
/// where `v(s, q)` denotes the vec on the space of q-grams of length q,
/// that contains the number of times a q-gram fragment appears for the str s
#[derive(Debug, Clone)]
pub struct QGram {
    /// Length of the fragment
    q: usize,
}

impl QGram {
    /// Creates a new [`QGram]` of length `q`.
    ///
    /// # Panics
    ///
    /// Panics if `q` is 0.
    pub fn new(q: usize) -> Self {
        assert_ne!(q, 0);
        Self { q }
    }
}

impl DistanceMetric for QGram {
    type Dist = usize;

    fn distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        let a: Vec<_> = a.into_iter().collect();
        let b: Vec<_> = b.into_iter().collect();

        let iter_a = QGramIter::new(&a, self.q);
        let iter_b = QGramIter::new(&b, self.q);

        eq_map(iter_a, iter_b)
            .into_iter()
            .map(|(n1, n2)| if n1 > n2 { n1 - n2 } else { n2 - n1 })
            .sum()
    }

    fn normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        let a = a.into_iter();
        let b = b.into_iter();

        let len_a = a.clone().count();
        let len_b = a.clone().count();

        if cmp::min(len_a, len_b) <= self.q {
            if a.eq(b) {
                0.
            } else {
                1.
            }
        } else {
            self.distance(a, b) as f64 / (len_a + len_b - 2 * self.q + 2) as f64
        }
    }
}

/// The Cosine distance corresponds to
///
/// ```text
///     1 - v(s1, q).v(s2, q)  / ||v(s1, q)|| * ||v(s2, q)||
/// ```
///
/// where `v(s, q)` denotes the vec on the space of q-grams of length q,
/// that contains the  number of times a q-gram appears for the str s.
///
/// If both inputs are empty a value of `0.` is returned. If one input is empty
/// and the other is not, a value of `1.` is returned. This avoids a return of
/// `f64::NaN` for those cases.
#[derive(Debug, Clone)]
pub struct Cosine {
    /// Length of the fragment
    q: usize,
}

impl Cosine {
    /// Creates a new [`Cosine]` metric of length `q`.
    ///
    /// # Panics
    ///
    /// Panics if `q` is 0.
    pub fn new(q: usize) -> Self {
        assert_ne!(q, 0);
        Self { q }
    }
}

impl DistanceMetric for Cosine {
    type Dist = f64;

    fn distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        let a: Vec<_> = a.into_iter().collect();
        let b: Vec<_> = b.into_iter().collect();

        // edge case where an input is empty
        if a.is_empty() || b.is_empty() {
            return if a.len() == b.len() { 0. } else { 1. };
        }

        let iter_a = QGramIter::new(&a, self.q);
        let iter_b = QGramIter::new(&b, self.q);

        let (norm_a, norm_b, norm_prod) = eq_map(iter_a, iter_b).into_iter().fold(
            (0usize, 0usize, 0usize),
            |(norm_a, norm_b, norm_prod), (n1, n2)| {
                (norm_a + n1 * n1, norm_b + n2 * n2, norm_prod + n1 * n2)
            },
        );
        1.0 - norm_prod as f64 / ((norm_a as f64).sqrt() * (norm_b as f64).sqrt())
    }

    fn normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        normalized_qgram(self, self.q, a, b)
    }
}

/// Represents a Jaccard metric where `q` is the length of a q-gram fragment.
///
/// The distance corresponds to
///
/// ```text
///     1 - |Q(s1, q) ∩ Q(s2, q)| / |Q(s1, q) ∪ Q(s2, q))|
/// ```
///
/// where ``Q(s, q)``  denotes the set of q-grams of length n for the str s.
///
/// If both inputs are empty a value of `0.` is returned. If one input is empty
/// and the other is not, a value of `1.` is returned. This avoids a return of
/// `f64::NaN` for those cases.
#[derive(Debug, Clone)]
pub struct Jaccard {
    /// Length of the fragment
    q: usize,
}

impl Jaccard {
    /// Creates a new [`Jaccard]` of length `q`.
    ///
    /// # Panics
    ///
    /// Panics if `q` is 0.
    pub fn new(q: usize) -> Self {
        assert_ne!(q, 0);
        Self { q }
    }
}

impl DistanceMetric for Jaccard {
    type Dist = f64;

    fn distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        let a: Vec<_> = a.into_iter().collect();
        let b: Vec<_> = b.into_iter().collect();

        // edge case where an input is empty
        if a.is_empty() || b.is_empty() {
            return if a.len() == b.len() { 0. } else { 1. };
        }

        let iter_a = QGramIter::new(&a, self.q);
        let iter_b = QGramIter::new(&b, self.q);

        let (num_dist_a, num_dist_b, num_intersect) = count_distinct_intersect(iter_a, iter_b);

        1.0 - num_intersect as f64 / ((num_dist_a + num_dist_b) as f64 - num_intersect as f64)
    }

    fn normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        normalized_qgram(self, self.q, a, b)
    }
}

/// Represents a SorensenDice metric where `q` is the length of a q-gram
/// fragment.
///
/// The distance corresponds to
///
/// ```text
///     1 - 2 * |Q(s1, q) ∩ Q(s2, q)|  / (|Q(s1, q)| + |Q(s2, q))|)
/// ```
///
/// where `Q(s, q)`  denotes the set of q-grams of length n for the str s
///
/// If both inputs are empty a value of `1.` is returned. If one input is empty
/// and the other is not, a value of `0.` is returned. This avoids a return of
/// `f64::NaN` for those cases.
#[derive(Debug, Clone)]
pub struct SorensenDice {
    /// Length of the fragment
    q: usize,
}

impl SorensenDice {
    /// Creates a new [`SorensenDice]` of length `q`.
    ///
    /// # Panics
    ///
    /// Panics if `q` is 0.
    pub fn new(q: usize) -> Self {
        assert_ne!(q, 0);
        Self { q }
    }
}

impl Default for SorensenDice {
    /// Use a bigram as default fragment length.
    fn default() -> Self {
        SorensenDice::new(2)
    }
}

impl DistanceMetric for SorensenDice {
    type Dist = f64;

    fn distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        let a: Vec<_> = a.into_iter().collect();
        let b: Vec<_> = b.into_iter().collect();

        // edge case where an input is empty
        if a.is_empty() || b.is_empty() {
            return if a.len() == b.len() { 0. } else { 1. };
        }

        let iter_a = QGramIter::new(&a, self.q);
        let iter_b = QGramIter::new(&b, self.q);

        let (num_dist_a, num_dist_b, num_intersect) = count_distinct_intersect(iter_a, iter_b);
        1.0 - 2.0 * num_intersect as f64 / (num_dist_a + num_dist_b) as f64
    }

    fn normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        normalized_qgram(self, self.q, a, b)
    }
}

/// Represents a Overlap metric where `q` is the length of a q-gram
/// fragment.
///
/// The distance corresponds to
///
/// ```text
///     1 - |Q(s1, q) ∩ Q(s2, q)|  / min(|Q(s1, q)|, |Q(s2, q)|)
/// ```
///
/// where `Q(s, q)`  denotes the set of q-grams of length n for the str s
///
/// If both inputs are empty a value of `1.` is returned. If one input is empty
/// and the other is not, a value of `0.` is returned. This avoids a return of
/// `f64::NaN` for those cases.
#[derive(Debug, Clone)]
pub struct Overlap {
    /// Length of the fragment
    q: usize,
}

impl Overlap {
    /// Creates a new [`Overlap]` of length `q`.
    ///
    /// # Panics
    ///
    /// Panics if `q` is 0.
    pub fn new(q: usize) -> Self {
        assert_ne!(q, 0);
        Self { q }
    }
}

impl Default for Overlap {
    /// Use a monogram as default overlap fragment length.
    fn default() -> Self {
        Overlap::new(1)
    }
}

impl DistanceMetric for Overlap {
    type Dist = f64;

    fn distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        let a: Vec<_> = a.into_iter().collect();
        let b: Vec<_> = b.into_iter().collect();

        // edge case where an input is empty
        if a.is_empty() || b.is_empty() {
            return if a.len() == b.len() { 0. } else { 1. };
        }

        let iter_a = QGramIter::new(&a, self.q);
        let iter_b = QGramIter::new(&b, self.q);

        let (num_dist_a, num_dist_b, num_intersect) = count_distinct_intersect(iter_a, iter_b);
        1.0 - num_intersect as f64 / cmp::min(num_dist_a, num_dist_b) as f64
    }

    fn normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        normalized_qgram(self, self.q, a, b)
    }
}

/// A Iterator that behaves similar to [`std::slice::Chunks`], but increases the
/// start index into the slice only by one each iteration.
#[derive(Debug, Clone)]
pub struct QGramIter<'a, T> {
    items: &'a [T],
    index: usize,
    chunk_size: usize,
}

impl<'a, T> QGramIter<'a, T> {
    /// Constructs the iterator that yields all possible q-grams of the
    /// underlying slice.
    ///
    /// A chunk size greater than the length of the underlying slice will result
    /// directly in a `None` value.
    ///
    /// # Panics
    ///
    /// Panics if `q` is 0.
    pub fn new(items: &'a [T], chunk_size: usize) -> Self {
        assert_ne!(chunk_size, 0);
        Self {
            items,
            chunk_size,
            index: 0,
        }
    }

    /// Resets the index back the beginning.
    pub fn reset(&mut self) {
        self.index = 0;
    }
}

impl<'a, T> Iterator for QGramIter<'a, T> {
    type Item = &'a [T];

    fn next(&mut self) -> Option<Self::Item> {
        if self.items.is_empty()
            || self.index + self.chunk_size > self.items.len()
            || self.chunk_size > self.items.len()
        {
            None
        } else {
            let q = &self.items[self.index..self.index + self.chunk_size];
            self.index += 1;
            Some(q)
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.items.is_empty()
            || self.index + self.chunk_size > self.items.len()
            || self.chunk_size > self.items.len()
        {
            (0, Some(0))
        } else {
            let n = self.items.len() + 1 - self.chunk_size;
            let rem = n - self.index;
            (rem, Some(rem))
        }
    }
}

/// Normalize the metric, so that it returns always a f64 between 0 and 1.
/// If a str length < q, returns a == b
fn normalized_qgram<Q, S, T>(metric: &Q, q: usize, a: S, b: T) -> Q::Dist
where
    Q: DistanceMetric<Dist = f64>,
    S: IntoIterator,
    T: IntoIterator,
    <S as IntoIterator>::IntoIter: Clone,
    <T as IntoIterator>::IntoIter: Clone,
    <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
    <T as IntoIterator>::Item: PartialEq,
{
    let a = a.into_iter();
    let b = b.into_iter();

    let len_a = a.clone().count();
    let len_b = b.clone().count();

    if cmp::min(len_a, len_b) <= q {
        if a.eq(b) {
            0.
        } else {
            1.
        }
    } else {
        metric.distance(a, b)
    }
}

fn count_distinct_intersect<S, T>(a: QGramIter<S>, b: QGramIter<T>) -> (usize, usize, usize)
where
    S: PartialEq + PartialEq<T>,
    T: PartialEq,
{
    eq_map(a, b).into_iter().fold(
        (0, 0, 0),
        |(num_dist_a, num_dist_b, num_intersect), (n1, n2)| {
            if n1 > 0 {
                if n2 > 0 {
                    (num_dist_a + 1, num_dist_b + 1, num_intersect + 1)
                } else {
                    (num_dist_a + 1, num_dist_b, num_intersect)
                }
            } else if n2 > 0 {
                (num_dist_a, num_dist_b + 1, num_intersect)
            } else {
                (num_dist_a, num_dist_b, num_intersect)
            }
        },
    )
}

/// Returns a list of tuples with the numbers of times a qgram appears in a and
/// b
///
/// This exists only to remove the necessity for `S: Hash + Eq, T:Hash + Eq`.
fn eq_map<'a, S, T>(a: QGramIter<'a, S>, b: QGramIter<'a, T>) -> Vec<(usize, usize)>
where
    S: PartialEq + PartialEq<T>,
    T: PartialEq,
{
    // remove duplicates and count how often a qgram occurs
    fn count_distinct<U: PartialEq>(v: &mut Vec<(U, usize)>) {
        'outer: for idx in (0..v.len()).rev() {
            let (qgram, num) = v.swap_remove(idx);
            for (other, num_other) in v.iter_mut() {
                if *other == qgram {
                    *num_other += num;
                    continue 'outer;
                }
            }
            v.push((qgram, num));
        }
    }
    let mut distinct_a: Vec<_> = a.map(|s| (s, 1)).collect();
    let mut distinct_b: Vec<_> = b.map(|s| (s, 1)).collect();

    count_distinct(&mut distinct_a);
    count_distinct(&mut distinct_b);

    let mut nums: Vec<_> = distinct_a.iter().map(|(_, n)| (*n, 0)).collect();

    'outer: for (qgram_b, num_b) in distinct_b {
        for (idx, (qgram_a, num_a)) in distinct_a.iter().enumerate() {
            if *qgram_a == qgram_b {
                nums[idx] = (*num_a, num_b);
                continue 'outer;
            }
        }
        nums.push((0, num_b));
    }
    nums
}

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

    #[test]
    fn qgram_distance() {
        assert_eq!(QGram::new(2).str_distance("abc", "abc"), 0);
        assert_eq!(QGram::new(1).str_distance("abc", "cba"), 0);
        assert_eq!(QGram::new(1).str_distance("abc", "ccc"), 4);
        assert_eq!(QGram::new(4).str_distance("aü☃", "aüaüafs"), 4);
        assert_eq!(QGram::new(4).str_distance("abcdefg", "defgabc"), 6);
    }

    #[test]
    fn cosine_distance() {
        assert_eq!(Cosine::new(1).str_distance("", ""), 0.);
        assert_eq!(Cosine::new(2).str_distance("abc", "ccc"), 1.);
        assert_eq!(
            format!("{:.6}", Cosine::new(2).str_distance("leia", "leela")),
            "0.711325"
        );
        assert_eq!(
            format!("{:.6}", Cosine::new(2).str_distance("achieve", "acheive")),
            "0.500000"
        );
        assert_eq!(Cosine::new(3).str_distance("achieve", "acheive"), 0.8);
    }

    #[test]
    fn jaccard_distance() {
        assert_eq!(Jaccard::new(1).str_distance("", ""), 0.);
        assert_eq!(Jaccard::new(1).str_distance("", "x"), 1.);
        assert_eq!(Jaccard::new(3).str_distance("abc", "abc"), 0.);
        assert_eq!(
            format!("{:.6}", Jaccard::new(1).str_distance("abc", "ccc")),
            "0.666667"
        );
    }

    #[test]
    fn sorensen_dice_distance() {
        assert_eq!(SorensenDice::new(1).str_distance("", ""), 0.);
        assert_eq!(SorensenDice::new(3).str_distance("", "abc"), 1.);
        assert_eq!(SorensenDice::new(3).str_distance("abc", "abc"), 0.);
        assert_eq!(SorensenDice::new(3).str_distance("abc", "xxx"), 1.);
        assert_eq!(SorensenDice::new(2).str_distance("monday", "montag"), 0.6);
        assert_eq!(SorensenDice::new(2).str_distance("nacht", "night"), 0.75);

        // 1-
        // assert_eq!(SorensenDice::new(2).distance("nacht", "night"),
        // strsim::sorensen_dice("nacht", "night"))
    }

    #[test]
    fn overlap_distance() {
        assert_eq!(SorensenDice::new(1).str_distance("", ""), 0.);
        assert_eq!(SorensenDice::new(1).str_distance("", "abc"), 1.);
        assert_eq!(SorensenDice::new(3).str_distance("abc", "abc"), 0.);
        assert_eq!(SorensenDice::new(3).str_distance("abc", "xxx"), 1.);
        assert_eq!(
            format!(
                "{:.6}",
                SorensenDice::new(1).str_distance("monday", "montag")
            ),
            "0.333333"
        );
        assert_eq!(SorensenDice::new(1).str_distance("nacht", "night"), 0.4);
    }

    #[test]
    fn qgram_iter() {
        let s: Vec<_> = "hello".chars().collect();
        let mut iter = QGramIter::new(&s, 2);
        assert_eq!(iter.next(), Some(['h', 'e'].as_ref()));
        assert_eq!(iter.next(), Some(['e', 'l'].as_ref()));
        assert_eq!(iter.next(), Some(['l', 'l'].as_ref()));
        assert_eq!(iter.next(), Some(['l', 'o'].as_ref()));
        assert_eq!(iter.next(), None);
        assert_eq!(iter.next(), None);

        let s: Vec<_> = "abc".chars().collect();
        let mut iter = QGramIter::new(&s, 2);
        assert_eq!(iter.next(), Some(['a', 'b'].as_ref()));
        assert_eq!(iter.next(), Some(['b', 'c'].as_ref()));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn empty_qgram() {
        let s: Vec<_> = "".chars().collect();
        let mut iter = QGramIter::new(&s, 1);
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn single_qgram() {
        let s: Vec<_> = "hel".chars().collect();
        let mut iter = QGramIter::new(&s, 3);
        assert_eq!(iter.next(), Some(['h', 'e', 'l'].as_ref()));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn exceeding_qgram() {
        let s: Vec<_> = "hel".chars().collect();
        let mut iter = QGramIter::new(&s, 4);
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn size_hint() {
        let s: Vec<_> = "hel".chars().collect();
        let iter = QGramIter::new(&s, 4);
        assert_eq!(iter.size_hint(), (0, Some(0)));

        let s: Vec<_> = "hello".chars().collect();
        let iter = QGramIter::new(&s, 2);
        assert_eq!(iter.size_hint(), (4, Some(4)));
        assert_eq!(iter.count(), 4);

        let s: Vec<_> = "hello".chars().collect();
        let iter = QGramIter::new(&s, 3);
        assert_eq!(iter.size_hint(), (3, Some(3)));
        assert_eq!(iter.count(), 3);

        let s: Vec<_> = "hello00000".chars().collect();
        let mut iter = QGramIter::new(&s, 5);
        assert_eq!(iter.size_hint(), (6, Some(6)));
        iter.next();
        assert_eq!(iter.size_hint(), (5, Some(5)));

        let s: Vec<_> = "hello00000".chars().collect();
        let mut iter = QGramIter::new(&s, 1);
        assert_eq!(iter.size_hint(), (10, Some(10)));
        iter.nth(8);
        assert_eq!(iter.size_hint(), (1, Some(1)));
        iter.next();
        assert_eq!(iter.size_hint(), (0, Some(0)));
    }

    #[test]
    fn count_eq() {
        let s1: Vec<_> = "abc".chars().collect();
        let s2: Vec<_> = "abc".chars().collect();
        let q1 = QGramIter::new(&s1, 2);
        let q2 = QGramIter::new(&s2, 2);

        assert_eq!(eq_map(q1, q2), vec![(1, 1), (1, 1)]);

        let s1: Vec<_> = "abc".chars().collect();
        let s2: Vec<_> = "abcdef".chars().collect();
        let q1 = QGramIter::new(&s1, 2);
        let q2 = QGramIter::new(&s2, 2);

        assert_eq!(eq_map(q1, q2), vec![(1, 1), (1, 1), (0, 1), (0, 1), (0, 1)]);
    }
}