collection-selection-confection 0.0.3

Exploring a new way to work with Rust collections
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
use super::LinkedList;
use super::cursor::CursorMut;

pub struct SelectionMut<'a, T> {
    list: &'a mut LinkedList<T>,
    start: usize,
    end: usize,
}

impl<'a, T> SelectionMut<'a, T> {
    pub(super) fn new(list: &'a mut LinkedList<T>, start: usize, end: usize) -> Self {
        Self { list, start, end }
    }

    /// Converts into a cursor at the start of the selection.
    pub fn into_cursor_at_start(self) -> CursorMut<'a, T> {
        CursorMut::new(self.list, self.start)
    }

    /// Converts into a cursor at the end of the selection.
    pub fn into_cursor_at_end(self) -> CursorMut<'a, T> {
        CursorMut::new(self.list, self.end)
    }

    pub fn len(&self) -> usize {
        self.end - self.start
    }

    pub fn is_empty(&self) -> bool {
        self.start == self.end
    }

    /// Grows the selection backward by one.
    ///
    /// Returns `false`, without moving, if already at the start of the list.
    pub fn move_start_prev(&mut self) -> bool {
        if self.start == 0 {
            return false;
        }
        self.start -= 1;
        true
    }

    /// Shrinks the selection from the start by one.
    ///
    /// Returns `false`, without moving, if the selection is empty.
    pub fn move_start_next(&mut self) -> bool {
        if self.start == self.end {
            return false;
        }
        self.start += 1;
        true
    }

    /// Shrinks the selection from the end by one.
    ///
    /// Returns `false`, without moving, if the selection is empty.
    pub fn move_end_prev(&mut self) -> bool {
        if self.end == self.start {
            return false;
        }
        self.end -= 1;
        true
    }

    /// Grows the selection forward by one.
    ///
    /// Returns `false`, without moving, if already at the end of the list.
    pub fn move_end_next(&mut self) -> bool {
        if self.end == self.list.items.len() {
            return false;
        }
        self.end += 1;
        true
    }

    /// Grows the selection to the start of the list.
    pub fn absorb_all_before(&mut self) {
        self.start = 0;
    }

    /// Grows the selection to the end of the list.
    pub fn absorb_all_after(&mut self) {
        self.end = self.list.items.len();
    }

    /// Returns the first selected element.
    pub fn first(&self) -> Option<&T> {
        if self.is_empty() {
            return None;
        }
        self.list.items.get(self.start)
    }

    /// Returns the last selected element.
    pub fn last(&self) -> Option<&T> {
        if self.is_empty() {
            return None;
        }
        self.list.items.get(self.end - 1)
    }

    pub fn first_mut(&mut self) -> Option<&mut T> {
        if self.is_empty() {
            return None;
        }
        self.list.items.get_mut(self.start)
    }

    pub fn last_mut(&mut self) -> Option<&mut T> {
        if self.is_empty() {
            return None;
        }
        self.list.items.get_mut(self.end - 1)
    }

    /// Returns the element just before the selection.
    pub fn peek_before_start(&self) -> Option<&T> {
        let i = self.start.checked_sub(1)?;
        self.list.items.get(i)
    }

    /// Returns the element just after the selection.
    pub fn peek_after_end(&self) -> Option<&T> {
        self.list.items.get(self.end)
    }

    pub fn peek_before_start_mut(&mut self) -> Option<&mut T> {
        let i = self.start.checked_sub(1)?;
        self.list.items.get_mut(i)
    }

    pub fn peek_after_end_mut(&mut self) -> Option<&mut T> {
        self.list.items.get_mut(self.end)
    }

    /// Inserts an element just before the selection.
    pub fn insert_before_start(&mut self, value: T) {
        self.list.items.insert(self.start, value);
        self.start += 1;
        self.end += 1;
    }

    /// Inserts an element at the start of the selection.
    pub fn insert_at_start(&mut self, value: T) {
        self.list.items.insert(self.start, value);
        self.end += 1;
    }

    /// Inserts an element at the end of the selection.
    pub fn insert_at_end(&mut self, value: T) {
        self.list.items.insert(self.end, value);
        self.end += 1;
    }

    /// Inserts an element just after the selection.
    pub fn insert_after_end(&mut self, value: T) {
        self.list.items.insert(self.end, value);
    }

    /// Removes and returns the element just before the selection.
    pub fn remove_before_start(&mut self) -> Option<T> {
        if self.start == 0 {
            return None;
        }
        self.start -= 1;
        self.end -= 1;
        Some(self.list.items.remove(self.start))
    }

    /// Removes and returns the first selected element.
    pub fn remove_first(&mut self) -> Option<T> {
        if self.is_empty() {
            return None;
        }
        self.end -= 1;
        Some(self.list.items.remove(self.start))
    }

    /// Removes and returns the last selected element.
    pub fn remove_last(&mut self) -> Option<T> {
        if self.is_empty() {
            return None;
        }
        self.end -= 1;
        Some(self.list.items.remove(self.end))
    }

    /// Removes and returns the element just after the selection.
    pub fn remove_after_end(&mut self) -> Option<T> {
        if self.end == self.list.items.len() {
            return None;
        }
        Some(self.list.items.remove(self.end))
    }

    /// Removes and returns the selected elements, leaving the selection empty.
    pub fn take(&mut self) -> LinkedList<T> {
        let items = self.list.items.drain(self.start..self.end).collect();
        self.end = self.start;
        LinkedList { items }
    }

    /// Replaces the selected elements with `other`.
    ///
    /// Afterwards, the selection covers exactly the elements from `other`.
    ///
    /// Returns the previously selected elements.
    pub fn replace(&mut self, other: LinkedList<T>) -> LinkedList<T> {
        let len = other.items.len();
        let items = self
            .list
            .items
            .splice(self.start..self.end, other.items)
            .collect();
        self.end = self.start + len;
        LinkedList { items }
    }

    /// Iterates over the selected elements.
    pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T> + ExactSizeIterator {
        self.list.items[self.start..self.end].iter()
    }

    pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> + ExactSizeIterator {
        self.list.items[self.start..self.end].iter_mut()
    }
}

#[cfg(test)]
mod tests {
    use crate::list::tests::list_of;

    #[test]
    fn cursor_round_trip() {
        let mut list = list_of(&[0, 1]);
        let mut cursor = list.cursor_mut_at_start();
        cursor.move_next();
        let selection = cursor.into_selection();
        assert!(selection.is_empty());
        assert_eq!(selection.peek_before_start(), Some(&0));
        assert_eq!(selection.peek_after_end(), Some(&1));
        let cursor = selection.into_cursor_at_end();
        assert_eq!(cursor.peek_before(), Some(&0));
        assert_eq!(cursor.peek_after(), Some(&1));
    }

    #[test]
    fn into_cursor_at_each_end() {
        let mut list = list_of(&[0, 1, 2]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        let cursor = selection.into_cursor_at_end();
        assert_eq!(cursor.peek_before(), Some(&1));
        let mut selection = cursor.into_selection();
        selection.move_start_prev();
        let cursor = selection.into_cursor_at_start();
        assert_eq!(cursor.peek_before(), Some(&0));
        assert_eq!(cursor.peek_after(), Some(&1));
    }

    #[test]
    fn boundaries_stop_at_each_other_and_ends() {
        let mut list = list_of(&[0, 1]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        assert!(!selection.move_start_prev());
        assert!(!selection.move_start_next());
        assert!(!selection.move_end_prev());
        assert!(selection.move_end_next());
        assert!(selection.move_end_next());
        assert!(!selection.move_end_next());
        assert_eq!(selection.len(), 2);
        assert!(selection.move_start_next());
        assert!(selection.move_start_next());
        assert!(!selection.move_start_next());
        assert!(selection.is_empty());
    }

    #[test]
    fn absorb_all_on_each_side() {
        let mut list = list_of(&[0, 1, 2, 3]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        selection.absorb_all_before();
        assert_eq!(selection.iter().copied().collect::<Vec<_>>(), [0, 1]);
        selection.absorb_all_after();
        assert_eq!(selection.iter().copied().collect::<Vec<_>>(), [0, 1, 2, 3]);
        assert_eq!(selection.peek_before_start(), None);
        assert_eq!(selection.peek_after_end(), None);
    }

    #[test]
    fn first_and_last_inside_selection() {
        let mut list = list_of(&[0, 1, 2, 3]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        assert_eq!(selection.first(), None);
        assert_eq!(selection.last(), None);
        selection.move_end_next();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        assert_eq!(selection.first(), Some(&1));
        assert_eq!(selection.last(), Some(&2));
        *selection.first_mut().unwrap() += 10;
        *selection.last_mut().unwrap() += 20;
        assert_eq!(list.items, [0, 11, 22, 3]);
    }

    #[test]
    fn peek_mut_outside_selection() {
        let mut list = list_of(&[0, 1, 2]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        *selection.peek_before_start_mut().unwrap() += 10;
        *selection.peek_after_end_mut().unwrap() += 20;
        assert_eq!(list.items, [10, 1, 22]);
    }

    #[test]
    fn insert_outside_selection() {
        let mut list = list_of(&[1]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.insert_before_start(0);
        selection.insert_after_end(2);
        assert_eq!(selection.iter().copied().collect::<Vec<_>>(), [1]);
        assert_eq!(list.items, [0, 1, 2]);
    }

    #[test]
    fn insert_inside_selection() {
        let mut list = list_of(&[0, 2, 4]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        selection.insert_at_start(1);
        selection.insert_at_end(3);
        assert_eq!(selection.iter().copied().collect::<Vec<_>>(), [1, 2, 3]);
        assert_eq!(selection.peek_before_start(), Some(&0));
        assert_eq!(selection.peek_after_end(), Some(&4));
        assert_eq!(list.items, [0, 1, 2, 3, 4]);
    }

    #[test]
    fn insert_inside_empty_selection() {
        let mut list = list_of(&[0, 3]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_start_next();
        selection.insert_at_start(2);
        selection.insert_at_start(1);
        assert_eq!(selection.len(), 2);
        assert_eq!(selection.first(), Some(&1));
        assert_eq!(selection.last(), Some(&2));
        assert_eq!(list.items, [0, 1, 2, 3]);
    }

    #[test]
    fn remove_outside_selection() {
        let mut list = list_of(&[0, 1, 2]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        assert_eq!(selection.remove_before_start(), Some(0));
        assert_eq!(selection.remove_after_end(), Some(2));
        assert_eq!(selection.remove_before_start(), None);
        assert_eq!(selection.remove_after_end(), None);
        assert_eq!(selection.iter().copied().collect::<Vec<_>>(), [1]);
        assert_eq!(list.items, [1]);
    }

    #[test]
    fn remove_inside_selection() {
        let mut list = list_of(&[0, 1, 2, 3, 4]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        assert_eq!(selection.remove_first(), Some(1));
        assert_eq!(selection.remove_last(), Some(3));
        assert_eq!(selection.iter().copied().collect::<Vec<_>>(), [2]);
        assert_eq!(selection.peek_before_start(), Some(&0));
        assert_eq!(selection.peek_after_end(), Some(&4));
        assert_eq!(selection.remove_last(), Some(2));
        assert_eq!(selection.remove_first(), None);
        assert_eq!(selection.remove_last(), None);
        assert_eq!(list.items, [0, 4]);
    }

    #[test]
    fn take_empties_selection() {
        let mut list = list_of(&[0, 1, 2, 3]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        let taken = selection.take();
        assert!(selection.is_empty());
        assert_eq!(selection.peek_before_start(), Some(&0));
        assert_eq!(selection.peek_after_end(), Some(&3));
        assert_eq!(taken.items, [1, 2]);
        assert_eq!(list.items, [0, 3]);
    }

    #[test]
    fn replace_selects_new_contents() {
        let mut list = list_of(&[0, 1, 2]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        let old = selection.replace(list_of(&[10, 11, 12]));
        assert_eq!(selection.iter().copied().collect::<Vec<_>>(), [10, 11, 12]);
        assert_eq!(selection.peek_after_end(), Some(&2));
        assert_eq!(old.items, [1]);
        assert_eq!(list.items, [0, 10, 11, 12, 2]);
    }

    #[test]
    fn iter_mut_within_selection() {
        let mut list = list_of(&[0, 1, 2]);
        let mut selection = list.cursor_mut_at_start().into_selection();
        selection.move_end_next();
        selection.move_end_next();
        selection.move_start_next();
        for value in selection.iter_mut() {
            *value *= 10;
        }
        assert_eq!(selection.iter().rev().copied().collect::<Vec<_>>(), [10]);
        assert_eq!(list.items, [0, 10, 2]);
    }
}