Struct AllOne

Source
pub struct AllOne { /* private fields */ }
Expand description

双向链表 + 两个 HashMap

本实现使用 NonNull 封装裸指针,需要注意在删除节点或者 Drop 整个结构的时候需要用 Box::from_raw() 来释放内存。

  • keys: HashMap<String, u32> 记录该数据结构内所有的 key 对应的值,以便用 O(1) 的时间查找 key
  • index: HashMap<u32, NonNull<Node>> 记录该数据结构内所有 key 的值(作为 key)在链表结构里对应的节点的指针

Implementations§

Source§

impl AllOne

&self means the method takes an immutable reference. If you need a mutable reference, change it to &mut self instead.

Source

pub fn new() -> Self

Initialize your data structure here.

Source

pub fn inc(&mut self, key: String)

Inserts a new key with value 1. Or increments an existing key by 1.

Source

pub fn dec(&mut self, key: String)

Decrements an existing key by 1. If Key’s value is 1, remove it from the data structure.

Source

pub fn get_max_key(&self) -> String

Returns one of the keys with maximal value.

Source

pub fn get_min_key(&self) -> String

Returns one of the keys with Minimal value.

Trait Implementations§

Source§

impl Debug for AllOne

Source§

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

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

impl Default for AllOne

Source§

fn default() -> AllOne

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

impl Drop for AllOne

Source§

fn drop(&mut self)

Must pop every node to drop allocated memory of nodes

Source§

impl Send for AllOne

Source§

impl Sync for AllOne

Auto Trait Implementations§

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.