docs

您正在閱讀 v0.1.2 的文件。Nabi 0.1.2 已停止支援。 最新版本:v0.1.3 →

core::collections::sorted

本頁內容

API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。

SortedDict<K, V> and SortedSet<T>: ordered by K: Ord, kept in a B-tree. Lookup, insert and remove are O(log n); iteration is in key order, and range, first / last, floor / ceil find keys by order.

The nodes live in flat lists by node number: node n's keys and values fill slots n * FULL .. of keys / vals, its children slots n * (FULL + 1) .. of kids, and lens[n] counts its keys. Every node but the root holds MIN - 1 to FULL keys. A vacated slot is zeroed, so a removed entry keeps nothing alive; a removed node's number goes on free for reuse.

Structs

SortedDictSource

pub struct SortedDict<K, V>

A map ordered by key. Copies share the map, as with Dict.

  • pub const fn new() -> SortedDict<K, V>§

    Source

    An empty map.

  • pub const fn len(self) -> usize§

    Source

    The number of entries.

  • pub const fn is_empty(self) -> bool§

    Source

    true when the map has no entry.

  • pub const fn get(self, k: K) -> V | none§

    Source

    k's value, or none. O(log n).

  • pub const fn get_or(self, k: K, default: V) -> V§

    Source

    k's value, or default when absent.

  • pub const fn contains_key(self, k: K) -> bool§

    Source

    true when k has an entry.

  • pub const fn insert(self, k: K, v: V) -> V | none§

    Source

    Store v under k; the old value, or none when k is new. O(log n).

    nabi
    use core::collections::SortedDict;
    
    let d: SortedDict<str, i32> = SortedDict::new();
    d.insert("b", 2);
    d["a"] = 1;
    assert(d.insert("b", 3) == 2 && d["b"] == 3);
    assert(d.keys().collect::<List<str>>() == ["a", "b"]);
    
  • pub const fn remove(self, k: K) -> V | none§

    Source

    Remove k; its value, or none when absent. O(log n).

    nabi
    use core::collections::SortedDict;
    
    let d: SortedDict<i64, str> = SortedDict::from([(3, "c"), (1, "a")]);
    assert(d.remove(1) == "a" && d.remove(1) is none && d.len() == 1);
    
  • pub const fn clear(self)§

    Source

    Remove every entry.

  • pub const fn first(self) -> (K, V) | none§

    Source

    The entry with the smallest key, or none when empty.

    nabi
    use core::collections::SortedDict;
    
    let d: SortedDict<i64, str> = SortedDict::from([(5, "e"), (1, "a"), (3, "c")]);
    assert(d.first() == (1, "a") && d.last() == (5, "e"));
    assert(d.pop_first() == (1, "a") && d.len() == 2);
    
  • pub const fn last(self) -> (K, V) | none§

    Source

    The entry with the largest key, or none when empty.

  • pub const fn pop_first(self) -> (K, V) | none§

    Source

    Remove and return the entry with the smallest key.

  • pub const fn pop_last(self) -> (K, V) | none§

    Source

    Remove and return the entry with the largest key.

  • pub const fn floor(self, k: K) -> (K, V) | none§

    Source

    The entry with the largest key at or below k, or none.

    nabi
    use core::collections::SortedDict;
    
    let d: SortedDict<i64, str> = SortedDict::from([(10, "a"), (20, "b"), (30, "c")]);
    assert(d.floor(25) == (20, "b") && d.floor(20) == (20, "b") && d.floor(5) is none);
    assert(d.ceil(25) == (30, "c") && d.lower(20) == (10, "a") && d.higher(20) == (30, "c"));
    
  • pub const fn ceil(self, k: K) -> (K, V) | none§

    Source

    The entry with the smallest key at or above k, or none.

  • pub const fn lower(self, k: K) -> (K, V) | none§

    Source

    The entry with the largest key strictly below k, or none.

  • pub const fn higher(self, k: K) -> (K, V) | none§

    Source

    The entry with the smallest key strictly above k, or none.

  • pub const fn iter(self) -> SortedIter<K, V>§

    Source

    The entries in key order, lazily.

  • pub const fn iter_rev(self) -> SortedIter<K, V>§

    Source

    The entries in descending key order, lazily.

  • pub const fn keys(self) -> SortedKeys<K, V>§

    Source

    The keys in order, lazily.

  • pub const fn values(self) -> SortedValues<K, V>§

    Source

    The values in key order, lazily.

  • pub const fn range<R: KeyRange<K>>(self, r: R) -> SortedIter<K, V>§

    Source

    The entries whose keys are in r (a..b, a..=b, a.., ..b, ..), in key order, lazily; O(log n) to the first. The map must not change while it is walked.

    nabi
    use core::collections::SortedDict;
    
    let d: SortedDict<i64, str> = SortedDict::from([(1, "a"), (4, "d"), (7, "g"), (9, "i")]);
    assert(d.range(3..9).map(|(k, _)| k).collect::<List<i64>>() == [4, 7]);
    assert(d.range(4..=9).count() == 3 && d.range((..4)).count() == 1);
    assert(d.range_rev(2..).map(|(k, _)| k).collect::<List<i64>>() == [9, 7, 4]);
    
  • pub const fn range_rev<R: KeyRange<K>>(self, r: R) -> SortedIter<K, V>§

    Source

    range in descending key order.

Trait implementations 12

SortedIterSource

pub struct SortedIter<K, V>

The entries of a SortedDict in key order (or reversed), within a range.

Trait implementations 2
  • impl<K, V> Debug for SortedIter<K, V>
  • impl<K: Ord, V> Iterator for SortedIter<K, V>

SortedKeysSource

pub struct SortedKeys<K, V>

The keys of a SortedDict in order.

Trait implementations 2
  • impl<K, V> Debug for SortedKeys<K, V>
  • impl<K: Ord, V> Iterator for SortedKeys<K, V>

SortedValuesSource

pub struct SortedValues<K, V>

The values of a SortedDict in key order.

Trait implementations 2
  • impl<K, V> Debug for SortedValues<K, V>
  • impl<K: Ord, V> Iterator for SortedValues<K, V>

SortedSetSource

pub struct SortedSet<T>

A set ordered by T: Ord, kept in a B-tree. Copies share the set.

  • pub const fn new() -> SortedSet<T>§

    Source

    An empty set.

  • pub const fn len(self) -> usize§

    Source

    The number of members.

  • pub const fn is_empty(self) -> bool§

    Source

    true when the set has no member.

  • pub const fn insert(self, x: T) -> bool§

    Source

    Add x; true when it was not there. O(log n).

    nabi
    use core::collections::SortedSet;
    
    let s: SortedSet<i64> = SortedSet::from([5, 1, 3, 1]);
    assert(s.to_list() == [1, 3, 5] && !s.insert(3) && s.insert(4));
    assert(s.range(2..5).collect::<List<i64>>() == [3, 4]);
    assert(s.floor(2) == 1 && s.ceil(2) == 3 && s.first() == 1 && s.last() == 5);
    
  • pub const fn remove(self, x: T) -> bool§

    Source

    Remove x; true when it was there.

  • pub const fn contains(self, x: T) -> bool§

    Source

    true when x is a member.

  • pub const fn clear(self)§

    Source

    Remove every member.

  • pub const fn first(self) -> T | none§

    Source

    The smallest member, or none.

  • pub const fn last(self) -> T | none§

    Source

    The largest member, or none.

  • pub const fn pop_first(self) -> T | none§

    Source

    Remove and return the smallest member.

  • pub const fn pop_last(self) -> T | none§

    Source

    Remove and return the largest member.

  • pub const fn floor(self, x: T) -> T | none§

    Source

    The largest member at or below x, or none.

  • pub const fn ceil(self, x: T) -> T | none§

    Source

    The smallest member at or above x, or none.

  • pub const fn lower(self, x: T) -> T | none§

    Source

    The largest member strictly below x, or none.

  • pub const fn higher(self, x: T) -> T | none§

    Source

    The smallest member strictly above x, or none.

  • pub const fn iter(self) -> SortedKeys<T, bool>§

    Source

    The members in order, lazily.

  • pub const fn iter_rev(self) -> SortedKeys<T, bool>§

    Source

    The members in descending order, lazily.

  • pub const fn range<R: KeyRange<T>>(self, r: R) -> SortedKeys<T, bool>§

    Source

    The members in r, in order, lazily.

  • pub const fn range_rev<R: KeyRange<T>>(self, r: R) -> SortedKeys<T, bool>§

    Source

    range in descending order.

  • pub const fn to_list(self) -> List<T>§

    Source

    The members in a new list, in order.

  • pub const fn union(self, other: SortedSet<T>) -> SortedSet<T>§

    Source

    Members of either set.

    nabi
    use core::collections::SortedSet;
    
    let a: SortedSet<i64> = SortedSet::from([1, 2, 3]);
    let b: SortedSet<i64> = SortedSet::from([2, 3, 4]);
    assert(a.union(b).to_list() == [1, 2, 3, 4]);
    assert(a.intersection(b).to_list() == [2, 3] && a.difference(b).to_list() == [1]);
    assert(!a.is_subset(b) && a.intersection(b).is_subset(a));
    
  • pub const fn intersection(self, other: SortedSet<T>) -> SortedSet<T>§

    Source

    Members of both sets.

  • pub const fn difference(self, other: SortedSet<T>) -> SortedSet<T>§

    Source

    Members of self that are not in other.

  • pub const fn symmetric_difference(self, other: SortedSet<T>) -> SortedSet<T>§

    Source

    Members of exactly one of the sets.

  • pub const fn is_subset(self, other: SortedSet<T>) -> bool§

    Source

    true when every member is in other.

  • pub const fn is_superset(self, other: SortedSet<T>) -> bool§

    Source

    true when every member of other is in self.

  • pub const fn is_disjoint(self, other: SortedSet<T>) -> bool§

    Source

    true when no member is in both sets.

Trait implementations 11

Traits

KeyRangeSource

pub trait KeyRange<K>

The bounds of a key range: a..b, a..=b, a.., ..b, ..=b and .. implement it, so d.range(3..7) takes any of them.

  • fn lower(self) -> (K, bool) | none§

    Source

    The lower bound and whether it is included; none when open.

  • fn upper(self) -> (K, bool) | none§

    Source

    The upper bound and whether it is included; none when open.