Skip to content

Commit 7127046

Browse files
committed
add put bool
1 parent 273ba6b commit 7127046

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["algorithm-macro"]
33

44
[package]
55
name = "algorithm"
6-
version = "0.1.14"
6+
version = "0.1.15"
77
edition = "2021"
88
authors = ["tickbh <tickdream125@hotmail.com>"]
99
description = "about algorithm data structure, now has ttl with lru/lru-k/lfu/arc and slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构"

src/buf/bt.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ pub trait Bt {
191191
dst.len()
192192
}
193193

194+
fn get_bool(&mut self) -> bool {
195+
assert!(self.remaining() >= 1);
196+
let ret = self.chunk()[0];
197+
self.advance(1);
198+
ret == 1
199+
}
200+
201+
fn peek_bool(&mut self) -> bool {
202+
assert!(self.remaining() >= 1);
203+
let ret = self.chunk()[0];
204+
ret == 1
205+
}
206+
207+
fn try_get_bool(&mut self) -> io::Result<bool> {
208+
try_advance!(self.remaining() >= 1);
209+
Ok(self.get_bool())
210+
}
211+
194212
fn get_u8(&mut self) -> u8 {
195213
assert!(self.remaining() >= 1);
196214
let ret = self.chunk()[0];

src/buf/bt_mut.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ pub unsafe trait BtMut {
9191
}
9292
cnt
9393
}
94+
95+
fn put_bool(&mut self, b: bool) -> usize {
96+
let src = [if b { 1 } else { 0 }];
97+
self.put_slice(&src);
98+
1
99+
}
94100

95101
fn put_u8(&mut self, n: u8) -> usize {
96102
let src = [n];

src/timer/timer_rbtree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ impl<T: Timer> TimerRBTree<T> {
170170
/// }
171171
pub fn add_timer(&mut self, val: T) -> u64 {
172172
let timer_id = self.get_next_timerid();
173-
self._add_timer(timer_id, val);
173+
self.add_timer_by_id(timer_id, val);
174174
timer_id
175175
}
176176

177-
fn _add_timer(&mut self, timer_id: u64, val: T) {
177+
pub fn add_timer_by_id(&mut self, timer_id: u64, val: T) {
178178
let when = val.when();
179179
self.tree.insert(TreeKey(when, timer_id), val);
180180
self.map.insert(timer_id, when);
@@ -339,7 +339,7 @@ impl<T: Timer> TimerRBTree<T> {
339339
}
340340
}
341341
for (timer_id, v) in collect_result.drain(..) {
342-
self._add_timer(timer_id, v);
342+
self.add_timer_by_id(timer_id, v);
343343
}
344344
}
345345
}

src/timer/timer_wheel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ impl<T: Timer> TimerWheel<T> {
451451
}
452452
}
453453
for (timer_id, val) in collect_result.drain(..) {
454-
self._add_timer(timer_id, val);
454+
self.add_timer_by_id(timer_id, val);
455455
}
456456
}
457457
}
@@ -604,11 +604,11 @@ impl<T: Timer> TimerWheel<T> {
604604
pub fn add_timer(&mut self, val: T) -> u64 {
605605
debug_assert!(!self.greatest.is_null(), "必须设置时轮才能添加元素");
606606
let timer_id = self.get_next_timerid();
607-
self._add_timer(timer_id, val);
607+
self.add_timer_by_id(timer_id, val);
608608
timer_id
609609
}
610610

611-
fn _add_timer(&mut self, timer_id: u64, mut val: T) {
611+
pub fn add_timer_by_id(&mut self, timer_id: u64, mut val: T) {
612612
let entry = Entry {
613613
when: val.when_mut().max(1),
614614
val,

0 commit comments

Comments
 (0)