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.
An empty map.
The number of entries.
truewhen the map has no entry.k's value, ornone. O(log n).k's value, ordefaultwhen absent.truewhenkhas an entry.Store
vunderk; the old value, ornonewhenkis new. O(log n).nabiuse 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"]);Remove
k; its value, ornonewhen absent. O(log n).nabiuse 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);Remove every entry.
The entry with the smallest key, or
nonewhen empty.nabiuse 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);The entry with the largest key, or
nonewhen empty.Remove and return the entry with the smallest key.
Remove and return the entry with the largest key.
The entry with the largest key at or below
k, ornone.nabiuse 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"));The entry with the smallest key at or above
k, ornone.The entry with the largest key strictly below
k, ornone.The entry with the smallest key strictly above
k, ornone.
Sourcepub const fn iter(§self) -> SortedIter<K, V> The entries in key order, lazily.
Sourcepub const fn iter_rev(§self) -> SortedIter<K, V> The entries in descending key order, lazily.
Sourcepub const fn keys(§self) -> SortedKeys<K, V> The keys in order, lazily.
Sourcepub const fn values(§self) -> SortedValues<K, V> The values in key order, lazily.
Sourcepub const fn range<R: KeyRange<K>>(§self, r: R) -> SortedIter<K, V> 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.nabiuse 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]);
Sourcepub const fn range_rev<R: KeyRange<K>>(§self, r: R) -> SortedIter<K, V> rangein descending key order.
Trait implementations 12
impl<K, V> !Sync for SortedDict<K, V>impl<K: Ord, V> IntoIterator for SortedDict<K, V>impl<K: Ord, V> Index<K> for SortedDict<K, V>impl<K: Ord, V> IndexSet<K, V> for SortedDict<K, V>impl<K: Ord, V> Default for SortedDict<K, V>impl<K: Ord, V> FromIterator<(K, V)> for SortedDict<K, V>impl<K: Ord, V> From<List<(K, V)>> for SortedDict<K, V>impl<K: Ord + Clone, V: Clone> Clone for SortedDict<K, V>impl<K: Ord, V: PartialEq> PartialEq for SortedDict<K, V>impl<K: Ord, V: Eq> Eq for SortedDict<K, V>impl<K: Ord + Hash, V: Hash> Hash for SortedDict<K, V>impl<K: Ord + Debug, V: Debug> Debug for SortedDict<K, V>
SortedIterSource
pub struct SortedIter<K, V>The entries of a SortedDict in key order (or reversed), within a
range.
SortedKeysSource
pub struct SortedKeys<K, V>The keys of a SortedDict in order.
SortedValuesSource
pub struct SortedValues<K, V>The values of a SortedDict in key order.
SortedSetSource
pub struct SortedSet<T>A set ordered by T: Ord, kept in a B-tree. Copies share the set.
An empty set.
The number of members.
truewhen the set has no member.Add
x;truewhen it was not there. O(log n).nabiuse 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);Remove
x;truewhen it was there.truewhenxis a member.Remove every member.
The smallest member, or
none.The largest member, or
none.Remove and return the smallest member.
Remove and return the largest member.
The largest member at or below
x, ornone.The smallest member at or above
x, ornone.The largest member strictly below
x, ornone.The smallest member strictly above
x, ornone.
Sourcepub const fn iter(§self) -> SortedKeys<T, bool> The members in order, lazily.
Sourcepub const fn iter_rev(§self) -> SortedKeys<T, bool> The members in descending order, lazily.
Sourcepub const fn range<R: KeyRange<T>>(§self, r: R) -> SortedKeys<T, bool> The members in
r, in order, lazily.
Sourcepub const fn range_rev<R: KeyRange<T>>(§self, r: R) -> SortedKeys<T, bool> rangein descending order.The members in a new list, in order.
Members of either set.
nabiuse 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));Members of both sets.
Members of
selfthat are not inother.Members of exactly one of the sets.
truewhen every member is inother.truewhen every member ofotheris inself.truewhen no member is in both sets.
Trait implementations 11
impl<T> !Sync for SortedSet<T>impl<T: Ord> IntoIterator for SortedSet<T>impl<T: Ord> Default for SortedSet<T>impl<T: Ord> FromIterator<T> for SortedSet<T>impl<T: Ord> From<List<T>> for SortedSet<T>impl<T: Ord> AddAssign<T> for SortedSet<T>impl<T: Ord + Clone> Clone for SortedSet<T>impl<T: Ord> PartialEq for SortedSet<T>impl<T: Ord> Eq for SortedSet<T>impl<T: Ord + Hash> Hash for SortedSet<T>impl<T: Ord + Debug> Debug for SortedSet<T>
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.
The lower bound and whether it is included;
nonewhen open.The upper bound and whether it is included;
nonewhen open.