diamond-types 1.0.0

The world's fastest text CRDT
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
use std::cmp::Ordering::*;
use std::fmt::Debug;
use std::iter::{FromIterator, Cloned};
use std::ops::{Index, Range};
use std::slice::SliceIndex;

use humansize::{file_size_opts, FileSize};

use rle::{AppendRle, HasLength, MergableSpan, MergeableIterator, MergeIter, SplitableSpan, SplitableSpanCtx};
use rle::Searchable;
use crate::dtrange::DTRange;

use crate::rle::{RleKeyed, RleKeyedAndSplitable, RleSpanHelpers};

// Each entry has a key (which we search by), a span and a value at that key.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct RleVec<V: HasLength + MergableSpan + Sized>(pub Vec<V>);

impl<V: HasLength + MergableSpan + Sized> RleVec<V> {
    pub fn new() -> Self { Self(Vec::new()) }

    /// Append a new value to the end of the RLE list. This method is fast - O(1) average time.
    /// The new item will extend the last entry in the list if possible.
    ///
    /// Returns true if the item was merged into the previous item. False if it was appended new.
    pub fn push(&mut self, val: V) -> bool {
        self.0.push_rle(val)
    }

    #[allow(unused)]
    pub fn push_will_merge(&self, item: &V) -> bool {
        if let Some(v) = self.last() {
            v.can_append(item)
        } else { false }
    }

    // Forward to vec.
    pub fn last(&self) -> Option<&V> { self.0.last() }

    #[allow(unused)]
    pub fn num_entries(&self) -> usize { self.0.len() }

    /// Returns past the end of the last key.
    pub fn end(&self) -> usize where V: RleKeyed {
        if let Some(v) = self.last() {
            v.end()
        } else {
            0
        }
    }

    #[allow(unused)]
    pub fn is_empty(&self) -> bool { self.0.is_empty() }

    pub fn iter(&self) -> std::slice::Iter<V> { self.0.iter() }

    pub fn iter_merged(&self) -> MergeIter<Cloned<std::slice::Iter<V>>> { self.0.iter().cloned().merge_spans() }

    pub fn print_stats(&self, name: &str, _detailed: bool) {
        let size = std::mem::size_of::<V>();
        println!("-------- {} RLE --------", name);
        println!("number of {} byte entries: {}", size, self.0.len());
        println!("size: {}", (self.0.capacity() * size).file_size(file_size_opts::CONVENTIONAL).unwrap());
        println!("(efficient size: {})", (self.0.len() * size).file_size(file_size_opts::CONVENTIONAL).unwrap());

        // for item in self.0[..100].iter() {
        //     println!("{:?}", item);
        // }
    }
}

// impl<K: Copy + Eq + Ord + Add<Output = K> + Sub<Output = K> + AddAssign, V: Copy + Eq> RLE<K, V> {
impl<V: HasLength + MergableSpan + RleKeyed + Clone + Sized> RleVec<V> {
    pub(crate) fn find_index(&self, needle: usize) -> Result<usize, usize> {
        self.0.binary_search_by(|entry| {
            let key = entry.rle_key();
            if needle < key { Greater }
            else if needle >= key + entry.len() { Less }
            else { Equal }
        })
    }

    // /// This is a variant of find_index for data sets where we normally know the index (via
    // /// iteration).
    // pub(crate) fn find_hinted(&self, needle: usize, hint: &mut usize) -> Result<usize, usize> {
    //     if self.is_empty() { return Err(0); }
    //
    //     if *hint < self.0.len() {
    //         let e = &self.0[*hint];
    //         if needle >= e.get_rle_key() && needle < e.end() {
    //             return Ok(*hint);
    //         } else if needle < e.get_rle_key() {
    //             if hint > 0 {
    //                 todo!()
    //             } else {
    //                 *hint = 0;
    //                 return Err()
    //             }
    //         } else {
    //             debug_assert!(needle >= e.end());
    //         }
    //     }
    //     todo!()
    // }

    /// Find an entry in the list with the specified key using binary search.
    ///
    /// If found returns Some(found value).
    pub fn find(&self, needle: usize) -> Option<&V> {
        self.find_index(needle).ok().map(|idx| {
            &self.0[idx]
        })
    }

    /// Same as list.find_with_offset(needle) except for lists where there are no gaps in the RLE list.
    pub fn find_packed(&self, needle: usize) -> &V {
        self.find(needle).unwrap()
    }

    /// Find the item at range, cloning and trimming it down to size. This is generally less
    /// efficient than using find_with_offset and friends, but its much more convenient.
    ///
    /// Note the returned value might be smaller than the passed range.
    #[allow(unused)]
    pub fn find_packed_and_split(&self, range: DTRange) -> V where V: SplitableSpan {
        self.find_packed_and_split_ctx(range, &())
    }

    #[allow(unused)]
    pub fn find_packed_and_split_ctx(&self, range: DTRange, ctx: &V::Ctx) -> V where V: SplitableSpanCtx {
        let (item, offset) = self.find_packed_with_offset(range.start);
        let mut item = item.clone();
        item.truncate_keeping_right_ctx(offset, ctx);
        if item.len() > range.len() {
            item.truncate_ctx(range.len(), ctx);
        }
        item
    }

    /// Find an entry in the list with the specified key using binary search.
    ///
    /// If found returns Some((found value, internal offset))
    pub fn find_with_offset(&self, needle: usize) -> Option<(&V, usize)> {
        self.find_index(needle).ok().map(|idx| {
            let entry = &self.0[idx];
            (entry, needle - entry.rle_key())
        })
    }

    /// Same as list.find_with_offset(needle) except for lists where there are no gaps in the RLE list.
    pub fn find_packed_with_offset(&self, needle: usize) -> (&V, usize) {
        self.find_with_offset(needle).unwrap()
    }

    // pub fn find_packed_range(&self, needle: TimeSpan) -> (&V, TimeSpan) {
    //     let (v, offset) = self.find_packed(needle.start);
    //
    //     (v,
    // }

    /// This method is similar to find, except instead of returning None when the value doesn't
    /// exist in the RLE list, we return the position in the empty span.
    ///
    /// This method assumes the "base" of the RLE is 0.
    ///
    /// Returns (Ok(elem), offset) if item is found, otherwise (Err(void range), offset into void)
    #[allow(unused)]
    pub fn find_sparse(&self, needle: usize) -> (Result<&V, DTRange>, usize) {
        match self.find_index(needle) {
            Ok(idx) => {
                let entry = &self.0[idx];
                (Ok(entry), needle - entry.rle_key())
            }
            Err(idx) => {
                let next_key = if let Some(entry) = self.0.get(idx) {
                    entry.rle_key()
                } else {
                    usize::MAX
                };

                if idx == 0 {
                    (Err((0..next_key).into()), needle)
                } else {
                    let end = self.0[idx - 1].end();
                    (Err((end..next_key).into()), needle - end)
                }
            }
        }
    }

    /// Find an entry in the list with the specified key using binary search.
    ///
    /// If found, item is returned by mutable reference as Some((&mut item, offset)).
    #[allow(unused)]
    pub fn find_mut(&mut self, needle: usize) -> Option<(&mut V, usize)> {
        self.find_index(needle).ok().map(move |idx| {
            let entry = &mut self.0[idx];
            let offset = needle - entry.rle_key();
            (entry, offset)
        })
    }

    /// Insert an item at this location in the RLE list. This method is O(n) as it needs to shift
    /// subsequent elements forward.
    #[allow(unused)]
    pub fn insert(&mut self, val: V) {
        let idx = self.find_index(val.rle_key()).expect_err("Item already exists");

        // Extend the next / previous item if possible
        if idx >= 1 {
            let prev = &mut self.0[idx - 1];
            if prev.can_append(&val) {
                prev.append(val);
                return;
            }
        }

        if idx < self.0.len() {
            let next = &mut self.0[idx];
            debug_assert!(val.rle_key() + val.len() <= next.rle_key(), "Items overlap");

            if val.can_append(next) {
                next.prepend(val);
                return
            }
        }

        self.0.insert(idx, val);
    }

    /// Remove an item. This may need to shuffle indexes around. This method is O(n) with the number
    /// of items between this entry and the end of the list.
    ///
    /// This method silently ignores requests to delete ranges we don't have.
    pub fn remove_ctx(&mut self, mut deleted_range: DTRange, ctx: &V::Ctx) where V: SplitableSpanCtx {
        // Fast case - the requested entry is at the end.
        loop {
            if let Some(last) = self.0.last_mut() {
                let last_span = last.span();

                // Range is past the end of the list. Nothing to do here!
                if deleted_range.start >= last_span.end { return; }

                // Need slow approach.
                if deleted_range.end < last_span.end { break; }

                if deleted_range.start <= last_span.start {
                    // Remove entire last entry.
                    self.0.pop();
                    if deleted_range.start == last_span.start {
                        // Easiest case. We're done.
                        return;
                    }
                } else {
                    // Truncate last entry and return.
                    last.truncate_from_ctx(deleted_range.start, ctx);
                    return;
                }
            } else {
                // The list is empty. Nothing more to do.
                return;
            }
        }

        // Slow case - the requested range is in the middle of the list somewhere. We need to carve
        // it out.
        let mut idx = match self.find_index(deleted_range.start) {
            Ok(idx) => idx,
            Err(idx) => {
                if let Some(entry) = self.0.get(idx) {
                    deleted_range.truncate_keeping_right_from(entry.rle_key());
                } else { return; }
                idx
            }
        };

        loop {
            if idx >= self.0.len() { break; }
            let e = &mut self.0[idx];

            debug_assert!(e.rle_key() <= deleted_range.start);

            // There's 4 cases here.

            let e_end = e.end();

            let keep_start = e.rle_key() < deleted_range.start;
            let keep_end = e_end > deleted_range.end();
            match (keep_start, keep_end) {
                (false, false) => {
                    // Remove the entry and iterate.
                    self.0.remove(idx);
                },

                (false, true) => {
                    // Trim the start, trim the end.
                    e.truncate_keeping_right_from_ctx(deleted_range.start, ctx);
                    break;
                },

                (true, false) => {
                    // Trim the end
                    e.truncate_from_ctx(deleted_range.start, ctx);
                    idx += 1;
                }

                (true, true) => {
                    // Trim in the middle.
                    let mut remainder = e.truncate_from_ctx(deleted_range.start, ctx);
                    remainder.truncate_keeping_right_from_ctx(deleted_range.end, ctx);
                    self.insert(remainder);
                    break;
                }
            }

            if e_end == deleted_range.end() { break; }
        }
    }

    /// Search forward from idx until we find needle. idx is modified. Returns either the item if
    /// successful, or the key of the subsequent item.
    #[allow(unused)]
    pub(crate) fn search_scanning_sparse(&self, needle: usize, idx: &mut usize) -> Result<&V, usize> {
        while *idx < self.0.len() {
            // TODO: Is this bounds checking? It shouldn't need to... Fix if it is.
            let e = &self[*idx];
            if needle < e.end() {
                return if needle >= e.rle_key() {
                    Ok(e)
                } else {
                    Err(e.rle_key())
                };
            }

            *idx += 1;
        }
        Err(usize::MAX)
    }

    #[allow(unused)]
    pub(crate) fn search_scanning_packed(&self, needle: usize, idx: &mut usize) -> &V {
        self.search_scanning_sparse(needle, idx).unwrap()
    }

    /// Search backwards from idx until we find needle. idx is modified. Returns either the item or
    /// the end of the preceeding range. Note the end could be == needle. (But cannot be greater
    /// than it).
    #[allow(unused)]
    pub(crate) fn search_scanning_backwards_sparse(&self, needle: usize, idx: &mut usize) -> Result<&V, usize> {
        // This conditional looks inverted given we're looping backwards, but I'm using
        // wrapping_sub - so when we reach the end the index wraps around and we'll hit usize::MAX.
        while *idx < self.0.len() {
            let e = &self[*idx];
            if needle >= e.rle_key() {
                return if needle < e.end() {
                    Ok(e)
                } else {
                    Err(e.end())
                };
            }
            *idx = idx.wrapping_sub(1);
        }
        Err(0)
    }

    /// Visit each item or gap in this (sparse) RLE list, ending at end with the passed visitor
    /// method.
    #[allow(unused)]
    pub fn for_each_sparse<F>(&self, end: usize, mut visitor: F)
    where F: FnMut(Result<&V, Range<usize>>) {
        let mut key = 0;

        for e in self.iter() {
            let next_key = e.rle_key();
            if key < next_key {
                // Visit the empty range
                visitor(Err(key..next_key));
            }

            // Ok now visit the entry we found.
            visitor(Ok(e));
            key = e.end();
            debug_assert!(key <= end);
        }
        // And visit the remainder, if there is any.
        if key < end {
            visitor(Err(key..end));
        }
    }

    /// Check that the RLE is contiguous and packed. Panic if not.
    #[allow(unused)]
    pub(crate) fn check_packed(&self) {
        let mut expect_next = 0;
        for (i, entry) in self.0.iter().enumerate() {
            if i != 0 {
                assert_eq!(entry.rle_key(), expect_next);
            }
            expect_next = entry.end();
        }
    }
}

impl<V: HasLength + MergableSpan + Sized> FromIterator<V> for RleVec<V> {
    fn from_iter<T: IntoIterator<Item=V>>(iter: T) -> Self {
        let mut rle = Self::new();
        for item in iter {
            rle.push(item);
        }
        rle
    }
}

impl<V: HasLength + MergableSpan + Sized> Extend<V> for RleVec<V> {
    fn extend<T: IntoIterator<Item=V>>(&mut self, iter: T) {
        for item in iter {
            self.push(item);
        }
    }
}

impl<V: HasLength + MergableSpan + Sized> Default for RleVec<V> {
    fn default() -> Self {
        Self(Vec::default())
    }
}

// impl<'a, V: 'a + SplitableSpan + Clone + Sized> FromIterator<&'a V> for Rle<V> {
//     fn from_iter<T: IntoIterator<Item=&'a V>>(iter: T) -> Self {
//         let mut rle = Self::new();
//         for item in iter {
//             rle.append(item.clone());
//         }
//         rle
//     }
// }

impl<V: HasLength + MergableSpan + Searchable + RleKeyed> RleVec<V> {
    pub fn get(&self, idx: usize) -> V::Item {
        let (v, offset) = self.find_with_offset(idx).unwrap();
        v.at_offset(offset)
    }
}

// Seems kinda redundant but eh.
impl<V: HasLength + MergableSpan + Debug + Sized> AppendRle<V> for RleVec<V> {
    fn push_rle(&mut self, item: V) -> bool { self.push(item) }
    fn push_reversed_rle(&mut self, _item: V) -> bool { unimplemented!(); }
}

impl<T: HasLength + MergableSpan, I: SliceIndex<[T]>> Index<I> for RleVec<T> {
    type Output = I::Output;

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

fn id_clone<V: Clone>(v: &V) -> V {
    v.clone()
}

impl<V: HasLength + SplitableSpan + RleKeyed + MergableSpan> RleVec<V> {
    #[allow(unused)]
    pub fn iter_range_packed(&self, range: DTRange) -> RleVecRangeIter<V, V, impl Fn(&V) -> V> {
        self.iter_range_map_packed_ctx(range, &(), id_clone)
    }
}

impl<V: HasLength + SplitableSpanCtx + RleKeyed + MergableSpan> RleVec<V> {
    pub fn iter_range_packed_ctx<'a>(&'a self, range: DTRange, ctx: &'a V::Ctx) -> RleVecRangeIter<'a, V, V, impl Fn(&V) -> V> {
        self.iter_range_map_packed_ctx(range, ctx, id_clone)
    }
}

impl<V: HasLength + RleKeyed + MergableSpan> RleVec<V> {
    pub fn iter_range_map_packed<I: SplitableSpan + HasLength, F: Fn(&V) -> I>(&self, range: DTRange, map_fn: F) -> RleVecRangeIter<V, I, F> {
        self.iter_range_map_packed_ctx(range, &(), map_fn)
    }

    pub fn iter_range_map_packed_ctx<'a, I: SplitableSpanCtx + HasLength, F: Fn(&V) -> I>(&'a self, range: DTRange, ctx: &'a I::Ctx, map_fn: F) -> RleVecRangeIter<'a, V, I, F> {
        let idx = self.find_index(range.start).unwrap();

        let entry = &self.0[idx];
        let offset = range.start - entry.rle_key();

        RleVecRangeIter {
            offset,
            idx,
            len_remaining: range.len(),
            map_fn,
            data: &self.0,
            ctx,
        }
    }
}

#[derive(Debug, Clone)]
pub struct RleVecRangeIter<'a, V: HasLength + MergableSpan, I: HasLength + SplitableSpanCtx, F: Fn(&V) -> I> {
    offset: usize,
    idx: usize,
    len_remaining: usize,
    map_fn: F,
    data: &'a [V],

    ctx: &'a I::Ctx, // This could have a different lifetime specifier.
}

impl<'a, V: HasLength + MergableSpan, I: HasLength + SplitableSpanCtx, F: Fn(&V) -> I> Iterator for RleVecRangeIter<'a, V, I, F> {
    type Item = I;

    fn next(&mut self) -> Option<Self::Item> {
        if self.len_remaining == 0 || self.idx >= self.data.len() { return None; }

        let mut item = (self.map_fn)(&self.data[self.idx]);
        if self.offset > 0 {
            assert!(self.offset < item.len());
            item.truncate_keeping_right_ctx(self.offset, self.ctx);
            self.offset = 0;
        }

        if item.len() > self.len_remaining {
            item.truncate_ctx(self.len_remaining, self.ctx);
            self.len_remaining = 0;
        } else {
            self.idx += 1;
            self.len_remaining -= item.len();
        }

        Some(item)
    }
}

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

    #[test]
    fn rle_iter_range() {
        let mut rle: RleVec<DTRange> = RleVec::new();
        rle.push((0..10).into());

        // This is a sad example.
        let items = rle.iter_range_packed((5..8).into()).collect::<Vec<_>>();
        assert_eq!(&items, &[(5..8).into()]);
    }

    // use crate::order::OrderSpan;
    // use crate::rle::KVPair;
    // use crate::rle::simple_rle::RleVec;
    //
    // #[test]
    // fn rle_finds_at_offset() {
    //     let mut rle: RleVec<KVPair<OrderSpan>> = RleVec::new();
    //
    //     rle.push(KVPair(1, OrderSpan { order: 1000, len: 2 }));
    //     assert_eq!(rle.find_with_offset(1), Some((&KVPair(1, OrderSpan { order: 1000, len: 2 }), 0)));
    //     assert_eq!(rle.find_with_offset(2), Some((&KVPair(1, OrderSpan { order: 1000, len: 2 }), 1)));
    //     assert_eq!(rle.find_with_offset(3), None);
    //
    //     // This should get appended.
    //     rle.push(KVPair(3, OrderSpan { order: 1002, len: 1 }));
    //     assert_eq!(rle.find_with_offset(3), Some((&KVPair(1, OrderSpan { order: 1000, len: 3 }), 2)));
    //     assert_eq!(rle.0.len(), 1);
    // }
    //
    // #[test]
    // fn insert_inside() {
    //     let mut rle: RleVec<KVPair<OrderSpan>> = RleVec::new();
    //
    //     rle.insert(KVPair(5, OrderSpan { order: 1000, len: 2}));
    //     // Prepend
    //     rle.insert(KVPair(3, OrderSpan { order: 998, len: 2}));
    //     assert_eq!(rle.0.len(), 1);
    //
    //     // Append
    //     rle.insert(KVPair(7, OrderSpan { order: 1002, len: 5}));
    //     assert_eq!(rle.0.len(), 1);
    //
    //     // Items which cannot be merged
    //     rle.insert(KVPair(1, OrderSpan { order: 1, len: 1}));
    //     assert_eq!(rle.0.len(), 2);
    //
    //     rle.insert(KVPair(100, OrderSpan { order: 40, len: 1}));
    //     assert_eq!(rle.0.len(), 3);
    //
    //     // dbg!(&rle);
    // }
    //
    // #[test]
    // fn test_find_sparse() {
    //     let mut rle: RleVec<KVPair<OrderSpan>> = RleVec::new();
    //
    //     assert_eq!(rle.find_sparse(0), (Err(0), 0));
    //     assert_eq!(rle.find_sparse(10), (Err(0), 10));
    //
    //     rle.insert(KVPair(15, OrderSpan { order: 40, len: 2}));
    //     assert_eq!(rle.find_sparse(10), (Err(0), 10));
    //     assert_eq!(rle.find_sparse(15), (Ok(&rle.0[0]), 0));
    //     assert_eq!(rle.find_sparse(16), (Ok(&rle.0[0]), 1));
    //     assert_eq!(rle.find_sparse(17), (Err(17), 0));
    //     assert_eq!(rle.find_sparse(20), (Err(17), 3));
    // }

    // #[test]
    // fn align() {
    //     use std::mem::{size_of, align_of};
    //     #[repr(transparent)]
    //     struct A(u32);
    //     // #[repr(packed)]
    //     struct B(u64, u32);
    //     // #[repr(packed)]
    //     struct C(B, u32);
    //
    //     dbg!(size_of::<A>(), align_of::<A>());
    //     dbg!(size_of::<B>(), align_of::<B>());
    //     dbg!(size_of::<C>(), align_of::<C>());
    // }
}