use crate::{IntoIter, IntoValues, Iter, Keys, Map, Values};
use std::marker::PhantomData;
use std::ptr;
impl<V: Clone> Map<V> {
#[inline]
#[must_use]
pub const fn iter(&self) -> Iter<V> {
#[cfg(debug_assertions)]
assert!(self.initialized, "Can't iter() non-initialized Map");
Iter {
max: self.max,
pos: 0,
head: self.head,
_marker: PhantomData,
}
}
#[inline]
#[must_use]
pub const fn into_iter(&self) -> IntoIter<V> {
#[cfg(debug_assertions)]
assert!(self.initialized, "Can't into_iter() non-initialized Map");
IntoIter {
max: self.max,
pos: 0,
head: self.head,
}
}
#[inline]
#[must_use]
pub const fn values(&self) -> Values<V> {
#[cfg(debug_assertions)]
assert!(self.initialized, "Can't values() non-initialized Map");
Values {
max: self.max,
pos: 0,
head: self.head,
_marker: PhantomData,
}
}
#[inline]
#[must_use]
pub const fn into_values(&self) -> IntoValues<V> {
#[cfg(debug_assertions)]
assert!(self.initialized, "Can't into_values() non-initialized Map");
IntoValues {
max: self.max,
pos: 0,
head: self.head,
}
}
#[inline]
#[must_use]
pub const fn keys(&self) -> Keys<V> {
#[cfg(debug_assertions)]
assert!(self.initialized, "Can't keys() non-initialized Map");
Keys {
max: self.max,
pos: 0,
head: self.head,
}
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
#[cfg(debug_assertions)]
assert!(self.initialized, "Can't do len() on non-initialized Map");
let mut busy = 0;
for i in 0..self.max {
if self.get(i).is_some() {
busy += 1;
}
}
busy
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn contains_key(&self, k: usize) -> bool {
#[cfg(debug_assertions)]
assert!(k < self.capacity(), "Over the boundary");
matches!(unsafe { &*self.head.add(k) }, Some(_))
}
#[inline]
pub fn remove(&mut self, k: usize) {
#[cfg(debug_assertions)]
assert!(k < self.capacity(), "Over the boundary");
unsafe {
ptr::write(self.head.add(k), None);
}
}
#[inline]
pub fn push(&mut self, v: V) -> usize {
let k = self.next_key();
self.insert(k, v);
k
}
#[inline]
pub fn insert(&mut self, k: usize, v: V) {
#[cfg(debug_assertions)]
assert!(k < self.capacity(), "Over the boundary");
unsafe {
ptr::write(self.head.add(k), Some(v));
}
if self.max <= k {
self.max = k + 1;
}
}
#[inline]
#[must_use]
pub fn get(&self, k: usize) -> Option<&V> {
#[cfg(debug_assertions)]
assert!(k < self.capacity(), "Over the boundary");
unsafe { &*self.head.add(k) }.as_ref()
}
#[inline]
#[must_use]
pub fn get_mut(&mut self, k: usize) -> Option<&mut V> {
#[cfg(debug_assertions)]
assert!(k < self.capacity(), "Over the boundary");
unsafe { &mut *(self.head.add(k)) }.as_mut()
}
#[inline]
pub fn clear(&mut self) {
self.max = 0;
}
#[inline]
pub fn retain<F: Fn(&usize, &V) -> bool>(&mut self, f: F) {
#[cfg(debug_assertions)]
assert!(self.initialized, "Can't do retain() on non-initialized Map");
for i in 0..self.max {
if let Some(p) = self.get_mut(i) {
if !f(&i, p) {
unsafe {
ptr::write(self.head.add(i), None);
}
}
}
}
}
}
#[test]
fn insert_and_check_length() {
let mut m: Map<&str> = Map::with_capacity_none(16);
m.insert(0, "zero");
assert_eq!(1, m.len());
m.insert(1, "first");
assert_eq!(2, m.len());
m.insert(1, "first");
assert_eq!(2, m.len());
}
#[test]
fn empty_length() {
let m: Map<u32> = Map::with_capacity_none(16);
assert_eq!(0, m.len());
}
#[test]
fn is_empty_check() {
let mut m: Map<u32> = Map::with_capacity_none(16);
assert!(m.is_empty());
m.insert(0, 42);
assert!(!m.is_empty());
}
#[test]
fn insert_and_gets() {
let mut m: Map<&str> = Map::with_capacity_none(16);
m.insert(0, "zero");
m.insert(1, "one");
assert_eq!("one", *m.get(1).unwrap());
}
#[test]
fn insert_and_gets_mut() {
let mut m: Map<[i32; 3]> = Map::with_capacity_none(16);
m.insert(0, [1, 2, 3]);
let a = m.get_mut(0).unwrap();
a[0] = 500;
assert_eq!(500, m.get(0).unwrap()[0]);
}
#[test]
fn checks_key() {
let mut m: Map<&str> = Map::with_capacity_none(16);
m.insert(0, "one");
assert!(m.contains_key(0));
m.insert(8, "");
m.remove(8);
assert!(!m.contains_key(8));
}
#[test]
fn gets_missing_key() {
let mut m: Map<&str> = Map::with_capacity_none(16);
m.insert(0, "one");
m.insert(1, "one");
m.remove(1);
assert!(m.get(1).is_none());
}
#[test]
fn mut_gets_missing_key() {
let mut m: Map<&str> = Map::with_capacity_none(16);
m.insert(0, "one");
m.insert(1, "one");
m.remove(1);
assert!(m.get_mut(1).is_none());
}
#[test]
fn removes_simple_pair() {
let mut m: Map<&str> = Map::with_capacity_none(16);
m.insert(0, "one");
m.remove(0);
m.remove(1);
assert!(m.get(0).is_none());
}
#[cfg(test)]
#[derive(Clone, Copy)]
struct Foo {
v: [u32; 3],
}
#[test]
fn insert_struct() {
let mut m: Map<Foo> = Map::with_capacity_none(16);
let foo = Foo { v: [1, 2, 100] };
m.insert(0, foo);
assert_eq!(100, m.into_iter().next().unwrap().1.v[2]);
}
#[test]
fn large_map_in_heap() {
let m: Box<Map<[u64; 10]>> = Box::new(Map::with_capacity_none(16));
assert_eq!(0, m.len());
}
#[test]
fn clears_it_up() {
let mut m: Map<&str> = Map::with_capacity_none(16);
m.insert(7, "one");
m.clear();
assert_eq!(0, m.len());
}
#[test]
fn pushes_into() {
let mut m: Map<&str> = Map::with_capacity_none(16);
assert_eq!(0, m.push("one"));
assert_eq!(1, m.push("two"));
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn insert_out_of_boundary() {
let mut m: Map<&str> = Map::with_capacity(1);
m.insert(5, "one");
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn get_out_of_boundary() {
let m: Map<&str> = Map::with_capacity(1);
m.get(5).unwrap();
}
#[test]
#[should_panic]
#[cfg(debug_assertions)]
fn remove_out_of_boundary() {
let mut m: Map<&str> = Map::with_capacity(1);
m.remove(5);
}