SplayMap

Struct SplayMap 

Source
pub struct SplayMap<K, V, A = MemoryPool<Node<((K, V), usize)>>>
where A: Allocator<Node<((K, V), usize)>>,
{ root: Root<SizedSplay<(K, V)>>, length: usize, alloc: ManuallyDrop<A>, }

Fields§

§root: Root<SizedSplay<(K, V)>>§length: usize§alloc: ManuallyDrop<A>

Implementations§

Source§

impl<K, V> SplayMap<K, V>

Source

pub fn new() -> Self

Source

pub fn with_capacity(capacity: usize) -> Self

Source§

impl<K, V, A> SplayMap<K, V, A>
where A: Allocator<Node<((K, V), usize)>>,

Source

pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Ord + ?Sized,

Source

fn splay_by_key<Q>(&mut self, key: &Q) -> Option<Ordering>
where K: Borrow<Q>, Q: Ord + ?Sized,

Examples found in repository?
crates/competitive/src/data_structure/splay_tree/sized_map.rs (line 168)
163    pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
164    where
165        K: Borrow<Q>,
166        Q: Ord + ?Sized,
167    {
168        if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
169            return None;
170        }
171        self.root.root().map(|node| {
172            let ((k, v), _) = node.data();
173            (k, v)
174        })
175    }
176    fn splay_at(&mut self, index: usize) -> Option<Ordering> {
177        self.root.splay_by(SeekBySize::new(index))
178    }
179    pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)> {
180        if index >= self.length {
181            return None;
182        }
183        self.splay_at(index);
184        self.root.root().map(|node| {
185            let ((k, v), _) = node.data();
186            (k, v)
187        })
188    }
189    pub fn insert(&mut self, key: K, value: V) -> Option<V>
190    where
191        K: Ord,
192    {
193        let ord = self.splay_by_key(&key);
194        self.length += (ord != Some(Ordering::Equal)) as usize;
195        match ord {
196            Some(Ordering::Equal) => {
197                return Some(replace(
198                    &mut (self.root.root_data_mut().unwrap().data_mut().0).1,
199                    value,
200                ));
201            }
202            Some(Ordering::Less) => unsafe {
203                self.root.insert_left(NodeRef::from_data(
204                    ((key, value), 1),
205                    self.alloc.deref_mut(),
206                ));
207            },
208            _ => unsafe {
209                self.root.insert_right(NodeRef::from_data(
210                    ((key, value), 1),
211                    self.alloc.deref_mut(),
212                ));
213            },
214        }
215        None
216    }
217    pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
218    where
219        K: Borrow<Q>,
220        Q: Ord + ?Sized,
221    {
222        if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
223            return None;
224        }
225        self.length -= 1;
226        let node = self.root.take_root().unwrap().into_dying();
227        unsafe { Some((node.into_data(self.alloc.deref_mut()).0).1) }
228    }
Source

pub fn get_key_value<Q>(&mut self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Ord + ?Sized,

Examples found in repository?
crates/competitive/src/data_structure/splay_tree/sized_map.rs (line 154)
149    pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
150    where
151        K: Borrow<Q>,
152        Q: Ord + ?Sized,
153    {
154        self.get_key_value(key).map(|(_, v)| v)
155    }
Source

fn splay_at(&mut self, index: usize) -> Option<Ordering>

Examples found in repository?
crates/competitive/src/data_structure/splay_tree/sized_map.rs (line 183)
179    pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)> {
180        if index >= self.length {
181            return None;
182        }
183        self.splay_at(index);
184        self.root.root().map(|node| {
185            let ((k, v), _) = node.data();
186            (k, v)
187        })
188    }
189    pub fn insert(&mut self, key: K, value: V) -> Option<V>
190    where
191        K: Ord,
192    {
193        let ord = self.splay_by_key(&key);
194        self.length += (ord != Some(Ordering::Equal)) as usize;
195        match ord {
196            Some(Ordering::Equal) => {
197                return Some(replace(
198                    &mut (self.root.root_data_mut().unwrap().data_mut().0).1,
199                    value,
200                ));
201            }
202            Some(Ordering::Less) => unsafe {
203                self.root.insert_left(NodeRef::from_data(
204                    ((key, value), 1),
205                    self.alloc.deref_mut(),
206                ));
207            },
208            _ => unsafe {
209                self.root.insert_right(NodeRef::from_data(
210                    ((key, value), 1),
211                    self.alloc.deref_mut(),
212                ));
213            },
214        }
215        None
216    }
217    pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
218    where
219        K: Borrow<Q>,
220        Q: Ord + ?Sized,
221    {
222        if !matches!(self.splay_by_key(key)?, Ordering::Equal) {
223            return None;
224        }
225        self.length -= 1;
226        let node = self.root.take_root().unwrap().into_dying();
227        unsafe { Some((node.into_data(self.alloc.deref_mut()).0).1) }
228    }
229    pub fn remove_at(&mut self, index: usize) -> Option<(K, V)> {
230        if index >= self.length {
231            return None;
232        }
233        self.splay_at(index);
234        self.length -= 1;
235        let node = self.root.take_root().unwrap().into_dying();
236        unsafe { Some(node.into_data(self.alloc.deref_mut()).0) }
237    }
Source

pub fn get_key_value_at(&mut self, index: usize) -> Option<(&K, &V)>

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>
where K: Ord,

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Ord + ?Sized,

Source

pub fn remove_at(&mut self, index: usize) -> Option<(K, V)>

Source

pub fn len(&self) -> usize

Examples found in repository?
crates/competitive/src/data_structure/splay_tree/sized_map.rs (line 242)
241    pub fn is_empty(&self) -> bool {
242        self.len() == 0
243    }
Source

pub fn is_empty(&self) -> bool

Source

pub fn iter(&mut self) -> Iter<'_, K, V>

Source

pub fn range<Q, R>(&mut self, range: R) -> Iter<'_, K, V>
where K: Borrow<Q>, Q: Ord + ?Sized, R: RangeBounds<Q>,

Source

pub fn range_at<R>(&mut self, range: R) -> Iter<'_, K, V>
where R: RangeBounds<usize>,

Trait Implementations§

Source§

impl<K, V, A> Debug for SplayMap<K, V, A>
where K: Debug, V: Debug, A: Allocator<Node<((K, V), usize)>>,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V, A> Default for SplayMap<K, V, A>
where A: Allocator<Node<((K, V), usize)>> + Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K, V, A> Drop for SplayMap<K, V, A>
where A: Allocator<Node<((K, V), usize)>>,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<K, V, A> Freeze for SplayMap<K, V, A>
where A: Freeze,

§

impl<K, V, A> RefUnwindSafe for SplayMap<K, V, A>

§

impl<K, V, A = MemoryPool<Node<((K, V), usize)>>> !Send for SplayMap<K, V, A>

§

impl<K, V, A = MemoryPool<Node<((K, V), usize)>>> !Sync for SplayMap<K, V, A>

§

impl<K, V, A> Unpin for SplayMap<K, V, A>
where A: Unpin,

§

impl<K, V, A> UnwindSafe for SplayMap<K, V, A>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.