Skip to content

Commit 976ca88

Browse files
committed
add key
1 parent b2424c2 commit 976ca88

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ opt-level = 3
3232
debug = true
3333

3434
[features]
35-
default = ["hashbrown", "ttl"]
35+
default = ["ttl"]
3636
hashbrown = []
3737
ttl = []

examples/zset.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1-
use algorithm::ZSet;
1+
use std::borrow::Borrow;
2+
3+
use algorithm::{KeyRef, KeyWrapper, ZSet};
24
fn main() {
35
let mut val = ZSet::new();
4-
val.add_or_update("key", 1);
5-
assert_eq!(val.len(), 1);
6-
println!("ok!!!!!!!!!!");
6+
7+
// let wrap = KeyWrapper::from_ref(&"bb");
8+
// let key_ref = KeyRef::new(&"bb");
9+
// println!("aaaaaaaaaaaaaa = {}", wrap.eq(key_ref.borrow()));
10+
11+
// val.add_or_update(11, 10);
12+
// val.add_or_update(22, 12);
13+
// assert_eq!(val.score(&22), 12);
14+
// assert_eq!(val.len(), 2);
15+
// assert_eq!(val.rank(&22), 0);
16+
// val.add_or_update(22, 9);
17+
// assert_eq!(val.rank(&22), 1);
18+
// assert_eq!(val.len(), 2);
19+
// val.add_or_update("aa", 10);
20+
val.add_or_update("xxx", 12);
21+
assert_eq!(val.score(&"xxx"), 12);
22+
assert_eq!(val.len(), 2);
23+
assert_eq!(val.rank(&"bb"), 0);
24+
val.add_or_update("bb", 9);
25+
assert_eq!(val.rank(&"bb"), 1);
26+
assert_eq!(val.len(), 2);
727
}

src/key.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::any::{type_name, TypeId};
12
use std::borrow::Borrow;
23
use std::hash::Hash;
34
use std::ptr;
@@ -71,7 +72,10 @@ impl<Q: ?Sized + Hash> Hash for KeyWrapper<Q> {
7172

7273
impl<Q: ?Sized + PartialEq> PartialEq for KeyWrapper<Q> {
7374
fn eq(&self, other: &Self) -> bool {
74-
(self.0).eq(&other.0)
75+
println!("aaa = {}", (self.0).eq(&other.0));
76+
println!("aaabbb = {}", (&self.0).eq(&other.0));
77+
println!("aaabbbccccc = {}", self.0 == other.0);
78+
(&self.0).eq(&other.0)
7579
}
7680
}
7781

@@ -83,6 +87,7 @@ where
8387
Q: ?Sized,
8488
{
8589
fn borrow(&self) -> &KeyWrapper<Q> {
90+
println!("tttttttt = {} vv = {}", type_name::<K>(), type_name::<Q>());
8691
let key = unsafe { &*self.k }.borrow();
8792
KeyWrapper::from_ref(key)
8893
}

src/map/zset.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
mem, usize,
55
};
66

7-
use hashbrown::HashMap;
7+
use std::collections::HashMap;
88

99
use crate::{KeyRef, KeyWrapper, SkipList, SkipNode};
1010

@@ -41,6 +41,23 @@ impl<K: Hash> PartialOrd for Context<K> {
4141
}
4242
}
4343

44+
/// 一种可排序的Set类型
45+
///
46+
/// # Examples
47+
///
48+
/// ```
49+
/// use algorithm::ZSet;
50+
/// fn main() {
51+
/// let mut val = ZSet::new();
52+
/// val.add_or_update("aa", 10);
53+
/// val.add_or_update("bb", 12);
54+
/// assert_eq!(val.len(), 2);
55+
/// assert_eq!(val.rank(&"bb"), 0);
56+
/// val.add_or_update("bb", 9);
57+
/// assert_eq!(val.rank(&"bb"), 1);
58+
/// assert_eq!(val.len(), 2);
59+
/// }
60+
/// ```
4461
pub struct ZSet<K: Hash + Eq> {
4562
max_count: usize,
4663
reverse: bool,
@@ -125,6 +142,7 @@ impl<K: Hash + Eq> ZSet<K> {
125142

126143
let key_ref = KeyRef::new(unsafe { &*context.key.as_ptr() });
127144

145+
128146
if let Some(v) = self.dict.remove(&key_ref) {
129147
let ret = self.zsl.update(unsafe { &(*v).score }, context);
130148
self.dict.insert(key_ref, ret);

0 commit comments

Comments
 (0)