1
use alloc::vec::Vec;
2
use alloc::{slice, vec};
3
use core::cmp::Ordering;
4
use core::fmt::Debug;
5
use core::ops::{Index, IndexMut};
6

            
7
use crate::unordered::{DrainAll, DrainFilter, Lots};
8
use crate::LotId;
9

            
10
/// A collection of `T` values that maintains the order of elements.
11
///
12
/// This collection can be accessed by index (`usize`) or the [`LotId`] it is
13
/// assigned upon insertion or pushing.
14
///
15
/// This collection has Vec-like performance except when removing elements by
16
/// [`LotId`], which is an O(n) operation.
17
#[derive(Clone)]
18
#[allow(clippy::module_name_repetitions)]
19
pub struct OrderedLots<T> {
20
    slots: Lots<T>,
21
    order: Vec<LotId>,
22
}
23

            
24
impl<T> Default for OrderedLots<T> {
25
    #[inline]
26
2
    fn default() -> Self {
27
2
        Self::new()
28
2
    }
29
}
30

            
31
impl<T> OrderedLots<T> {
32
    /// Returns a new, empty collection.
33
    #[inline]
34
    #[must_use]
35
11
    pub const fn new() -> Self {
36
11
        Self {
37
11
            slots: Lots::new(),
38
11
            order: Vec::new(),
39
11
        }
40
11
    }
41

            
42
    /// Returns an empty collection that can hold `initial_capacity` values
43
    /// without reallocation.
44
    #[inline]
45
    #[must_use]
46
2
    pub fn with_capacity(initial_capacity: usize) -> Self {
47
2
        Self {
48
2
            slots: Lots::with_capacity(initial_capacity),
49
2
            order: Vec::with_capacity(initial_capacity),
50
2
        }
51
2
    }
52

            
53
    /// Adds `value` to the end of the collection, returning the value's unique
54
    /// [`LotId`].
55
    #[inline]
56
40
    pub fn push(&mut self, value: T) -> LotId {
57
40
        let slot_id = self.slots.push(value);
58
40
        self.order.push(slot_id);
59
40
        slot_id
60
40
    }
61

            
62
    /// Removes the last element of the collection, if one is present.
63
    #[inline]
64
    #[must_use]
65
2
    pub fn pop(&mut self) -> Option<T> {
66
2
        self.pop_entry().map(|(_, v)| v)
67
2
    }
68

            
69
    /// Removes the last element of the collection, if one is present.
70
    #[inline]
71
    #[must_use]
72
3
    pub fn pop_entry(&mut self) -> Option<(LotId, T)> {
73
3
        self.order
74
3
            .pop()
75
3
            .and_then(|id| self.slots.remove(id).map(|v| (id, v)))
76
3
    }
77

            
78
    /// Inserts `value` at `offset` inside of the collection.
79
    ///
80
    /// # Panics
81
    ///
82
    /// This funciton panics if offset is greater than the length of the
83
    /// collection.
84
    #[inline]
85
7
    pub fn insert(&mut self, offset: usize, value: T) -> LotId {
86
7
        // Before modifying the map, check the only logic condition that will
87
7
        // panic.
88
7
        assert!(offset <= self.order.len());
89
7
        let slot_id = self.slots.push(value);
90
7
        self.order.insert(offset, slot_id);
91
7
        slot_id
92
7
    }
93

            
94
    /// Removes the value with the associated [`LotId`], if found.
95
    ///
96
    /// Note: It is possible, but unlikely, for a [`LotId`] that has been
97
    /// removed to be reused.
98
    #[inline]
99
11
    pub fn remove(&mut self, lot: LotId) -> Option<T> {
100
11
        let value = self.slots.remove(lot)?;
101
11
        if let Some((index, _)) = self.order.iter().enumerate().find(|(_, id)| **id == lot) {
102
10
            self.order.remove(index);
103
10
        }
104
10
        Some(value)
105
11
    }
106

            
107
    /// Removes the value at `index`, returning the [`LotId`] and value if
108
    /// found.
109
    #[inline]
110
    #[allow(clippy::must_use_candidate)]
111
1
    pub fn remove_by_index(&mut self, index: usize) -> Option<(LotId, T)> {
112
1
        let id = *self.order.get(index)?;
113

            
114
1
        self.remove(id).map(|value| (id, value))
115
1
    }
116

            
117
    /// Clears all values from this collection.
118
1
    pub fn clear(&mut self) {
119
1
        self.slots.clear();
120
1
        self.order.clear();
121
1
    }
122

            
123
    /// Sets this collection's length to `new_len` if it is less than or equal
124
    /// to the current length.
125
    pub fn truncate(&mut self, new_len: usize) {
126
        if new_len == 0 {
127
            self.clear();
128
        } else if new_len <= self.order.len() {
129
            for to_remove in self.order.drain(new_len..) {
130
                self.slots.remove(to_remove);
131
            }
132
        }
133
    }
134

            
135
    /// Orders the elements in this collection leveraging the standard library's
136
    /// sorting implementation. See [`slice::sort()`] for more information.
137
    #[inline]
138
2
    pub fn sort(&mut self)
139
2
    where
140
2
        T: Ord,
141
2
    {
142
8
        self.order.sort_by_key(|id| &self.slots[*id]);
143
2
    }
144

            
145
    /// Orders the elements in this collection leveraging the standard library's
146
    /// sorting implementation. See [`slice::sort_by()`] for more information.
147
    #[inline]
148
1
    pub fn sort_by<F: Fn(&T, &T) -> Ordering>(&mut self, comparison: F) {
149
1
        self.order
150
3
            .sort_by(|a, b| comparison(&self.slots[*a], &self.slots[*b]));
151
1
    }
152

            
153
    /// Orders the elements in this collection leveraging the standard library's
154
    /// sorting implementation. See [`slice::sort_by_key()`] for more information.
155
    #[inline]
156
1
    pub fn sort_by_key<K, F>(&mut self, mut f: F)
157
1
    where
158
1
        F: FnMut(&T) -> K,
159
1
        K: Ord,
160
1
    {
161
6
        self.order.sort_by_key(|id| f(&self.slots[*id]));
162
1
    }
163

            
164
    /// Orders the elements in this collection leveraging the standard library's
165
    /// sorting implementation. See [`slice::sort_by_cached_key()`] for more information.
166
    #[inline]
167
1
    pub fn sort_by_cached_key<K, F>(&mut self, mut f: F)
168
1
    where
169
1
        F: FnMut(&T) -> K,
170
1
        K: Ord,
171
1
    {
172
3
        self.order.sort_by_cached_key(|id| f(&self.slots[*id]));
173
1
    }
174

            
175
    /// Orders the elements in this collection leveraging the standard library's
176
    /// sorting implementation. See [`slice::sort_unstable()`] for more information.
177
    #[inline]
178
1
    pub fn sort_unstable(&mut self)
179
1
    where
180
1
        T: Ord,
181
1
    {
182
6
        self.order.sort_unstable_by_key(|id| &self.slots[*id]);
183
1
    }
184

            
185
    /// Orders the elements in this collection leveraging the standard library's
186
    /// sorting implementation. See [`slice::sort_unstable_by()`] for more
187
    /// information.
188
    #[inline]
189
1
    pub fn sort_unstable_by<F: Fn(&T, &T) -> Ordering>(&mut self, comparison: F) {
190
1
        self.order
191
3
            .sort_unstable_by(|a, b| comparison(&self.slots[*a], &self.slots[*b]));
192
1
    }
193

            
194
    /// Orders the elements in this collection leveraging the standard library's
195
    /// sorting implementation. See [`slice::sort_unstable_by_key()`] for more
196
    /// information.
197
    #[inline]
198
1
    pub fn sort_unstable_by_key<K, F>(&mut self, mut f: F)
199
1
    where
200
1
        F: FnMut(&T) -> K,
201
1
        K: Ord,
202
1
    {
203
6
        self.order.sort_unstable_by_key(|id| f(&self.slots[*id]));
204
1
    }
205

            
206
    /// Looks up a previously stored value by its [`LotId`]. If the value hasn't
207
    /// been removed, a reference will be returned.
208
    ///
209
    /// Note: It is possible, but unlikely, for a [`LotId`] that has been
210
    /// removed to be reused.
211
    #[inline]
212
    #[must_use]
213
24
    pub fn get(&self, id: LotId) -> Option<&T> {
214
24
        self.slots.get(id)
215
24
    }
216

            
217
    /// Looks up a previously stored value by its [`LotId`]. If the value hasn't
218
    /// been removed, a mutable reference will be returned.
219
    ///
220
    /// Note: It is possible, but unlikely, for a [`LotId`] that has been
221
    /// removed to be reused.
222
    #[inline]
223
    #[must_use]
224
2
    pub fn get_mut(&mut self, id: LotId) -> Option<&mut T> {
225
2
        self.slots.get_mut(id)
226
2
    }
227

            
228
    /// Looks up a value by index. If `index` is greater than or equal to the
229
    /// collections length, `None` will be returned.
230
    #[inline]
231
    #[must_use]
232
36
    pub fn get_by_index(&self, index: usize) -> Option<&T> {
233
36
        self.order
234
36
            .get(index)
235
36
            .and_then(|index| self.slots.get(*index))
236
36
    }
237

            
238
    /// Looks up a mutable reference to a value by index. If `index` is greater
239
    /// than or equal to the collections length, `None` will be returned.
240
    #[inline]
241
    #[must_use]
242
2
    pub fn get_mut_by_index(&mut self, index: usize) -> Option<&mut T> {
243
2
        self.order
244
2
            .get(index)
245
2
            .and_then(|index| self.slots.get_mut(*index))
246
2
    }
247

            
248
    /// Returns the index of `id`, or None if the id is not contained in this
249
    /// collection.
250
    #[inline]
251
    #[must_use]
252
3
    pub fn index_of_id(&self, id: LotId) -> Option<usize> {
253
3
        self.order
254
3
            .iter()
255
3
            .enumerate()
256
4
            .find_map(|(index, &stored)| (stored == id).then_some(index))
257
3
    }
258

            
259
    /// Returns the id of the entry at `index`, or None if `index` is outside of
260
    /// the bounds of this collection.
261
    #[must_use]
262
1
    pub fn id(&self, index: usize) -> Option<LotId> {
263
1
        self.order.get(index).copied()
264
1
    }
265

            
266
    /// Returns the [`LotId`] associated with a given index. Returns `None` if
267
    /// `index` is greater than or equal to the collections length.
268
    #[inline]
269
    #[must_use]
270
1
    pub fn key(&self, index: usize) -> Option<LotId> {
271
1
        self.order.get(index).copied()
272
1
    }
273

            
274
    /// Returns an iterator that returns all the contained values in this
275
    /// collection as they're removed from the collection.
276
    ///
277
    /// Dropping the iterator will still result in the elements being removed.
278
    #[inline]
279
1
    pub fn drain(&mut self) -> Drain<'_, T, DrainAll> {
280
1
        Drain {
281
1
            map: self,
282
1
            index: 0,
283
1
            filter: DrainAll,
284
1
        }
285
1
    }
286

            
287
    /// Returns an iterator that invokes `filter` for each item in the
288
    /// collection. If `filter` returns true for that value, it will be removed
289
    /// and returned from the iterator. When false is returned, the value is
290
    /// kept in the collection.
291
    ///
292
    /// Dropping the iterator will still result in the filtered elements being
293
    /// removed.
294
    #[inline]
295
1
    pub fn drain_filter<Filter>(&mut self, filter: Filter) -> Drain<'_, T, Filter>
296
1
    where
297
1
        Filter: FnMut(&mut T) -> bool,
298
1
    {
299
1
        Drain {
300
1
            map: self,
301
1
            index: 0,
302
1
            filter,
303
1
        }
304
1
    }
305

            
306
    /// Returns the number of values contained in this collection.
307
    #[inline]
308
    #[must_use]
309
8
    pub fn len(&self) -> usize {
310
8
        self.order.len()
311
8
    }
312

            
313
    /// Returns true if this collection has no values.
314
    #[inline]
315
    #[must_use]
316
2
    pub fn is_empty(&self) -> bool {
317
2
        self.order.is_empty()
318
2
    }
319

            
320
    /// Returns an iterator of references to all contained values.
321
    #[inline]
322
    #[must_use]
323
7
    pub fn iter(&self) -> Iter<'_, T> {
324
7
        self.into_iter()
325
7
    }
326

            
327
    /// Returns an `Iterator<Item = (LotId, &T)>` for all contained values.
328
    #[inline]
329
    #[must_use]
330
1
    pub fn entries(&self) -> EntryIter<'_, T> {
331
1
        EntryIter {
332
1
            order: self.order.iter(),
333
1
            map: &self.slots,
334
1
        }
335
1
    }
336

            
337
    /// Returns the first entry in this collection, or None if the collection is
338
    /// empty.
339
    #[must_use]
340
1
    pub fn first(&self) -> Option<&T> {
341
1
        self.get_by_index(0)
342
1
    }
343

            
344
    /// Returns a mutable reference to the first entry in this collection, or
345
    /// None if the collection is empty.
346
    #[must_use]
347
1
    pub fn first_mut(&mut self) -> Option<&mut T> {
348
1
        self.get_mut_by_index(0)
349
1
    }
350

            
351
    /// Returns the last entry in this collection, or None if the collection is
352
    /// empty.
353
    #[must_use]
354
1
    pub fn last(&self) -> Option<&T> {
355
1
        self.order.last().map(|&index| &self.slots[index])
356
1
    }
357

            
358
    /// Returns a mutable reference to the last entry in this collection, or
359
    /// None if the collection is empty.
360
    #[must_use]
361
1
    pub fn last_mut(&mut self) -> Option<&mut T> {
362
1
        self.order.last().map(|&index| &mut self.slots[index])
363
1
    }
364

            
365
    /// Swaps the elements at index `a` and `b`.
366
    ///
367
    /// Internally, this only moves a `LotId`. The underlying data does not
368
    /// change, and its each value's associated `LotId` does not change.
369
1
    pub fn swap(&mut self, a: usize, b: usize) {
370
1
        self.order.swap(a, b);
371
1
    }
372
}
373

            
374
impl<T> Index<LotId> for OrderedLots<T> {
375
    type Output = T;
376

            
377
    #[inline]
378
23
    fn index(&self, index: LotId) -> &Self::Output {
379
23
        self.get(index).expect("invalid lot id")
380
23
    }
381
}
382

            
383
impl<T> IndexMut<LotId> for OrderedLots<T> {
384
    #[inline]
385
1
    fn index_mut(&mut self, index: LotId) -> &mut Self::Output {
386
1
        self.get_mut(index).expect("invalid lot id")
387
1
    }
388
}
389

            
390
impl<T> Index<usize> for OrderedLots<T> {
391
    type Output = T;
392

            
393
    #[inline]
394
35
    fn index(&self, index: usize) -> &Self::Output {
395
35
        self.get_by_index(index).expect("invalid index")
396
35
    }
397
}
398

            
399
impl<T> IndexMut<usize> for OrderedLots<T> {
400
    #[inline]
401
1
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
402
1
        self.get_mut_by_index(index).expect("invalid index")
403
1
    }
404
}
405
impl<T> FromIterator<T> for OrderedLots<T> {
406
    #[inline]
407
2
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
408
2
        let iter = iter.into_iter();
409
2
        let size_hint = iter.size_hint().0;
410
2
        let mut map = Self::with_capacity(size_hint);
411
7
        for item in iter {
412
5
            map.push(item);
413
5
        }
414
2
        map
415
2
    }
416
}
417

            
418
/// An iterator over all values contained in an [`OrderedLots<T>`].
419
#[derive(Clone)]
420
pub struct Iter<'a, T> {
421
    order: slice::Iter<'a, LotId>,
422
    map: &'a Lots<T>,
423
}
424

            
425
impl<'a, T> Iterator for Iter<'a, T> {
426
    type Item = &'a T;
427

            
428
    #[inline]
429
15
    fn next(&mut self) -> Option<Self::Item> {
430
15
        let id = self.order.next()?;
431
11
        Some(&self.map[*id])
432
15
    }
433
}
434

            
435
/// An iterator over an [`OrderedLots<T>`] that returns each contained value and
436
/// its associated [`LotId`].
437
#[derive(Clone)]
438
pub struct EntryIter<'a, T> {
439
    order: slice::Iter<'a, LotId>,
440
    map: &'a Lots<T>,
441
}
442

            
443
impl<'a, T> Iterator for EntryIter<'a, T> {
444
    type Item = (LotId, &'a T);
445

            
446
    #[inline]
447
3
    fn next(&mut self) -> Option<Self::Item> {
448
3
        let id = self.order.next()?;
449
2
        Some((*id, &self.map[*id]))
450
3
    }
451
}
452

            
453
/// An iterator over values being remoed from a [`OrderedLots<T>`].
454
pub struct Drain<'a, T, Filter>
455
where
456
    Filter: DrainFilter<T>,
457
{
458
    map: &'a mut OrderedLots<T>,
459
    index: usize,
460
    filter: Filter,
461
}
462

            
463
impl<'a, T, Filter> Iterator for Drain<'a, T, Filter>
464
where
465
    Filter: DrainFilter<T>,
466
{
467
    type Item = T;
468

            
469
    #[inline]
470
7
    fn next(&mut self) -> Option<Self::Item> {
471
        loop {
472
9
            let id = *self.map.order.get(self.index)?;
473
5
            let lot = self.map.slots.get_mut(id)?;
474
5
            if self.filter.filter(lot) {
475
3
                self.map.order.remove(self.index);
476
3
                return self.map.slots.remove(id);
477
2
            }
478
2

            
479
2
            self.index += 1;
480
        }
481
7
    }
482
}
483

            
484
impl<'a, T, Filter> Drop for Drain<'a, T, Filter>
485
where
486
    Filter: DrainFilter<T>,
487
{
488
    #[inline]
489
2
    fn drop(&mut self) {
490
        // Exhaust the iterator on drop to ensure we fully execute the drain.
491
2
        for _ in self {}
492
2
    }
493
}
494

            
495
impl<'a, T> IntoIterator for &'a OrderedLots<T> {
496
    type IntoIter = Iter<'a, T>;
497
    type Item = &'a T;
498

            
499
    #[inline]
500
7
    fn into_iter(self) -> Self::IntoIter {
501
7
        Iter {
502
7
            order: self.order.iter(),
503
7
            map: &self.slots,
504
7
        }
505
7
    }
506
}
507

            
508
/// An iterator that removes all values from the collection and frees the
509
/// underlying collection.
510
pub struct IntoIter<T> {
511
    order: vec::IntoIter<LotId>,
512
    slots: Lots<T>,
513
}
514

            
515
impl<T> IntoIterator for OrderedLots<T> {
516
    type IntoIter = IntoIter<T>;
517
    type Item = T;
518

            
519
    #[inline]
520
2
    fn into_iter(self) -> Self::IntoIter {
521
2
        IntoIter {
522
2
            order: self.order.into_iter(),
523
2
            slots: self.slots,
524
2
        }
525
2
    }
526
}
527

            
528
impl<T> Iterator for IntoIter<T> {
529
    type Item = T;
530

            
531
    #[inline]
532
7
    fn next(&mut self) -> Option<Self::Item> {
533
7
        let id = self.order.next()?;
534
5
        self.slots.remove(id)
535
7
    }
536
}
537

            
538
impl<T> Eq for OrderedLots<T> where T: Eq {}
539

            
540
impl<T> PartialEq for OrderedLots<T>
541
where
542
    T: PartialEq,
543
{
544
    #[inline]
545
2
    fn eq(&self, other: &Self) -> bool {
546
3
        self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
547
2
    }
548
}
549

            
550
impl<T> PartialEq<[T]> for OrderedLots<T>
551
where
552
    T: PartialEq,
553
{
554
    #[inline]
555
2
    fn eq(&self, other: &[T]) -> bool {
556
2
        self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
557
2
    }
558
}
559

            
560
impl<'a, T> PartialEq<&'a [T]> for OrderedLots<T>
561
where
562
    T: PartialEq,
563
{
564
    #[inline]
565
1
    fn eq(&self, other: &&'a [T]) -> bool {
566
1
        self == *other
567
1
    }
568
}
569

            
570
impl<T, const N: usize> PartialEq<[T; N]> for OrderedLots<T>
571
where
572
    T: PartialEq,
573
{
574
    #[inline]
575
1
    fn eq(&self, other: &[T; N]) -> bool {
576
1
        self.eq(&other[0..N])
577
1
    }
578
}
579

            
580
impl<'a, T, const N: usize> PartialEq<&'a [T; N]> for OrderedLots<T>
581
where
582
    T: PartialEq,
583
{
584
    #[inline]
585
1
    fn eq(&self, other: &&'a [T; N]) -> bool {
586
1
        self.eq(*other)
587
1
    }
588
}
589

            
590
impl<T> Debug for OrderedLots<T>
591
where
592
    T: Debug,
593
{
594
1
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
595
1
        let mut map = f.debug_map();
596

            
597
2
        for (key, value) in self.entries() {
598
2
            map.entry(&key, value);
599
2
        }
600

            
601
1
        map.finish()
602
1
    }
603
}
604

            
605
#[test]
606
1
fn ordered_tests() {
607
7
    fn test_sorting_callback(cb: impl FnOnce(&mut OrderedLots<i32>)) {
608
7
        let mut ordered = OrderedLots::new();
609
7
        let three = ordered.push(3);
610
7
        let one = ordered.push(1);
611
7
        let two = ordered.push(2);
612
7
        cb(&mut ordered);
613
7
        assert_eq!(ordered[one], 1);
614
7
        assert_eq!(ordered[two], 2);
615
7
        assert_eq!(ordered[three], 3);
616
7
        ordered.remove(one);
617
7
        assert_eq!(ordered[0], 2);
618
7
        assert_eq!(ordered[1], 3);
619
7
        ordered.insert(0, 1);
620
7
        assert_eq!(ordered[0], 1);
621
7
        assert_eq!(ordered[1], 2);
622
7
        assert_eq!(ordered[2], 3);
623
7
    }
624

            
625
1
    test_sorting_callback(OrderedLots::sort);
626
1
    test_sorting_callback(OrderedLots::sort_unstable);
627
1
    test_sorting_callback(|ordered| ordered.sort_by(Ord::cmp));
628
1
    test_sorting_callback(|ordered| ordered.sort_unstable_by(Ord::cmp));
629
6
    test_sorting_callback(|ordered| ordered.sort_by_key(|a| *a));
630
3
    test_sorting_callback(|ordered| ordered.sort_by_cached_key(|a| *a));
631
6
    test_sorting_callback(|ordered| ordered.sort_unstable_by_key(|a| *a));
632
1
}
633

            
634
#[test]
635
1
fn basics() {
636
1
    let mut map = OrderedLots::new();
637
1
    let first = map.push(1);
638
1
    assert_eq!(map.len(), 1);
639
1
    assert_eq!(map[first], 1);
640
1
    assert_eq!(map.key(0), Some(first));
641
1
    map[first] = 2;
642
1
    assert_eq!(map[first], 2);
643
1
    map[0] = 3;
644
1
    // PartialEq for array
645
1
    assert_eq!(map, &[3]);
646
    // PartialEq for slice
647
1
    assert_eq!(map, &[3][..]);
648
1
    let drain = map.drain().collect::<Vec<_>>();
649
1
    assert_eq!(drain, &[3]);
650
1
    assert!(map.is_empty());
651

            
652
1
    assert!(map.get(first).is_none());
653
1
    assert!(map.get_mut(first).is_none());
654
1
    assert!(map.remove(first).is_none());
655
1
    let second = map.push(1);
656
1
    assert_eq!(map.remove(second), Some(1));
657

            
658
1
    map.push(2);
659
1
    assert_eq!(map.pop(), Some(2));
660
1
    let fourth = map.push(3);
661
1
    assert_eq!(map.pop_entry(), Some((fourth, 3)));
662

            
663
1
    map.clear();
664
1
    assert_eq!(map.len(), 0);
665
1
    assert!(map.is_empty());
666
1
}
667

            
668
#[test]
669
1
fn iteration() {
670
1
    let mut map = OrderedLots::default();
671
1
    map.push(1);
672
1
    let two = map.push(2);
673
1
    map.push(3);
674
1
    map.push(4);
675
1

            
676
1
    // Create a gap for the iterator.
677
1
    map.remove(two);
678
1

            
679
1
    let values = map.iter().copied().collect::<Vec<_>>();
680
1
    assert_eq!(values, &[1, 3, 4]);
681
1
    let values = map.into_iter().collect::<Vec<_>>();
682
1
    assert_eq!(values, &[1, 3, 4]);
683
1
}
684

            
685
#[test]
686
1
fn drain_filter() {
687
1
    let mut map = OrderedLots::default();
688
1
    map.push(1_i32);
689
1
    map.push(2);
690
1
    map.push(3);
691
1
    map.push(4);
692
4
    let odd = map.drain_filter(|v| *v % 2 == 1).collect::<Vec<_>>();
693
1
    assert_eq!(odd, &[1, 3]);
694
1
    assert_eq!(map.into_iter().collect::<Vec<_>>(), &[2, 4]);
695
1
}
696

            
697
#[test]
698
1
fn dbg() {
699
1
    let mut map = OrderedLots::from_iter([1, 2, 3]);
700
1
    let _: Option<i32> = map.pop();
701
1
    assert_eq!(alloc::format!("{map:?}"), "{LotId(0g1): 1, LotId(1g1): 2}");
702
1
}
703

            
704
#[test]
705
1
fn clone_and_eq() {
706
1
    let map = OrderedLots::from_iter([2, 1]);
707
1
    let mut map2 = map.clone();
708
1
    assert_eq!(map, map2);
709
1
    map2.sort();
710
1
    assert_ne!(map, map2);
711
1
}
712

            
713
#[test]
714
1
fn index_ops() {
715
1
    let mut map = OrderedLots::new();
716
1
    let one = map.push(1);
717
1
    let two = map.push(2);
718
1
    assert_eq!(map.id(0), Some(one));
719
1
    assert_eq!(map.index_of_id(two), Some(1));
720

            
721
1
    assert_eq!(map.first(), Some(&1));
722
1
    assert_eq!(map.last(), Some(&2));
723
1
    assert_eq!(map.first_mut(), Some(&mut 1));
724
1
    assert_eq!(map.last_mut(), Some(&mut 2));
725

            
726
1
    map.swap(0, 1);
727
1

            
728
1
    assert_eq!(map.remove_by_index(0), Some((two, 2)));
729
1
    assert_eq!(map.index_of_id(one), Some(0));
730
1
    assert_eq!(map.index_of_id(two), None);
731
1
}