stable_vec/lib.rs
1//! A `Vec<T>`-like collection which guarantees stable indices and features
2//! O(1) deletion of elements.
3//!
4//! You can find nearly all the relevant documentation on the type
5//! [`StableVecFacade`]. This is the main type which is configurable over the
6//! core implementation. To use a pre-configured stable vector, use
7//! [`StableVec`].
8//!
9//! This crate uses `#![no_std]` but requires the `alloc` crate.
10//!
11//!
12//! # Why?
13//!
14//! The standard `Vec<T>` always stores all elements contiguously. While this
15//! has many advantages (most notable: cache friendliness), it has the
16//! disadvantage that you can't simply remove an element from the middle; at
17//! least not without shifting all elements after it to the left. And this has
18//! two major drawbacks:
19//!
20//! 1. It has a linear O(n) time complexity
21//! 2. It invalidates all indices of the shifted elements
22//!
23//! Invalidating an index means that a given index `i` who referred to an
24//! element `a` before, now refers to another element `b`. On the contrary, a
25//! *stable* index means, that the index always refers to the same element.
26//!
27//! Stable indices are needed in quite a few situations. One example are graph
28//! data structures (or complex data structures in general). Instead of
29//! allocating heap memory for every node and edge, all nodes and all edges are
30//! stored in a vector (each). But how does the programmer unambiguously refer
31//! to one specific node? A pointer is not possible due to the reallocation
32//! strategy of most dynamically growing arrays (the pointer itself is not
33//! *stable*). Thus, often the index is used.
34//!
35//! But in order to use the index, it has to be stable. This is one example,
36//! where this data structure comes into play.
37//!
38//!
39//! # How?
40//!
41//! We can trade O(1) deletions and stable indices for a higher memory
42//! consumption.
43//!
44//! When `StableVec::remove()` is called, the element is just marked as
45//! "deleted" (and the actual element is dropped), but other than that, nothing
46//! happens. This has the very obvious disadvantage that deleted objects (so
47//! called empty slots) just waste space. This is also the most important thing
48//! to understand:
49//!
50//! The memory requirement of this data structure is `O(|inserted elements|)`;
51//! instead of `O(|inserted elements| - |removed elements|)`. The latter is the
52//! memory requirement of normal `Vec<T>`. Thus, if deletions are far more
53//! numerous than insertions in your situation, then this data structure is
54//! probably not fitting your needs.
55//!
56//!
57//! # Why not?
58//!
59//! As mentioned above, this data structure is rather simple and has many
60//! disadvantages on its own. Here are some reason not to use it:
61//!
62//! - You don't need stable indices or O(1) removal
63//! - Your deletions significantly outnumber your insertions
64//! - You want to choose your keys/indices
65//! - Lookup times do not matter so much to you
66//!
67//! Especially in the last two cases, you could consider using a `HashMap` with
68//! integer keys, best paired with a fast hash function for small keys.
69//!
70//! If you not only want stable indices, but stable pointers, you might want
71//! to use something similar to a linked list. Although: think carefully about
72//! your problem before using a linked list.
73//!
74//!
75//! # Use of `unsafe` in this crate
76//!
77//! Unfortunately, implementing the features of this crate in a fast manner
78//! requires `unsafe`. This was measured in micro-benchmarks (included in this
79//! repository) and on a larger project using this crate. Thus, the use of
80//! `unsafe` is measurement-guided and not just because it was assumed `unsafe`
81//! makes things faster.
82//!
83//! This crate takes great care to ensure that all instances of `unsafe` are
84//! actually safe. All methods on the (low level) `Core` trait have extensive
85//! documentation of preconditions, invariants and postconditions. Comments in
86//! functions usually describe why `unsafe` is safe. This crate contains a
87//! fairly large number of unit tests and some tests with randomized input.
88//! These tests are executed with `miri` to try to catch UB caused by invalid
89//! `unsafe` code.
90//!
91//! That said, of course it cannot be guaranteed this crate is perfectly safe.
92//! If you think you found an instance of incorrect usage of `unsafe` or any
93//! UB, don't hesitate to open an issue immediately. Also, if you find `unsafe`
94//! code that is not necessary and you can show that removing it does not
95//! decrease execution speed, please also open an issue or PR!
96//!
97
98#![deny(missing_debug_implementations)]
99#![deny(broken_intra_doc_links)]
100
101// enable `no_std` for everything except for tests.
102#![cfg_attr(not(test), no_std)]
103extern crate alloc;
104
105
106use ::core::{
107 cmp,
108 fmt,
109 iter::FromIterator,
110 mem,
111 ops::{Index, IndexMut},
112};
113use alloc::vec::Vec;
114
115use crate::{
116 core::{Core, DefaultCore, OwningCore, OptionCore, BitVecCore},
117 iter::{Indices, Iter, IterMut, IntoIter, Values, ValuesMut},
118};
119
120#[cfg(test)]
121mod tests;
122pub mod core;
123pub mod iter;
124
125
126
127/// A stable vector with the default core implementation.
128pub type StableVec<T> = StableVecFacade<T, DefaultCore<T>>;
129
130/// A stable vector which stores the "deleted information" inline. This is very
131/// close to `Vec<Option<T>>`.
132///
133/// This is particularly useful if `T` benefits from "null optimization", i.e.
134/// if `size_of::<T>() == size_of::<Option<T>>()`.
135pub type InlineStableVec<T> = StableVecFacade<T, OptionCore<T>>;
136
137/// A stable vector which stores the "deleted information" externally in a bit
138/// vector.
139pub type ExternStableVec<T> = StableVecFacade<T, BitVecCore<T>>;
140
141
142/// A `Vec<T>`-like collection which guarantees stable indices and features
143/// O(1) deletion of elements.
144///
145///
146/// # Terminology and overview of a stable vector
147///
148/// A stable vector has slots. Each slot can either be filled or empty. There
149/// are three numbers describing a stable vector (each of those functions runs
150/// in O(1)):
151///
152/// - [`capacity()`][StableVecFacade::capacity]: the total number of slots
153/// (filled and empty).
154/// - [`num_elements()`][StableVecFacade::num_elements]: the number of filled
155/// slots.
156/// - [`next_push_index()`][StableVecFacade::next_push_index]: the index of the
157/// first slot (i.e. with the smallest index) that was never filled. This is
158/// the index that is returned by [`push`][StableVecFacade::push]. This
159/// implies that all filled slots have indices smaller than
160/// `next_push_index()`.
161///
162/// Here is an example visualization (with `num_elements = 4`).
163///
164/// ```text
165/// 0 1 2 3 4 5 6 7 8 9 10
166/// ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
167/// │ a │ - │ b │ c │ - │ - │ d │ - │ - │ - │
168/// └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
169/// ↑ ↑
170/// next_push_index capacity
171/// ```
172///
173/// Unlike `Vec<T>`, `StableVecFacade` allows access to all slots with indices
174/// between 0 and `capacity()`. In particular, it is allowed to call
175/// [`insert`][StableVecFacade::insert] with all indices smaller than
176/// `capacity()`.
177///
178///
179/// # The Core implementation `C`
180///
181/// You might have noticed the type parameter `C`. There are actually multiple
182/// ways how to implement the abstact data structure described above. One might
183/// basically use a `Vec<Option<T>>`. But there are other ways, too.
184///
185/// Most of the time, you can simply use the alias [`StableVec`] which uses the
186/// [`DefaultCore`]. This is fine for almost all cases. That's why all
187/// documentation examples use that type instead of the generic
188/// `StableVecFacade`.
189///
190///
191/// # Implemented traits
192///
193/// This type implements a couple of traits. Some of those implementations
194/// require further explanation:
195///
196/// - `Clone`: the cloned instance is exactly the same as the original,
197/// including empty slots.
198/// - `Extend`, `FromIterator`, `From<AsRef<[T]>>`: these impls work as if all
199/// of the source elements are just `push`ed onto the stable vector in order.
200/// - `PartialEq<Self>`/`Eq`: empty slots, capacity, `next_push_index` and the
201/// indices of elements are all checked. In other words: all observable
202/// properties of the stable vectors need to be the same for them to be
203/// "equal".
204/// - `PartialEq<[B]>`/`PartialEq<Vec<B>>`: capacity, `next_push_index`, empty
205/// slots and indices are ignored for the comparison. It is equivalent to
206/// `sv.iter().eq(vec)`.
207///
208/// # Overview of important methods
209///
210/// (*there are more methods than mentioned in this overview*)
211///
212/// **Creating a stable vector**
213///
214/// - [`new`][StableVecFacade::new]
215/// - [`with_capacity`][StableVecFacade::with_capacity]
216/// - [`FromIterator::from_iter`](#impl-FromIterator<T>)
217///
218/// **Adding and removing elements**
219///
220/// - [`push`][StableVecFacade::push]
221/// - [`insert`][StableVecFacade::insert]
222/// - [`remove`][StableVecFacade::remove]
223///
224/// **Accessing elements**
225///
226/// - [`get`][StableVecFacade::get] and [`get_mut`][StableVecFacade::get_mut]
227/// (returns `Option<&T>` and `Option<&mut T>`)
228/// - [the `[]` index operator](#impl-Index<usize>) (returns `&T` or `&mut T`)
229/// - [`remove`][StableVecFacade::remove] (returns `Option<T>`)
230///
231/// **Stable vector specifics**
232///
233/// - [`has_element_at`][StableVecFacade::has_element_at]
234/// - [`next_push_index`][StableVecFacade::next_push_index]
235/// - [`is_compact`][StableVecFacade::is_compact]
236///
237#[derive(Clone)]
238pub struct StableVecFacade<T, C: Core<T>> {
239 core: OwningCore<T, C>,
240 num_elements: usize,
241}
242
243impl<T, C: Core<T>> StableVecFacade<T, C> {
244 /// Constructs a new, empty stable vector.
245 ///
246 /// The stable-vector will not allocate until elements are pushed onto it.
247 pub fn new() -> Self {
248 Self {
249 core: OwningCore::new(C::new()),
250 num_elements: 0,
251 }
252 }
253
254 /// Constructs a new, empty stable vector with the specified capacity.
255 ///
256 /// The stable-vector will be able to hold exactly `capacity` elements
257 /// without reallocating. If `capacity` is 0, the stable-vector will not
258 /// allocate any memory. See [`reserve`][StableVecFacade::reserve] for more
259 /// information.
260 pub fn with_capacity(capacity: usize) -> Self {
261 let mut out = Self::new();
262 out.reserve_exact(capacity);
263 out
264 }
265
266 /// Inserts the new element `elem` at index `self.next_push_index` and
267 /// returns said index.
268 ///
269 /// The inserted element will always be accessible via the returned index.
270 ///
271 /// This method has an amortized runtime complexity of O(1), just like
272 /// `Vec::push`.
273 ///
274 /// # Example
275 ///
276 /// ```
277 /// # use stable_vec::StableVec;
278 /// let mut sv = StableVec::new();
279 /// let star_idx = sv.push('★');
280 /// let heart_idx = sv.push('♥');
281 ///
282 /// assert_eq!(sv.get(heart_idx), Some(&'♥'));
283 ///
284 /// // After removing the star we can still use the heart's index to access
285 /// // the element!
286 /// sv.remove(star_idx);
287 /// assert_eq!(sv.get(heart_idx), Some(&'♥'));
288 /// ```
289 pub fn push(&mut self, elem: T) -> usize {
290 let index = self.core.len();
291 self.reserve(1);
292
293 unsafe {
294 // Due to `reserve`, the core holds at least one empty slot, so we
295 // know that `index` is smaller than the capacity. We also know
296 // that at `index` there is no element (the definition of `len`
297 // guarantees this).
298 self.core.set_len(index + 1);
299 self.core.insert_at(index, elem);
300 }
301
302 self.num_elements += 1;
303 index
304 }
305
306 /// Inserts the given value at the given index.
307 ///
308 /// If the slot at `index` is empty, the `elem` is inserted at that
309 /// position and `None` is returned. If there is an existing element `x` at
310 /// that position, that element is replaced by `elem` and `Some(x)` is
311 /// returned. The `next_push_index` is adjusted accordingly if `index >=
312 /// next_push_index()`.
313 ///
314 ///
315 /// # Panics
316 ///
317 /// Panics if the index is `>= self.capacity()`.
318 ///
319 /// # Example
320 ///
321 /// ```
322 /// # use stable_vec::StableVec;
323 /// let mut sv = StableVec::new();
324 /// let star_idx = sv.push('★');
325 /// let heart_idx = sv.push('♥');
326 ///
327 /// // Inserting into an empty slot (element was deleted).
328 /// sv.remove(star_idx);
329 /// assert_eq!(sv.num_elements(), 1);
330 /// assert_eq!(sv.insert(star_idx, 'x'), None);
331 /// assert_eq!(sv.num_elements(), 2);
332 /// assert_eq!(sv[star_idx], 'x');
333 ///
334 /// // We can also reserve memory (create new empty slots) and insert into
335 /// // such a new slot. Note that that `next_push_index` gets adjusted.
336 /// sv.reserve_for(5);
337 /// assert_eq!(sv.insert(5, 'y'), None);
338 /// assert_eq!(sv.num_elements(), 3);
339 /// assert_eq!(sv.next_push_index(), 6);
340 /// assert_eq!(sv[5], 'y');
341 ///
342 /// // Inserting into a filled slot replaces the value and returns the old
343 /// // value.
344 /// assert_eq!(sv.insert(heart_idx, 'z'), Some('♥'));
345 /// assert_eq!(sv[heart_idx], 'z');
346 /// ```
347 pub fn insert(&mut self, index: usize, mut elem: T) -> Option<T> {
348 // If the index is out of bounds, we cannot insert the new element.
349 if index >= self.core.cap() {
350 panic!(
351 "`index ({}) >= capacity ({})` in `StableVecFacade::insert`",
352 index,
353 self.core.cap(),
354 );
355 }
356
357 if self.has_element_at(index) {
358 unsafe {
359 // We just checked there is an element at that position, so
360 // this is fine.
361 mem::swap(self.core.get_unchecked_mut(index), &mut elem);
362 }
363 Some(elem)
364 } else {
365 if index >= self.core.len() {
366 // Due to the bounds check above, we know that `index + 1` is ≤
367 // `capacity`.
368 unsafe {
369 self.core.set_len(index + 1);
370 }
371 }
372
373 unsafe {
374 // `insert_at` requires that `index < cap` and
375 // `!has_element_at(index)`. Both of these conditions are met
376 // by the two explicit checks above.
377 self.core.insert_at(index, elem);
378 }
379
380 self.num_elements += 1;
381
382 None
383 }
384 }
385
386 /// Removes and returns the element at position `index`. If the slot at
387 /// `index` is empty, nothing is changed and `None` is returned.
388 ///
389 /// This simply marks the slot at `index` as empty. The elements after the
390 /// given index are **not** shifted to the left. Thus, the time complexity
391 /// of this method is O(1).
392 ///
393 /// # Panic
394 ///
395 /// Panics if `index >= self.capacity()`.
396 ///
397 /// # Example
398 ///
399 /// ```
400 /// # use stable_vec::StableVec;
401 /// let mut sv = StableVec::new();
402 /// let star_idx = sv.push('★');
403 /// let heart_idx = sv.push('♥');
404 ///
405 /// assert_eq!(sv.remove(star_idx), Some('★'));
406 /// assert_eq!(sv.remove(star_idx), None); // the star was already removed
407 ///
408 /// // We can use the heart's index here. It has not been invalidated by
409 /// // the removal of the star.
410 /// assert_eq!(sv.remove(heart_idx), Some('♥'));
411 /// assert_eq!(sv.remove(heart_idx), None); // the heart was already removed
412 /// ```
413 pub fn remove(&mut self, index: usize) -> Option<T> {
414 // If the index is out of bounds, we cannot insert the new element.
415 if index >= self.core.cap() {
416 panic!(
417 "`index ({}) >= capacity ({})` in `StableVecFacade::remove`",
418 index,
419 self.core.cap(),
420 );
421 }
422
423 if self.has_element_at(index) {
424 // We checked with `Self::has_element_at` that the conditions for
425 // `remove_at` are met.
426 let elem = unsafe {
427 self.core.remove_at(index)
428 };
429
430 self.num_elements -= 1;
431 Some(elem)
432 } else {
433 None
434 }
435 }
436
437 /// Removes all elements from this collection.
438 ///
439 /// After calling this, `num_elements()` will return 0. All indices are
440 /// invalidated. However, no memory is deallocated, so the capacity stays
441 /// as it was before. `self.next_push_index` is 0 after calling this method.
442 ///
443 /// # Example
444 ///
445 /// ```
446 /// # use stable_vec::StableVec;
447 /// let mut sv = StableVec::from(&['a', 'b']);
448 ///
449 /// sv.clear();
450 /// assert_eq!(sv.num_elements(), 0);
451 /// assert!(sv.capacity() >= 2);
452 /// ```
453 pub fn clear(&mut self) {
454 // We are not using `Core::clear` here, as we need to decrement
455 // `num_elements` in lockstep with dropping the elements, as otherwise
456 // a panic can corrupt it.
457 unsafe {
458 for idx in 0..self.core.len() {
459 if self.core.has_element_at(idx) {
460 self.num_elements -= 1;
461 drop(self.core.remove_at(idx));
462 }
463 }
464 self.core.set_len(0);
465 }
466 }
467
468 /// Returns a reference to the element at the given index, or `None` if
469 /// there exists no element at that index.
470 ///
471 /// If you are calling `unwrap()` on the result of this method anyway,
472 /// rather use the index operator instead: `stable_vec[index]`.
473 pub fn get(&self, index: usize) -> Option<&T> {
474 if self.has_element_at(index) {
475 // We might call this, because we checked both conditions via
476 // `Self::has_element_at`.
477 let elem = unsafe {
478 self.core.get_unchecked(index)
479 };
480 Some(elem)
481 } else {
482 None
483 }
484 }
485
486 /// Returns a mutable reference to the element at the given index, or
487 /// `None` if there exists no element at that index.
488 ///
489 /// If you are calling `unwrap()` on the result of this method anyway,
490 /// rather use the index operator instead: `stable_vec[index]`.
491 pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
492 if self.has_element_at(index) {
493 // We might call this, because we checked both conditions via
494 // `Self::has_element_at`.
495 let elem = unsafe {
496 self.core.get_unchecked_mut(index)
497 };
498 Some(elem)
499 } else {
500 None
501 }
502 }
503
504 /// Returns a reference to the element at the given index without checking
505 /// the index.
506 ///
507 /// # Security
508 ///
509 /// When calling this method `self.has_element_at(index)` has to be `true`,
510 /// otherwise this method's behavior is undefined! This requirement implies
511 /// the requirement `index < self.next_push_index()`.
512 pub unsafe fn get_unchecked(&self, index: usize) -> &T {
513 self.core.get_unchecked(index)
514 }
515
516 /// Returns a mutable reference to the element at the given index without
517 /// checking the index.
518 ///
519 /// # Security
520 ///
521 /// When calling this method `self.has_element_at(index)` has to be `true`,
522 /// otherwise this method's behavior is undefined! This requirement implies
523 /// the requirement `index < self.next_push_index()`.
524 pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
525 self.core.get_unchecked_mut(index)
526 }
527
528 /// Returns `true` if there exists an element at the given index (i.e. the
529 /// slot at `index` is *not* empty), `false` otherwise.
530 ///
531 /// An element is said to exist if the index is not out of bounds and the
532 /// slot at the given index is not empty. In particular, this method can
533 /// also be called with indices larger than the current capacity (although,
534 /// `false` is always returned in those cases).
535 ///
536 /// # Example
537 ///
538 /// ```
539 /// # use stable_vec::StableVec;
540 /// let mut sv = StableVec::new();
541 /// assert!(!sv.has_element_at(3)); // no: index out of bounds
542 ///
543 /// let heart_idx = sv.push('♥');
544 /// assert!(sv.has_element_at(heart_idx)); // yes
545 ///
546 /// sv.remove(heart_idx);
547 /// assert!(!sv.has_element_at(heart_idx)); // no: was removed
548 /// ```
549 pub fn has_element_at(&self, index: usize) -> bool {
550 if index >= self.core.cap() {
551 false
552 } else {
553 unsafe {
554 // The index is smaller than the capacity, as checked aboved,
555 // so we can call this without a problem.
556 self.core.has_element_at(index)
557 }
558 }
559 }
560
561 /// Returns the number of existing elements in this collection.
562 ///
563 /// As long as no element is ever removed, `num_elements()` equals
564 /// `next_push_index()`. Once an element has been removed, `num_elements()`
565 /// will always be less than `next_push_index()` (assuming
566 /// `[reordering_]make_compact()` is not called).
567 ///
568 /// # Example
569 ///
570 /// ```
571 /// # use stable_vec::StableVec;
572 /// let mut sv = StableVec::new();
573 /// assert_eq!(sv.num_elements(), 0);
574 ///
575 /// let heart_idx = sv.push('♥');
576 /// assert_eq!(sv.num_elements(), 1);
577 ///
578 /// sv.remove(heart_idx);
579 /// assert_eq!(sv.num_elements(), 0);
580 /// ```
581 pub fn num_elements(&self) -> usize {
582 self.num_elements
583 }
584
585 /// Returns the index that would be returned by calling
586 /// [`push()`][StableVecFacade::push]. All filled slots have indices below
587 /// `next_push_index()`.
588 ///
589 /// # Example
590 ///
591 /// ```
592 /// # use stable_vec::StableVec;
593 /// let mut sv = StableVec::from(&['a', 'b', 'c']);
594 ///
595 /// let next_push_index = sv.next_push_index();
596 /// let index_of_d = sv.push('d');
597 ///
598 /// assert_eq!(next_push_index, index_of_d);
599 /// ```
600 pub fn next_push_index(&self) -> usize {
601 self.core.len()
602 }
603
604 /// Returns the number of slots in this stable vector.
605 pub fn capacity(&self) -> usize {
606 self.core.cap()
607 }
608
609 /// Returns `true` if this collection doesn't contain any existing
610 /// elements.
611 ///
612 /// This means that `is_empty()` returns true iff no elements were inserted
613 /// *or* all inserted elements were removed again.
614 ///
615 /// # Example
616 ///
617 /// ```
618 /// # use stable_vec::StableVec;
619 /// let mut sv = StableVec::new();
620 /// assert!(sv.is_empty());
621 ///
622 /// let heart_idx = sv.push('♥');
623 /// assert!(!sv.is_empty());
624 ///
625 /// sv.remove(heart_idx);
626 /// assert!(sv.is_empty());
627 /// ```
628 pub fn is_empty(&self) -> bool {
629 self.num_elements == 0
630 }
631
632 /// Returns `true` if all existing elements are stored contiguously from
633 /// the beginning (in other words: there are no empty slots with indices
634 /// below `self.next_push_index()`).
635 ///
636 /// # Example
637 ///
638 /// ```
639 /// # use stable_vec::StableVec;
640 /// let mut sv = StableVec::from(&[0, 1, 2, 3, 4]);
641 /// assert!(sv.is_compact());
642 ///
643 /// sv.remove(1);
644 /// assert!(!sv.is_compact());
645 /// ```
646 pub fn is_compact(&self) -> bool {
647 self.num_elements == self.core.len()
648 }
649
650 /// Returns an iterator over indices and immutable references to the stable
651 /// vector's elements. Elements are yielded in order of their increasing
652 /// indices.
653 ///
654 /// Note that you can also obtain this iterator via the `IntoIterator` impl
655 /// of `&StableVecFacade`.
656 ///
657 /// # Example
658 ///
659 /// ```
660 /// # use stable_vec::StableVec;
661 /// let mut sv = StableVec::from(&[10, 11, 12, 13, 14]);
662 /// sv.remove(1);
663 ///
664 /// let mut it = sv.iter().filter(|&(_, &n)| n <= 13);
665 /// assert_eq!(it.next(), Some((0, &10)));
666 /// assert_eq!(it.next(), Some((2, &12)));
667 /// assert_eq!(it.next(), Some((3, &13)));
668 /// assert_eq!(it.next(), None);
669 /// ```
670 pub fn iter(&self) -> Iter<'_, T, C> {
671 Iter::new(self)
672 }
673
674 /// Returns an iterator over indices and mutable references to the stable
675 /// vector's elements. Elements are yielded in order of their increasing
676 /// indices.
677 ///
678 /// Note that you can also obtain this iterator via the `IntoIterator` impl
679 /// of `&mut StableVecFacade`.
680 ///
681 /// # Example
682 ///
683 /// ```
684 /// # use stable_vec::StableVec;
685 /// let mut sv = StableVec::from(&[10, 11, 12, 13, 14]);
686 /// sv.remove(1);
687 ///
688 /// for (idx, elem) in &mut sv {
689 /// if idx % 2 == 0 {
690 /// *elem *= 2;
691 /// }
692 /// }
693 ///
694 /// assert_eq!(sv, vec![20, 24, 13, 28]);
695 /// ```
696 pub fn iter_mut(&mut self) -> IterMut<'_, T, C> {
697 IterMut::new(self)
698 }
699
700 /// Returns an iterator over immutable references to the existing elements
701 /// of this stable vector. Elements are yielded in order of their
702 /// increasing indices.
703 ///
704 /// # Example
705 ///
706 /// ```
707 /// # use stable_vec::StableVec;
708 /// let mut sv = StableVec::from(&[0, 1, 2, 3, 4]);
709 /// sv.remove(1);
710 ///
711 /// let mut it = sv.values().filter(|&&n| n <= 3);
712 /// assert_eq!(it.next(), Some(&0));
713 /// assert_eq!(it.next(), Some(&2));
714 /// assert_eq!(it.next(), Some(&3));
715 /// assert_eq!(it.next(), None);
716 /// ```
717 pub fn values(&self) -> Values<'_, T, C> {
718 Values::new(self)
719 }
720
721 /// Returns an iterator over mutable references to the existing elements
722 /// of this stable vector. Elements are yielded in order of their
723 /// increasing indices.
724 ///
725 /// Through this iterator, the elements within the stable vector can be
726 /// mutated.
727 ///
728 /// # Examples
729 ///
730 /// ```
731 /// # use stable_vec::StableVec;
732 /// let mut sv = StableVec::from(&[1.0, 2.0, 3.0]);
733 ///
734 /// for e in sv.values_mut() {
735 /// *e *= 2.0;
736 /// }
737 ///
738 /// assert_eq!(sv, &[2.0, 4.0, 6.0] as &[_]);
739 /// ```
740 pub fn values_mut(&mut self) -> ValuesMut<'_, T, C> {
741 ValuesMut::new(self)
742 }
743
744 /// Returns an iterator over all indices of filled slots of this stable
745 /// vector. Indices are yielded in increasing order.
746 ///
747 /// # Example
748 ///
749 /// ```
750 /// # use stable_vec::StableVec;
751 /// let mut sv = StableVec::from(&['a', 'b', 'c', 'd']);
752 /// sv.remove(1);
753 ///
754 /// let mut it = sv.indices();
755 /// assert_eq!(it.next(), Some(0));
756 /// assert_eq!(it.next(), Some(2));
757 /// assert_eq!(it.next(), Some(3));
758 /// assert_eq!(it.next(), None);
759 /// ```
760 ///
761 /// Simply using the `for`-loop:
762 ///
763 /// ```
764 /// # use stable_vec::StableVec;
765 /// let mut sv = StableVec::from(&['a', 'b', 'c', 'd']);
766 ///
767 /// for index in sv.indices() {
768 /// println!("index: {}", index);
769 /// }
770 /// ```
771 pub fn indices(&self) -> Indices<'_, T, C> {
772 Indices::new(self)
773 }
774
775 /// Reserves memory for at least `additional` more elements to be inserted
776 /// at indices `>= self.next_push_index()`.
777 ///
778 /// This method might allocate more than `additional` to avoid frequent
779 /// reallocations. Does nothing if the current capacity is already
780 /// sufficient. After calling this method, `self.capacity()` is ≥
781 /// `self.next_push_index() + additional`.
782 ///
783 /// Unlike `Vec::reserve`, the additional reserved memory is not completely
784 /// unaccessible. Instead, additional empty slots are added to this stable
785 /// vector. These can be used just like any other empty slot; in
786 /// particular, you can insert into it.
787 ///
788 /// # Example
789 ///
790 /// ```
791 /// # use stable_vec::StableVec;
792 /// let mut sv = StableVec::new();
793 /// let star_idx = sv.push('★');
794 ///
795 /// // After we inserted one element, the next element would sit at index
796 /// // 1, as expected.
797 /// assert_eq!(sv.next_push_index(), 1);
798 ///
799 /// sv.reserve(2); // insert two empty slots
800 ///
801 /// // `reserve` doesn't change any of this
802 /// assert_eq!(sv.num_elements(), 1);
803 /// assert_eq!(sv.next_push_index(), 1);
804 ///
805 /// // We can now insert an element at index 2.
806 /// sv.insert(2, 'x');
807 /// assert_eq!(sv[2], 'x');
808 ///
809 /// // These values get adjusted accordingly.
810 /// assert_eq!(sv.num_elements(), 2);
811 /// assert_eq!(sv.next_push_index(), 3);
812 /// ```
813 pub fn reserve(&mut self, additional: usize) {
814 #[inline(never)]
815 #[cold]
816 fn capacity_overflow() -> ! {
817 panic!("capacity overflow in `stable_vec::StableVecFacade::reserve` (attempt \
818 to allocate more than `isize::MAX` elements");
819 }
820
821 //: new_cap = len + additional ∧ additional >= 0
822 //: => new_cap >= len
823 let new_cap = match self.core.len().checked_add(additional) {
824 None => capacity_overflow(),
825 Some(new_cap) => new_cap,
826 };
827
828 if self.core.cap() < new_cap {
829 // We at least double our capacity. Otherwise repeated `push`es are
830 // O(n²).
831 //
832 // This multiplication can't overflow, because we know the capacity
833 // is `<= isize::MAX`.
834 //
835 //: new_cap = max(new_cap_before, 2 * cap)
836 //: ∧ cap >= len
837 //: ∧ new_cap_before >= len
838 //: => new_cap >= len
839 let new_cap = cmp::max(new_cap, 2 * self.core.cap());
840
841 if new_cap > isize::max_value() as usize {
842 capacity_overflow();
843 }
844
845 //: new_cap >= len ∧ new_cap <= isize::MAX
846 //
847 // These both properties are exactly the preconditions of
848 // `realloc`, so we can safely call that method.
849 unsafe {
850 self.core.realloc(new_cap);
851 }
852 }
853 }
854
855 /// Reserve enough memory so that there is a slot at `index`. Does nothing
856 /// if `index < self.capacity()`.
857 ///
858 /// This method might allocate more memory than requested to avoid frequent
859 /// allocations. After calling this method, `self.capacity() >= index + 1`.
860 ///
861 ///
862 /// # Example
863 ///
864 /// ```
865 /// # use stable_vec::StableVec;
866 /// let mut sv = StableVec::new();
867 /// let star_idx = sv.push('★');
868 ///
869 /// // Allocate enough memory so that we have a slot at index 5.
870 /// sv.reserve_for(5);
871 /// assert!(sv.capacity() >= 6);
872 ///
873 /// // We can now insert an element at index 5.
874 /// sv.insert(5, 'x');
875 /// assert_eq!(sv[5], 'x');
876 ///
877 /// // This won't do anything as the slot with index 3 already exists.
878 /// let capacity_before = sv.capacity();
879 /// sv.reserve_for(3);
880 /// assert_eq!(sv.capacity(), capacity_before);
881 /// ```
882 pub fn reserve_for(&mut self, index: usize) {
883 #[inline(never)]
884 #[cold]
885 fn capacity_overflow() -> ! {
886 panic!("capacity overflow in `stable_vec::StableVecFacade::reserve_for` (attempt \
887 to allocate more than `isize::MAX` elements");
888 }
889
890 if index >= self.capacity() {
891 // Won't underflow as `index >= capacity >= next_push_index`.
892 let additional = (index - self.next_push_index())
893 .checked_add(1)
894 .unwrap_or_else(|| capacity_overflow());
895 self.reserve(additional);
896 }
897 }
898
899 /// Like [`reserve`][StableVecFacade::reserve], but tries to allocate
900 /// memory for exactly `additional` more elements.
901 ///
902 /// The underlying allocator might allocate more memory than requested,
903 /// meaning that you cannot rely on the capacity of this stable vector
904 /// having an exact value after calling this method.
905 pub fn reserve_exact(&mut self, additional: usize) {
906 #[inline(never)]
907 #[cold]
908 fn capacity_overflow() -> ! {
909 panic!("capacity overflow in `stable_vec::StableVecFacade::reserve_exact` (attempt \
910 to allocate more than `isize::MAX` elements");
911 }
912
913 //: new_cap = len + additional ∧ additional >= 0
914 //: => new_cap >= len
915 let new_cap = match self.core.len().checked_add(additional) {
916 None => capacity_overflow(),
917 Some(new_cap) => new_cap,
918 };
919
920 if self.core.cap() < new_cap {
921 if new_cap > isize::max_value() as usize {
922 capacity_overflow();
923 }
924
925 //: new_cap >= len ∧ new_cap <= isize::MAX
926 //
927 // These both properties are exactly the preconditions of
928 // `realloc`, so we can safely call that method.
929 unsafe {
930 self.core.realloc(new_cap);
931 }
932 }
933 }
934
935 /// Removes and returns the first element from this collection, or `None`
936 /// if it's empty.
937 ///
938 /// This method uses exactly the same deletion strategy as
939 /// [`remove()`][StableVecFacade::remove].
940 ///
941 /// # Example
942 ///
943 /// ```
944 /// # use stable_vec::StableVec;
945 /// let mut sv = StableVec::from(&[1, 2, 3]);
946 /// assert_eq!(sv.remove_first(), Some(1));
947 /// assert_eq!(sv, vec![2, 3]);
948 /// ```
949 ///
950 /// # Note
951 ///
952 /// This method needs to find the index of the first valid element. Finding
953 /// it has a worst case time complexity of O(n). If you already know the
954 /// index, use [`remove()`][StableVecFacade::remove] instead.
955 pub fn remove_first(&mut self) -> Option<T> {
956 self.find_first_index().and_then(|index| self.remove(index))
957 }
958
959 /// Removes and returns the last element from this collection, or `None` if
960 /// it's empty.
961 ///
962 /// This method uses exactly the same deletion strategy as
963 /// [`remove()`][StableVecFacade::remove].
964 ///
965 /// # Example
966 ///
967 /// ```
968 /// # use stable_vec::StableVec;
969 /// let mut sv = StableVec::from(&[1, 2, 3]);
970 /// assert_eq!(sv.remove_last(), Some(3));
971 /// assert_eq!(sv, vec![1, 2]);
972 /// ```
973 ///
974 /// # Note
975 ///
976 /// This method needs to find the index of the last valid element. Finding
977 /// it has a worst case time complexity of O(n). If you already know the
978 /// index, use [`remove()`][StableVecFacade::remove] instead.
979 pub fn remove_last(&mut self) -> Option<T> {
980 self.find_last_index().and_then(|index| self.remove(index))
981 }
982
983 /// Finds the first element and returns a reference to it, or `None` if
984 /// the stable vector is empty.
985 ///
986 /// This method has a worst case time complexity of O(n).
987 ///
988 /// # Example
989 ///
990 /// ```
991 /// # use stable_vec::StableVec;
992 /// let mut sv = StableVec::from(&[1, 2]);
993 /// sv.remove(0);
994 /// assert_eq!(sv.find_first(), Some(&2));
995 /// ```
996 pub fn find_first(&self) -> Option<&T> {
997 self.find_first_index().map(|index| unsafe { self.core.get_unchecked(index) })
998 }
999
1000 /// Finds the first element and returns a mutable reference to it, or
1001 /// `None` if the stable vector is empty.
1002 ///
1003 /// This method has a worst case time complexity of O(n).
1004 ///
1005 /// # Example
1006 ///
1007 /// ```
1008 /// # use stable_vec::StableVec;
1009 /// let mut sv = StableVec::from(&[1, 2]);
1010 /// {
1011 /// let first = sv.find_first_mut().unwrap();
1012 /// assert_eq!(*first, 1);
1013 ///
1014 /// *first = 3;
1015 /// }
1016 /// assert_eq!(sv, vec![3, 2]);
1017 /// ```
1018 pub fn find_first_mut(&mut self) -> Option<&mut T> {
1019 self.find_first_index().map(move |index| unsafe { self.core.get_unchecked_mut(index) })
1020 }
1021
1022 /// Finds the last element and returns a reference to it, or `None` if
1023 /// the stable vector is empty.
1024 ///
1025 /// This method has a worst case time complexity of O(n).
1026 ///
1027 /// # Example
1028 ///
1029 /// ```
1030 /// # use stable_vec::StableVec;
1031 /// let mut sv = StableVec::from(&[1, 2]);
1032 /// sv.remove(1);
1033 /// assert_eq!(sv.find_last(), Some(&1));
1034 /// ```
1035 pub fn find_last(&self) -> Option<&T> {
1036 self.find_last_index().map(|index| unsafe { self.core.get_unchecked(index) })
1037 }
1038
1039 /// Finds the last element and returns a mutable reference to it, or `None`
1040 /// if the stable vector is empty.
1041 ///
1042 /// This method has a worst case time complexity of O(n).
1043 ///
1044 /// # Example
1045 ///
1046 /// ```
1047 /// # use stable_vec::StableVec;
1048 /// let mut sv = StableVec::from(&[1, 2]);
1049 /// {
1050 /// let last = sv.find_last_mut().unwrap();
1051 /// assert_eq!(*last, 2);
1052 ///
1053 /// *last = 3;
1054 /// }
1055 /// assert_eq!(sv, vec![1, 3]);
1056 /// ```
1057 pub fn find_last_mut(&mut self) -> Option<&mut T> {
1058 self.find_last_index().map(move |index| unsafe { self.core.get_unchecked_mut(index) })
1059 }
1060
1061 /// Performs a forwards search starting at index `start`, returning the
1062 /// index of the first filled slot that is found.
1063 ///
1064 /// Specifically, if an element at index `start` exists, `Some(start)` is
1065 /// returned. If all slots with indices `start` and higher are empty (or
1066 /// don't exist), `None` is returned. This method can be used to iterate
1067 /// over all existing elements without an iterator object.
1068 ///
1069 /// The inputs `start >= self.next_push_index()` are only allowed for
1070 /// convenience. For those `start` values, `None` is always returned.
1071 ///
1072 /// # Panics
1073 ///
1074 /// Panics if `start > self.capacity()`. Note: `start == self.capacity()`
1075 /// is allowed for convenience, but always returns `None`.
1076 ///
1077 /// # Example
1078 ///
1079 /// ```
1080 /// # use stable_vec::StableVec;
1081 /// let mut sv = StableVec::from(&[0, 1, 2, 3, 4]);
1082 /// sv.remove(1);
1083 /// sv.remove(2);
1084 /// sv.remove(4);
1085 ///
1086 /// assert_eq!(sv.first_filled_slot_from(0), Some(0));
1087 /// assert_eq!(sv.first_filled_slot_from(1), Some(3));
1088 /// assert_eq!(sv.first_filled_slot_from(2), Some(3));
1089 /// assert_eq!(sv.first_filled_slot_from(3), Some(3));
1090 /// assert_eq!(sv.first_filled_slot_from(4), None);
1091 /// assert_eq!(sv.first_filled_slot_from(5), None);
1092 /// ```
1093 pub fn first_filled_slot_from(&self, start: usize) -> Option<usize> {
1094 if start > self.core.cap() {
1095 panic!(
1096 "`start` is {}, but capacity is {} in `first_filled_slot_from`",
1097 start,
1098 self.capacity(),
1099 );
1100 } else {
1101 // The precondition `start <= self.core.cap()` is satisfied.
1102 unsafe { self.core.first_filled_slot_from(start) }
1103 }
1104 }
1105
1106 /// Performs a backwards search starting at index `start - 1`, returning
1107 /// the index of the first filled slot that is found. For `start == 0`,
1108 /// `None` is returned.
1109 ///
1110 /// Note: passing in `start >= self.len()` just wastes time, as those slots
1111 /// are never filled.
1112 ///
1113 /// # Panics
1114 ///
1115 /// Panics if `start > self.capacity()`. Note: `start == self.capacity()`
1116 /// is allowed for convenience, but wastes time.
1117 ///
1118 /// # Example
1119 ///
1120 /// ```
1121 /// # use stable_vec::StableVec;
1122 /// let mut sv = StableVec::from(&[0, 1, 2, 3, 4]);
1123 /// sv.remove(0);
1124 /// sv.remove(2);
1125 /// sv.remove(3);
1126 ///
1127 /// assert_eq!(sv.first_filled_slot_below(0), None);
1128 /// assert_eq!(sv.first_filled_slot_below(1), None);
1129 /// assert_eq!(sv.first_filled_slot_below(2), Some(1));
1130 /// assert_eq!(sv.first_filled_slot_below(3), Some(1));
1131 /// assert_eq!(sv.first_filled_slot_below(4), Some(1));
1132 /// assert_eq!(sv.first_filled_slot_below(5), Some(4));
1133 /// ```
1134 pub fn first_filled_slot_below(&self, start: usize) -> Option<usize> {
1135 if start > self.core.cap() {
1136 panic!(
1137 "`start` is {}, but capacity is {} in `first_filled_slot_below`",
1138 start,
1139 self.capacity(),
1140 );
1141 } else {
1142 // The precondition `start <= self.core.cap()` is satisfied.
1143 unsafe { self.core.first_filled_slot_below(start) }
1144 }
1145 }
1146
1147 /// Performs a forwards search starting at index `start`, returning the
1148 /// index of the first empty slot that is found.
1149 ///
1150 /// Specifically, if the slot at index `start` is empty, `Some(start)` is
1151 /// returned. If all slots with indices `start` and higher are filled,
1152 /// `None` is returned.
1153 ///
1154 ///
1155 /// # Panics
1156 ///
1157 /// Panics if `start > self.capacity()`. Note: `start == self.capacity()`
1158 /// is allowed for convenience, but always returns `None`.
1159 ///
1160 /// # Example
1161 ///
1162 /// ```
1163 /// # use stable_vec::StableVec;
1164 /// let mut sv = StableVec::from(&[0, 1, 2, 3, 4, 5]);
1165 /// sv.remove(1);
1166 /// sv.remove(2);
1167 /// sv.remove(4);
1168 ///
1169 /// assert_eq!(sv.first_empty_slot_from(0), Some(1));
1170 /// assert_eq!(sv.first_empty_slot_from(1), Some(1));
1171 /// assert_eq!(sv.first_empty_slot_from(2), Some(2));
1172 /// assert_eq!(sv.first_empty_slot_from(3), Some(4));
1173 /// assert_eq!(sv.first_empty_slot_from(4), Some(4));
1174 ///
1175 /// // Make sure we have at least one empty slot at the end
1176 /// sv.reserve_for(6);
1177 /// assert_eq!(sv.first_empty_slot_from(5), Some(6));
1178 /// assert_eq!(sv.first_empty_slot_from(6), Some(6));
1179 /// ```
1180 pub fn first_empty_slot_from(&self, start: usize) -> Option<usize> {
1181 if start > self.core.cap() {
1182 panic!(
1183 "`start` is {}, but capacity is {} in `first_empty_slot_from`",
1184 start,
1185 self.capacity(),
1186 );
1187 } else {
1188 unsafe { self.core.first_empty_slot_from(start) }
1189 }
1190 }
1191
1192 /// Performs a backwards search starting at index `start - 1`, returning
1193 /// the index of the first empty slot that is found. For `start == 0`,
1194 /// `None` is returned.
1195 ///
1196 /// If all slots with indices below `start` are filled, `None` is returned.
1197 ///
1198 /// # Example
1199 ///
1200 /// ```
1201 /// # use stable_vec::StableVec;
1202 /// let mut sv = StableVec::from(&[0, 1, 2, 3, 4, 5]);
1203 /// sv.remove(1);
1204 /// sv.remove(2);
1205 /// sv.remove(4);
1206 ///
1207 /// assert_eq!(sv.first_empty_slot_below(0), None);
1208 /// assert_eq!(sv.first_empty_slot_below(1), None);
1209 /// assert_eq!(sv.first_empty_slot_below(2), Some(1));
1210 /// assert_eq!(sv.first_empty_slot_below(3), Some(2));
1211 /// assert_eq!(sv.first_empty_slot_below(4), Some(2));
1212 /// assert_eq!(sv.first_empty_slot_below(5), Some(4));
1213 /// assert_eq!(sv.first_empty_slot_below(6), Some(4));
1214 /// ```
1215 pub fn first_empty_slot_below(&self, start: usize) -> Option<usize> {
1216 if start > self.core.cap() {
1217 panic!(
1218 "`start` is {}, but capacity is {} in `first_empty_slot_below`",
1219 start,
1220 self.capacity(),
1221 );
1222 } else {
1223 unsafe { self.core.first_empty_slot_below(start) }
1224 }
1225 }
1226
1227
1228 /// Finds the first element and returns its index, or `None` if the stable
1229 /// vector is empty.
1230 ///
1231 /// This method has a worst case time complexity of O(n).
1232 ///
1233 /// # Example
1234 ///
1235 /// ```
1236 /// # use stable_vec::StableVec;
1237 /// let mut sv = StableVec::from(&[1, 2]);
1238 /// sv.remove(0);
1239 /// assert_eq!(sv.find_first_index(), Some(1));
1240 /// ```
1241 pub fn find_first_index(&self) -> Option<usize> {
1242 // `0 <= self.core.cap()` is always true
1243 unsafe {
1244 self.core.first_filled_slot_from(0)
1245 }
1246 }
1247
1248 /// Finds the last element and returns its index, or `None` if the stable
1249 /// vector is empty.
1250 ///
1251 /// This method has a worst case time complexity of O(n).
1252 ///
1253 /// # Example
1254 ///
1255 /// ```
1256 /// # use stable_vec::StableVec;
1257 /// let mut sv = StableVec::from(&[1, 2]);
1258 /// sv.remove(1);
1259 /// assert_eq!(sv.find_last_index(), Some(0));
1260 /// ```
1261 pub fn find_last_index(&self) -> Option<usize> {
1262 // `self.core.len() <= self.core.cap()` is always true
1263 unsafe {
1264 self.core.first_filled_slot_below(self.core.len())
1265 }
1266 }
1267
1268 /// Reallocates to have a capacity as small as possible while still holding
1269 /// `self.next_push_index()` slots.
1270 ///
1271 /// Note that this does not move existing elements around and thus does not
1272 /// invalidate indices. This method also doesn't change what
1273 /// `next_push_index` returns. Instead, only the capacity is changed. Due
1274 /// to the underlying allocator, it cannot be guaranteed that the capacity
1275 /// is exactly `self.next_push_index()` after calling this method.
1276 ///
1277 /// If you want to compact this stable vector by removing deleted elements,
1278 /// use the method [`make_compact`][StableVecFacade::make_compact] or
1279 /// [`reordering_make_compact`][StableVecFacade::reordering_make_compact]
1280 /// instead.
1281 pub fn shrink_to_fit(&mut self) {
1282 // `realloc` has the following preconditions:
1283 // - (a) `new_cap ≥ self.len()`
1284 // - (b) `new_cap ≤ isize::MAX`
1285 //
1286 // It's trivial to see that (a) is not violated here. (b) is also never
1287 // violated, because the `Core` trait says that `len < cap` and `cap <
1288 // isize::MAX`.
1289 unsafe {
1290 let new_cap = self.core.len();
1291 self.core.realloc(new_cap);
1292 }
1293 }
1294
1295 /// Rearranges elements to reclaim memory. **Invalidates indices!**
1296 ///
1297 /// After calling this method, all existing elements stored contiguously in
1298 /// memory. You might want to call [`shrink_to_fit()`][StableVecFacade::shrink_to_fit]
1299 /// afterwards to actually free memory previously used by removed elements.
1300 /// This method itself does not deallocate any memory.
1301 ///
1302 /// The `next_push_index` value is also changed by this method (if the
1303 /// stable vector wasn't compact before).
1304 ///
1305 /// In comparison to
1306 /// [`reordering_make_compact()`][StableVecFacade::reordering_make_compact],
1307 /// this method does not change the order of elements. Due to this, this
1308 /// method is a bit slower.
1309 ///
1310 /// # Warning
1311 ///
1312 /// This method invalidates the indices of all elements that are stored
1313 /// after the first empty slot in the stable vector!
1314 pub fn make_compact(&mut self) {
1315 if self.is_compact() {
1316 return;
1317 }
1318
1319 // We only have to move elements, if we have any.
1320 if self.num_elements > 0 {
1321 unsafe {
1322 // We have to find the position of the first hole. As we are not
1323 // compact, there is at least one hole.
1324 let first_hole_index = self.core.first_empty_slot_from(0)
1325 .unwrap_or_else(|| inconsistent_num_elements());
1326
1327 // We use two indices:
1328 // - `hole_index`: always points at a hole, starts from first hole,
1329 // incrementing till it reaches `num_elements`.
1330 // - `element_index`: of the next element to fill the whole with,
1331 // always larger than `hole_index` and advances faster than it.
1332 let mut element_index = first_hole_index + 1;
1333
1334 // Beginning from the first hole, we have to fill each index with
1335 // a new value. This is required to keep the order of elements.
1336 for hole_index in first_hole_index..self.num_elements {
1337 // Actually find the next element which we can use to fill
1338 // the hole.
1339 //
1340 // We deliberately use the bounded `first_filled_slot_from`:
1341 // if `num_elements` were ever too large, the latter would
1342 // happily run past `cap` and cause UB. This way, a wrong
1343 // `num_elements` only leads to a panic.
1344 element_index = self.core.first_filled_slot_from(element_index)
1345 .unwrap_or_else(|| inconsistent_num_elements());
1346
1347 // So at this point `hole_index` points to a valid hole and
1348 // `element_index` points to a valid element. Time to swap!
1349 self.core.swap(hole_index, element_index);
1350 }
1351 }
1352 }
1353
1354 // We can safely call `set_len()` here: all elements are in the
1355 // range 0..self.num_elements.
1356 unsafe {
1357 self.core.set_len(self.num_elements);
1358 }
1359 }
1360
1361 /// Rearranges elements to reclaim memory. **Invalidates indices and
1362 /// changes the order of the elements!**
1363 ///
1364 /// After calling this method, all existing elements stored contiguously
1365 /// in memory. You might want to call [`shrink_to_fit()`][StableVecFacade::shrink_to_fit]
1366 /// afterwards to actually free memory previously used by removed elements.
1367 /// This method itself does not deallocate any memory.
1368 ///
1369 /// The `next_push_index` value is also changed by this method (if the
1370 /// stable vector wasn't compact before).
1371 ///
1372 /// If you do need to preserve the order of elements, use
1373 /// [`make_compact()`][StableVecFacade::make_compact] instead. However, if
1374 /// you don't care about element order, you should prefer using this
1375 /// method, because it is faster.
1376 ///
1377 /// # Warning
1378 ///
1379 /// This method invalidates the indices of all elements that are stored
1380 /// after the first hole and it does not preserve the order of elements!
1381 pub fn reordering_make_compact(&mut self) {
1382 if self.is_compact() {
1383 return;
1384 }
1385
1386 // We only have to move elements, if we have any.
1387 if self.num_elements > 0 {
1388 unsafe {
1389 // We use two indices:
1390 //
1391 // - `hole_index` starts from the front and searches for a hole
1392 // that can be filled with an element.
1393 // - `element_index` starts from the back and searches for an
1394 // element.
1395 let len = self.core.len();
1396 let mut element_index = len;
1397 let mut hole_index = 0;
1398 loop {
1399 element_index = self.core.first_filled_slot_below(element_index).unwrap_or(0);
1400 hole_index = self.core.first_empty_slot_from(hole_index).unwrap_or(len);
1401
1402 // If both indices passed each other, we can stop. There are no
1403 // holes left of `hole_index` and no element right of
1404 // `element_index`.
1405 if hole_index >= element_index {
1406 break;
1407 }
1408
1409 // We found an element and a hole left of the element. That
1410 // means that we can swap.
1411 self.core.swap(hole_index, element_index);
1412 }
1413 }
1414 }
1415
1416 // We can safely call `set_len()` here: all elements are in the
1417 // range 0..self.num_elements.
1418 unsafe {
1419 self.core.set_len(self.num_elements);
1420 }
1421 }
1422
1423 /// Returns `true` if the stable vector contains an element with the given
1424 /// value, `false` otherwise.
1425 ///
1426 /// ```
1427 /// # use stable_vec::StableVec;
1428 /// let mut sv = StableVec::from(&['a', 'b', 'c']);
1429 /// assert!(sv.contains(&'b'));
1430 ///
1431 /// sv.remove(1); // 'b' is stored at index 1
1432 /// assert!(!sv.contains(&'b'));
1433 /// ```
1434 pub fn contains<U>(&self, item: &U) -> bool
1435 where
1436 U: PartialEq<T>,
1437 {
1438 self.values().any(|e| item == e)
1439 }
1440
1441 /// Swaps the slot at index `a` with the slot at index `b`.
1442 ///
1443 /// The full slots are swapped, including the element and the "filled"
1444 /// state. If you swap slots with an element in it, that element's index is
1445 /// invalidated, of course. This method automatically sets
1446 /// `next_push_index` to a larger value if that's necessary.
1447 ///
1448 /// # Panics
1449 ///
1450 /// This panics if `a` or `b` are not smaller than `self.capacity()`.
1451 ///
1452 /// # Example
1453 ///
1454 /// ```
1455 /// # use stable_vec::StableVec;
1456 /// let mut sv = StableVec::from(&['a', 'b', 'c', 'd']);
1457 /// sv.reserve_for(5);
1458 /// assert_eq!(sv.next_push_index(), 4);
1459 ///
1460 /// // Swapping an empty slot with a filled one
1461 /// sv.swap(0, 5);
1462 /// assert_eq!(sv.get(0), None);
1463 /// assert_eq!(sv.get(1), Some(&'b'));
1464 /// assert_eq!(sv.get(2), Some(&'c'));
1465 /// assert_eq!(sv.get(3), Some(&'d'));
1466 /// assert_eq!(sv.get(4), None);
1467 /// assert_eq!(sv.get(5), Some(&'a'));
1468 /// assert_eq!(sv.next_push_index(), 6);
1469 ///
1470 /// // Swapping two filled slots
1471 /// sv.swap(1, 2);
1472 /// assert_eq!(sv.get(0), None);
1473 /// assert_eq!(sv.get(1), Some(&'c'));
1474 /// assert_eq!(sv.get(2), Some(&'b'));
1475 /// assert_eq!(sv.get(3), Some(&'d'));
1476 /// assert_eq!(sv.get(4), None);
1477 /// assert_eq!(sv.get(5), Some(&'a'));
1478 ///
1479 /// // You can also swap two empty slots, but that doesn't change anything.
1480 /// sv.swap(0, 4);
1481 /// assert_eq!(sv.get(0), None);
1482 /// assert_eq!(sv.get(1), Some(&'c'));
1483 /// assert_eq!(sv.get(2), Some(&'b'));
1484 /// assert_eq!(sv.get(3), Some(&'d'));
1485 /// assert_eq!(sv.get(4), None);
1486 /// assert_eq!(sv.get(5), Some(&'a'));
1487 /// ```
1488 pub fn swap(&mut self, a: usize, b: usize) {
1489 assert!(a < self.core.cap());
1490 assert!(b < self.core.cap());
1491
1492 // Adjust the `len`
1493 let mut len = self.core.len();
1494 if a >= len && self.has_element_at(b) {
1495 len = a + 1;
1496 }
1497 if b >= len && self.has_element_at(a) {
1498 len = b + 1;
1499 }
1500
1501 // Both indices are less than `cap`. These indices + 1 are <= cap. And
1502 // all slots with indices > `len` are empty.
1503 unsafe { self.core.set_len(len) };
1504
1505 // With the asserts above we made sure the preconditions are met. The
1506 // maintain the core invariants, we increased the length.
1507 unsafe { self.core.swap(a, b) };
1508
1509 }
1510
1511 /// Retains only the elements specified by the given predicate.
1512 ///
1513 /// Each element `e` for which `should_be_kept(&e)` returns `false` is
1514 /// removed from the stable vector.
1515 ///
1516 /// # Example
1517 ///
1518 /// ```
1519 /// # use stable_vec::StableVec;
1520 /// let mut sv = StableVec::from(&[1, 2, 3, 4, 5]);
1521 /// sv.retain(|&e| e % 2 == 0);
1522 ///
1523 /// assert_eq!(sv, &[2, 4] as &[_]);
1524 /// ```
1525 pub fn retain<P>(&mut self, mut should_be_kept: P)
1526 where
1527 P: FnMut(&T) -> bool,
1528 {
1529 let mut pos = 0;
1530
1531 // These unsafe calls are fine: indices returned by
1532 // `first_filled_slot_from` are always valid and point to an existing
1533 // element.
1534 unsafe {
1535 while let Some(idx) = self.core.first_filled_slot_from(pos) {
1536 let elem = self.core.get_unchecked(idx);
1537 if !should_be_kept(elem) {
1538 self.num_elements -= 1;
1539 drop(self.core.remove_at(idx));
1540 }
1541
1542 pos = idx + 1;
1543 }
1544 }
1545 }
1546
1547 /// Retains only the elements with indices specified by the given
1548 /// predicate.
1549 ///
1550 /// Each element with index `i` for which `should_be_kept(i)` returns
1551 /// `false` is removed from the stable vector.
1552 ///
1553 /// # Example
1554 ///
1555 /// ```
1556 /// # use stable_vec::StableVec;
1557 /// let mut sv = StableVec::new();
1558 /// sv.push(1);
1559 /// let two = sv.push(2);
1560 /// sv.push(3);
1561 /// sv.retain_indices(|i| i == two);
1562 ///
1563 /// assert_eq!(sv, &[2] as &[_]);
1564 /// ```
1565 pub fn retain_indices<P>(&mut self, mut should_be_kept: P)
1566 where
1567 P: FnMut(usize) -> bool,
1568 {
1569 let mut pos = 0;
1570
1571 // These unsafe call is fine: indices returned by
1572 // `first_filled_slot_from` are always valid and point to an existing
1573 // element.
1574 unsafe {
1575 while let Some(idx) = self.core.first_filled_slot_from(pos) {
1576 if !should_be_kept(idx) {
1577 self.num_elements -= 1;
1578 drop(self.core.remove_at(idx));
1579 }
1580
1581 pos = idx + 1;
1582 }
1583 }
1584 }
1585
1586 /// Appends all elements in `new_elements` to this stable vector. This is
1587 /// equivalent to calling [`push()`][StableVecFacade::push] for each
1588 /// element.
1589 pub fn extend_from_slice(&mut self, new_elements: &[T])
1590 where
1591 T: Clone,
1592 {
1593 let len = new_elements.len();
1594
1595 self.reserve(len);
1596
1597 // It's important that a panic in `clone()` does not lead to memory
1598 // unsafety! The only way that could happen is if some uninitialized
1599 // values would be read when `out` is dropped. However, this won't
1600 // happen: the core won't ever drop uninitialized elements.
1601 //
1602 // So that's good. But we also would like to drop all elements that
1603 // have already been inserted. That's why we set the length first.
1604 //
1605 // For the same reason, `num_elements` is only increased after an
1606 // element was actually inserted. If `clone()` panics, all remaining
1607 // elements are never inserted, so counting them in advance would
1608 // leave `num_elements` larger than the number of filled slots. And a
1609 // lot of `unsafe` code relies on that number being exact (e.g. all
1610 // iterators use it as their number of remaining elements).
1611 unsafe {
1612 let mut i = self.core.len();
1613 let new_len = self.core.len() + len;
1614 self.core.set_len(new_len);
1615
1616 for elem in new_elements {
1617 self.core.insert_at(i, elem.clone());
1618 i += 1;
1619 self.num_elements += 1;
1620 }
1621 }
1622 }
1623}
1624
1625
1626#[inline(never)]
1627#[cold]
1628fn index_fail(idx: usize) -> ! {
1629 panic!("attempt to index StableVec with index {}, but no element exists at that index", idx);
1630}
1631
1632/// Called when `num_elements` is found to disagree with the actual number of
1633/// existing elements.
1634#[inline(never)]
1635#[cold]
1636fn inconsistent_num_elements() -> ! {
1637 panic!("bug in `stable_vec`: `num_elements` does not match the number of existing elements");
1638}
1639
1640impl<T, C: Core<T>> Index<usize> for StableVecFacade<T, C> {
1641 type Output = T;
1642
1643 fn index(&self, index: usize) -> &T {
1644 match self.get(index) {
1645 Some(v) => v,
1646 None => index_fail(index),
1647 }
1648 }
1649}
1650
1651impl<T, C: Core<T>> IndexMut<usize> for StableVecFacade<T, C> {
1652 fn index_mut(&mut self, index: usize) -> &mut T {
1653 match self.get_mut(index) {
1654 Some(v) => v,
1655 None => index_fail(index),
1656 }
1657 }
1658}
1659
1660impl<T, C: Core<T>> Default for StableVecFacade<T, C> {
1661 fn default() -> Self {
1662 Self::new()
1663 }
1664}
1665
1666impl<T, S, C: Core<T>> From<S> for StableVecFacade<T, C>
1667where
1668 S: AsRef<[T]>,
1669 T: Clone,
1670{
1671 fn from(slice: S) -> Self {
1672 let mut out = Self::new();
1673 out.extend_from_slice(slice.as_ref());
1674 out
1675 }
1676}
1677
1678impl<T, C: Core<T>> FromIterator<T> for StableVecFacade<T, C> {
1679 fn from_iter<I>(iter: I) -> Self
1680 where
1681 I: IntoIterator<Item = T>,
1682 {
1683 let mut out = Self::new();
1684 out.extend(iter);
1685 out
1686 }
1687}
1688
1689impl<T, C: Core<T>> Extend<T> for StableVecFacade<T, C> {
1690 fn extend<I>(&mut self, iter: I)
1691 where
1692 I: IntoIterator<Item = T>,
1693 {
1694 let it = iter.into_iter();
1695 self.reserve(it.size_hint().0);
1696
1697 for elem in it {
1698 self.push(elem);
1699 }
1700 }
1701}
1702
1703impl<'a, T, C: Core<T>> IntoIterator for &'a StableVecFacade<T, C> {
1704 type Item = (usize, &'a T);
1705 type IntoIter = Iter<'a, T, C>;
1706 fn into_iter(self) -> Self::IntoIter {
1707 self.iter()
1708 }
1709}
1710
1711impl<'a, T, C: Core<T>> IntoIterator for &'a mut StableVecFacade<T, C> {
1712 type Item = (usize, &'a mut T);
1713 type IntoIter = IterMut<'a, T, C>;
1714 fn into_iter(self) -> Self::IntoIter {
1715 self.iter_mut()
1716 }
1717}
1718
1719impl<T, C: Core<T>> IntoIterator for StableVecFacade<T, C> {
1720 type Item = (usize, T);
1721 type IntoIter = IntoIter<T, C>;
1722 fn into_iter(self) -> Self::IntoIter {
1723 IntoIter::new(self)
1724 }
1725}
1726
1727impl<T: fmt::Debug, C: Core<T>> fmt::Debug for StableVecFacade<T, C> {
1728 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1729 write!(f, "StableVec ")?;
1730 f.debug_list().entries(self.values()).finish()
1731 }
1732}
1733
1734impl<Ta, Tb, Ca, Cb> PartialEq<StableVecFacade<Tb, Cb>> for StableVecFacade<Ta, Ca>
1735where
1736 Ta: PartialEq<Tb>,
1737 Ca: Core<Ta>,
1738 Cb: Core<Tb>,
1739{
1740 fn eq(&self, other: &StableVecFacade<Tb, Cb>) -> bool {
1741 self.num_elements() == other.num_elements()
1742 && self.capacity() == other.capacity()
1743 && self.next_push_index() == other.next_push_index()
1744 && (0..self.next_push_index()).all(|idx| {
1745 match (self.get(idx), other.get(idx)) {
1746 (None, None) => true,
1747 (Some(a), Some(b)) => a == b,
1748 _ => false,
1749 }
1750 })
1751 }
1752}
1753
1754impl<T: Eq, C: Core<T>> Eq for StableVecFacade<T, C> {}
1755
1756impl<A, B, C: Core<A>> PartialEq<[B]> for StableVecFacade<A, C>
1757where
1758 A: PartialEq<B>,
1759{
1760 fn eq(&self, other: &[B]) -> bool {
1761 self.values().eq(other)
1762 }
1763}
1764
1765impl<'other, A, B, C: Core<A>> PartialEq<&'other [B]> for StableVecFacade<A, C>
1766where
1767 A: PartialEq<B>,
1768{
1769 fn eq(&self, other: &&'other [B]) -> bool {
1770 self == *other
1771 }
1772}
1773
1774impl<A, B, C: Core<A>> PartialEq<Vec<B>> for StableVecFacade<A, C>
1775where
1776 A: PartialEq<B>,
1777{
1778 fn eq(&self, other: &Vec<B>) -> bool {
1779 self == &other[..]
1780 }
1781}