Skip to content

Commit

Permalink
add put bool
Browse files Browse the repository at this point in the history
  • Loading branch information
tickbh committed Nov 22, 2024
1 parent 273ba6b commit 7127046
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["algorithm-macro"]

[package]
name = "algorithm"
version = "0.1.14"
version = "0.1.15"
edition = "2021"
authors = ["tickbh <[email protected]>"]
description = "about algorithm data structure, now has ttl with lru/lru-k/lfu/arc and slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构"
Expand Down
18 changes: 18 additions & 0 deletions src/buf/bt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ pub trait Bt {
dst.len()
}

fn get_bool(&mut self) -> bool {
assert!(self.remaining() >= 1);
let ret = self.chunk()[0];
self.advance(1);
ret == 1
}

fn peek_bool(&mut self) -> bool {
assert!(self.remaining() >= 1);
let ret = self.chunk()[0];
ret == 1
}

fn try_get_bool(&mut self) -> io::Result<bool> {
try_advance!(self.remaining() >= 1);
Ok(self.get_bool())
}

fn get_u8(&mut self) -> u8 {
assert!(self.remaining() >= 1);
let ret = self.chunk()[0];
Expand Down
6 changes: 6 additions & 0 deletions src/buf/bt_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ pub unsafe trait BtMut {
}
cnt
}

fn put_bool(&mut self, b: bool) -> usize {
let src = [if b { 1 } else { 0 }];
self.put_slice(&src);
1
}

fn put_u8(&mut self, n: u8) -> usize {
let src = [n];
Expand Down
6 changes: 3 additions & 3 deletions src/timer/timer_rbtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ impl<T: Timer> TimerRBTree<T> {
/// }
pub fn add_timer(&mut self, val: T) -> u64 {
let timer_id = self.get_next_timerid();
self._add_timer(timer_id, val);
self.add_timer_by_id(timer_id, val);
timer_id
}

fn _add_timer(&mut self, timer_id: u64, val: T) {
pub fn add_timer_by_id(&mut self, timer_id: u64, val: T) {
let when = val.when();
self.tree.insert(TreeKey(when, timer_id), val);
self.map.insert(timer_id, when);
Expand Down Expand Up @@ -339,7 +339,7 @@ impl<T: Timer> TimerRBTree<T> {
}
}
for (timer_id, v) in collect_result.drain(..) {
self._add_timer(timer_id, v);
self.add_timer_by_id(timer_id, v);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/timer/timer_wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl<T: Timer> TimerWheel<T> {
}
}
for (timer_id, val) in collect_result.drain(..) {
self._add_timer(timer_id, val);
self.add_timer_by_id(timer_id, val);
}
}
}
Expand Down Expand Up @@ -604,11 +604,11 @@ impl<T: Timer> TimerWheel<T> {
pub fn add_timer(&mut self, val: T) -> u64 {
debug_assert!(!self.greatest.is_null(), "必须设置时轮才能添加元素");
let timer_id = self.get_next_timerid();
self._add_timer(timer_id, val);
self.add_timer_by_id(timer_id, val);
timer_id
}

fn _add_timer(&mut self, timer_id: u64, mut val: T) {
pub fn add_timer_by_id(&mut self, timer_id: u64, mut val: T) {
let entry = Entry {
when: val.when_mut().max(1),
val,
Expand Down

0 comments on commit 7127046

Please sign in to comment.