kodiak-sets 0.2.0

A library to manage generic sets supporting unique features.
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
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
//! `Sequence` is designed for use cases in which the elements' order has to get persisted
//! but the chosen persistence layer requires metadata to do so, e.g. a database.
//! `Sequence` allows to add and remove elements at any position, virtually infinitely.

// Re-exports for convenient use within crate.
// none

// Publicly re-exporting all items valuable to users.
// (avoids explicitly listing re-exports in crate documentation as there is no alternate path to those items)
use std::cmp::Ordering;
use std::fmt::{Debug, Formatter};
use std::ops::{Add, AddAssign};
use std::ops::{Index, IndexMut};

use num_integer::gcd;
#[cfg(feature = "serde-derive")]
use serde::{Deserialize, Serialize};

//
// Node
//
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde-derive", derive(Serialize, Deserialize))]
pub struct Node<T> {
    position: Pos,
    element: Option<T>,
}

impl<T> Node<T> {
    #[inline]
    #[must_use]
    fn new(position: Pos, element: T) -> Self {
        Node {
            position,
            element: Some(element),
        }
    }

    #[inline]
    #[must_use]
    fn new_empty(position: Pos) -> Self {
        Node { position, element: None }
    }

    #[inline]
    #[must_use]
    fn position(&self) -> Pos {
        self.position
    }

    #[inline]
    #[must_use]
    pub fn pos(&self) -> (u64, u64) {
        (self.position.num, self.position.denom)
    }

    #[inline]
    #[must_use]
    pub fn num(&self) -> u64 {
        self.position.num
    }

    #[inline]
    #[must_use]
    pub fn denom(&self) -> u64 {
        self.position.denom
    }

    #[inline]
    #[must_use]
    pub fn element(self) -> Option<T> {
        self.element
    }

    #[inline]
    #[must_use]
    pub fn element_as_ref(&self) -> Option<&T> {
        self.element.as_ref()
    }

    #[inline]
    #[must_use]
    pub fn element_as_mut(&mut self) -> Option<&mut T> {
        self.element.as_mut()
    }

    #[inline]
    #[must_use]
    pub fn is_none(&self) -> bool {
        self.element.is_none()
    }

    #[inline]
    #[must_use]
    pub fn is_some(&self) -> bool {
        self.element.is_some()
    }

    #[inline]
    fn set(&mut self, element: T) {
        self.element = Some(element)
    }
}

#[cfg(test)]
#[path = "tests/node_tests.rs"]
mod node_tests;

//
// Position
//
const DENOM_MIN: u64 = 1;

/// `Pos` defines the ordering of `Node`s in a `Sequence`.
#[derive(Copy, Clone)]
#[cfg_attr(feature = "serde-derive", derive(Serialize, Deserialize))]
pub struct Pos {
    num: u64,
    denom: u64,
}

trait Min {
    const MIN: Pos;
}

impl Min for Pos {
    const MIN: Pos = Pos { num: u64::MIN, denom: 1 };
}

trait Max {
    const MAX: Pos;
}

impl Max for Pos {
    const MAX: Pos = Pos { num: u64::MAX, denom: 1 };
}

impl Pos {
    /// Creates a valid `Pos`, i.e. the denominator >= 1
    /// If denominator is set to 0, the `Pos` will have a denominator of 1.
    #[inline]
    #[must_use]
    fn new(num: u64, denom: u64) -> Self {
        if denom < DENOM_MIN {
            Pos { num, denom: DENOM_MIN }
        } else {
            Pos { num, denom }
        }
    }

    // Not a valid position, used for incrementing a position only.
    #[inline]
    #[must_use]
    fn n1d0() -> Self {
        Self { num: 1, denom: 0 }
    }

    #[inline]
    #[must_use]
    fn mid(first: Self, second: Self) -> Self {
        first + second
    }
}

/// `Pos` are added to each other by adding their numerators and denominators separately.
/// So, `Pos` does not follow the rules for adding fractions.
impl Add for Pos {
    type Output = Self;

    #[inline]
    #[must_use]
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            num: self.num + rhs.num,
            denom: self.denom + rhs.denom,
        }
    }
}

/// `Pos` are added to each other by adding their numerators and denominators separately.
/// So, `Pos` does not follow the rules for adding fractions.
impl AddAssign for Pos {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl PartialEq for Pos {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        if self.num == other.num && self.denom == other.denom {
            true
        } else {
            let self_divisor = gcd(self.num, self.denom);
            let other_divisor = gcd(other.num, other.denom);

            // If greatest common divisor (gcd) is not 1 we have to reduce the
            // Position first by dividing numerator and denominator by the gcd.
            match (self_divisor, other_divisor) {
                (1, 1) => false,
                #[rustfmt::skip]
                (_, 1) => { // marked for tarpaulin
                    let f1 = Self::new(self.num / self_divisor, self.denom / self_divisor);
                    f1 == *other
                }
                #[rustfmt::skip]
                (1, _) => { // marked for tarpaulin
                    let f2 = Self::new(other.num / other_divisor, other.denom / other_divisor);
                    f2 == *self
                }
                #[rustfmt::skip]
                (_, _) => { // marked for tarpaulin
                    let f1 = Self::new(self.num / self_divisor, self.denom / self_divisor);
                    let f2 = Self::new(other.num / other_divisor, other.denom / other_divisor);
                    f1 == f2
                }
            }
        }
    }
}

impl PartialOrd for Pos {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let f1 = self.num as f64 / self.denom as f64;
        let f2 = other.num as f64 / self.denom as f64;

        f1.partial_cmp(&f2)
    }
}

impl Debug for Pos {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}/{} ({})", self.num, self.denom, self.num as f64 / self.denom as f64)
    }
}

impl Default for Pos {
    #[inline]
    #[must_use]
    fn default() -> Self {
        Self::new(1, DENOM_MIN)
    }
}

#[cfg(test)]
#[path = "tests/position_tests.rs"]
mod position_tests;

//
// Sequence
//

/// A `Sequence` with a deterministic and easy to persist ordering.
///
/// What you can do
/// * Create a Sequence: [new()](`Sequence::new()`) or with_capacity()
/// * Determine the capacity: capacity()
/// * Determine if it contains elements: is_empty()
/// * Determine how many elements it contains: len()
/// * Get the first element: first()
/// * Get the last element: last()
/// * Get an immutable reference to an element: get()
/// * Get a mutable reference to an element: get_mut()
/// * Insert an element at a defined index: insert()
/// * Insert an element at a defined positions: insert_at()
/// * Get an element's position from its index: position_from() and pos_from()
/// * Append an element to the sequence: push()
/// * Remove an element at a defined index: remove()
/// * Remove an element at a defined position: remove_at()
#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde-derive", derive(Serialize, Deserialize))]
pub struct Sequence<T> {
    nodes: Vec<Node<T>>,
    len: usize,
}

impl<T> Sequence<T> {
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            len: 0,
        }
    }

    #[inline]
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            nodes: Vec::with_capacity(capacity),
            len: 0,
        }
    }

    /// Returns the total number of elements the sequence can
    /// hold without reallocating.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.nodes.capacity()
    }

    /// Returns true if the sequence contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns the number of elements in the sequence, also
    /// referred to as its 'length'.
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns the first element of the slice, or None
    /// if it is empty.
    #[inline]
    #[must_use]
    pub fn first(&self) -> Option<&T> {
        if self.len == 0 {
            return None;
        }

        for node in self.nodes.iter() {
            if node.is_some() {
                return node.element_as_ref();
            }
        }

        #[cfg(not(tarpaulin_include))]
        {
            None
        }
    }

    /// Returns the last element of the slice, or None
    /// if it is empty.
    #[inline]
    #[must_use]
    pub fn last(&self) -> Option<&T> {
        if self.len == 0 {
            return None;
        }

        for node in self.nodes.iter().rev() {
            if node.is_some() {
                return node.element_as_ref();
            }
        }

        #[cfg(not(tarpaulin_include))]
        {
            None
        }
    }

    #[inline]
    #[must_use]
    /// Returns `Some<T>`
    /// Returns None when out of bounds
    pub fn get(&self, index: usize) -> Option<&T> {
        if index >= self.len {
            return None;
        }

        for node in self.nodes[index..].iter() {
            if node.is_some() {
                return node.element_as_ref();
            }
        }

        #[cfg(not(tarpaulin_include))]
        {
            None
        }
    }

    #[inline]
    #[must_use]
    /// Returns `Some<T>`
    /// Returns None when out of bounds
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        if index >= self.len {
            return None;
        }

        for node in self.nodes[index..].iter_mut() {
            if node.is_some() {
                return node.element_as_mut();
            }
        }

        #[cfg(not(tarpaulin_include))]
        {
            None
        }
    }

    /// Iterates the sequence searching for position.
    /// Returns None if position is not in sequence.
    /// Returns Some(index) if position is in sequence.
    ///
    /// # Panics
    /// This function might panic if the iterator has more than usize::MAX non-matching elements.
    // todo: legacy!
    // todo: cover panic!
    pub fn index_from(&self, position: Pos) -> Option<usize> {
        self.nodes.iter().position(|node| node.position() == position)
    }

    /// Inserts an element at index, shifting all elements after it to the right.
    /// Appends the element to the vector if index > len.
    /// Inserts an element into an existing Node or by creating a new Node.
    /// Remember: Index starts at 0.
    ///
    /// if node is empty (i.e. node.element == None) set node.element to Some(element).
    /// if node is not empty (i.e. node.element == Some) insert element into self.nodes.
    ///
    /// # Panics
    /// Unlike `std::vec::Vec`, does not panic.
    pub fn insert(&mut self, index: usize, element: T) {
        if index >= self.nodes.len() {
            // Append element (self.len is incremented by push()).
            self.push(element);
        } else {
            if self.nodes[index].is_some() {
                let pos = if index == 0 {
                    // unwrap() is safe because the prior if covered the self.len() == 0 case.
                    Pos::mid(Pos::MIN, self.nodes.first().map(|node| node.position()).unwrap())
                } else {
                    Pos::mid(
                        self.nodes.get(index - 1).map(|node| node.position()).unwrap(),
                        self.nodes.get(index).map(|node| node.position()).unwrap(),
                    )
                };

                let node = Node::new(pos, element);
                self.nodes.insert(index, node);
            } else {
                self.nodes[index].set(element);
            }

            self.len += 1;
        }
    }

    /// Inserts an element at position. If there is an element at the position, it is overwritten.
    /// If not, element is inserted and all following elements after shifted to the right.
    // todo: use binary_search which required Ord for Position
    pub fn insert_at(&mut self, position: Pos, element: T) {
        match self.index_from(position) {
            None => {
                let index = self.nodes.partition_point(|node| node.position() < position);
                self.insert(index, element);
            }
            Some(index) => {
                // If node does not contain an element, increase len before setting the element.
                if self.nodes[index].is_none() {
                    self.len += 1;
                }

                // Replace the prior element.
                self.nodes[index].set(element);
            }
        }
    }

    // todo: legacy
    pub fn position_from(&self, index: usize) -> Option<Pos> {
        if index >= self.nodes.len() {
            None
        } else {
            Some(self.nodes[index].position())
        }
    }

    #[allow(unused)]
    fn pos_from(&self, index: usize) -> Option<(u64, u64)> {
        if index >= self.nodes.len() {
            None
        } else {
            Some(self.nodes[index].pos())
        }
    }

    /// Appends an element to the back of the sequence.
    #[inline]
    pub fn push(&mut self, element: T) {
        let pos = match self.last_position() {
            None => Pos::new(1, 1),
            Some(pos) => pos + Pos::n1d0(),
        };
        let node = Node::new(pos, element);

        self.nodes.push(node);
        self.len += 1;
    }

    /// Removes and returns the element at index.
    /// Unlike `std::vec::Vec`, does not shift elements after it to the left
    /// because it just replaces the element with None.
    /// So, this has performance of O(1).
    ///
    /// # Panics
    /// Unlike `std::vec::Vec`, does not panic.
    pub fn remove(&mut self, index: usize) -> Option<T> {
        if index >= self.len {
            return None;
        }

        for (i, node) in self.nodes[index..].iter().enumerate() {
            if node.is_some() {
                self.len -= 1;

                // Push an empty node to the end of Vec self.nodes
                // using the position of the node to be removed.
                // Then swap the empty node with the node to remove.
                // Finally, remove the node.
                let node = Node::new_empty(self.nodes[index + i].position());
                self.nodes.push(node);
                return self.nodes.swap_remove(index + i).element();
            }
        }

        #[cfg(not(tarpaulin_include))]
        {
            None
        }
    }

    /// Removes and returns the element at position.
    /// Unlike `std::vec::Vec`, does not shift elements after it to the left
    /// because it just replaces the element with None.
    /// Because we have to search the `Position`, it has performance of O(n/2).
    ///
    /// # Panics
    /// Unlike `std::vec::Vec`, does not panic.
    pub fn remove_at(&mut self, position: Pos) -> Option<T> {
        match self.index_from(position) {
            None => None,
            Some(index) => self.remove(index),
        }
    }

    #[inline]
    #[must_use]
    fn last_position(&self) -> Option<Pos> {
        self.nodes.last().map(|node| node.position())
    }

    #[cfg(not(tarpaulin_include))]
    #[allow(dead_code)]
    fn shrink(&mut self) {
        unimplemented!()
    }
}

impl<T> Default for Sequence<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Index<usize> for Sequence<T> {
    type Output = Node<T>;

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        &self.nodes[index]
    }
}

impl<T> IndexMut<usize> for Sequence<T> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.nodes[index]
    }
}

impl<T: Clone> Clone for Sequence<T> {
    fn clone(&self) -> Self {
        let mut seq: Sequence<T> = Sequence::new();

        for node in self {
            let node = match node.element_as_ref() {
                None => Node::new_empty(node.position()),
                Some(element) => Node::new(node.position(), element.clone()),
            };
            seq.nodes.push(node);
        }
        seq.len = self.len;
        seq
    }
}

//
// Consuming Iterator
//

// IntoIterator for Sequence<T>
// - consumes the `Sequence`
// - removes all empty Nodes from `Sequence`
// - iterates over remaining Nodes
impl<T> IntoIterator for Sequence<T> {
    type Item = Node<T>;
    type IntoIter = std::vec::IntoIter<Node<T>>;

    #[inline]
    fn into_iter(mut self) -> Self::IntoIter {
        self.nodes.retain(|node| node.element_as_ref().is_some());
        self.nodes.into_iter()
    }
}

//
// Immutable Iterator
//

// Iterator / IntoIterator over a `Sequence` represented by a slice of immutable `Option<&Node>`
// - allows to use a Sequence in a for loop
pub struct SequenceIterator<'iterator, T: 'iterator>(Option<&'iterator [Node<T>]>);

impl<'iterator, T: 'iterator> Iterator for SequenceIterator<'iterator, T> {
    type Item = &'iterator Node<T>;

    fn next(self: &'_ mut SequenceIterator<'iterator, T>) -> Option<Self::Item> {
        self.0.and_then(|v| {
            let (head, tail) = v.split_first()?;
            self.0 = Some(tail);
            Some(head)
        })
    }
}

impl<'iterator, T: 'iterator> IntoIterator for &'iterator Sequence<T> {
    type Item = &'iterator Node<T>;
    type IntoIter = SequenceIterator<'iterator, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        SequenceIterator(Some(self.nodes.as_slice()))
    }
}

//
// Mutable Iterator
//

// Iterator / IntoIterator over a `Sequence` represented by a slice of mutable `Option<&mut Node>`
// - allows to use a Sequence in a for loop
pub struct SequenceIteratorMut<'iterator, T: 'iterator>(Option<&'iterator mut [Node<T>]>);

impl<'iterator, T: 'iterator> Iterator for SequenceIteratorMut<'iterator, T> {
    type Item = &'iterator mut Node<T>;

    fn next(self: &'_ mut SequenceIteratorMut<'iterator, T>) -> Option<Self::Item> {
        self.0.take().and_then(|v| {
            let (head, tail) = v.split_first_mut()?;
            self.0 = Some(tail);
            Some(head)
        })
    }
}

impl<'iterator, T: 'iterator> IntoIterator for &'iterator mut Sequence<T> {
    type Item = &'iterator mut Node<T>;
    type IntoIter = SequenceIteratorMut<'iterator, T>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        SequenceIteratorMut(Some(self.nodes.as_mut_slice()))
    }
}

#[cfg(test)]
#[path = "tests/sequence_tests.rs"]
mod sequence_tests;