Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 14, 2024
2 parents 615ece1 + 4ecad7f commit 4c419e1
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 143 deletions.
54 changes: 54 additions & 0 deletions src/drain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::Drain;
use core::iter::FusedIterator;

impl<'a, K, V> Drop for Drain<'a, K, V> {
fn drop(&mut self) {
for pair in &mut self.iter {
unsafe { pair.assume_init_drop() };
}
}
}

impl<'a, K: PartialEq, V> Iterator for Drain<'a, K, V> {
type Item = (K, V);

#[inline]
#[must_use]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|p| unsafe { p.assume_init_read() })
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.iter.len(), Some(self.iter.len()))
}
}

impl<'a, K: PartialEq, V> ExactSizeIterator for Drain<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.iter.len()
}
}

impl<'a, K: PartialEq, V> FusedIterator for Drain<'a, K, V> {}
128 changes: 128 additions & 0 deletions src/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (c) 2024 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::{Entry, OccupiedEntry, VacantEntry};
use core::mem;

impl<'a, K: PartialEq, V, const N: usize> Entry<'a, K, V, N> {
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default),
}
}

pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default()),
}
}

pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let value = default(entry.key());
entry.insert(value)
}
}
}

pub fn key(&self) -> &K {
match self {
Entry::Occupied(entry) => entry.key(),
Entry::Vacant(entry) => entry.key(),
}
}

#[must_use]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Entry::Occupied(mut entry) => {
f(entry.get_mut());
Entry::Occupied(entry)
}
Entry::Vacant(entry) => Entry::Vacant(entry),
}
}
}

impl<'a, K: PartialEq, V: Default, const N: usize> Entry<'a, K, V, N> {
pub fn or_default(self) -> &'a mut V {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(V::default()),
}
}
}

impl<'a, K: PartialEq, V, const N: usize> OccupiedEntry<'a, K, V, N> {
#[must_use]
pub fn key(&self) -> &K {
&self.table.item_ref(self.index).0
}

#[must_use]
pub fn remove_entry(self) -> (K, V) {
self.table.remove_index_read(self.index)
}

#[must_use]
pub fn get(&self) -> &V {
&self.table.item_ref(self.index).1
}

pub fn get_mut(&mut self) -> &mut V {
self.table.item_mut(self.index)
}

#[must_use]
pub fn into_mut(self) -> &'a mut V {
self.table.item_mut(self.index)
}

pub fn insert(&mut self, value: V) -> V {
mem::replace(self.get_mut(), value)
}

#[must_use]
pub fn remove(self) -> V {
self.table.remove_index_read(self.index).1
}
}

impl<'a, K: PartialEq, V, const N: usize> VacantEntry<'a, K, V, N> {
pub const fn key(&self) -> &K {
&self.key
}

pub fn into_key(self) -> K {
self.key
}

pub fn insert(self, value: V) -> &'a mut V {
let (index, _) = self.table.insert_i(self.key, value);
self.table.item_mut(index)
}
}
69 changes: 20 additions & 49 deletions src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use crate::{IntoIter, Iter, IterMut, Map};
use core::iter::FusedIterator;
use core::mem::ManuallyDrop;

impl<K: PartialEq, V, const N: usize> Map<K, V, N> {
/// Make an iterator over all pairs.
Expand Down Expand Up @@ -48,6 +47,11 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
(&p.0, &p.1)
})
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}

impl<'a, K, V> Iterator for IterMut<'a, K, V> {
Expand All @@ -60,6 +64,11 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
(&p.0, &mut p.1)
})
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}

impl<K: PartialEq, V, const N: usize> Iterator for IntoIter<K, V, N> {
Expand All @@ -68,14 +77,18 @@ impl<K: PartialEq, V, const N: usize> Iterator for IntoIter<K, V, N> {
#[inline]
#[must_use]
fn next(&mut self) -> Option<Self::Item> {
if self.pos < self.map.len {
let p = self.map.item_read(self.pos);
self.pos += 1;
Some(p)
if self.map.len > 0 {
self.map.len -= 1;
Some(self.map.item_read(self.map.len))
} else {
None
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.map.len, Some(self.map.len))
}
}

impl<'a, K: PartialEq, V, const N: usize> IntoIterator for &'a Map<K, V, N> {
Expand Down Expand Up @@ -110,49 +123,7 @@ impl<K: PartialEq, V, const N: usize> IntoIterator for Map<K, V, N> {
#[inline]
#[must_use]
fn into_iter(self) -> Self::IntoIter {
IntoIter {
pos: 0,
map: ManuallyDrop::new(self),
}
}
}

impl<K: PartialEq, V, const N: usize> Drop for IntoIter<K, V, N> {
fn drop(&mut self) {
for i in self.pos..self.map.len {
self.map.item_drop(i);
}
}
}

impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|p| {
let p = unsafe { p.assume_init_ref() };
(&p.0, &p.1)
})
}
}

impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|p| {
let p = unsafe { p.assume_init_mut() };
(&p.0, &mut p.1)
})
}
}

impl<K: PartialEq, V, const N: usize> DoubleEndedIterator for IntoIter<K, V, N> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.pos < self.map.len {
self.map.len -= 1;
let i = self.map.len;
let p = self.map.item_read(i);
Some(p)
} else {
None
}
IntoIter { map: self }
}
}

Expand All @@ -170,7 +141,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {

impl<K: PartialEq, V, const N: usize> ExactSizeIterator for IntoIter<K, V, N> {
fn len(&self) -> usize {
self.map.len - self.pos
self.map.len
}
}

Expand Down
20 changes: 9 additions & 11 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|p| p.0)
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}

impl<K: PartialEq, V, const N: usize> Iterator for IntoKeys<K, V, N> {
Expand All @@ -53,17 +58,10 @@ impl<K: PartialEq, V, const N: usize> Iterator for IntoKeys<K, V, N> {
fn next(&mut self) -> Option<K> {
self.iter.next().map(|p| p.0)
}
}

impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|p| p.0)
}
}

impl<K: PartialEq, V, const N: usize> DoubleEndedIterator for IntoKeys<K, V, N> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|p| p.0)
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}

Expand Down Expand Up @@ -103,7 +101,7 @@ mod test {
m.insert("bar".to_string(), 0);
assert_eq!(
m.into_keys().collect::<Vec<_>>(),
["foo".to_string(), "bar".to_string()]
["bar".to_string(), "foo".to_string()]
);
}
}
Loading

0 comments on commit 4c419e1

Please sign in to comment.