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
//! Provides an object based allocator [`Slab<T>`] backed by a contiguous
//! growable array of slots.
//!
//! The slab allocator pre-allocates memory for objects of same type so that
//! it reduces fragmentation caused by allocations and deallocations. When
//! allocating memory for an object, it just finds a free (unused) slot, marks
//! it as used, and returns the index of the slot for later access to the
//! object. When freeing an object, it just adds the slot holding the object
//! to the list of free (unused) slots after dropping the object.
//!
//! # Examples
//!
//! ```
//! # use ruyi_slab::Slab;
//! // Explicitly create a Slab<T> with new
//! let mut slab = Slab::new();
//!
//! // Insert an object into the slab
//! let one = slab.insert(1);
//!
//! // Remove an object at the specified index
//! let removed = slab.remove(one);
//!
//! assert_eq!(removed.unwrap(), 1);
//! ```
//!
//! [`Slab<T>`]: struct.Slab.html

#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

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

use core::fmt;
use core::mem;
use core::ops::{Index, IndexMut};

#[cfg(debug_assertions)]
#[inline]
fn unreachable() -> ! {
    unreachable!()
}

#[cfg(not(debug_assertions))]
unsafe fn unreachable() -> ! {
    core::hint::unreachable_unchecked()
}

enum Slot<T> {
    Used(T),
    Free(usize),
}

impl<T> Slot<T> {
    #[inline]
    unsafe fn get_unchecked(&self) -> &T {
        match self {
            Slot::Used(obj) => obj,
            Slot::Free(_) => unreachable(),
        }
    }

    #[inline]
    unsafe fn get_unchecked_mut(&mut self) -> &mut T {
        match self {
            Slot::Used(obj) => obj,
            Slot::Free(_) => unreachable(),
        }
    }

    #[inline]
    unsafe fn get_free_unchecked(&self) -> usize {
        match self {
            Slot::Free(index) => *index,
            Slot::Used(_) => unreachable(),
        }
    }

    #[inline]
    unsafe fn unwrap_unchecked(self) -> T {
        match self {
            Slot::Used(obj) => obj,
            Slot::Free(_) => unreachable(),
        }
    }

    #[inline]
    unsafe fn take(&mut self, index: usize) -> T {
        mem::replace(self, Slot::Free(index)).unwrap_unchecked()
    }

    #[inline]
    unsafe fn put(&mut self, obj: T) -> usize {
        mem::replace(self, Slot::Used(obj)).get_free_unchecked()
    }
}

impl<T: fmt::Debug> fmt::Debug for Slot<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Slot::Used(obj) => write!(f, "Used({:?}", obj),
            Slot::Free(index) => write!(f, "Free({})", index),
        }
    }
}

/// An object based allocator backed by a contiguous growable array of slots.
///
/// # Examples
/// ```
/// # use ruyi_slab::Slab;
/// let mut slab = Slab::new();
/// let one = slab.insert(1);
/// let two = slab.insert(2);
///
/// assert_eq!(slab.len(), 2);
/// assert_eq!(slab[two], 2);
///
/// slab.remove(one);
///
/// assert_eq!(slab.len(), 1);
///
/// let entry = slab.free_entry();
/// let index = entry.index();
/// entry.insert(index);
///
/// assert_eq!(slab.len(), 2);
/// assert_eq!(slab[index], index);
/// ```
///
/// # Capacity and reallocation
///
/// The capacity of a slab is the amount of space allocated for any future
/// objects that will be inserted to the slab. This is not to be confused with
/// the *length* of a slab, which specifies the number of actual objects
/// within the slab. If a slab's length exceeds its capacity, its capacity
/// will automatically be increased, but its objects will have to be
/// reallocated.
///
/// For example, a slab with capacity 10 and length 0 would be an empty slab
/// with space for 10 more objects. Inserting 10 or fewer objects into the
/// slab will not change its capacity or cause reallocation to occur. However,
/// if the slab's length is increased to 11, it will have to reallocate, which
/// can be slow. For this reason, it is recommended to use [`Slab::with_capacity`]
/// whenever possible to specify how big the slab is expected to get.
///
/// [`Slab::with_capacity`]: #method.with_capacity
#[derive(Debug)]
pub struct Slab<T> {
    slots: Vec<Slot<T>>,
    len: usize,
    free: usize,
}

unsafe impl<T: Send> Send for Slab<T> {}

impl<T> Slab<T> {
    const NULL: usize = core::usize::MAX;

    /// Constructs a new empty `Slab<T>`.
    /// The allocator will not allocate until the first object is inserted.
    ///
    /// # Examples
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::new();
    /// # slab.insert(1);
    /// ```
    #[inline]
    pub const fn new() -> Self {
        Self {
            slots: Vec::new(),
            len: 0,
            free: Self::NULL,
        }
    }

    /// Constructs a new, empty `Slab<T>` with the specified capacity.
    ///
    /// The slab will be able to hold exactly `capacity` objects without
    /// reallocating. If `capacity` is 0, the slab will not allocate.
    ///
    /// It is important to note that although the returned slab has the
    /// *capacity* specified, the slab will have a zero *length*. For an
    /// explanation of the difference between length and capacity, see
    /// *[Capacity and reallocation]*.
    ///
    /// [Capacity and reallocation]: #capacity-and-reallocation
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(10);
    ///
    /// // The slab contains no objects, even though it has capacity for more
    /// assert_eq!(slab.len(), 0);
    ///
    /// // These are all done without reallocating...
    /// for i in 0..10 {
    ///     slab.insert(i);
    /// }
    ///
    /// // ...but this may make the slab reallocate
    /// slab.insert(11);
    /// ```
    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            slots: Vec::with_capacity(capacity),
            len: 0,
            free: Self::NULL,
        }
    }

    /// Returns the number of objects in the slab.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(3);
    /// slab.insert(1);
    /// slab.insert(2);
    /// slab.insert(3);
    ///
    /// assert_eq!(slab.len(), 3);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns the number of objects the slab can hold without
    /// reallocating.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let slab: Slab<i32> = Slab::with_capacity(10);
    ///
    /// assert_eq!(slab.capacity(), 10);
    /// ```
    #[inline]
    pub fn capacity(&self) -> usize {
        self.slots.capacity()
    }

    /// Returns `true` if the slab contains no objects.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::new();
    ///
    /// assert!(slab.is_empty());
    ///
    /// slab.insert(1);
    ///
    /// assert!(!slab.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Clears the slab, removing all objects.
    ///
    /// Note that this method has no effect on the allocated capacity
    /// of the slab.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(3);
    /// slab.insert(1);
    /// slab.insert(2);
    /// slab.clear();
    ///
    /// assert!(slab.is_empty());
    /// ```
    #[inline]
    pub fn clear(&mut self) {
        if self.len > 0 {
            self.slots.clear();
            self.len = 0;
            self.free = Self::NULL;
        } else {
            unsafe {
                self.slots.set_len(0);
            }
        }
    }

    /// Reserves capacity for at least `additional` more objects to be inserted
    /// in the given `Slab<T>`. The slab may reserve more space to avoid
    /// frequent reallocations. After calling `reserve`, capacity will be
    /// greater than or equal to `self.len() + additional`. Does nothing if
    /// capacity is already sufficient.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity overflows `usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(1);
    /// slab.insert(1);
    /// slab.reserve(10);
    ///
    /// assert!(slab.capacity() >= 11);
    /// ```
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        let n = self.slots.capacity() - self.len;
        if additional > n {
            self.slots.reserve(additional - n);
        }
    }

    /// Reserves the minimum capacity for exactly `additional` more objects to
    /// be inserted in the given `Slab<T>`. After calling `reserve_exact`,
    /// capacity will be greater than or equal to `self.len() + additional`.
    /// Does nothing if the capacity is already sufficient.
    ///
    /// Note that the allocator may give the collection more space than it
    /// requests. Therefore, capacity can not be relied upon to be precisely
    /// minimal. Prefer `reserve` if future insertions are expected.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity overflows`usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(1);
    /// slab.insert(1);
    /// slab.reserve_exact(10);
    ///
    /// assert!(slab.capacity() >= 11);
    /// ```
    #[inline]
    pub fn reserve_exact(&mut self, additional: usize) {
        let n = self.slots.capacity() - self.len;
        if additional > n {
            self.slots.reserve_exact(additional - n);
        }
    }

    /// Inserts an object to the slab.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(3);
    /// slab.insert(1);
    /// slab.insert(2);
    ///
    /// assert_eq!(slab.len(), 2);
    ///
    /// slab.insert(3);
    ///
    /// assert_eq!(slab.len(), 3);
    /// ```
    #[inline]
    pub fn insert(&mut self, obj: T) -> usize {
        let cur;
        if self.has_free_slots() {
            cur = self.free;
            self.free = unsafe { self.slots.get_unchecked_mut(cur).put(obj) };
        } else {
            cur = self.len;
            self.slots.push(Slot::Used(obj));
        }
        self.len += 1;
        cur
    }

    /// Returns an entry referring to an unused slot for further manipulation.
    /// It is useful when an object to be inserted need know its slab index.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(1);
    /// let entry = slab.free_entry();
    /// let index = entry.index();
    /// let obj = (index, "My slab index");
    /// entry.insert(obj);
    ///
    /// assert_eq!(slab[index].0, index);
    #[inline]
    pub fn free_entry(&mut self) -> Entry<'_, T> {
        Entry::new(self)
    }

    /// Removes and returns the object at the specified `index`, and the slot
    /// will be put to the list of free slots for reusing. `None` is returned
    /// if no object is found at the specified `index`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(1);
    /// let one = slab.insert(1);
    ///
    /// assert_eq!(slab.len(), 1);
    /// assert_eq!(slab.remove(one).unwrap(), 1);
    /// assert!(slab.is_empty());
    /// ```
    #[inline]
    pub fn remove(&mut self, index: usize) -> Option<T> {
        if let Some(slot) = self.slots.get_mut(index) {
            if let Slot::Used(_) = slot {
                let obj = unsafe { slot.take(self.free) };
                self.free = index;
                self.len -= 1;
                return Some(obj);
            }
        }
        None
    }

    /// Returns a reference to the object at the specified `index` if the
    /// object exists. Otherwise, `None` is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(3);
    /// let one = slab.insert(1);
    /// let two = slab.insert(2);
    /// let three = slab.insert(3);
    ///
    /// assert_eq!(slab.get(one), Some(&1));
    /// assert_eq!(slab.get(three), Some(&3));
    /// assert_eq!(slab.get(slab.len()), None);
    ///
    /// slab.remove(two);
    ///
    /// assert_eq!(slab.get(two), None);
    /// assert_eq!(slab.get(slab.capacity()), None);
    /// ```
    #[inline]
    pub fn get(&self, index: usize) -> Option<&T> {
        if let Some(slot) = self.slots.get(index) {
            if let Slot::Used(obj) = slot {
                return Some(obj);
            }
        }
        None
    }

    /// Returns a mutable reference to the object at the specified `index`
    /// if the object exists. Otherwise, `None` is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(2);
    /// let one = slab.insert(1);
    /// let two = slab.insert(2);
    ///
    /// assert_eq!(slab[one], 1);
    /// assert_eq!(slab[two], 2);
    ///
    /// *slab.get_mut(one).unwrap() = 3;
    /// slab.remove(two);
    ///
    /// assert_eq!(slab[one], 3);
    /// assert_eq!(slab.get_mut(two), None);
    /// ```
    #[inline]
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        if let Some(slot) = self.slots.get_mut(index) {
            if let Slot::Used(obj) = slot {
                return Some(obj);
            }
        }
        None
    }

    /// Removes and returns the object at the specified `index` without
    /// checking if the object exists or not.
    ///
    /// # Safety
    ///
    /// If the slot at the specified `index` does not have an object, the
    /// behavior of calling this method is undefined.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(1);
    /// let one = slab.insert(1);
    ///
    /// assert_eq!(slab.len(), 1);
    /// unsafe {
    ///     assert_eq!(slab.remove_unchecked(one), 1);
    /// }
    /// assert!(slab.is_empty());
    /// ```
    #[inline]
    pub unsafe fn remove_unchecked(&mut self, index: usize) -> T {
        let obj = self.slots.get_unchecked_mut(index).take(self.free);
        self.free = index;
        self.len -= 1;
        obj
    }

    /// Returns a reference to the object at the specified `index` without
    /// checking if the object exists or not.
    ///
    /// # Safety
    ///
    /// If the slot at the specified `index` does not have an object, the
    /// behavior of calling this method is undefined even if the resulting
    /// reference is not used.
    ///
    /// For a safe alternative see [`get`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(1);
    /// let one = slab.insert(1);
    ///
    /// unsafe {
    ///     assert_eq!(slab.get_unchecked(one), &1);
    /// }
    /// ```
    ///
    /// [`get`]: #method.get
    #[inline]
    pub unsafe fn get_unchecked(&self, index: usize) -> &T {
        self.slots.get_unchecked(index).get_unchecked()
    }

    /// Returns a mutable reference to the object at the specified `index`
    /// without checking if the object exists or not.
    ///
    /// # Safety
    ///
    /// If the slot at the specified `index` does not have an object, the
    /// behavior of calling this method is undefined even if the resulting
    /// reference is not used.
    ///
    /// For a safe alternative see [`get_mut`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(1);
    /// let one = slab.insert(1);
    ///
    /// assert_eq!(slab[one], 1);
    ///
    /// unsafe {
    ///     *slab.get_unchecked_mut(one) = 2;
    /// }
    ///
    /// assert_eq!(slab[one], 2);
    /// ```
    ///
    /// [`get_mut`]: #method.get_mut
    #[inline]
    pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
        self.slots.get_unchecked_mut(index).get_unchecked_mut()
    }

    #[inline]
    fn has_free_slots(&self) -> bool {
        self.free != Self::NULL
    }

    #[inline]
    fn next_free(&self) -> usize {
        if self.has_free_slots() {
            self.free
        } else {
            self.len
        }
    }
}

impl<T> Default for Slab<T> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Drop for Slab<T> {
    #[inline]
    fn drop(&mut self) {
        self.clear();
    }
}

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

    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        match self.get(index) {
            Some(obj) => obj,
            None => panic!("invalid slab index {}", index),
        }
    }
}

impl<T> IndexMut<usize> for Slab<T> {
    #[inline]
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        match self.get_mut(index) {
            Some(obj) => obj,
            None => panic!("invalid slab index {}", index),
        }
    }
}

/// A handle to a free slot in a `Slab<T>`.
#[derive(Debug)]
pub struct Entry<'a, T> {
    slab: &'a mut Slab<T>,
}

impl<'a, T> Entry<'a, T> {
    #[inline]
    fn new(slab: &'a mut Slab<T>) -> Self {
        Self { slab }
    }

    /// Returns the index of the free slot that this entry refers to.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(2);
    /// slab.insert(1);
    /// let entry = slab.free_entry();
    /// let index = entry.index();
    /// entry.insert(index);
    ///
    /// assert_eq!(slab.len(), 2);
    /// assert_eq!(slab[index], index);
    /// ```
    #[inline]
    pub fn index(&self) -> usize {
        self.slab.next_free()
    }

    /// Inserts the specified object into the slot this entry refers to.
    ///
    /// # Examples
    ///
    /// ```
    /// # use ruyi_slab::Slab;
    /// let mut slab = Slab::with_capacity(2);
    /// slab.insert(1);
    /// let entry = slab.free_entry();
    /// let index = entry.index();
    /// entry.insert(index);
    ///
    /// assert_eq!(slab.len(), 2);
    /// assert_eq!(slab[index], index);
    /// ```
    #[inline]
    pub fn insert(self, obj: T) {
        self.slab.insert(obj);
    }
}