zset/
lib.rs

1use std::{borrow::Borrow, hash::Hash, ops::RangeBounds};
2
3mod arc_m;
4pub use arc_m::ArcM;
5
6#[cfg(feature = "impl")]
7pub mod score_member;
8#[cfg(feature = "impl")]
9pub mod zset_impl;
10#[cfg(feature = "impl")]
11pub use zset_impl::Zset;
12
13pub trait Score: Ord + Clone {}
14
15impl<T: Ord + Clone> Score for T {}
16
17pub trait Key: Eq + Hash + Ord + Clone {}
18
19impl<T: Eq + Hash + Ord + Clone> Key for T {}
20
21pub trait Member<K: Key>: Borrow<K> {}
22
23impl<T, K: Key> Member<K> for T where T: Borrow<K> {}
24
25/// The public API for the Zset.
26/// Zset 的公共 API。
27pub trait Api<M, S>
28where
29  M: Borrow<Self::K>,
30  S: Score,
31{
32  type K: Key;
33  /// Adds the specified member with the specified score to the sorted set.
34  /// If the member is already a member of the sorted set, the score is updated.
35  /// Returns `true` if the score was updated, `false` if a new member was added.
36  /// 将指定的成员和分数添加到排序集合中。
37  /// 如果成员已经是排序集合的成员,则更新其分数。
38  /// 如果分数被更新,则返回 `true`;如果是新成员,则返回 `false`。
39  fn add(&self, member: impl Into<ArcM<Self::K, M>>, score: S) -> bool;
40
41  /// Removes the specified member from the sorted set.
42  /// 从排序集合中移除指定的成员。
43  fn rm(&self, member: impl Borrow<Self::K>) -> bool;
44
45  /// Removes a range of members in the sorted set, with scores ordered from low to high.
46  /// The `range` is a 0-based.
47  /// Returns the number of members removed.
48  /// 按排名(0-based)从低到高,删除排序集合中指定范围的成员。
49  /// 返回被删除的成员数量。
50  fn rm_range_by_rank(&self, range: impl RangeBounds<usize>) -> usize;
51
52  /// Returns the score of the specified member.
53  /// 返回指定成员的分数。
54  fn score(&self, member: impl Borrow<Self::K>) -> Option<S>;
55
56  /// Returns the 0-based rank of the member in the sorted set, with scores ordered from low to high.
57  /// 返回成员在排序集合中的排名(0-based),分数从低到高排序。
58  fn rank(&self, member: impl Borrow<Self::K>) -> Option<usize>;
59
60  /// Returns the number of elements in the sorted set (leninality).
61  /// 返回排序集合中的元素数量(基数)。
62  fn len(&self) -> usize;
63
64  /// Returns the number of elements in the sorted set. This is an alias for `len()`.
65  /// 返回排序集合中的元素数量。这是 `len()` 的别名。
66  #[cfg(feature = "card")]
67  fn card(&self) -> usize {
68    self.len()
69  }
70
71  /// Returns `true` if the sorted set contains no elements.
72  /// 如果排序集合为空,则返回 `true`。
73  fn is_empty(&self) -> bool;
74
75  /// Returns a range of members in the sorted set, with scores ordered from low to high.
76  /// The `range` is a 0-based, half-open interval (`start..end`).
77  /// 返回排序集合中指定范围的成员,分数从低到高排序。
78  /// `range` 是一个 0-based 的半开区间 (`start..end`)。
79  fn range(&self, range: impl RangeBounds<usize>) -> Vec<ArcM<Self::K, M>>;
80
81  /// Returns a range of members with their scores in the sorted set, with scores ordered from low to high.
82  /// The `range` is a 0-based, half-open interval (`start..end`).
83  /// 返回排序集合中指定范围的成员及其分数,分数从低到高排序。
84  /// `range` 是一个 0-based 的半开区间 (`start..end`)。
85  fn range_with_scores(&self, range: impl RangeBounds<usize>) -> Vec<(ArcM<Self::K, M>, S)>;
86
87  /// Returns the member at the specified 0-based rank, with scores ordered from low to high.
88  /// 返回指定排名(0-based)的成员,分数从低到高排序。
89  fn get(&self, rank: usize) -> Option<ArcM<Self::K, M>>;
90
91  /// Returns the member and score at the specified 0-based rank, with scores ordered from low to high.
92  /// 返回指定排名(0-based)的成员和分数,分数从低到高排序。
93  fn get_with_score(&self, rank: usize) -> Option<(ArcM<Self::K, M>, S)>;
94}