Skip to content

Commit

Permalink
Move some slice methods for better doc order
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed May 27, 2021
1 parent 6004ebd commit a6a98df
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 73 deletions.
54 changes: 53 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::collections::hash_map::RandomState;

use self::core::IndexMapCore;
use crate::equivalent::Equivalent;
use crate::util::third;
use crate::util::{third, try_simplify_range};
use crate::{Bucket, Entries, HashValue};

pub use self::core::{Entry, OccupiedEntry, VacantEntry};
Expand Down Expand Up @@ -703,6 +703,20 @@ where
}

impl<K, V, S> IndexMap<K, V, S> {
/// Returns a slice of all the key-value pairs in the map.
///
/// Computes in **O(1)** time.
pub fn as_slice(&self) -> &Slice<K, V> {
Slice::from_slice(self.as_entries())
}

/// Returns a mutable slice of all the key-value pairs in the map.
///
/// Computes in **O(1)** time.
pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
Slice::from_mut_slice(self.as_entries_mut())
}

/// Get a key-value pair by index
///
/// Valid indices are *0 <= index < self.len()*
Expand All @@ -721,6 +735,28 @@ impl<K, V, S> IndexMap<K, V, S> {
self.as_entries_mut().get_mut(index).map(Bucket::muts)
}

/// Returns a slice of key-value pairs in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
///
/// Computes in **O(1)** time.
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<K, V>> {
let entries = self.as_entries();
let range = try_simplify_range(range, entries.len())?;
entries.get(range).map(Slice::from_slice)
}

/// Returns a mutable slice of key-value pairs in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
///
/// Computes in **O(1)** time.
pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<K, V>> {
let entries = self.as_entries_mut();
let range = try_simplify_range(range, entries.len())?;
entries.get_mut(range).map(Slice::from_mut_slice)
}

/// Get the first key-value pair
///
/// Computes in **O(1)** time.
Expand Down Expand Up @@ -911,6 +947,13 @@ pub struct Iter<'a, K, V> {
iter: SliceIter<'a, Bucket<K, V>>,
}

impl<'a, K, V> Iter<'a, K, V> {
/// Returns a slice of the remaining entries in the iterator.
pub fn as_slice(&self) -> &'a Slice<K, V> {
Slice::from_slice(self.iter.as_slice())
}
}

impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);

Expand Down Expand Up @@ -955,6 +998,15 @@ pub struct IterMut<'a, K, V> {
iter: SliceIterMut<'a, Bucket<K, V>>,
}

impl<'a, K, V> IterMut<'a, K, V> {
/// Returns a slice of the remaining entries in the iterator.
///
/// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
pub fn into_slice(self) -> &'a mut Slice<K, V> {
Slice::from_mut_slice(self.iter.into_slice())
}
}

impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);

Expand Down
50 changes: 2 additions & 48 deletions src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,19 @@ pub struct Slice<K, V> {

#[allow(unsafe_code)]
impl<K, V> Slice<K, V> {
fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
pub(super) fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
// SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
// and the lifetimes are bound together by this function's signature.
unsafe { &*(entries as *const [Bucket<K, V>] as *const Self) }
}

fn from_mut_slice(entries: &mut [Bucket<K, V>]) -> &mut Self {
pub(super) fn from_mut_slice(entries: &mut [Bucket<K, V>]) -> &mut Self {
// SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
// and the lifetimes are bound together by this function's signature.
unsafe { &mut *(entries as *mut [Bucket<K, V>] as *mut Self) }
}
}

impl<K, V, S> IndexMap<K, V, S> {
/// Returns a slice of all the entries in the map.
pub fn as_slice(&self) -> &Slice<K, V> {
Slice::from_slice(self.as_entries())
}

/// Returns a mutable slice of all the entries in the map.
pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
Slice::from_mut_slice(self.as_entries_mut())
}

/// Returns a slice of entries in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<K, V>> {
let entries = self.as_entries();
let range = try_simplify_range(range, entries.len())?;
entries.get(range).map(Slice::from_slice)
}

/// Returns a mutable slice of entries in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<K, V>> {
let entries = self.as_entries_mut();
let range = try_simplify_range(range, entries.len())?;
entries.get_mut(range).map(Slice::from_mut_slice)
}
}

impl<'a, K, V> Iter<'a, K, V> {
/// Returns a slice of the remaining entries in the iterator.
pub fn as_slice(&self) -> &'a Slice<K, V> {
Slice::from_slice(self.iter.as_slice())
}
}

impl<'a, K, V> IterMut<'a, K, V> {
/// Returns a slice of the remaining entries in the iterator.
///
/// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
pub fn into_slice(self) -> &'a mut Slice<K, V> {
Slice::from_mut_slice(self.iter.into_slice())
}
}

impl<K, V> Slice<K, V> {
/// Return the number of key-value pairs in the map slice.
#[inline]
Expand Down
26 changes: 26 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use crate::rayon::set as rayon;
#[cfg(has_std)]
use std::collections::hash_map::RandomState;

use crate::util::try_simplify_range;
use crate::vec::{self, Vec};
use core::cmp::Ordering;
use core::fmt;
Expand Down Expand Up @@ -597,6 +598,13 @@ where
}

impl<T, S> IndexSet<T, S> {
/// Returns a slice of all the values in the set.
///
/// Computes in **O(1)** time.
pub fn as_slice(&self) -> &Slice<T> {
Slice::from_slice(self.as_entries())
}

/// Get a value by index
///
/// Valid indices are *0 <= index < self.len()*
Expand All @@ -606,6 +614,17 @@ impl<T, S> IndexSet<T, S> {
self.as_entries().get(index).map(Bucket::key_ref)
}

/// Returns a slice of values in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
///
/// Computes in **O(1)** time.
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<T>> {
let entries = self.as_entries();
let range = try_simplify_range(range, entries.len())?;
entries.get(range).map(Slice::from_slice)
}

/// Get the first value
///
/// Computes in **O(1)** time.
Expand Down Expand Up @@ -741,6 +760,13 @@ pub struct Iter<'a, T> {
iter: SliceIter<'a, Bucket<T>>,
}

impl<'a, T> Iter<'a, T> {
/// Returns a slice of the remaining entries in the iterator.
pub fn as_slice(&self) -> &'a Slice<T> {
Slice::from_slice(self.iter.as_slice())
}
}

impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

Expand Down
25 changes: 1 addition & 24 deletions src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,13 @@ pub struct Slice<T> {

#[allow(unsafe_code)]
impl<T> Slice<T> {
fn from_slice(entries: &[Bucket<T>]) -> &Self {
pub(super) fn from_slice(entries: &[Bucket<T>]) -> &Self {
// SAFETY: `Slice<T>` is a transparent wrapper around `[Bucket<T>]`,
// and the lifetimes are bound together by this function's signature.
unsafe { &*(entries as *const [Bucket<T>] as *const Self) }
}
}

impl<T, S> IndexSet<T, S> {
/// Returns a slice of all the values in the set.
pub fn as_slice(&self) -> &Slice<T> {
Slice::from_slice(self.as_entries())
}

/// Returns a slice of values in the given range of indices.
///
/// Valid indices are *0 <= index < self.len()*
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<T>> {
let entries = self.as_entries();
let range = try_simplify_range(range, entries.len())?;
entries.get(range).map(Slice::from_slice)
}
}

impl<'a, T> Iter<'a, T> {
/// Returns a slice of the remaining entries in the iterator.
pub fn as_slice(&self) -> &'a Slice<T> {
Slice::from_slice(self.iter.as_slice())
}
}

impl<T> Slice<T> {
/// Return the number of elements in the set slice.
pub fn len(&self) -> usize {
Expand Down

0 comments on commit a6a98df

Please sign in to comment.