use crate::{hashmap::HashMap, Value};
use core::hash::{BuildHasher, Hash};
#[cfg(feature = "stable_alloc")]
use allocator_api2::alloc::Allocator;
#[cfg(not(feature = "stable_alloc"))]
use std::alloc::Allocator;
pub struct OwnedIter<K, V, H: BuildHasher, A: Allocator> {
map: HashMap<K, V, H, A>,
current: usize,
}
impl<K, V, H, A> OwnedIter<K, V, H, A>
where
K: Eq + Hash + Clone,
V: Value,
H: BuildHasher,
A: Allocator,
{
pub(crate) fn new(map: HashMap<K, V, H, A>) -> Self {
Self {
map,
current: 0usize,
}
}
}
impl<K, V, H, A> Iterator for OwnedIter<K, V, H, A>
where
K: Eq + Hash + Clone,
V: Value,
H: BuildHasher + Default,
A: Allocator,
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
loop {
let current = self.current;
if let Some(cell) = self.map.get_cell_at_index(current) {
self.current = current + 1;
if cell.is_empty() || cell.value.is_null() {
continue;
}
return Some((cell.key.clone(), cell.value));
} else {
return None;
}
}
}
}
unsafe impl<K, V, H, A> Send for OwnedIter<K, V, H, A>
where
K: Send,
V: Send,
H: BuildHasher + Send,
A: Allocator + Send,
{
}
unsafe impl<K, V, H, A> Sync for OwnedIter<K, V, H, A>
where
K: Sync,
V: Sync,
H: BuildHasher + Sync,
A: Allocator + Sync,
{
}
pub struct Iter<'a, K, V, H: BuildHasher, A: Allocator> {
map: &'a HashMap<K, V, H, A>,
current: usize,
}
impl<'a, K, V, H, A> Clone for Iter<'a, K, V, H, A>
where
K: Eq + Hash + Clone,
V: Value,
H: BuildHasher + Default,
A: Allocator,
{
fn clone(&self) -> Self {
Iter::new(self.map)
}
}
impl<'a, K, V, H, A> Iter<'a, K, V, H, A>
where
K: Eq + Hash + Clone,
V: Value,
H: BuildHasher + 'a,
A: Allocator + 'a,
{
pub(crate) fn new(map: &'a HashMap<K, V, H, A>) -> Self {
Self {
map,
current: 0usize,
}
}
}
impl<'a, K, V, H, A> Iterator for Iter<'a, K, V, H, A>
where
K: Eq + Hash + Clone,
V: Value,
H: BuildHasher + Default + 'a,
A: Allocator + 'a,
{
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
loop {
let current = self.current;
if let Some(cell) = self.map.get_cell_at_index(current) {
self.current = current + 1;
if cell.is_empty() || cell.value.is_null() {
continue;
}
return Some((&cell.key, &cell.value));
} else {
return None;
}
}
}
}
unsafe impl<'a, K, V, H, A> Send for Iter<'a, K, V, H, A>
where
K: Send,
V: Send,
H: BuildHasher + Send + 'a,
A: Allocator + Send + 'a,
{
}
unsafe impl<'a, K, V, H, A> Sync for Iter<'a, K, V, H, A>
where
K: Sync,
V: Sync,
H: BuildHasher + Sync + 'a,
A: Allocator + Sync + 'a,
{
}
pub struct IterMut<'a, K, V, H: BuildHasher, A: Allocator> {
map: &'a mut HashMap<K, V, H, A>,
current: usize,
}
impl<'a, K, V, H, A> IterMut<'a, K, V, H, A>
where
K: Eq + Hash + Clone,
V: Value,
H: BuildHasher + 'a,
A: Allocator + 'a,
{
pub(crate) fn new(map: &'a mut HashMap<K, V, H, A>) -> Self {
Self {
map,
current: 0usize,
}
}
}
impl<'a, K, V, H, A> Iterator for IterMut<'a, K, V, H, A>
where
K: Eq + Hash + Clone,
V: Value,
H: BuildHasher + Default + 'a,
A: Allocator + 'a,
{
type Item = (&'a K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
loop {
let current = self.current;
if let Some(cell) = self.map.get_cell_at_index_mut(current) {
self.current = current + 1;
if cell.is_empty() || cell.value.is_null() {
continue;
}
return Some((&cell.key, &mut cell.value));
} else {
return None;
}
}
}
}
unsafe impl<'a, K, V, H, A> Send for IterMut<'a, K, V, H, A>
where
K: Send,
V: Send,
H: BuildHasher + Send + 'a,
A: Allocator + Send + 'a,
{
}
unsafe impl<'a, K, V, H, A> Sync for IterMut<'a, K, V, H, A>
where
K: Sync,
V: Sync,
H: BuildHasher + Sync + 'a,
A: Allocator + Sync + 'a,
{
}