SeekBySize

Struct SeekBySize 

Source
struct SeekBySize<K, V> {
    index: usize,
    _marker: PhantomData<fn() -> (K, V)>,
}

Fields§

§index: usize§_marker: PhantomData<fn() -> (K, V)>

Implementations§

Source§

impl<K, V> SeekBySize<K, V>

Source

fn new(index: usize) -> Self

Examples found in repository?
crates/competitive/src/data_structure/splay_tree/sized_map.rs (line 177)
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    }
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    }
238    pub fn len(&self) -> usize {
239        self.length
240    }
241    pub fn is_empty(&self) -> bool {
242        self.len() == 0
243    }
244    pub fn iter(&mut self) -> Iter<'_, K, V> {
245        Iter {
246            iter: NodeRange::new(&mut self.root),
247        }
248    }
249    pub fn range<Q, R>(&mut self, range: R) -> Iter<'_, K, V>
250    where
251        K: Borrow<Q>,
252        Q: Ord + ?Sized,
253        R: RangeBounds<Q>,
254    {
255        let start = match range.start_bound() {
256            Bound::Included(key) => Bound::Included(SeekByKey::new(key)),
257            Bound::Excluded(key) => Bound::Excluded(SeekByKey::new(key)),
258            Bound::Unbounded => Bound::Unbounded,
259        };
260        let end = match range.end_bound() {
261            Bound::Included(key) => Bound::Included(SeekByKey::new(key)),
262            Bound::Excluded(key) => Bound::Excluded(SeekByKey::new(key)),
263            Bound::Unbounded => Bound::Unbounded,
264        };
265        Iter {
266            iter: self.root.range(start, end),
267        }
268    }
269    pub fn range_at<R>(&mut self, range: R) -> Iter<'_, K, V>
270    where
271        R: RangeBounds<usize>,
272    {
273        let start = match range.start_bound() {
274            Bound::Included(&index) => Bound::Included(SeekBySize::new(index)),
275            Bound::Excluded(&index) => Bound::Excluded(SeekBySize::new(index)),
276            Bound::Unbounded => Bound::Unbounded,
277        };
278        let end = match range.end_bound() {
279            Bound::Included(&index) => Bound::Included(SeekBySize::new(index)),
280            Bound::Excluded(&index) => Bound::Excluded(SeekBySize::new(index)),
281            Bound::Unbounded => Bound::Unbounded,
282        };
283        Iter {
284            iter: self.root.range(start, end),
285        }
286    }

Trait Implementations§

Source§

impl<K, V> SplaySeeker for SeekBySize<K, V>

Source§

type S = SizedSplay<(K, V)>

Source§

fn splay_seek(&mut self, node: NodeRef<Immut<'_>, Self::S>) -> Ordering

Auto Trait Implementations§

§

impl<K, V> Freeze for SeekBySize<K, V>

§

impl<K, V> RefUnwindSafe for SeekBySize<K, V>

§

impl<K, V> Send for SeekBySize<K, V>

§

impl<K, V> Sync for SeekBySize<K, V>

§

impl<K, V> Unpin for SeekBySize<K, V>

§

impl<K, V> UnwindSafe for SeekBySize<K, V>

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.