uniques 0.2.1

Analyze items in a slice and calculate the unique, first, duplicate, and subsequent items and their indices
Documentation
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
#![doc = include_str!("../README.md")]

//--------------------------------------------------------------------------------------------------
// Crates

use std::collections::{HashMap, hash_map::Entry};

//--------------------------------------------------------------------------------------------------
// Structs

/**
Analyze items in a slice and calculate the unique, first, duplicate, and subsequent items and their
indices

| [`Uniques`] field  | Description                                | [`Itertools`] method      |
|--------------------|--------------------------------------------|---------------------------|
| [`unique`]         | Values appearing exactly once for indices  | *none*                    |
| [`first`]          | First occurring values for indices         | [`Itertools::unique`]     |
| [`duplicate`]      | Duplicate values for indices               | [`Itertools::duplicates`] |
| [`subsequent`]     | Subsequent duplicate values for indices    | [`Itertools::duplicates`] |
| [`unique_idx`]     | Indices of values that appear exactly once | *none*                    |
| [`first_idx`]      | Indices of first occurring values          | *none*                    |
| [`duplicate_idx`]  | Indices of values that are duplicated      | *none*                    |
| [`subsequent_idx`] | Indices of subsequent duplicate values     | *none*                    |

> Note that [`Itertools`] methods do not include indices.

[`unique_idx`]: [`Uniques::uniques_idx`]
[`first_idx`]: [`Uniques::first_idx`]
[`duplicate_idx`]: [`Uniques::duplicate_idx`]
[`subsequent_idx`]: [`Uniques::subsequent_idx`]

[`unique`]: [`Uniques::uniques`]
[`first`]: [`Uniques::first`]
[`duplicate`]: [`Uniques::duplicate`]
[`subsequent`]: [`Uniques::subsequent`]

[`Itertools`]: https://docs.rs/itertools/latest/itertools/trait.Itertools.html
[`Itertools::duplicates`]: https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.duplicates
[`Itertools::unique`]: https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.unique

# Examples

## Example with no duplicate values and only unique values

```rust
use std::collections::HashMap;
use uniques::*;

let result = Uniques::new(&["a", "b", "c"]);

assert_eq!(
    result.unique_idx,
    HashMap::from([(&"a", 0), (&"b", 1), (&"c", 2)]),
);

assert_eq!(
    result.first_idx,
    HashMap::from([(&"a", 0), (&"b", 1), (&"c", 2)]),
);

assert!(result.duplicate_idx.is_empty());

assert!(result.subsequent_idx.is_empty());

assert_eq!(
    result.unique,
    HashMap::from([(0, &"a"), (1, &"b"), (2, &"c")]),
);

assert_eq!(
    result.first,
    HashMap::from([(0, &"a"), (1, &"b"), (2, &"c")]),
);

assert!(result.duplicate.is_empty());

assert!(result.subsequent.is_empty());
```

## Example with duplicate values and unique values

```rust
use std::collections::HashMap;
use uniques::*;

let result = Uniques::new(&["a", "b", "c", "a", "b"]);

assert_eq!(
    result.unique_idx,
    HashMap::from([(&"c", 2)]),
);

assert_eq!(
    result.first_idx,
    HashMap::from([(&"a", 0), (&"b", 1), (&"c", 2)]),
);

assert_eq!(
    result.duplicate_idx,
    HashMap::from([(&"a", vec![0, 3]), (&"b", vec![1, 4])]),
);

assert_eq!(
    result.subsequent_idx,
    HashMap::from([(&"a", vec![3]), (&"b", vec![4])]),
);

assert_eq!(
    result.unique,
    HashMap::from([(2, &"c")]),
);

assert_eq!(
    result.first,
    HashMap::from([(0, &"a"), (1, &"b"), (2, &"c")]),
);

assert_eq!(
    result.duplicate,
    HashMap::from([(0, &"a"), (1, &"b"), (3, &"a"), (4, &"b")]),
);

assert_eq!(
    result.subsequent,
    HashMap::from([(3, &"a"), (4, &"b")]),
);
```

## Example with only duplicate values and no unique values

```rust
use std::collections::HashMap;
use uniques::*;

let result = Uniques::new(&["a", "b", "c", "a", "b", "c"]);

assert!(result.unique_idx.is_empty());

assert_eq!(
    result.first_idx,
    HashMap::from([(&"a", 0), (&"b", 1), (&"c", 2)]),
);

assert_eq!(
    result.duplicate_idx,
    HashMap::from([
        (&"a", vec![0, 3]),
        (&"b", vec![1, 4]),
        (&"c", vec![2, 5]),
    ]),
);

assert_eq!(
    result.subsequent_idx,
    HashMap::from([(&"a", vec![3]), (&"b", vec![4]), (&"c", vec![5])]),
);

assert!(result.unique.is_empty());

assert_eq!(
    result.first,
    HashMap::from([(0, &"a"), (1, &"b"), (2, &"c")]),
);

assert_eq!(
    result.duplicate,
    HashMap::from([
        (0, &"a"),
        (1, &"b"),
        (2, &"c"),
        (3, &"a"),
        (4, &"b"),
        (5, &"c"),
    ]),
);

assert_eq!(
    result.subsequent,
    HashMap::from([(3, &"a"), (4, &"b"), (5, &"c")]),
);
```
*/
pub struct Uniques<'a, T> {
    /// Indices of values that appear exactly once
    pub unique_idx: HashMap<&'a T, usize>,

    /// Indices of first occurring values
    pub first_idx: HashMap<&'a T, usize>,

    /// Indices of values that are duplicated
    pub duplicate_idx: HashMap<&'a T, Vec<usize>>,

    /// Indices of subsequent duplicate values
    pub subsequent_idx: HashMap<&'a T, Vec<usize>>,

    /// Values appearing exactly once for indices
    pub unique: HashMap<usize, &'a T>,

    /**
    First occurring values for indices

    Similar to
    [`Itertools::unique`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.unique)
    but includes indices
    */
    pub first: HashMap<usize, &'a T>,

    /**
    Duplicate values for indices

    Similar to
    [`Itertools::duplicates`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.duplicates)
    but includes indices
    */
    pub duplicate: HashMap<usize, &'a T>,

    /**
    Subsequent duplicate values for indices

    Similar to
    [`Itertools::duplicates`](https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.duplicates)
    but includes indices
    */
    pub subsequent: HashMap<usize, &'a T>,
}

impl<'a, T> Uniques<'a, T> {
    /**
    Analyze items in a slice and calculate the unique, first, and duplicate items and their indices
    */
    pub fn new(items: &'a [T]) -> Uniques<'a, T>
    where
        T: std::hash::Hash + Eq,
    {
        let mut unique_idx = HashMap::new();
        let mut first_idx = HashMap::new();
        let mut duplicate_idx = HashMap::new();
        let mut subsequent_idx = HashMap::new();

        let mut unique = HashMap::new();
        let mut first = HashMap::new();
        let mut duplicate = HashMap::new();
        let mut subsequent = HashMap::new();

        for (i, x) in items.iter().enumerate() {
            match first_idx.entry(x) {
                Entry::Vacant(first_idx_x) => {
                    first_idx_x.insert(i);
                    first.insert(i, x);

                    unique_idx.insert(x, i);
                    unique.insert(i, x);
                }
                Entry::Occupied(first_idx_x) => {
                    let first_i = *first_idx_x.get();
                    duplicate_idx
                        .entry(x)
                        .and_modify(|v: &mut Vec<_>| v.push(i))
                        .or_insert_with(|| {
                            duplicate.insert(first_i, first[&first_i]);
                            vec![first_i, i]
                        });
                    duplicate.insert(i, x);

                    subsequent_idx
                        .entry(x)
                        .and_modify(|v: &mut Vec<_>| v.push(i))
                        .or_insert_with(|| vec![i]);
                    subsequent.insert(i, x);

                    if let Some(j) = unique_idx.remove(x) {
                        unique.remove(&j);
                    }
                }
            }
        }

        Uniques {
            unique_idx,
            first_idx,
            duplicate_idx,
            subsequent_idx,
            unique,
            first,
            duplicate,
            subsequent,
        }
    }
}

//--------------------------------------------------------------------------------------------------
// Tests

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

    #[test]
    fn integers_empty() {
        let input: &[usize] = &[];

        let unique_idx = HashMap::new();
        let first_idx = HashMap::new();
        let duplicate_idx = HashMap::new();
        let subsequent_idx = HashMap::new();

        let unique = HashMap::new();
        let first = HashMap::new();
        let duplicate = HashMap::new();
        let subsequent = HashMap::new();

        let result = Uniques::new(input);

        assert_eq!(result.unique_idx, unique_idx);
        assert_eq!(result.first_idx, first_idx);
        assert_eq!(result.duplicate_idx, duplicate_idx);
        assert_eq!(result.subsequent_idx, subsequent_idx);

        assert_eq!(result.unique, unique);
        assert_eq!(result.first, first);
        assert_eq!(result.duplicate, duplicate);
        assert_eq!(result.subsequent, subsequent);
    }

    #[test]
    fn integers_no_duplicates() {
        let input = &[0, 1, 2];

        let unique_idx = HashMap::from([(&0, 0), (&1, 1), (&2, 2)]);
        let first_idx = HashMap::from([(&0, 0), (&1, 1), (&2, 2)]);
        let duplicate_idx = HashMap::new();
        let subsequent_idx = HashMap::new();

        let unique = HashMap::from([(0, &0), (1, &1), (2, &2)]);
        let first = HashMap::from([(0, &0), (1, &1), (2, &2)]);
        let duplicate = HashMap::new();
        let subsequent = HashMap::new();

        let result = Uniques::new(input);

        assert_eq!(result.unique_idx, unique_idx);
        assert_eq!(result.first_idx, first_idx);
        assert_eq!(result.duplicate_idx, duplicate_idx);
        assert_eq!(result.subsequent_idx, subsequent_idx);

        assert_eq!(result.unique, unique);
        assert_eq!(result.first, first);
        assert_eq!(result.duplicate, duplicate);
        assert_eq!(result.subsequent, subsequent);
    }

    #[test]
    fn integers_with_duplicates() {
        let input = &[0, 1, 2, 0, 1];

        let unique_idx = HashMap::from([(&2, 2)]);
        let first_idx = HashMap::from([(&0, 0), (&1, 1), (&2, 2)]);
        let duplicate_idx = HashMap::from([(&0, vec![0, 3]), (&1, vec![1, 4])]);
        let subsequent_idx = HashMap::from([(&0, vec![3]), (&1, vec![4])]);

        let unique = HashMap::from([(2, &2)]);
        let first = HashMap::from([(0, &0), (1, &1), (2, &2)]);
        let duplicate = HashMap::from([(0, &0), (1, &1), (3, &0), (4, &1)]);
        let subsequent = HashMap::from([(3, &0), (4, &1)]);

        let result = Uniques::new(input);

        assert_eq!(result.unique_idx, unique_idx);
        assert_eq!(result.first_idx, first_idx);
        assert_eq!(result.duplicate_idx, duplicate_idx);
        assert_eq!(result.subsequent_idx, subsequent_idx);

        assert_eq!(result.unique, unique);
        assert_eq!(result.first, first);
        assert_eq!(result.duplicate, duplicate);
        assert_eq!(result.subsequent, subsequent);
    }

    #[test]
    fn strs_empty() {
        let input: &[&str] = &[];

        let unique_idx = HashMap::new();
        let first_idx = HashMap::new();
        let duplicate_idx = HashMap::new();
        let subsequent_idx = HashMap::new();

        let unique = HashMap::new();
        let first = HashMap::new();
        let duplicate = HashMap::new();
        let subsequent = HashMap::new();

        let result = Uniques::new(input);

        assert_eq!(result.unique_idx, unique_idx);
        assert_eq!(result.first_idx, first_idx);
        assert_eq!(result.duplicate_idx, duplicate_idx);
        assert_eq!(result.subsequent_idx, subsequent_idx);

        assert_eq!(result.unique, unique);
        assert_eq!(result.first, first);
        assert_eq!(result.duplicate, duplicate);
        assert_eq!(result.subsequent, subsequent);
    }

    #[test]
    fn strs_no_duplicates() {
        let input = &["a", "b", "c"];

        let unique_idx = HashMap::from([(&"a", 0), (&"b", 1), (&"c", 2)]);
        let first_idx = HashMap::from([(&"a", 0), (&"b", 1), (&"c", 2)]);
        let duplicate_idx = HashMap::new();
        let subsequent_idx = HashMap::new();

        let unique = HashMap::from([(0, &"a"), (1, &"b"), (2, &"c")]);
        let first = HashMap::from([(0, &"a"), (1, &"b"), (2, &"c")]);
        let duplicate = HashMap::new();
        let subsequent = HashMap::new();

        let result = Uniques::new(input);

        assert_eq!(result.unique_idx, unique_idx);
        assert_eq!(result.first_idx, first_idx);
        assert_eq!(result.duplicate_idx, duplicate_idx);
        assert_eq!(result.subsequent_idx, subsequent_idx);

        assert_eq!(result.unique, unique);
        assert_eq!(result.first, first);
        assert_eq!(result.duplicate, duplicate);
        assert_eq!(result.subsequent, subsequent);
    }

    #[test]
    fn strs_with_duplicates() {
        let input = &["a", "b", "c", "a", "b"];

        let unique_idx = HashMap::from([(&"c", 2)]);
        let first_idx = HashMap::from([(&"a", 0), (&"b", 1), (&"c", 2)]);
        let duplicate_idx = HashMap::from([(&"a", vec![0, 3]), (&"b", vec![1, 4])]);
        let subsequent_idx = HashMap::from([(&"a", vec![3]), (&"b", vec![4])]);

        let unique = HashMap::from([(2, &"c")]);
        let first = HashMap::from([(0, &"a"), (1, &"b"), (2, &"c")]);
        let duplicate = HashMap::from([(0, &"a"), (1, &"b"), (3, &"a"), (4, &"b")]);
        let subsequent = HashMap::from([(3, &"a"), (4, &"b")]);

        let result = Uniques::new(input);

        assert_eq!(result.unique_idx, unique_idx);
        assert_eq!(result.first_idx, first_idx);
        assert_eq!(result.duplicate_idx, duplicate_idx);
        assert_eq!(result.subsequent_idx, subsequent_idx);

        assert_eq!(result.unique, unique);
        assert_eq!(result.first, first);
        assert_eq!(result.duplicate, duplicate);
        assert_eq!(result.subsequent, subsequent);
    }
}