Returns the number of elements that can be hold without
allocating new memory.
use enum_vec::vec_u128::EnumVec;
let ev = EnumVec::<bool>::with_capacity(53);
assert!(ev.capacity() >= 53);
Reserves capacity for at least additional
more elements.
use enum_vec::vec_u128::EnumVec;
let mut ev: EnumVec<Option<()>> = vec![None, None, None].into();
ev.reserve(100);
assert!(ev.capacity() >= 100 + 3);
let mut a: EnumVec<bool> = EnumVec::new();
a.reserve(1);
assert_eq!(a.len(), 0);
assert!(ev.storage().len() > 0);
Shrinks the capacity as much as possible.
Remove an element from an arbitrary position in O(1) time,
but without preserving the ordering.
This is accomplished by swapping the desired element with
the last element, and then calling pop()
.
use enum_vec::vec_u128::EnumVec;
let mut ev: EnumVec<bool> = vec![true, true, true, false, false].into();
ev.swap_remove(0);
assert_eq!(&ev.to_vec(), &[false, true, true, false]);
ev.swap_remove(1);
assert_eq!(&ev.to_vec(), &[false, false, true]);
Insert an element into an arbitrary position. This operation is very
expensive, as it must shift all the elements to make space for the new
one. Prefer using push()
.
use enum_vec::vec_u128::EnumVec;
let mut ev = EnumVec::new();
ev.insert(0, true);
assert_eq!(ev.to_vec(), vec![true]);
ev.insert(0, false);
assert_eq!(ev.to_vec(), vec![false, true]);
ev.insert(1, true);
assert_eq!(ev.to_vec(), vec![false, true, true]);
ev.insert(1, false);
assert_eq!(ev.to_vec(), vec![false, false, true, true]);
let mut ev: EnumVec<_> = vec![false; 127].into();
assert_eq!(ev.len(), 127);
ev.push(true);
assert_eq!(ev.len(), 127 + 1);
ev.push(true);
assert_eq!(ev.len(), 127 + 2);
ev.insert(0, true);
ev.insert(0, true);
assert_eq!((ev.get(0).unwrap(), ev.get(1).unwrap()), (true, true));
assert_eq!(ev.len(), 127 + 4);
Remove an element from an arbitrary position. This operation is very
expensive, as it must shift all the elements to fill the hole.
When preserving the order is not important, consider using
swap_remove()
.
use enum_vec::vec_u128::EnumVec;
let mut ev: EnumVec<_> = vec![None; 129].into();
let item = Some([true, false, true, false]);
ev.insert(0, item);
assert_eq!(ev.remove(0), item);
ev.insert(127, item);
assert_eq!(ev.remove(127), item);
ev.insert(127, item);
ev.insert(128, item);
assert_eq!(ev.remove(127), item);
assert_eq!(ev.remove(127), item);
assert_eq!(ev.remove(127), None);
assert_eq!(ev.remove(127), None);
assert_eq!(ev.len(), 127);
Retains only the elements specified by the predicate
use enum_vec::vec_u128::EnumVec;
let mut v: EnumVec<(bool, bool)> = vec![(true, true), (false, false), (true, false),
(false, true)].into();
v.retain(|x| x.0 == true);
let a = v.to_vec();
assert_eq!(&a, &[(true, true), (true, false)]);
Push an element to the end of the vector.
use enum_vec::vec_u128::EnumVec;
let mut ev: EnumVec<_> = vec![false; 127].into();
ev.push(true);
Appends all the elements from other
into self
, leaving
other
empty.
This can be more efficient than using extend
when the internal
storage is block-aligned. For example when each element is 4-bits wide
and the storage is a u32, it is block-aligned when it has 8*k elements.
Also, it doesn't have to map all the elements from T
to discr.
use enum_vec::vec_u128::EnumVec;
let mut a: EnumVec<_> = vec![None, None, Some(())].into();
let mut b = a.clone();
b.push(None);
a.append(&mut b);
assert_eq!(a.get(6), Some(None));
assert_eq!(a.len(), 3+4);
assert_eq!(b.len(), 0);
Sets the length to zero, removing all the elements.
use enum_vec::vec_u128::EnumVec;
let mut ev = EnumVec::new();
ev.push(Some(false));
assert_eq!(ev.len(), 1);
ev.clear();
assert_eq!(ev.len(), 0);
assert!(ev.is_empty());
unsafe {
ev.set_len(1);
assert_eq!(ev.pop().unwrap(), Some(false));
}
Returns the length of the vector, the number of elements it holds.
Get the raw discriminant without bounds checking
Set the raw discriminant without bounds checking. It is assumed that
the discriminant is lower than T::NUM_ELEMENTS
.
Apply a function to each element in place, this is a substitute to
for loops:
use enum_vec::vec_u128::EnumVec;
let mut v = vec![true, false, true];
for x in v.iter_mut() {
*x = !*x;
}
let mut ev: EnumVec<_> = vec![true, false, true].into();
ev.for_each(|x| {
*x = !*x;
});
assert_eq!(v, ev.to_vec());
assert_eq!(&v, &[false, true, false]);
Copies self
into a plain Vec
.
use enum_vec::vec_u128::EnumVec;
let mut ev = EnumVec::new();
ev.push(true);
ev.push(false);
let v = vec![true, false];
assert_eq!(ev.to_vec(), v);
#[macro_use] extern crate enum_vec;
use enum_vec::vec_u128::EnumVec;
let ev = enum_vec![true, false, false, true];
assert_eq!(ev.len(), 4);
let mut a = enum_vec![false; 8];
a.extend(ev);
Access the internal storage
use enum_vec::vec_u128::EnumVec;
let ev: EnumVec<_> = vec![true; 100].into();
assert_eq!(ev.len(), 100);
assert!(ev.storage().len() > 0);
Access and modify the internal storage.
This function is unsafe because shrinking the storage
may lead to reading and writing uninitialized memory.
extern crate enum_like;
extern crate enum_vec;
use enum_like::EnumLike;
use enum_vec::vec_u128::EnumVec;
let mut ev: EnumVec<_> = vec![true; 100].into();
assert_eq!(ev.len(), 100);
assert!(ev.storage().len() > 0);
assert_eq!(ev.get(0).unwrap(), true);
unsafe {
let s = ev.storage_mut();
assert_eq!(false.to_discr(), 0);
s[0] = 0;
}
assert_eq!(ev.get(0).unwrap(), false);
Check whether any of the elements is equal to x
.
This method uses arcane bithack magic to test many elements at once.
extern crate enum_like;
extern crate enum_vec;
use enum_like::EnumLike;
use enum_vec::vec_u128::EnumVec;
let mut ev: EnumVec<_> = vec![true; 200].into();
assert_eq!(ev.any(true), true);
assert_eq!(ev.any(false), false);
ev.push(true);
assert_eq!(ev.any(true), true);
assert_eq!(ev.any(false), false);
ev.set(0, false);
assert_eq!(ev.any(false), true);
let mut ev: EnumVec<_> = vec![None; 200].into();
assert_eq!(ev.any(Some(false)), false);
assert_eq!(ev.any(Some(true)), false);
assert_eq!(ev.any(None), true);
ev.set(1, Some(false));
assert_eq!(ev.any(Some(false)), true);
assert_eq!(ev.any(Some(true)), false);
assert_eq!(ev.any(None), true);
ev.set(2, Some(true));
assert_eq!(ev.any(Some(false)), true);
assert_eq!(ev.any(Some(true)), true);
assert_eq!(ev.any(None), true);
Check whether all of the elements are equal to x
.
This method uses arcane bithack magic to test many elements at once.
extern crate enum_like;
extern crate enum_vec;
use enum_like::EnumLike;
use enum_vec::vec_u128::EnumVec;
let mut ev: EnumVec<_> = vec![true; 200].into();
assert_eq!(ev.all(true), true);
assert_eq!(ev.all(false), false);
ev.push(true);
assert_eq!(ev.all(true), true);
assert_eq!(ev.all(false), false);
ev.set(0, false);
assert_eq!(ev.all(true), false);
assert_eq!(ev.all(false), false);
let mut ev: EnumVec<_> = vec![None; 200].into();
assert_eq!(ev.all(Some(false)), false);
assert_eq!(ev.all(Some(true)), false);
assert_eq!(ev.all(None), true);
ev.set(1, Some(false));
assert_eq!(ev.all(Some(false)), false);
assert_eq!(ev.all(Some(true)), false);
assert_eq!(ev.all(None), false);