ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional type classes
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//! Persistent Deque (Double-Ended Queue) implementation.
//!
//! A fully persistent double-ended queue supporting efficient operations
//! at both ends.

use core::iter::FromIterator;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "serde")]
use serde::Serialize;

/// A persistent (immutable) double-ended queue.
///
/// Supports efficient push/pop at both front and back using a pair of
/// vectors. Based on the real-time deque from Okasaki's PFDS.
///
/// # Time Complexity
///
/// | Operation    | Time (amortized) |
/// |--------------|------------------|
/// | `push_front` | O(1)             |
/// | `push_back`  | O(1)             |
/// | `pop_front`  | O(1)             |
/// | `pop_back`   | O(1)             |
/// | `peek_front` | O(1)             |
/// | `peek_back`  | O(1)             |
/// | `len`        | O(1)             |
///
/// # Example
///
/// ```rust
/// use ordofp_core::pfds::Deque;
///
/// let d = Deque::new()
///     .push_back(1)
///     .push_back(2)
///     .push_front(0);
///
/// assert_eq!(d.peek_front(), Some(&0));
/// assert_eq!(d.peek_back(), Some(&2));
///
/// let (front, d) = d.pop_front().unwrap();
/// assert_eq!(front, 0);
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg(feature = "alloc")]
pub struct Deque<A> {
    /// Front elements (top of vec is front of deque).
    front: Vec<A>,
    /// Back elements (top of vec is back of deque).
    back: Vec<A>,
    /// Cached length for O(1) access.
    len: usize,
}

#[cfg(all(feature = "serde", feature = "alloc"))]
impl<'de, A: serde::Deserialize<'de>> serde::Deserialize<'de> for Deque<A> {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        #[derive(serde::Deserialize)]
        #[serde(rename = "Deque")]
        struct Wire<A> {
            front: Vec<A>,
            back: Vec<A>,
            // Accepted for round-trip compatibility, never trusted: the whole
            // type relies on len == front.len() + back.len().
            #[serde(default)]
            #[allow(dead_code)]
            len: usize,
        }
        let w = Wire::deserialize(d)?;
        Ok(Deque {
            len: w.front.len() + w.back.len(),
            front: w.front,
            back: w.back,
        })
    }
}

#[cfg(feature = "alloc")]
impl<A> Default for Deque<A> {
    fn default() -> Self {
        Deque::new()
    }
}

#[cfg(feature = "alloc")]
impl<A: PartialEq> PartialEq for Deque<A> {
    fn eq(&self, other: &Self) -> bool {
        if self.len != other.len {
            return false;
        }
        // Compare element by element
        let self_iter = self.front.iter().rev().chain(self.back.iter());
        let other_iter = other.front.iter().rev().chain(other.back.iter());
        self_iter.eq(other_iter)
    }
}

#[cfg(feature = "alloc")]
impl<A: Eq> Eq for Deque<A> {}

#[cfg(feature = "alloc")]
impl<A> Deque<A> {
    /// Create a new empty deque.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Deque;
    ///
    /// let d: Deque<i32> = Deque::new();
    /// assert!(d.is_empty());
    /// ```
    #[inline]
    pub fn new() -> Self {
        Deque {
            front: Vec::new(),
            back: Vec::new(),
            len: 0,
        }
    }

    /// Check if the deque is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Get the number of elements in the deque.
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Push a value to the front of the deque.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Deque;
    ///
    /// let d = Deque::new().push_front(1).push_front(2);
    /// assert_eq!(d.peek_front(), Some(&2));
    /// ```
    #[inline]
    pub fn push_front(mut self, value: A) -> Self {
        self.front.push(value);
        Deque {
            front: self.front,
            back: self.back,
            len: self.len + 1,
        }
    }

    /// Push a value to the back of the deque.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Deque;
    ///
    /// let d = Deque::new().push_back(1).push_back(2);
    /// assert_eq!(d.peek_back(), Some(&2));
    /// ```
    #[inline]
    pub fn push_back(mut self, value: A) -> Self {
        self.back.push(value);
        Deque {
            front: self.front,
            back: self.back,
            len: self.len + 1,
        }
    }

    /// Pop a value from the front of the deque.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Deque;
    ///
    /// let d = Deque::new().push_back(1).push_back(2);
    /// let (first, rest) = d.pop_front().unwrap();
    /// assert_eq!(first, 1);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics only if the internal front/back balance invariant is violated
    /// (a non-empty deque must have a non-empty front after rebalancing),
    /// which indicates a bug in this crate.
    #[inline]
    pub fn pop_front(mut self) -> Option<(A, Self)>
    where
        A: Clone,
    {
        if self.is_empty() {
            return None;
        }

        // Rebalance if front is empty
        if self.front.is_empty() {
            self.rebalance_front();
        }

        let value = self.front.pop().unwrap();
        Some((
            value,
            Deque {
                front: self.front,
                back: self.back,
                len: self.len - 1,
            },
        ))
    }

    /// Pop a value from the back of the deque.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ordofp_core::pfds::Deque;
    ///
    /// let d = Deque::new().push_front(1).push_front(2);
    /// let (last, rest) = d.pop_back().unwrap();
    /// assert_eq!(last, 1);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics only if the internal front/back balance invariant is violated
    /// (a non-empty deque must have a non-empty back after rebalancing),
    /// which indicates a bug in this crate.
    #[inline]
    pub fn pop_back(mut self) -> Option<(A, Self)>
    where
        A: Clone,
    {
        if self.is_empty() {
            return None;
        }

        // Rebalance if back is empty
        if self.back.is_empty() {
            self.rebalance_back();
        }

        let value = self.back.pop().unwrap();
        Some((
            value,
            Deque {
                front: self.front,
                back: self.back,
                len: self.len - 1,
            },
        ))
    }

    /// Peek at the front value without removing it.
    #[inline]
    pub fn peek_front(&self) -> Option<&A> {
        if self.is_empty() {
            None
        } else if !self.front.is_empty() {
            self.front.last()
        } else {
            self.back.first()
        }
    }

    /// Peek at the back value without removing it.
    #[inline]
    pub fn peek_back(&self) -> Option<&A> {
        if self.is_empty() {
            None
        } else if !self.back.is_empty() {
            self.back.last()
        } else {
            self.front.first()
        }
    }

    /// Rebalance when front is empty: move half of back to front.
    fn rebalance_front(&mut self)
    where
        A: Clone,
    {
        if self.back.is_empty() {
            return;
        }

        let mid = self.back.len() / 2;
        let mut new_front: Vec<A> = self.back.drain(..=mid).collect();
        new_front.reverse();
        self.front = new_front;
    }

    /// Rebalance when back is empty: move half of front to back.
    fn rebalance_back(&mut self)
    where
        A: Clone,
    {
        if self.front.is_empty() {
            return;
        }

        let mid = self.front.len() / 2;
        let mut new_back: Vec<A> = self.front.drain(..=mid).collect();
        new_back.reverse();
        self.back = new_back;
    }

    /// Map a function over the deque.
    #[inline]
    pub fn map<B, F>(&self, f: F) -> Deque<B>
    where
        A: Clone,
        F: Fn(&A) -> B,
    {
        Deque {
            front: self.front.iter().map(&f).collect(),
            back: self.back.iter().map(&f).collect(),
            len: self.len,
        }
    }

    /// Fold the deque from front to back.
    #[inline]
    pub fn fold<B, F>(&self, init: B, f: F) -> B
    where
        A: Clone,
        F: Fn(B, &A) -> B,
    {
        let acc = self.front.iter().rev().fold(init, &f);
        self.back.iter().fold(acc, f)
    }

    /// Filter elements that satisfy the predicate.
    #[inline]
    pub fn filter<F>(&self, pred: F) -> Self
    where
        A: Clone,
        F: Fn(&A) -> bool,
    {
        let front: Vec<A> = self.front.iter().filter(|x| pred(x)).cloned().collect();
        let back: Vec<A> = self.back.iter().filter(|x| pred(x)).cloned().collect();
        let len = front.len() + back.len();
        Deque { front, back, len }
    }

    /// Convert to a Vec (front to back order).
    #[inline]
    pub fn to_vec(&self) -> Vec<A>
    where
        A: Clone,
    {
        let mut result: Vec<A> = self.front.iter().rev().cloned().collect();
        result.extend(self.back.iter().cloned());
        result
    }

    /// Reverse the deque.
    #[inline]
    pub fn reverse(self) -> Self {
        Deque {
            front: self.back,
            back: self.front,
            len: self.len,
        }
    }

    /// Concatenate two deques.
    #[inline]
    pub fn concat(&self, other: &Self) -> Self
    where
        A: Clone,
    {
        let mut result = self.clone();
        // Add front of other (in order)
        for item in other.front.iter().rev() {
            result = result.push_back(item.clone());
        }
        // Add back of other
        for item in &other.back {
            result = result.push_back(item.clone());
        }
        result
    }
}

#[cfg(feature = "alloc")]
impl<A: Clone> From<Vec<A>> for Deque<A> {
    fn from(vec: Vec<A>) -> Self {
        let len = vec.len();
        Deque {
            front: Vec::new(),
            back: vec,
            len,
        }
    }
}

#[cfg(feature = "alloc")]
impl<A: Clone> FromIterator<A> for Deque<A> {
    fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self {
        let back: Vec<A> = iter.into_iter().collect();
        let len = back.len();
        Deque {
            front: Vec::new(),
            back,
            len,
        }
    }
}

/// Iterator over a Deque (front to back).
#[cfg(feature = "alloc")]
pub struct DequeIter<A> {
    front: Vec<A>,
    back: Vec<A>,
}

#[cfg(feature = "alloc")]
impl<A> Iterator for DequeIter<A> {
    type Item = A;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.front.is_empty() {
            Some(self.front.pop().unwrap())
        } else if !self.back.is_empty() {
            // Reverse back so we can pop() from the logical front — O(n) once,
            // then each subsequent call is O(1) instead of O(n) via remove(0).
            self.back.reverse();
            core::mem::swap(&mut self.front, &mut self.back);
            self.front.pop()
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.front.len() + self.back.len();
        (len, Some(len))
    }
}

#[cfg(feature = "alloc")]
impl<A: Clone> IntoIterator for Deque<A> {
    type Item = A;
    type IntoIter = DequeIter<A>;

    fn into_iter(self) -> Self::IntoIter {
        DequeIter {
            front: self.front,
            back: self.back,
        }
    }
}

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

    #[test]
    fn test_new_is_empty() {
        let d: Deque<i32> = Deque::new();
        assert!(d.is_empty());
        assert_eq!(d.len(), 0);
    }

    #[test]
    fn test_push_front() {
        let d = Deque::new().push_front(1).push_front(2).push_front(3);
        assert_eq!(d.len(), 3);
        assert_eq!(d.peek_front(), Some(&3));
        assert_eq!(d.peek_back(), Some(&1));
    }

    #[test]
    fn test_push_back() {
        let d = Deque::new().push_back(1).push_back(2).push_back(3);
        assert_eq!(d.len(), 3);
        assert_eq!(d.peek_front(), Some(&1));
        assert_eq!(d.peek_back(), Some(&3));
    }

    #[test]
    fn test_pop_front() {
        let d = Deque::new().push_back(1).push_back(2).push_back(3);

        let (first, d) = d
            .pop_front()
            .expect("deque with 3 elements should have a front element");
        assert_eq!(first, 1);

        let (second, d) = d
            .pop_front()
            .expect("deque with 2 remaining elements should have a front element");
        assert_eq!(second, 2);

        let (third, d) = d
            .pop_front()
            .expect("deque with 1 remaining element should have a front element");
        assert_eq!(third, 3);

        assert!(d.is_empty());
    }

    #[test]
    fn test_pop_back() {
        let d = Deque::new().push_front(1).push_front(2).push_front(3);

        let (last, d) = d
            .pop_back()
            .expect("deque with 3 elements should have a back element");
        assert_eq!(last, 1);

        let (second_last, d) = d
            .pop_back()
            .expect("deque with 2 remaining elements should have a back element");
        assert_eq!(second_last, 2);

        let (first, d) = d
            .pop_back()
            .expect("deque with 1 remaining element should have a back element");
        assert_eq!(first, 3);

        assert!(d.is_empty());
    }

    #[test]
    fn test_persistence() {
        let d1 = Deque::new().push_back(1).push_back(2);
        let d2 = d1.clone().push_back(3);

        // d1 unchanged
        assert_eq!(d1.len(), 2);

        // d2 has new element
        assert_eq!(d2.len(), 3);
    }

    #[test]
    fn test_mixed_operations() {
        let d = Deque::new()
            .push_back(2)
            .push_front(1)
            .push_back(3)
            .push_front(0);

        assert_eq!(d.peek_front(), Some(&0));
        assert_eq!(d.peek_back(), Some(&3));

        let items: Vec<_> = d.into_iter().collect();
        assert_eq!(items, vec![0, 1, 2, 3]);
    }

    #[test]
    fn test_reverse() {
        let d = Deque::new().push_back(1).push_back(2).push_back(3);
        let r = d.reverse();

        assert_eq!(r.peek_front(), Some(&3));
        assert_eq!(r.peek_back(), Some(&1));
    }

    #[test]
    fn test_map() {
        let d = Deque::new().push_back(1).push_back(2).push_back(3);
        let doubled = d.map(|x| x * 2);

        let items: Vec<_> = doubled.into_iter().collect();
        assert_eq!(items, vec![2, 4, 6]);
    }

    #[test]
    fn test_fold() {
        let d = Deque::new().push_back(1).push_back(2).push_back(3);
        let sum = d.fold(0, |acc, x| acc + x);
        assert_eq!(sum, 6);
    }

    #[test]
    fn test_filter() {
        let d = Deque::new()
            .push_back(1)
            .push_back(2)
            .push_back(3)
            .push_back(4);
        let evens = d.filter(|x| x % 2 == 0);

        assert_eq!(evens.len(), 2);
    }

    #[test]
    fn test_filter_empty_deque_returns_empty() {
        let d: Deque<i32> = Deque::new();
        let result = d.filter(|_| true);
        assert_eq!(result.len(), 0);
        assert_eq!(result.to_vec(), Vec::<i32>::new());
    }

    #[test]
    fn test_filter_no_match_returns_empty() {
        let d = Deque::new().push_back(1).push_back(3).push_back(5);
        let evens = d.filter(|x| x % 2 == 0);
        assert_eq!(evens.len(), 0);
        assert_eq!(evens.to_vec(), Vec::<i32>::new());
    }

    #[test]
    fn test_filter_preserves_element_values() {
        // Elements pushed to front land in the front half; verify filter
        // correctly retains values from both internal halves of the deque.
        let d = Deque::new()
            .push_front(4) // front: [4]
            .push_front(3) // front: [3, 4]
            .push_back(5) // back:  [5]
            .push_back(6); // back:  [5, 6]  → to_vec: [3, 4, 5, 6]
        let evens = d.filter(|x| x % 2 == 0);
        assert_eq!(evens.len(), 2);
        let mut v = evens.to_vec();
        v.sort_unstable();
        assert_eq!(v, vec![4, 6]);
    }

    #[test]
    fn test_to_vec() {
        let d = Deque::new().push_back(1).push_back(2).push_back(3);
        assert_eq!(d.to_vec(), vec![1, 2, 3]);
    }

    #[test]
    fn test_concat() {
        let d1 = Deque::new().push_back(1).push_back(2);
        let d2 = Deque::new().push_back(3).push_back(4);
        let combined = d1.concat(&d2);

        assert_eq!(combined.len(), 4);
        assert_eq!(combined.to_vec(), vec![1, 2, 3, 4]);
    }

    #[test]
    fn test_from_vec() {
        let v = vec![1, 2, 3];
        let d = Deque::from(v);

        assert_eq!(d.len(), 3);
        assert_eq!(d.to_vec(), vec![1, 2, 3]);
    }

    #[test]
    fn test_from_iter() {
        let d: Deque<i32> = vec![1, 2, 3].into_iter().collect();
        assert_eq!(d.to_vec(), vec![1, 2, 3]);
    }

    #[test]
    fn test_rebalance() {
        // Create deque with all elements in back
        let d = Deque::new()
            .push_back(1)
            .push_back(2)
            .push_back(3)
            .push_back(4);

        // Pop from front - should trigger rebalance
        let (first, d) = d
            .pop_front()
            .expect("deque should be non-empty before first front pop");
        assert_eq!(first, 1);

        let (second, _d) = d
            .pop_front()
            .expect("deque should be non-empty before second front pop");
        assert_eq!(second, 2);

        // Create deque with all elements in front
        let d = Deque::new()
            .push_front(1)
            .push_front(2)
            .push_front(3)
            .push_front(4);

        // Pop from back - should trigger rebalance
        let (last, d) = d
            .pop_back()
            .expect("deque should be non-empty before first back pop");
        assert_eq!(last, 1);

        let (second_last, _) = d
            .pop_back()
            .expect("deque should be non-empty before second back pop");
        assert_eq!(second_last, 2);
    }
}

#[cfg(all(test, feature = "serde"))]
mod serde_invariant_tests {
    use super::*;

    /// S2 regression: forged `len` made pop_front hit `.unwrap()` on an
    /// empty Vec (deque.rs:180). len is now recomputed, never trusted.
    #[test]
    fn forged_len_is_recomputed() {
        let d: Deque<i32> = serde_json::from_str(r#"{"front":[],"back":[],"len":5}"#).unwrap();
        assert_eq!(d.len(), 0);
        assert!(d.pop_front().is_none()); // pre-fix: panic
        let d: Deque<i32> =
            serde_json::from_str(r#"{"front":[1],"back":[2,3],"len":999}"#).unwrap();
        assert_eq!(d.len(), 3);
    }
}