├── src ├── tree │ └── mod.rs ├── map │ ├── ttl_hashmap.rs │ ├── mod.rs │ ├── zset.rs │ ├── bitmap.rs │ └── roaring_bitmap.rs ├── arr │ ├── mod.rs │ ├── circular_buffer.rs │ ├── skip_list.rs │ └── fix_vec.rs ├── cache │ └── mod.rs ├── util.rs ├── timer │ ├── step_timer.rs │ ├── mod.rs │ ├── stamp_timer.rs │ ├── timer_rbtree.rs │ └── timer_wheel.rs ├── buf │ ├── mod.rs │ ├── binary_ref.rs │ ├── binary.rs │ └── binary_mut.rs ├── lib.rs └── key.rs ├── .gitignore ├── .vscode ├── settings.json └── launch.json ├── examples ├── map_rbm.rs ├── circular.rs ├── fix_vec.rs ├── lruk.rs ├── lfu.rs ├── map_bitmap.rs ├── zset.rs ├── lru.rs ├── skip_list.rs ├── timer.rs ├── cache_macro.rs ├── slab.rs └── bench_lru.rs ├── .github └── workflows │ └── rust.yml ├── algorithm-macro ├── Cargo.toml └── src │ ├── config.rs │ └── lib.rs ├── Cargo.toml ├── benches └── lru.rs ├── Cargo.lock └── README.md /src/tree/mod.rs: -------------------------------------------------------------------------------- 1 | mod rbtree; 2 | 3 | pub use rbtree::RBTree; -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | .idea 3 | flamegraph.svg 4 | perf.data* 5 | -------------------------------------------------------------------------------- /src/map/ttl_hashmap.rs: -------------------------------------------------------------------------------- 1 | 2 | pub struct TtlHashMap { 3 | 4 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cmake.configureOnOpen": true, 3 | "rust-analyzer.cargo.features": [ 4 | "ttl" 5 | ], 6 | } -------------------------------------------------------------------------------- /examples/map_rbm.rs: -------------------------------------------------------------------------------- 1 | 2 | use algorithm::SkipList; 3 | fn main() { 4 | let mut val = SkipList::new(); 5 | val.insert(1); 6 | assert_eq!(val.len(), 1); 7 | } -------------------------------------------------------------------------------- /src/arr/mod.rs: -------------------------------------------------------------------------------- 1 | 2 | mod circular_buffer; 3 | mod fix_vec; 4 | mod skip_list; 5 | 6 | pub use circular_buffer::CircularBuffer; 7 | pub use fix_vec::FixedVec; 8 | pub use skip_list::{SkipList, SkipNode, SkipIter}; -------------------------------------------------------------------------------- /src/map/mod.rs: -------------------------------------------------------------------------------- 1 | 2 | mod bitmap; 3 | mod roaring_bitmap; 4 | mod ttl_hashmap; 5 | mod zset; 6 | 7 | pub use bitmap::BitMap; 8 | pub use roaring_bitmap::RoaringBitMap; 9 | pub use ttl_hashmap::TtlHashMap; 10 | pub use zset::ZSet; -------------------------------------------------------------------------------- /src/cache/mod.rs: -------------------------------------------------------------------------------- 1 | 2 | 3 | mod lfu; 4 | mod lru; 5 | mod lruk; 6 | mod arc; 7 | mod slab; 8 | 9 | pub use lru::LruCache; 10 | pub use lruk::LruKCache; 11 | pub use lfu::LfuCache; 12 | pub use arc::ArcCache; 13 | pub use slab::{Slab, Reinit}; 14 | -------------------------------------------------------------------------------- /examples/circular.rs: -------------------------------------------------------------------------------- 1 | use algorithm::CircularBuffer; 2 | fn main() { 3 | let mut circular = CircularBuffer::new(2); 4 | circular.push_back(1); 5 | circular.push_back(2); 6 | circular.push_back(3); 7 | assert_eq!(circular.len(), 2); 8 | assert_eq!(circular[0], 2); 9 | assert_eq!(circular[1], 3); 10 | } 11 | -------------------------------------------------------------------------------- /examples/fix_vec.rs: -------------------------------------------------------------------------------- 1 | use algorithm::FixedVec; 2 | fn main() { 3 | let mut val = FixedVec::new(5); 4 | val.insert_head(1); 5 | val.insert_head(2); 6 | val.insert_head(3); 7 | let _ = val.iter_mut().map(|(_, v)| *v = *v * 2).collect::>(); 8 | assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![6, 4, 2]); 9 | } -------------------------------------------------------------------------------- /src/util.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::time::SystemTime; 3 | 4 | #[inline(always)] 5 | pub fn get_timestamp() -> u64 { 6 | SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("ok").as_secs() 7 | } 8 | 9 | #[inline(always)] 10 | pub fn get_milltimestamp() -> u64 { 11 | SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("ok").as_millis() as u64 12 | } -------------------------------------------------------------------------------- /src/timer/step_timer.rs: -------------------------------------------------------------------------------- 1 | use super::Timer; 2 | 3 | pub struct StepTimer { 4 | pub step: T, 5 | pub val: V, 6 | } 7 | 8 | impl StepTimer { 9 | pub fn new(val: V, step: T) -> Self { 10 | Self { val, step } 11 | } 12 | } 13 | 14 | impl Timer for StepTimer { 15 | fn when(&self) -> u64 { 16 | self.step.when() 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | name: Rust 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Build 20 | run: cargo build --verbose 21 | - name: Run tests 22 | run: cargo test --verbose 23 | -------------------------------------------------------------------------------- /examples/lruk.rs: -------------------------------------------------------------------------------- 1 | 2 | use algorithm::LruKCache; 3 | fn main() { 4 | let mut lru = LruKCache::with_times(3, 3); 5 | lru.insert("this", "lru"); 6 | for _ in 0..3 { 7 | let _ = lru.get("this"); 8 | } 9 | lru.insert("hello", "algorithm"); 10 | lru.insert("auth", "tickbh"); 11 | assert!(lru.len() == 3); 12 | lru.insert("auth1", "tickbh"); 13 | assert_eq!(lru.get("this"), Some(&"lru")); 14 | assert_eq!(lru.get("hello"), None); 15 | assert!(lru.len() == 3); 16 | } 17 | -------------------------------------------------------------------------------- /examples/lfu.rs: -------------------------------------------------------------------------------- 1 | 2 | use algorithm::LfuCache; 3 | fn main() { 4 | let mut lru = LfuCache::new(3); 5 | lru.insert("hello", "algorithm"); 6 | lru.insert("this", "lru"); 7 | lru.set_reduce_count(100); 8 | assert!(lru.get_visit(&"hello") == Some(5)); 9 | assert!(lru.get_visit(&"this") == Some(5)); 10 | for _ in 0..98 { 11 | let _ = lru.get("this"); 12 | } 13 | lru.insert("hello", "new"); 14 | assert!(lru.get_visit(&"this") == Some(51)); 15 | assert!(lru.get_visit(&"hello") == Some(3)); 16 | let mut keys = lru.keys(); 17 | assert!(keys.next()==Some(&"this")); 18 | assert!(keys.next()==Some(&"hello")); 19 | assert!(keys.next() == None); 20 | } -------------------------------------------------------------------------------- /examples/map_bitmap.rs: -------------------------------------------------------------------------------- 1 | 2 | use algorithm::BitMap; 3 | 4 | fn main() { 5 | // let mut map = BitMap::new(10240); 6 | // map.add_many(&vec![1, 2, 3, 4, 10]); 7 | // assert!(map.contains(&1)); 8 | // assert!(!map.contains(&5)); 9 | // assert!(map.contains(&10)); 10 | // map.add_range(7, 16); 11 | // assert!(!map.contains(&6)); 12 | // assert!(map.contains(&7)); 13 | // assert!(map.contains(&16)); 14 | // assert!(!map.contains(&17)); 15 | 16 | let mut map = BitMap::new(10240); 17 | map.add(7); 18 | map.add_range(9, 12); 19 | map.add_many(&vec![20, 100, 300]); 20 | println!("value = {:?}", map.iter().collect::>()); 21 | assert!(map.iter().collect::>() == vec![7, 9, 10, 11, 12, 20, 100, 300]); 22 | } 23 | -------------------------------------------------------------------------------- /algorithm-macro/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm-macro" 3 | version = "0.1.0" 4 | edition = "2021" 5 | authors = ["tickbh "] 6 | description = "about algorithm data structure, now has ttl with lru/lru-k/lfu/arc and slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构" 7 | repository = "https://github.com/tickbh/algorithm-rs" 8 | license = "Apache-2.0" 9 | keywords = ["arc", "lru", "lfu", "timerwheel", "slab"] 10 | 11 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 12 | 13 | [dependencies] 14 | quote = "1.0.36" 15 | lazy_static = "1.2.0" 16 | proc-macro2 = "1.0.86" 17 | 18 | [dependencies.syn] 19 | version = "2.0.74" 20 | features = ["full", "extra-traits"] 21 | 22 | [lib] 23 | proc-macro = true 24 | -------------------------------------------------------------------------------- /examples/zset.rs: -------------------------------------------------------------------------------- 1 | // use algorithm::ZSet; 2 | // fn main() { 3 | // let mut val = ZSet::new(); 4 | // val.add_or_update("aa", 10); 5 | // val.add_or_update("bb", 12); 6 | // assert_eq!(val.score(&"bb"), 12); 7 | // assert_eq!(val.len(), 2); 8 | // assert_eq!(val.rank(&"bb"), 2); 9 | // val.add_or_update("bb", 9); 10 | // assert_eq!(val.rank(&"bb"), 1); 11 | // assert_eq!(val.len(), 2); 12 | 13 | // } 14 | 15 | 16 | use algorithm::ZSet; 17 | fn main() { 18 | let mut val = ZSet::new(); 19 | val.add_or_update("aa", 10); 20 | val.add_or_update("bb", 12); 21 | let mut iter = val.iter(); 22 | assert_eq!(iter.next(), Some((&"aa", 0, 10))); 23 | assert_eq!(iter.next(), Some((&"bb", 1, 12))); 24 | assert_eq!(iter.next(), None); 25 | } -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // 使用 IntelliSense 了解相关属性。 3 | // 悬停以查看现有属性的描述。 4 | // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "lldb", 9 | "request": "launch", 10 | "name": "quadsort", 11 | "cargo": { 12 | "args": [ 13 | "build", 14 | "--bin=algorithm", 15 | "--package=algorithm" 16 | ], 17 | "filter": { 18 | "name": "algorithm", 19 | "kind": "bin" 20 | } 21 | }, 22 | "args": ["run"], 23 | "cwd": "${workspaceFolder}" 24 | } 25 | 26 | ] 27 | } -------------------------------------------------------------------------------- /examples/lru.rs: -------------------------------------------------------------------------------- 1 | 2 | use algorithm::LruCache; 3 | 4 | #[cfg(feature="ttl")] 5 | fn run_ttl() { 6 | let mut lru = LruCache::new(3); 7 | lru.insert_with_ttl("help", "ok", 1); 8 | assert_eq!(lru.len(), 1); 9 | std::thread::sleep(std::time::Duration::from_secs(1)); 10 | assert_eq!(lru.get("help"), None); 11 | assert_eq!(lru.len(), 0); 12 | } 13 | 14 | 15 | fn main() { 16 | let mut lru = LruCache::new(3); 17 | lru.insert("now", "ok"); 18 | lru.insert("hello", "algorithm"); 19 | lru.insert("this", "lru"); 20 | lru.insert("auth", "tickbh"); 21 | assert!(lru.len() == 3); 22 | assert_eq!(lru.get("hello"), Some(&"algorithm")); 23 | assert_eq!(lru.get("this"), Some(&"lru")); 24 | assert_eq!(lru.get("now"), None); 25 | 26 | #[cfg(feature="ttl")] 27 | run_ttl(); 28 | } 29 | -------------------------------------------------------------------------------- /src/buf/mod.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2022 - 2023 Wenmeng See the COPYRIGHT 2 | // file at the top-level directory of this distribution. 3 | // 4 | // Licensed under the Apache License, Version 2.0 , at your 6 | // option. This file may not be copied, modified, or distributed 7 | // except according to those terms. 8 | // 9 | // Author: tickbh 10 | // ----- 11 | // Created Date: 2023/08/28 09:38:10 12 | 13 | // copy a large content from bytes. 14 | 15 | mod binary; 16 | mod binary_mut; 17 | mod binary_ref; 18 | mod bt; 19 | mod bt_mut; 20 | 21 | pub use binary::Binary; 22 | pub use binary_mut::BinaryMut; 23 | pub use binary_ref::BinaryRef; 24 | pub use bt::Bt; 25 | pub use bt_mut::BtMut; 26 | 27 | fn panic_advance(cnt: usize, left: usize) { 28 | panic!("当前只剩余:{},无法消耗:{}", left, cnt); 29 | } 30 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = ["algorithm-macro"] 3 | 4 | [package] 5 | name = "algorithm" 6 | version = "0.1.18" 7 | edition = "2021" 8 | authors = ["tickbh "] 9 | description = "about algorithm data structure, now has ttl with lru/lru-k/lfu/arc and slab/rbtree/roaring_bitmap/timer_wheelss, 关于算法常用的数据结构" 10 | repository = "https://github.com/tickbh/algorithm-rs" 11 | license = "Apache-2.0" 12 | keywords = ["arc", "lru", "lfu", "timerwheel", "slab"] 13 | 14 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 15 | 16 | [dependencies] 17 | lazy_static = "1.5.0" 18 | hashbrown = "0.15.2" 19 | log = "0.4.27" 20 | rand = "0.9.0" 21 | 22 | [dependencies.algorithm-macro] 23 | # path = "algorithm-macro" 24 | version = "0.1" 25 | 26 | [dev-dependencies] 27 | libc = "0.2.169" 28 | slab = "0.4.9" 29 | 30 | [profile.release] 31 | opt-level = 3 32 | debug = true 33 | 34 | [features] 35 | default = ["ttl"] 36 | hashbrown = [] 37 | ttl = [] 38 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod quadsort; 2 | pub use quadsort::{quad_sort, quad_sort_order_by}; 3 | 4 | mod arr; 5 | pub mod buf; 6 | mod cache; 7 | mod key; 8 | mod map; 9 | mod timer; 10 | mod tree; 11 | mod util; 12 | 13 | pub use arr::{CircularBuffer, FixedVec, SkipList, SkipNode}; 14 | pub use cache::{ArcCache, LfuCache, LruCache, LruKCache, Reinit, Slab}; 15 | pub use key::{KeyRef, KeyWrapper}; 16 | pub use map::{BitMap, RoaringBitMap, ZSet}; 17 | pub use timer::{StampTimer, StepTimer, Timer, TimerRBTree, TimerWheel}; 18 | pub use tree::RBTree; 19 | pub use util::*; 20 | 21 | #[cfg(feature = "hashbrown")] 22 | extern crate hashbrown; 23 | 24 | #[cfg(feature = "hashbrown")] 25 | pub use hashbrown::{HashMap, HashSet}; 26 | #[cfg(not(feature = "hashbrown"))] 27 | pub use std::collections::{HashMap, HashSet}; 28 | 29 | #[cfg(feature = "hashbrown")] 30 | pub type DefaultHasher = hashbrown::DefaultHashBuilder; 31 | #[cfg(not(feature = "hashbrown"))] 32 | pub type DefaultHasher = std::collections::hash_map::RandomState; 33 | -------------------------------------------------------------------------------- /examples/skip_list.rs: -------------------------------------------------------------------------------- 1 | // use algorithm::SkipList; 2 | // fn main() { 3 | // let mut val = SkipList::new(); 4 | // val.insert(4); 5 | // val.insert(2); 6 | // val.insert(1); 7 | // let mut iter = val.iter(); 8 | // assert_eq!(iter.next(), Some(&1)); 9 | // assert_eq!(iter.next(), Some(&2)); 10 | // assert_eq!(iter.next(), Some(&4)); 11 | // assert_eq!(iter.next(), None); 12 | // let mut iter = val.iter().rev(); 13 | // assert_eq!(iter.next(), Some(&4)); 14 | // assert_eq!(iter.next(), Some(&2)); 15 | // assert_eq!(iter.next(), Some(&1)); 16 | // assert_eq!(iter.next(), None); 17 | // } 18 | 19 | use algorithm::SkipList; 20 | fn main() { 21 | let mut val = SkipList::new(); 22 | val.insert(4); 23 | val.insert(2); 24 | val.insert(1); 25 | let mut iter = val.iter(); 26 | assert_eq!(iter.next(), Some((&1, 0))); 27 | assert_eq!(iter.next(), Some((&2, 1))); 28 | assert_eq!(iter.next(), Some((&4, 2))); 29 | assert_eq!(iter.next(), None); 30 | } -------------------------------------------------------------------------------- /src/timer/mod.rs: -------------------------------------------------------------------------------- 1 | 2 | 3 | pub trait Timer { 4 | /// 当时与现在的间隔,以确定插入确定的槽 5 | fn when(&self) -> u64; 6 | /// 可能需要修改对象,此处用可变值 7 | fn when_mut(&mut self) -> u64 { 8 | self.when() 9 | } 10 | } 11 | 12 | macro_rules! impl_primitive_timer { 13 | ($ty:ident) => { 14 | impl Timer for $ty { 15 | #[inline(always)] 16 | fn when(&self) -> u64 { 17 | *self as u64 18 | } 19 | } 20 | }; 21 | } 22 | 23 | impl_primitive_timer!(u8); 24 | impl_primitive_timer!(u16); 25 | impl_primitive_timer!(u32); 26 | impl_primitive_timer!(u64); 27 | impl_primitive_timer!(u128); 28 | impl_primitive_timer!(i8); 29 | impl_primitive_timer!(i16); 30 | impl_primitive_timer!(i32); 31 | impl_primitive_timer!(i64); 32 | impl_primitive_timer!(i128); 33 | impl_primitive_timer!(f32); 34 | impl_primitive_timer!(f64); 35 | impl_primitive_timer!(usize); 36 | 37 | mod timer_rbtree; 38 | mod timer_wheel; 39 | mod stamp_timer; 40 | mod step_timer; 41 | 42 | pub use timer_wheel::TimerWheel; 43 | pub use timer_rbtree::TimerRBTree; 44 | pub use stamp_timer::StampTimer; 45 | pub use step_timer::StepTimer; 46 | -------------------------------------------------------------------------------- /benches/lru.rs: -------------------------------------------------------------------------------- 1 | // bench.rs 2 | #![feature(test)] 3 | 4 | extern crate test; 5 | 6 | use algorithm::{ArcCache, LfuCache, LruCache, LruKCache}; 7 | use test::Bencher; 8 | 9 | static BENCH_SIZE: usize = 10000; 10 | 11 | macro_rules! do_test_bench { 12 | ($cache: expr) => { 13 | for i in 0..BENCH_SIZE { 14 | $cache.insert(i, i); 15 | $cache.get(&i); 16 | } 17 | }; 18 | } 19 | #[bench] 20 | fn calc_lru(b: &mut Bencher) { 21 | b.iter(|| { 22 | let mut lru = LruCache::new(BENCH_SIZE / 2); 23 | do_test_bench!(lru); 24 | }) 25 | } 26 | 27 | 28 | #[bench] 29 | fn calc_lruk(b: &mut Bencher) { 30 | b.iter(|| { 31 | let mut lruk = LruKCache::new(BENCH_SIZE / 2); 32 | do_test_bench!(lruk); 33 | }) 34 | } 35 | 36 | #[bench] 37 | fn calc_lfu(b: &mut Bencher) { 38 | b.iter(|| { 39 | let mut lfu = LfuCache::new(BENCH_SIZE / 2); 40 | do_test_bench!(lfu); 41 | }) 42 | } 43 | 44 | #[bench] 45 | fn calc_arc(b: &mut Bencher) { 46 | b.iter(|| { 47 | let mut arc = ArcCache::new(BENCH_SIZE / 2); 48 | do_test_bench!(arc); 49 | }) 50 | } 51 | 52 | -------------------------------------------------------------------------------- /src/timer/stamp_timer.rs: -------------------------------------------------------------------------------- 1 | use std::time::{Duration, SystemTime, UNIX_EPOCH}; 2 | 3 | use super::Timer; 4 | 5 | pub struct StampTimer { 6 | duration: Duration, 7 | is_sec: bool, 8 | pub val: T, 9 | } 10 | 11 | impl StampTimer { 12 | pub fn new(val: T, duration: Duration) -> Self { 13 | let is_sec = duration.as_secs() as u128 * 1000 == duration.as_millis(); 14 | Self { val, duration, is_sec } 15 | } 16 | 17 | pub fn new_second(val: T, duration: Duration) -> Self { 18 | Self { 19 | val, 20 | duration, 21 | is_sec: true, 22 | } 23 | } 24 | 25 | pub fn new_millis(val: T, duration: Duration) -> Self { 26 | Self { 27 | val, 28 | duration, 29 | is_sec: false, 30 | } 31 | } 32 | } 33 | 34 | impl Timer for StampTimer { 35 | fn when(&self) -> u64 { 36 | let when = SystemTime::now() + self.duration; 37 | if self.is_sec { 38 | when.duration_since(UNIX_EPOCH).unwrap().as_secs() 39 | } else { 40 | when.duration_since(UNIX_EPOCH).unwrap().as_millis() as u64 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /examples/timer.rs: -------------------------------------------------------------------------------- 1 | use algorithm::TimerWheel; 2 | 3 | fn main() { 4 | // let mut timer = TimerWheel::new(); 5 | // timer.append_timer_wheel(60, "SecondWheel"); 6 | // timer.append_timer_wheel(60, "MinuteWheel"); 7 | // timer.append_timer_wheel(12, "HourWheel"); 8 | 9 | // timer.add_timer(30); 10 | // assert_eq!(timer.get_delay_id(), 30); 11 | // timer.add_timer(149); 12 | // assert_eq!(timer.get_delay_id(), 30); 13 | // let t = timer.add_timer(600); 14 | // assert_eq!(timer.get_delay_id(), 30); 15 | // timer.add_timer(1); 16 | // assert_eq!(timer.get_delay_id(), 1); 17 | // timer.del_timer(t); 18 | // timer.add_timer(150); 19 | // assert_eq!(timer.get_delay_id(), 1); 20 | 21 | // let val = timer.update_deltatime(30).unwrap(); 22 | // assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![1, 30]); 23 | 24 | // timer.add_timer(2); 25 | 26 | // let val = timer.update_deltatime(119).unwrap(); 27 | // assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![2, 149]); 28 | 29 | // let val = timer.update_deltatime(1).unwrap(); 30 | // assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![150]); 31 | 32 | // assert!(timer.is_empty()); 33 | 34 | let mut timer = TimerWheel::new(); 35 | timer.set_one_step(5); 36 | timer.append_timer_wheel(200, "MillisWheel"); 37 | timer.append_timer_wheel(60, "SecondWheel"); 38 | timer.append_timer_wheel(60, "MinuteWheel"); 39 | 40 | timer.add_timer(1000); 41 | 42 | let val = timer.update_deltatime(2000).unwrap(); 43 | println!("val = {:?}", val); 44 | 45 | 46 | let val = timer.update_deltatime(2000); 47 | println!("val = {:?}", val); 48 | 49 | } 50 | -------------------------------------------------------------------------------- /examples/cache_macro.rs: -------------------------------------------------------------------------------- 1 | use std::{thread, time::{Duration, Instant}}; 2 | 3 | use algorithm::LruCache; 4 | use algorithm_macro::cache; 5 | 6 | #[cache(LruCache : LruCache::new(20))] 7 | #[cache_cfg(ignore_args = call_count)] 8 | #[cache_cfg(thread)] 9 | fn fib(x: u64, call_count: &mut u32) -> u64 { 10 | *call_count += 1; 11 | if x <= 1 { 12 | 1 13 | } else { 14 | fib(x - 1, call_count) + fib(x - 2, call_count) 15 | } 16 | } 17 | 18 | #[cache(LruCache : LruCache::new(20))] 19 | fn slow_func(u: u64) -> u64 { 20 | thread::sleep(Duration::from_secs(1)); 21 | u * 10 22 | } 23 | 24 | fn slow_func_not_cache(u: u64) -> u64 { 25 | thread::sleep(Duration::from_secs(1)); 26 | u * 10 27 | } 28 | fn main() { 29 | let now = Instant::now(); 30 | let cache_ret: u64 = (0..21).map(|v| slow_func(v % 3)).into_iter().sum(); 31 | let cache_elapsed = now.elapsed(); 32 | 33 | let now = Instant::now(); 34 | let normal_ret: u64 = (0..21).map(|v| slow_func_not_cache(v % 3)).into_iter().sum(); 35 | let normal_elapsed = now.elapsed(); 36 | 37 | assert_eq!(cache_ret, normal_ret); 38 | assert!(normal_elapsed.as_secs() > cache_elapsed.as_secs() * 6); 39 | 40 | println!("cache_elapsed = {}ms", cache_elapsed.as_millis()); 41 | println!("normal_elapsed = {}ms", normal_elapsed.as_millis()); 42 | // let mut call_count = 0; 43 | // assert_eq!(fib(39, &mut call_count), 102_334_155); 44 | // assert_eq!(call_count, 40); 45 | // const CALC_VALUE: u128 = 99; 46 | // let now = Instant::now(); 47 | // let cache_ret = cache_fib(CALC_VALUE); 48 | // let cache_elapsed = now.elapsed(); 49 | 50 | // let now = Instant::now(); 51 | // let normal_ret = fibonacci(CALC_VALUE); 52 | // let normal_elapsed = now.elapsed(); 53 | 54 | // assert_eq!(cache_ret, normal_ret); 55 | // println!("cache_elapsed = {}ms", cache_elapsed.as_millis()); 56 | 57 | // println!("normal_elapsed = {}ms", normal_elapsed.as_millis()); 58 | 59 | } 60 | -------------------------------------------------------------------------------- /examples/slab.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::{ptr, time::Instant}; 3 | 4 | use algorithm::{Reinit, Slab}; 5 | 6 | const ARRAY_SIZE: usize = 10240; 7 | const NUM: usize = usize::MAX - 99999; 8 | const ZERO_ARRAY: [usize; ARRAY_SIZE] = [NUM; ARRAY_SIZE]; 9 | struct TestStruct { 10 | array: [usize; ARRAY_SIZE], 11 | size: usize, 12 | val: String, 13 | } 14 | 15 | impl Default for TestStruct { 16 | fn default() -> Self { 17 | Self { array: [NUM; ARRAY_SIZE], size: 0, val: "slab".to_string(), } 18 | } 19 | } 20 | 21 | impl Reinit for TestStruct { 22 | #[inline(always)] 23 | fn reinit(&mut self) { 24 | self.size = 0; 25 | self.val.clear(); 26 | self.val.push_str("slab"); 27 | unsafe { 28 | ptr::copy_nonoverlapping(&ZERO_ARRAY[0], &mut self.array[0], ARRAY_SIZE); 29 | } 30 | } 31 | } 32 | 33 | fn main() { 34 | let times = 100000; 35 | let now = Instant::now(); 36 | let mut slab = Slab::::new(); 37 | let mut sum: usize = 0; 38 | for i in 0..times { 39 | let (next, test) = slab.get_reinit_next_val(); 40 | test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); 41 | sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); 42 | slab.remove(next); 43 | } 44 | println!("algorithm: all cost times {}ms, sum = {}", now.elapsed().as_millis(), sum); 45 | 46 | let now = Instant::now(); 47 | let mut slab = slab::Slab::::new(); 48 | let mut sum: usize = 0; 49 | for i in 0..times { 50 | let next = slab.insert(TestStruct::default()); 51 | let test = &mut slab[next]; 52 | test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); 53 | sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); 54 | slab.remove(next); 55 | } 56 | println!("tokio::slab: all cost times {}ms, sum = {}", now.elapsed().as_millis(), sum); 57 | 58 | let now = Instant::now(); 59 | let mut sum: usize = 0; 60 | for i in 0..times { 61 | let mut test = TestStruct::default(); 62 | test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); 63 | sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); 64 | drop(test); 65 | } 66 | println!("normal alloc: all cost times {}ms, sum = {}", now.elapsed().as_millis(), sum); 67 | } 68 | -------------------------------------------------------------------------------- /src/key.rs: -------------------------------------------------------------------------------- 1 | use std::borrow::Borrow; 2 | use std::hash::Hash; 3 | use std::ptr; 4 | 5 | pub struct KeyRef { 6 | pub k: *const K, 7 | } 8 | 9 | impl KeyRef { 10 | pub fn new(k: *const K) -> Self { 11 | Self { k } 12 | } 13 | } 14 | 15 | impl Default for KeyRef { 16 | fn default() -> Self { 17 | Self { k: ptr::null() } 18 | } 19 | } 20 | 21 | impl Clone for KeyRef { 22 | fn clone(&self) -> Self { 23 | Self { k: self.k } 24 | } 25 | } 26 | 27 | impl Hash for KeyRef { 28 | fn hash(&self, state: &mut H) { 29 | unsafe { 30 | (*self.k).hash(state); 31 | } 32 | } 33 | } 34 | 35 | impl PartialEq for KeyRef { 36 | fn eq(&self, other: &Self) -> bool { 37 | unsafe { (*self.k).eq(&*other.k) } 38 | } 39 | } 40 | 41 | impl Eq for KeyRef {} 42 | 43 | impl PartialOrd for KeyRef { 44 | fn partial_cmp(&self, other: &Self) -> Option { 45 | self.k.partial_cmp(&other.k) 46 | } 47 | } 48 | 49 | impl Ord for KeyRef { 50 | fn cmp(&self, other: &Self) -> std::cmp::Ordering { 51 | self.k.cmp(&other.k) 52 | } 53 | } 54 | 55 | // 确保新类型与其内部类型的内存布局完全相同 56 | #[repr(transparent)] 57 | pub struct KeyWrapper(Q); 58 | 59 | impl KeyWrapper { 60 | pub fn from_ref(key: &Q) -> &Self { 61 | // 类型一致,直接内存直接做转化 62 | unsafe { &*(key as *const Q as *const KeyWrapper) } 63 | } 64 | } 65 | 66 | impl Hash for KeyWrapper { 67 | fn hash(&self, state: &mut H) { 68 | (self.0).hash(state); 69 | } 70 | } 71 | 72 | impl PartialEq for KeyWrapper { 73 | fn eq(&self, other: &Self) -> bool { 74 | (&self.0).eq(&other.0) 75 | } 76 | } 77 | 78 | impl Eq for KeyWrapper {} 79 | 80 | impl Borrow> for KeyRef 81 | where 82 | K: Borrow, 83 | Q: ?Sized, 84 | { 85 | fn borrow(&self) -> &KeyWrapper { 86 | let key = unsafe { &*self.k }.borrow(); 87 | KeyWrapper::from_ref(key) 88 | } 89 | } 90 | 91 | impl Borrow> for *const K 92 | where 93 | K: Borrow, 94 | Q: ?Sized, 95 | { 96 | fn borrow(&self) -> &KeyWrapper { 97 | let key = unsafe { &**self }.borrow(); 98 | KeyWrapper::from_ref(key) 99 | } 100 | } 101 | 102 | -------------------------------------------------------------------------------- /examples/bench_lru.rs: -------------------------------------------------------------------------------- 1 | use std::time::Instant; 2 | use algorithm::{ArcCache, LfuCache, LruCache, LruKCache}; 3 | 4 | macro_rules! do_test_bench { 5 | ($name: expr, $cache: expr, $num: expr, $evict: expr, $data: expr) => { 6 | let mut cost = vec![]; 7 | let now = Instant::now(); 8 | let mut all = 0; 9 | let mut hit = 0; 10 | for v in $data { 11 | if v.1 == 0 { 12 | all += 1; 13 | if $cache.get(&v.0).is_some() { 14 | hit += 1; 15 | } 16 | } else { 17 | $cache.insert(v.0, v.1); 18 | } 19 | } 20 | cost.push(now.elapsed().as_micros()); 21 | println!("|{}|{}|{:.2}%|", $name, cost.iter().map(|v| v.to_string()).collect::>().join("\t"), hit as f64 * 100.0 / all as f64); 22 | }; 23 | } 24 | 25 | #[allow(dead_code)] 26 | fn build_order_data(num: usize) -> Vec<(usize, usize)> { 27 | let mut data = vec![]; 28 | for i in 0..num { 29 | data.push((i, i + 1)); 30 | data.push((i, 0)); 31 | } 32 | data 33 | } 34 | 35 | #[allow(dead_code)] 36 | fn build_freq_data(num: usize) -> Vec<(usize, usize)> { 37 | let mut data = vec![]; 38 | for i in 0..num { 39 | data.push((i, i + 1)); 40 | let ridx = i / 4 + 1; 41 | for _ in 0..1 { 42 | data.push((rand::random::() as usize % ridx, 0)); 43 | } 44 | } 45 | data 46 | } 47 | 48 | 49 | #[allow(dead_code)] 50 | fn build_high_freq_data(num: usize) -> Vec<(usize, usize)> { 51 | let mut data = vec![]; 52 | for i in 0..num { 53 | data.push((i, i + 1)); 54 | let ridx = (i / 4 + 1).min(1000); 55 | for _ in 0..10 { 56 | data.push((rand::random::() as usize % ridx, 0)); 57 | } 58 | for _ in 0..5 { 59 | data.push((i + num + rand::random::() as usize % num, i + 1)); 60 | } 61 | } 62 | data 63 | } 64 | 65 | fn do_bench(num: usize) { 66 | let evict = num * 2; 67 | let mut lru = LruCache::new(num); 68 | let mut lruk = LruKCache::new(num); 69 | let mut lfu = LfuCache::new(num); 70 | let mut arc = ArcCache::new(num / 2); 71 | println!("|名字|耗时|命中率|"); 72 | println!("|---|---|---|"); 73 | // let data = build_freq_data(evict); 74 | let data = build_high_freq_data(evict); 75 | // let data = build_order_data(evict); 76 | do_test_bench!("LruCache", lru, num, evict, &data); 77 | do_test_bench!("LruKCache", lruk, num, evict, &data); 78 | do_test_bench!("LfuCache", lfu, num, evict, &data); 79 | do_test_bench!("ArcCache", arc, num, evict, &data); 80 | } 81 | 82 | fn main() { 83 | do_bench(1e5 as usize); 84 | } -------------------------------------------------------------------------------- /algorithm-macro/src/config.rs: -------------------------------------------------------------------------------- 1 | use std::default::Default; 2 | use std::collections::HashSet; 3 | 4 | use quote::ToTokens; 5 | use syn::{self, Token, parenthesized}; 6 | use syn::parse::{Parse, ParseStream}; 7 | 8 | pub struct Config { 9 | pub ignore_args: HashSet, 10 | pub use_thread: bool, 11 | } 12 | 13 | struct IgnoreArgsAttrib { 14 | ignore_args: HashSet, 15 | } 16 | 17 | enum ConfigAttrib { 18 | IgnoreArgs(IgnoreArgsAttrib), 19 | UseTread, 20 | } 21 | 22 | const CONFIG_ATTRIBUTE_NAME: &'static str = "cache_cfg"; 23 | 24 | impl Config { 25 | // Parse any additional attributes present after `lru_cache` and return a configuration object 26 | // created from their contents. Additionally, return any attributes that were not handled here. 27 | pub fn parse_from_attributes(attribs: &[syn::Attribute]) -> syn::Result<(Config, Vec)> { 28 | let mut parsed_attributes = Vec::new(); 29 | let mut remaining_attributes = Vec::new(); 30 | 31 | for attrib in attribs { 32 | let segs = &attrib.path().segments; 33 | if segs.len() > 0 { 34 | if segs[0].ident == CONFIG_ATTRIBUTE_NAME { 35 | let tokens = attrib.meta.to_token_stream(); 36 | let parsed = syn::parse2::(tokens)?; 37 | parsed_attributes.push(parsed); 38 | } 39 | else { 40 | remaining_attributes.push(attrib.clone()); 41 | } 42 | } 43 | } 44 | 45 | let mut config: Config = Default::default(); 46 | 47 | for parsed_attrib in parsed_attributes { 48 | match parsed_attrib { 49 | ConfigAttrib::IgnoreArgs(val) => config.ignore_args = val.ignore_args, 50 | ConfigAttrib::UseTread => config.use_thread = true, 51 | } 52 | } 53 | 54 | Ok((config, remaining_attributes)) 55 | } 56 | } 57 | 58 | impl Default for Config { 59 | fn default() -> Config { 60 | Config { 61 | ignore_args: HashSet::new(), 62 | use_thread: false, 63 | } 64 | } 65 | } 66 | 67 | impl Parse for ConfigAttrib { 68 | fn parse(input: ParseStream) -> syn::parse::Result { 69 | let _name = input.parse::()?; 70 | let content; 71 | let _paren = parenthesized!(content in input); 72 | let name = content.parse::()?; 73 | match &name.to_string()[..] { 74 | "ignore_args" => Ok(ConfigAttrib::IgnoreArgs(content.parse::()?)), 75 | "thread" => Ok(ConfigAttrib::UseTread), 76 | _ => Err(syn::parse::Error::new( 77 | name.span(), format!("unrecognized config option '{}'", name.to_string()) 78 | )) 79 | } 80 | } 81 | } 82 | 83 | impl Parse for IgnoreArgsAttrib { 84 | fn parse(input: ParseStream) -> syn::parse::Result { 85 | input.parse::()?; 86 | let elems = syn::punctuated::Punctuated::::parse_terminated(input)?; 87 | Ok(IgnoreArgsAttrib { 88 | ignore_args: elems.into_iter().collect(), 89 | }) 90 | } 91 | } -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "algorithm" 7 | version = "0.1.18" 8 | dependencies = [ 9 | "algorithm-macro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 10 | "hashbrown", 11 | "lazy_static", 12 | "libc", 13 | "log", 14 | "rand", 15 | "slab", 16 | ] 17 | 18 | [[package]] 19 | name = "algorithm-macro" 20 | version = "0.1.0" 21 | dependencies = [ 22 | "lazy_static", 23 | "proc-macro2", 24 | "quote", 25 | "syn", 26 | ] 27 | 28 | [[package]] 29 | name = "algorithm-macro" 30 | version = "0.1.0" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "424b05459baefbb9b11558a464acc1b773a76d036bcaeee98b9bc6658c4ea926" 33 | dependencies = [ 34 | "lazy_static", 35 | "proc-macro2", 36 | "quote", 37 | "syn", 38 | ] 39 | 40 | [[package]] 41 | name = "allocator-api2" 42 | version = "0.2.18" 43 | source = "registry+https://github.com/rust-lang/crates.io-index" 44 | checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" 45 | 46 | [[package]] 47 | name = "autocfg" 48 | version = "1.3.0" 49 | source = "registry+https://github.com/rust-lang/crates.io-index" 50 | checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" 51 | 52 | [[package]] 53 | name = "bitflags" 54 | version = "2.9.0" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" 57 | 58 | [[package]] 59 | name = "cfg-if" 60 | version = "1.0.0" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 63 | 64 | [[package]] 65 | name = "equivalent" 66 | version = "1.0.1" 67 | source = "registry+https://github.com/rust-lang/crates.io-index" 68 | checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" 69 | 70 | [[package]] 71 | name = "foldhash" 72 | version = "0.1.4" 73 | source = "registry+https://github.com/rust-lang/crates.io-index" 74 | checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" 75 | 76 | [[package]] 77 | name = "getrandom" 78 | version = "0.3.2" 79 | source = "registry+https://github.com/rust-lang/crates.io-index" 80 | checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" 81 | dependencies = [ 82 | "cfg-if", 83 | "libc", 84 | "r-efi", 85 | "wasi", 86 | ] 87 | 88 | [[package]] 89 | name = "hashbrown" 90 | version = "0.15.2" 91 | source = "registry+https://github.com/rust-lang/crates.io-index" 92 | checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" 93 | dependencies = [ 94 | "allocator-api2", 95 | "equivalent", 96 | "foldhash", 97 | ] 98 | 99 | [[package]] 100 | name = "lazy_static" 101 | version = "1.5.0" 102 | source = "registry+https://github.com/rust-lang/crates.io-index" 103 | checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" 104 | 105 | [[package]] 106 | name = "libc" 107 | version = "0.2.169" 108 | source = "registry+https://github.com/rust-lang/crates.io-index" 109 | checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" 110 | 111 | [[package]] 112 | name = "log" 113 | version = "0.4.27" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" 116 | 117 | [[package]] 118 | name = "ppv-lite86" 119 | version = "0.2.17" 120 | source = "registry+https://github.com/rust-lang/crates.io-index" 121 | checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" 122 | 123 | [[package]] 124 | name = "proc-macro2" 125 | version = "1.0.86" 126 | source = "registry+https://github.com/rust-lang/crates.io-index" 127 | checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" 128 | dependencies = [ 129 | "unicode-ident", 130 | ] 131 | 132 | [[package]] 133 | name = "quote" 134 | version = "1.0.36" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" 137 | dependencies = [ 138 | "proc-macro2", 139 | ] 140 | 141 | [[package]] 142 | name = "r-efi" 143 | version = "5.2.0" 144 | source = "registry+https://github.com/rust-lang/crates.io-index" 145 | checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" 146 | 147 | [[package]] 148 | name = "rand" 149 | version = "0.9.0" 150 | source = "registry+https://github.com/rust-lang/crates.io-index" 151 | checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" 152 | dependencies = [ 153 | "rand_chacha", 154 | "rand_core", 155 | "zerocopy", 156 | ] 157 | 158 | [[package]] 159 | name = "rand_chacha" 160 | version = "0.9.0" 161 | source = "registry+https://github.com/rust-lang/crates.io-index" 162 | checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" 163 | dependencies = [ 164 | "ppv-lite86", 165 | "rand_core", 166 | ] 167 | 168 | [[package]] 169 | name = "rand_core" 170 | version = "0.9.3" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" 173 | dependencies = [ 174 | "getrandom", 175 | ] 176 | 177 | [[package]] 178 | name = "slab" 179 | version = "0.4.9" 180 | source = "registry+https://github.com/rust-lang/crates.io-index" 181 | checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" 182 | dependencies = [ 183 | "autocfg", 184 | ] 185 | 186 | [[package]] 187 | name = "syn" 188 | version = "2.0.74" 189 | source = "registry+https://github.com/rust-lang/crates.io-index" 190 | checksum = "1fceb41e3d546d0bd83421d3409b1460cc7444cd389341a4c880fe7a042cb3d7" 191 | dependencies = [ 192 | "proc-macro2", 193 | "quote", 194 | "unicode-ident", 195 | ] 196 | 197 | [[package]] 198 | name = "unicode-ident" 199 | version = "1.0.12" 200 | source = "registry+https://github.com/rust-lang/crates.io-index" 201 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" 202 | 203 | [[package]] 204 | name = "wasi" 205 | version = "0.14.2+wasi-0.2.4" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" 208 | dependencies = [ 209 | "wit-bindgen-rt", 210 | ] 211 | 212 | [[package]] 213 | name = "wit-bindgen-rt" 214 | version = "0.39.0" 215 | source = "registry+https://github.com/rust-lang/crates.io-index" 216 | checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" 217 | dependencies = [ 218 | "bitflags", 219 | ] 220 | 221 | [[package]] 222 | name = "zerocopy" 223 | version = "0.8.24" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879" 226 | dependencies = [ 227 | "zerocopy-derive", 228 | ] 229 | 230 | [[package]] 231 | name = "zerocopy-derive" 232 | version = "0.8.24" 233 | source = "registry+https://github.com/rust-lang/crates.io-index" 234 | checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be" 235 | dependencies = [ 236 | "proc-macro2", 237 | "quote", 238 | "syn", 239 | ] 240 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## algorithm/ 算法结构相关 2 | [![crates.io](https://img.shields.io/crates/v/algorithm.svg)](https://crates.io/crates/algorithm) 3 | [![rustc 1.70.0](https://img.shields.io/badge/rust-1.70%2B-orange.svg)](https://img.shields.io/badge/rust-1.70%2B-orange.svg) 4 | [![Released API docs](https://docs.rs/algorithm/badge.svg)](https://docs.rs/algorithm) 5 | 6 | 将提供一些常用的数据结构以供使用。目前提供的数据结构 7 | * **LruCache** 最近未使用缓存,可用feature启用ttl 8 | * **LruKCache** 最近未使用缓存, K次分类列表,可用feature启用ttl 9 | * **LfuCache** 按缓存访问次数做排序,优先淘汰访问最少次数的,可用feature启用ttl 10 | * **ArcCache** Adaptive Replacement Cache,自适应缓存替换算法,可用feature启用ttl 11 | * **Slab** 仿linux中的Slab结构,对大对象做到初始化缓存使用 12 | * **BitMap** 位图, 按位做标记的图 13 | * **RoaringBitMap** 位图, 因为位图占用的内存太大, 对于稀疏位图会更小内存 14 | * **TimerWheel** 计时器轮, 模仿时钟的高效定时器组件 15 | * **CircularBuffer** 环形Buffer组件, 适用于内存限定较严格的, 设置不超过缓存值的环形结构 16 | * **RBTree** 红黑村, 高效的排序树, 可用于做定时器组件 17 | * **FixedVec** 模拟指针的可变长数组 18 | 19 | # lru 全称是Least Recently Used,即最近最久未使用的意思。 20 | 每次元素访问将其更新到列表的最前,时间复杂度为O(1)。当达到容量限制时将淘汰双向列表中的链尾数据 21 | ```rust 22 | use algorithm::LruCache; 23 | fn main() { 24 | let mut lru = LruCache::new(3); 25 | lru.insert("now", "ok"); 26 | lru.insert("hello", "algorithm"); 27 | lru.insert("this", "lru"); 28 | lru.insert("auth", "tickbh"); 29 | assert!(lru.len() == 3); 30 | assert_eq!(lru.get("hello"), Some(&"algorithm")); 31 | assert_eq!(lru.get("this"), Some(&"lru")); 32 | assert_eq!(lru.get("now"), None); 33 | } 34 | ``` 35 | # lru-k 36 | 将访问次数达到k的目标值放进到优先队列,lru-k的主要目的是为了解决LRU算法“缓存污染”的问题,其核心思想是将“最近使用过1次”的判断标准扩展为“最近使用过K次”。 37 | 相比LRU,LRU-K需要多维护一个队列,用于记录所有缓存数据被访问的历史。只有当数据的访问次数达到K次的时候,才将数据放入缓存。当需要淘汰数据时,LRU-K会淘汰第K次访问时间距当前时间最大的数据。 38 | 39 | ```rust 40 | use algorithm::LruKCache; 41 | fn main() { 42 | let mut lru = LruKCache::with_times(3, 3); 43 | lru.insert("this", "lru"); 44 | for _ in 0..3 { 45 | let _ = lru.get("this"); 46 | } 47 | lru.insert("hello", "algorithm"); 48 | lru.insert("auth", "tickbh"); 49 | assert!(lru.len() == 3); 50 | lru.insert("auth1", "tickbh"); 51 | assert_eq!(lru.get("this"), Some(&"lru")); 52 | assert_eq!(lru.get("hello"), None); 53 | assert!(lru.len() == 3); 54 | } 55 | ``` 56 | 57 | # lfu (least frequently used)最近频次使用 58 | 每个元素在被访问或者更新的时候将其访问次数+1,当元素满时将优先淘汰掉访问次数最少的数据。 59 | ```rust 60 | 61 | use algorithm::LfuCache; 62 | fn main() { 63 | let mut lru = LfuCache::new(3); 64 | lru.insert("hello", "algorithm"); 65 | lru.insert("this", "lru"); 66 | lru.set_reduce_count(100); 67 | assert!(lru.get_visit(&"hello") == Some(5)); 68 | assert!(lru.get_visit(&"this") == Some(5)); 69 | for _ in 0..98 { 70 | let _ = lru.get("this"); 71 | } 72 | assert!(lru.get_visit(&"this") == Some(51)); 73 | assert!(lru.get_visit(&"hello") == Some(2)); 74 | let mut keys = lru.keys(); 75 | assert!(keys.next()==Some(&"this")); 76 | assert!(keys.next()==Some(&"hello")); 77 | assert!(keys.next() == None); 78 | } 79 | ``` 80 | 81 | # slab 缓存块组,linux中缓存对象的分配器 82 | 缓存对象需实现Default,将会使对象缓存起来,避免频繁的重复申请释放带来的开销 83 | 84 | 以下我们以简单的测试来进行对比,algorithm::Slab与slab::Slab与普通的alloc 85 | 86 | 以下测试场景相对简单,可能对`slab::Slab`较为不公平 87 | 88 | ```rust 89 | use std::{ptr, time::Instant}; 90 | 91 | use algorithm::{Reinit, Slab}; 92 | 93 | const ARRAY_SIZE: usize = 10240; 94 | const NUM: usize = usize::MAX - 99999; 95 | const ZERO_ARRAY: [usize; ARRAY_SIZE] = [NUM; ARRAY_SIZE]; 96 | struct TestStruct { 97 | array: [usize; ARRAY_SIZE], 98 | size: usize, 99 | val: String, 100 | } 101 | 102 | impl Default for TestStruct { 103 | fn default() -> Self { 104 | Self { array: [NUM; ARRAY_SIZE], size: 0, val: "slab".to_string(), } 105 | } 106 | } 107 | 108 | impl Reinit for TestStruct { 109 | #[inline(always)] 110 | fn reinit(&mut self) { 111 | self.size = 0; 112 | self.val.clear(); 113 | self.val.push_str("slab"); 114 | unsafe { 115 | ptr::copy_nonoverlapping(&ZERO_ARRAY[0], &mut self.array[0], ARRAY_SIZE); 116 | } 117 | } 118 | } 119 | 120 | fn main() { 121 | let times = 100000; 122 | let now = Instant::now(); 123 | let mut slab = Slab::::new(); 124 | let mut sum: usize = 0; 125 | for i in 0..times { 126 | let (next, test) = slab.get_reinit_next_val(); 127 | test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); 128 | sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); 129 | slab.remove(next); 130 | } 131 | println!("algorithm: all cost times {}ms, sum = {}", now.elapsed().as_millis(), sum); 132 | 133 | 134 | let now = Instant::now(); 135 | let mut slab = slab::Slab::::new(); 136 | let mut sum: usize = 0; 137 | for i in 0..times { 138 | let next = slab.insert(TestStruct::default()); 139 | let test = &mut slab[next]; 140 | test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); 141 | sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); 142 | slab.remove(next); 143 | } 144 | println!("tokio::slab: all cost times {}ms, sum = {}", now.elapsed().as_millis(), sum); 145 | 146 | let now = Instant::now(); 147 | let mut sum: usize = 0; 148 | for i in 0..times { 149 | let mut test = TestStruct::default(); 150 | test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); 151 | sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); 152 | drop(test); 153 | } 154 | println!("normal alloc: all cost times {}ms, sum = {}", now.elapsed().as_millis(), sum); 155 | } 156 | ``` 157 | 最终用release命令进行输出测试,结果均为一致 158 | 159 | 但是耗时algorithm避免了申请创建的开销,耗时相对较短,做的仅仅将对象重新reinit 160 | 161 | 在此场景中tokio::slab即进行了申请又开销了插入及删除,反而耗时最长 162 | ```console 163 | algorithm: all cost times 132ms, sum = 18446744063712505088 164 | tokio::slab: all cost times 477ms, sum = 18446744063712505088 165 | normal alloc: all cost times 337ms, sum = 18446744063712505088 166 | ``` 167 | 168 | # 计时器轮(TimerWheel),模拟时钟格式组成的高效计时器 169 | 170 | 1. **环形数据结构**:TimerWheel,即时间轮,是一个环形的数据结构,类似于时钟的面,被等分为多个格子或槽位(slot)。 171 | 172 | 2. **槽位时间间隔**:每个槽位代表一个固定的时间间隔,例如1毫秒、1秒等。这个时间间隔决定了定时器的精度。 173 | 174 | 3. **初始化**:在算法开始时,需要初始化时间轮,包括设定时间轮的大小(即槽位的数量)和每个槽位代表的时间间隔。即当插入数据后即不允许修改时轮信息。 175 | 176 | ```rust 177 | use algorithm::TimerWheel; 178 | 179 | fn main() { 180 | let mut timer = TimerWheel::new(); 181 | timer.append_timer_wheel(12, 60 * 60, "HourWheel"); 182 | timer.append_timer_wheel(60, 60, "MinuteWheel"); 183 | timer.append_timer_wheel(60, 1, "SecondWheel"); 184 | 185 | timer.add_timer(30); 186 | assert_eq!(timer.get_delay_id(), 30); 187 | timer.add_timer(149); 188 | assert_eq!(timer.get_delay_id(), 30); 189 | let t = timer.add_timer(600); 190 | assert_eq!(timer.get_delay_id(), 30); 191 | timer.add_timer(1); 192 | assert_eq!(timer.get_delay_id(), 1); 193 | timer.del_timer(t); 194 | timer.add_timer(150); 195 | assert_eq!(timer.get_delay_id(), 1); 196 | 197 | let val = timer.update_deltatime(30).unwrap(); 198 | assert_eq!(val, vec![1, 30]); 199 | 200 | timer.add_timer(2); 201 | 202 | let val = timer.update_deltatime(119).unwrap(); 203 | assert_eq!(val, vec![2, 149]); 204 | 205 | let val = timer.update_deltatime(1).unwrap(); 206 | assert_eq!(val, vec![150]); 207 | 208 | assert!(timer.is_empty()); 209 | } 210 | ``` 211 | 212 | 213 | # 添加宏支持, 可快速的缓存函数的结果 214 | 215 | 216 | ```rust 217 | use algorithm::LruCache; 218 | use algorithm_macro::cache; 219 | 220 | #[cache(LruCache : LruCache::new(20))] 221 | #[cache_cfg(ignore_args = call_count)] 222 | #[cache_cfg(thread)] 223 | fn fib(x: u64, call_count: &mut u32) -> u64 { 224 | *call_count += 1; 225 | if x <= 1 { 226 | 1 227 | } else { 228 | fib(x - 1, call_count) + fib(x - 2, call_count) 229 | } 230 | } 231 | ``` 232 | 如此就可以快速将函数的执行结果进行缓存加速. 233 | 234 | ## Star History 235 | 236 | [![Star History Chart](https://api.star-history.com/svg?repos=tickbh/algorithm-rs&type=Date)](https://star-history.com/#tickbh/algorithm-rs&Date) 237 | -------------------------------------------------------------------------------- /src/map/zset.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::marker::PhantomData; 3 | use std::time::{SystemTime, UNIX_EPOCH}; 4 | use std::{borrow::Borrow, hash::Hash, mem, usize}; 5 | 6 | use crate::arr::SkipIter; 7 | use crate::{KeyRef, KeyWrapper, SkipList, SkipNode}; 8 | 9 | struct Context { 10 | key: mem::MaybeUninit, 11 | score: isize, 12 | timestamp: usize, 13 | } 14 | 15 | impl Default for Context { 16 | fn default() -> Self { 17 | Self { 18 | key: mem::MaybeUninit::uninit(), 19 | score: Default::default(), 20 | timestamp: Default::default(), 21 | } 22 | } 23 | } 24 | 25 | impl PartialEq for Context { 26 | fn eq(&self, other: &Self) -> bool { 27 | self.score == other.score && self.timestamp == other.timestamp 28 | } 29 | } 30 | 31 | impl PartialOrd for Context { 32 | fn partial_cmp(&self, other: &Self) -> Option { 33 | match self.score.partial_cmp(&other.score) { 34 | Some(core::cmp::Ordering::Equal) => { 35 | return self.timestamp.partial_cmp(&other.timestamp) 36 | } 37 | ord => return ord, 38 | } 39 | } 40 | } 41 | 42 | /// 一种可排序的Set类型, 可以高效的对评分进行排序, 43 | /// 44 | /// # Examples 45 | /// 46 | /// ``` 47 | /// use algorithm::ZSet; 48 | /// fn main() { 49 | /// let mut val = ZSet::new(); 50 | /// val.add_or_update("aa", 10); 51 | /// val.add_or_update("bb", 12); 52 | /// assert_eq!(val.len(), 2); 53 | /// assert_eq!(val.rank(&"bb"), 2); 54 | /// val.add_or_update("bb", 9); 55 | /// assert_eq!(val.rank(&"bb"), 1); 56 | /// assert_eq!(val.len(), 2); 57 | /// } 58 | /// ``` 59 | pub struct ZSet { 60 | max_count: usize, 61 | reverse: bool, 62 | zsl: SkipList>, 63 | dict: HashMap, *mut SkipNode>>, 64 | } 65 | 66 | impl ZSet { 67 | pub fn new() -> Self { 68 | Self { 69 | max_count: usize::MAX, 70 | reverse: false, 71 | zsl: SkipList::new(), 72 | dict: HashMap::new(), 73 | } 74 | } 75 | 76 | pub fn new_with(max_count: usize, reverse: bool) -> Self { 77 | Self { 78 | max_count, 79 | reverse, 80 | zsl: SkipList::new(), 81 | dict: HashMap::new(), 82 | } 83 | } 84 | 85 | pub fn len(&self) -> usize { 86 | assert!(self.dict.len() == self.zsl.len()); 87 | self.dict.len() 88 | } 89 | 90 | /// 清除集合 91 | /// 92 | /// # Examples 93 | /// 94 | /// ``` 95 | /// use algorithm::ZSet; 96 | /// fn main() { 97 | /// let mut val = ZSet::new(); 98 | /// val.add_or_update("aa", 10); 99 | /// val.add_or_update("bb", 12); 100 | /// assert_eq!(val.len(), 2); 101 | /// val.clear(); 102 | /// assert_eq!(val.len(), 0); 103 | /// } 104 | /// ``` 105 | /// 106 | pub fn clear(&mut self) { 107 | self.dict.clear(); 108 | self.zsl.clear(); 109 | } 110 | 111 | /// 包含键值 112 | /// 113 | /// # Examples 114 | /// 115 | /// ``` 116 | /// use algorithm::ZSet; 117 | /// fn main() { 118 | /// let mut val = ZSet::new(); 119 | /// val.add_or_update("aa", 10); 120 | /// val.add_or_update("bb", 12); 121 | /// assert_eq!(val.contains_key(&"aa"), true); 122 | /// } 123 | /// ``` 124 | /// 125 | pub fn contains_key(&mut self, k: &Q) -> bool 126 | where 127 | K: Borrow, 128 | Q: Hash + Eq + ?Sized, 129 | { 130 | self.dict.contains_key(KeyWrapper::from_ref(k)) 131 | } 132 | 133 | /// 获取排序值 134 | /// 135 | /// # Examples 136 | /// 137 | /// ``` 138 | /// use algorithm::ZSet; 139 | /// fn main() { 140 | /// let mut val = ZSet::new(); 141 | /// val.add_or_update("aa", 10); 142 | /// val.add_or_update("bb", 12); 143 | /// assert_eq!(val.len(), 2); 144 | /// 145 | /// } 146 | /// ``` 147 | pub fn rank(&mut self, k: &Q) -> usize 148 | where 149 | K: Borrow, 150 | Q: Hash + Eq + ?Sized, 151 | { 152 | if let Some(v) = self.dict.get(KeyWrapper::from_ref(k)) { 153 | return self.zsl.get_rank(unsafe { &(**v).score }); 154 | } 155 | 0 156 | } 157 | 158 | /// 删除元素 159 | /// 160 | /// # Examples 161 | /// 162 | /// ``` 163 | /// use algorithm::ZSet; 164 | /// fn main() { 165 | /// let mut val = ZSet::new(); 166 | /// val.add_or_update("aa", 10); 167 | /// val.add_or_update("bb", 12); 168 | /// assert_eq!(val.len(), 2); 169 | /// assert!(val.remove(&"bb")); 170 | /// assert_eq!(val.len(), 1); 171 | /// } 172 | /// ``` 173 | pub fn remove(&mut self, k: &Q) -> bool 174 | where 175 | K: Borrow, 176 | Q: Hash + Eq + ?Sized, 177 | { 178 | if let Some(v) = self.dict.remove(KeyWrapper::from_ref(k)) { 179 | self.zsl.remove(unsafe { &(*v).score }) 180 | } else { 181 | false 182 | } 183 | } 184 | 185 | /// 添加或者更新值 186 | /// 187 | /// # Examples 188 | /// 189 | /// ``` 190 | /// use algorithm::ZSet; 191 | /// fn main() { 192 | /// let mut val = ZSet::new(); 193 | /// val.add_or_update("aa", 10); 194 | /// val.add_or_update("bb", 12); 195 | /// assert_eq!(val.len(), 2); 196 | /// val.add_or_update("bb", 14); 197 | /// assert_eq!(val.len(), 2); 198 | /// assert_eq!(val.score(&"bb"), 14); 199 | /// 200 | /// } 201 | /// ``` 202 | pub fn add_or_update(&mut self, key: K, mut score: isize) { 203 | if self.max_count == 0 || self.max_count == self.dict.len() { 204 | return; 205 | } 206 | 207 | if self.reverse { 208 | score = -score; 209 | } 210 | 211 | let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); 212 | let context = Context { 213 | key: mem::MaybeUninit::new(key), 214 | score, 215 | timestamp: now.as_millis() as usize, 216 | }; 217 | 218 | let key_ref = KeyRef::new(context.key.as_ptr()); 219 | if let Some(v) = self.dict.remove(&key_ref) { 220 | let ret = self.zsl.update(unsafe { &(*v).score }, context); 221 | let key_ref = KeyRef::new(unsafe { (*ret).score.key.as_ptr() }); 222 | self.dict.insert(key_ref, ret); 223 | } else { 224 | let ret = self.zsl.insert(context); 225 | let key_ref = KeyRef::new(unsafe { (*ret).score.key.as_ptr() }); 226 | self.dict.insert(key_ref, ret); 227 | } 228 | } 229 | 230 | /// 获取score值 231 | /// 232 | /// # Examples 233 | /// 234 | /// ``` 235 | /// use algorithm::ZSet; 236 | /// fn main() { 237 | /// let mut val = ZSet::new(); 238 | /// val.add_or_update("aa", 10); 239 | /// val.add_or_update("bb", 12); 240 | /// assert_eq!(val.score(&"bb"), 12); 241 | /// 242 | /// } 243 | /// ``` 244 | pub fn score(&mut self, k: &Q) -> isize 245 | where 246 | K: Borrow, 247 | Q: Hash + Eq + ?Sized, 248 | { 249 | if let Some(v) = self.dict.get(KeyWrapper::from_ref(k)) { 250 | return unsafe { (**v).score.score }; 251 | } 252 | 0 253 | } 254 | 255 | /// 遍历值 256 | /// 257 | /// # Examples 258 | /// 259 | /// ``` 260 | /// use algorithm::ZSet; 261 | /// fn main() { 262 | /// let mut val = ZSet::new(); 263 | /// val.add_or_update("aa", 10); 264 | /// val.add_or_update("bb", 12); 265 | /// let mut iter = val.iter(); 266 | /// assert_eq!(iter.next(), Some((&"aa", 0, 10))); 267 | /// assert_eq!(iter.next(), Some((&"bb", 1, 12))); 268 | /// assert_eq!(iter.next(), None); 269 | /// } 270 | /// ``` 271 | pub fn iter(&self) -> ZSetIter { 272 | ZSetIter::new(self.zsl.iter()) 273 | } 274 | } 275 | 276 | impl Drop for ZSet { 277 | fn drop(&mut self) { 278 | self.clear(); 279 | } 280 | } 281 | 282 | pub struct ZSetIter<'a, K: 'a + Hash + Eq> { 283 | iter: SkipIter<'a, Context>, 284 | data: PhantomData<&'a ()>, 285 | } 286 | 287 | impl<'a, T: Hash + Eq> ZSetIter<'a, T> { 288 | fn new(iter: SkipIter<'a, Context>) -> Self { 289 | Self { 290 | iter, 291 | data: PhantomData, 292 | } 293 | } 294 | } 295 | 296 | impl<'a, T: Hash + Eq> Iterator for ZSetIter<'a, T> { 297 | type Item = (&'a T, usize, isize); 298 | 299 | fn next(&mut self) -> Option { 300 | match self.iter.next() { 301 | None => return None, 302 | Some((v, s)) => return Some((unsafe { v.key.assume_init_ref() }, s, v.score)), 303 | } 304 | } 305 | 306 | fn size_hint(&self) -> (usize, Option) { 307 | self.iter.size_hint() 308 | } 309 | } 310 | 311 | impl<'a, T: Hash + Eq> DoubleEndedIterator for ZSetIter<'a, T> { 312 | fn next_back(&mut self) -> Option { 313 | match self.iter.next_back() { 314 | None => return None, 315 | Some((v, s)) => return Some((unsafe { v.key.assume_init_ref() }, s, v.score)), 316 | } 317 | } 318 | } 319 | -------------------------------------------------------------------------------- /algorithm-macro/src/lib.rs: -------------------------------------------------------------------------------- 1 | 2 | use proc_macro::TokenStream; 3 | use syn; 4 | use syn::{Token, parse_quote}; 5 | use syn::spanned::Spanned; 6 | use syn::punctuated::Punctuated; 7 | use quote::quote; 8 | use proc_macro2; 9 | 10 | mod config; 11 | 12 | use syn::parse::Parse; 13 | use syn::parse::ParseStream; 14 | use syn::parse_macro_input; 15 | 16 | struct Attr { 17 | cache_type: syn::Type, 18 | cache_creation_expr: syn::Expr, 19 | } 20 | 21 | impl Parse for Attr { 22 | fn parse(input: ParseStream) -> syn::parse::Result { 23 | let cache_type: syn::Type = input.parse()?; 24 | input.parse::()?; 25 | let cache_creation_expr: syn::Expr = input.parse()?; 26 | Ok(Attr { 27 | cache_type, 28 | cache_creation_expr, 29 | }) 30 | } 31 | } 32 | 33 | #[proc_macro_attribute] 34 | pub fn cache(attr: TokenStream, item: TokenStream) -> TokenStream { 35 | let attr = parse_macro_input!(attr as Attr); 36 | 37 | match algorithm_cache_impl(attr, item.clone()) { 38 | Ok(tokens) => return tokens, 39 | Err(e) => { 40 | panic!("error = {:?}", e); 41 | } 42 | } 43 | } 44 | 45 | // The main entry point for the macro. 46 | fn algorithm_cache_impl(attr: Attr, item: TokenStream) -> syn::parse::Result { 47 | let mut original_fn: syn::ItemFn = syn::parse(item.clone())?; 48 | let (macro_config, out_attributes) = 49 | { 50 | let attribs = &original_fn.attrs[..]; 51 | config::Config::parse_from_attributes(attribs)? 52 | }; 53 | original_fn.attrs = out_attributes; 54 | 55 | let mut new_fn = original_fn.clone(); 56 | let return_type = get_cache_fn_return_type(&original_fn)?; 57 | let new_name = format!("__cache_auto_{}", original_fn.sig.ident.to_string()); 58 | original_fn.sig.ident = syn::Ident::new(&new_name[..], original_fn.sig.ident.span()); 59 | let (call_args, types, cache_args) = get_args_and_types(&original_fn, ¯o_config)?; 60 | let cloned_args = make_cloned_args_tuple(&cache_args); 61 | let fn_path = path_from_ident(original_fn.sig.ident.clone()); 62 | let fn_call = syn::ExprCall { 63 | attrs: Vec::new(), 64 | paren_token: syn::token::Paren::default(), 65 | args: call_args, 66 | func: Box::new(fn_path) 67 | }; 68 | 69 | let tuple_type = syn::TypeTuple { 70 | paren_token: syn::token::Paren::default(), 71 | elems: types, 72 | }; 73 | 74 | let cache_type = &attr.cache_type; 75 | let cache_type_with_generics: syn::Type = parse_quote! { 76 | #cache_type<#tuple_type, #return_type, algorithm::DefaultHasher> 77 | }; 78 | let lru_body = build_cache_body(&cache_type_with_generics, &attr.cache_creation_expr, &cloned_args, 79 | &fn_call, ¯o_config); 80 | 81 | new_fn.block = Box::new(lru_body); 82 | let out = quote! { 83 | #original_fn 84 | #new_fn 85 | }; 86 | Ok(out.into()) 87 | } 88 | 89 | // Build the body of the caching function. What is constructed depends on the config value. 90 | fn build_cache_body(full_cache_type: &syn::Type, cache_new: &syn::Expr, 91 | cloned_args: &syn::ExprTuple, inner_fn_call: &syn::ExprCall, 92 | config: &config::Config) -> syn::Block 93 | { 94 | if config.use_thread { 95 | build_mutex_cache_body(full_cache_type, cache_new, cloned_args, inner_fn_call) 96 | } else { 97 | build_tls_cache_body(full_cache_type, cache_new, cloned_args, inner_fn_call) 98 | } 99 | } 100 | 101 | // Build the body of the caching function which puts the cache in thread-local storage. 102 | fn build_tls_cache_body(full_cache_type: &syn::Type, cache_new: &syn::Expr, 103 | cloned_args: &syn::ExprTuple, inner_fn_call: &syn::ExprCall) -> syn::Block 104 | { 105 | parse_quote! { 106 | { 107 | use std::cell::RefCell; 108 | use std::thread_local; 109 | thread_local!( 110 | static cache: RefCell<#full_cache_type> = 111 | RefCell::new(#cache_new); 112 | ); 113 | cache.with(|c| { 114 | let mut cache_ref = c.borrow_mut(); 115 | let cloned_args = #cloned_args; 116 | 117 | let stored_result = cache_ref.get_mut(&cloned_args); 118 | if let Some(stored_result) = stored_result { 119 | return stored_result.clone() 120 | } 121 | 122 | // Don't hold a mutable borrow across 123 | // the recursive function call 124 | drop(cache_ref); 125 | 126 | let ret = #inner_fn_call; 127 | c.borrow_mut().insert(cloned_args, ret.clone()); 128 | ret 129 | }) 130 | } 131 | } 132 | } 133 | 134 | // Build the body of the caching function which guards the static cache with a mutex. 135 | fn build_mutex_cache_body(full_cache_type: &syn::Type, cache_new: &syn::Expr, 136 | cloned_args: &syn::ExprTuple, inner_fn_call: &syn::ExprCall) -> syn::Block 137 | { 138 | parse_quote! { 139 | { 140 | use lazy_static::lazy_static; 141 | use std::sync::Mutex; 142 | 143 | lazy_static! { 144 | static ref cache: Mutex<#full_cache_type> = 145 | Mutex::new(#cache_new); 146 | } 147 | 148 | let cloned_args = #cloned_args; 149 | 150 | let mut cache_unlocked = cache.lock().unwrap(); 151 | let stored_result = cache_unlocked.get_mut(&cloned_args); 152 | if let Some(stored_result) = stored_result { 153 | return stored_result.clone(); 154 | }; 155 | 156 | // must unlock here to allow potentially recursive call 157 | drop(cache_unlocked); 158 | 159 | let ret = #inner_fn_call; 160 | let mut cache_unlocked = cache.lock().unwrap(); 161 | cache_unlocked.insert(cloned_args, ret.clone()); 162 | ret 163 | } 164 | } 165 | } 166 | 167 | fn get_cache_fn_return_type(original_fn: &syn::ItemFn) -> syn::Result> { 168 | if let syn::ReturnType::Type(_, ref ty) = original_fn.sig.output { 169 | Ok(ty.clone()) 170 | } else { 171 | return Err(syn::Error::new_spanned(original_fn, "There's no point of caching the output of a function that has no output")) 172 | } 173 | } 174 | 175 | fn path_from_ident(ident: syn::Ident) -> syn::Expr { 176 | let mut segments: Punctuated<_, Token![::]> = Punctuated::new(); 177 | segments.push(syn::PathSegment { ident: ident, arguments: syn::PathArguments::None }); 178 | syn::Expr::Path(syn::ExprPath { attrs: Vec::new(), qself: None, path: syn::Path { leading_colon: None, segments: segments} }) 179 | } 180 | 181 | fn make_cloned_args_tuple(args: &Punctuated) -> syn::ExprTuple { 182 | let mut cloned_args = Punctuated::<_, Token![,]>::new(); 183 | for arg in args { 184 | let call = syn::ExprMethodCall { 185 | attrs: Vec::new(), 186 | receiver: Box::new(arg.clone()), 187 | dot_token: syn::token::Dot { spans: [arg.span(); 1] }, 188 | method: syn::Ident::new("clone", proc_macro2::Span::call_site()), 189 | turbofish: None, 190 | paren_token: syn::token::Paren::default(), 191 | args: Punctuated::new(), 192 | }; 193 | cloned_args.push(syn::Expr::MethodCall(call)); 194 | } 195 | syn::ExprTuple { 196 | attrs: Vec::new(), 197 | paren_token: syn::token::Paren::default(), 198 | elems: cloned_args, 199 | } 200 | } 201 | 202 | fn get_args_and_types(f: &syn::ItemFn, config: &config::Config) -> 203 | syn::Result<(Punctuated, Punctuated, Punctuated)> 204 | { 205 | let mut call_args = Punctuated::<_, Token![,]>::new(); 206 | let mut types = Punctuated::<_, Token![,]>::new(); 207 | let mut cache_args = Punctuated::<_, Token![,]>::new(); 208 | 209 | for input in &f.sig.inputs { 210 | match input { 211 | syn::FnArg::Receiver(_) => { 212 | return Err(syn::Error::new(input.span(), "`self` arguments are currently unsupported by algorithm_cache")); 213 | 214 | } 215 | syn::FnArg::Typed(p) => { 216 | let mut segments: syn::punctuated::Punctuated<_, Token![::]> = syn::punctuated::Punctuated::new(); 217 | let arg_name; 218 | if let syn::Pat::Ident(ref pat_ident) = *p.pat { 219 | arg_name = pat_ident.ident.clone(); 220 | segments.push(syn::PathSegment { ident: pat_ident.ident.clone(), arguments: syn::PathArguments::None }); 221 | } else { 222 | return Err(syn::Error::new(input.span(), "unsupported argument kind")); 223 | } 224 | 225 | let arg_path = syn::Expr::Path(syn::ExprPath { attrs: Vec::new(), qself: None, path: syn::Path { leading_colon: None, segments } }); 226 | if !config.ignore_args.contains(&arg_name) { 227 | // If the arg type is a reference, remove the reference because the arg will be cloned 228 | if let syn::Type::Reference(type_reference) = &*p.ty { 229 | if let Some(_) = type_reference.mutability { 230 | call_args.push(arg_path); 231 | continue; 232 | // return Err(io::Error::new(io::ErrorKind::Other, "`mut` reference arguments are not supported as this could lead to incorrect results being stored")); 233 | } 234 | types.push(type_reference.elem.as_ref().to_owned()); // as_ref -> to_owned unboxes the type 235 | } else { 236 | types.push((*p.ty).clone()); 237 | } 238 | 239 | cache_args.push(arg_path.clone()); 240 | } 241 | call_args.push(arg_path); 242 | } 243 | } 244 | } 245 | 246 | if types.len() == 1 { 247 | types.push_punct(syn::token::Comma { spans: [proc_macro2::Span::call_site(); 1] }) 248 | } 249 | 250 | Ok((call_args, types, cache_args)) 251 | } -------------------------------------------------------------------------------- /src/timer/timer_rbtree.rs: -------------------------------------------------------------------------------- 1 | use crate::HashMap; 2 | 3 | use crate::RBTree; 4 | use std::cmp::Ordering; 5 | use std::u64; 6 | use std::vec; 7 | 8 | use super::Timer; 9 | 10 | #[derive(PartialEq, Eq)] 11 | struct TreeKey(u64, u64); 12 | 13 | impl Ord for TreeKey { 14 | fn cmp(&self, other: &Self) -> Ordering { 15 | if self.0 != other.0 { 16 | return self.0.cmp(&other.0); 17 | } 18 | other.1.cmp(&self.1) 19 | } 20 | } 21 | 22 | impl PartialOrd for TreeKey { 23 | fn partial_cmp(&self, other: &Self) -> Option { 24 | Some(self.cmp(other)) 25 | } 26 | } 27 | 28 | /// 计时器轮,模拟时钟格式组成的高效计时器 29 | /// 30 | /// 时间轮是一个环形的数据结构,可以想象成一个时钟的面,被分成多个格子 31 | /// 32 | /// 每个格子代表一段时间,这段时间越短,定时器的精度就越高。 33 | /// 34 | /// 每个格子用一个Vec存储放在该格子上的延时任务。 35 | /// 36 | /// Mark: 在Rust中双向链表中暂未提供元素关键列表的接口,这里改用Vec,删除时会额外移动Vec值 37 | /// 38 | /// # Examples 39 | /// 40 | /// ``` 41 | /// use algorithm::TimerRBTree; 42 | /// fn main() { 43 | /// let mut timer = TimerRBTree::new(); 44 | /// timer.add_timer(30); 45 | /// assert_eq!(timer.tick_first(), Some(30)); 46 | /// timer.add_timer(149); 47 | /// assert_eq!(timer.tick_first(), Some(30)); 48 | /// let t = timer.add_timer(600); 49 | /// assert_eq!(timer.tick_first(), Some(30)); 50 | /// timer.add_timer(1); 51 | /// assert_eq!(timer.tick_first(), Some(1)); 52 | /// timer.del_timer(t); 53 | /// timer.add_timer(150); 54 | /// assert_eq!(timer.tick_first(), Some(1)); 55 | /// let val = timer.update_deltatime(30).unwrap(); 56 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![1, 30]); 57 | /// timer.add_timer(2); 58 | /// let val = timer.update_deltatime(119).unwrap(); 59 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![2, 149]); 60 | /// let val = timer.update_deltatime(1).unwrap(); 61 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![150]); 62 | /// assert!(timer.is_empty()); 63 | /// } 64 | /// ``` 65 | pub struct TimerRBTree { 66 | tree: RBTree, 67 | 68 | map: HashMap, 69 | 70 | /// 当时记录的时序 71 | cur_step: u64, 72 | 73 | /// id记录 74 | next_timer_id: u64, 75 | /// max id 76 | max_timer_id: u64, 77 | } 78 | 79 | impl TimerRBTree { 80 | pub fn new() -> Self { 81 | Self { 82 | tree: RBTree::new(), 83 | map: HashMap::new(), 84 | cur_step: 0, 85 | next_timer_id: 1, 86 | max_timer_id: u64::MAX, 87 | } 88 | } 89 | 90 | /// 获取定时器的长度 91 | /// # Examples 92 | /// 93 | /// ``` 94 | /// use algorithm::TimerRBTree; 95 | /// fn main() { 96 | /// let mut timer = TimerRBTree::::new(); 97 | /// assert!(timer.is_empty()); 98 | /// timer.add_timer(1); 99 | /// assert_eq!(timer.len(), 1); 100 | /// let t = timer.add_timer(2); 101 | /// assert_eq!(timer.len(), 2); 102 | /// timer.del_timer(t); 103 | /// assert_eq!(timer.len(), 1); 104 | /// } 105 | /// ``` 106 | pub fn len(&self) -> usize { 107 | self.tree.len() 108 | } 109 | 110 | pub fn is_empty(&self) -> bool { 111 | self.tree.is_empty() 112 | } 113 | 114 | /// 清除所有的槽位 115 | /// # Examples 116 | /// 117 | /// ``` 118 | /// use algorithm::TimerRBTree; 119 | /// fn main() { 120 | /// let mut timer = TimerRBTree::::new(); 121 | /// assert!(timer.is_empty()); 122 | /// timer.add_timer(1); 123 | /// timer.add_timer(2); 124 | /// assert_eq!(timer.len(), 2); 125 | /// timer.clear(); 126 | /// assert_eq!(timer.len(), 0); 127 | /// } 128 | /// ``` 129 | pub fn clear(&mut self) { 130 | self.tree.clear(); 131 | self.map.clear(); 132 | self.cur_step = 0; 133 | self.next_timer_id = 1; 134 | } 135 | 136 | pub fn get_max_timerid(&self) -> u64 { 137 | self.max_timer_id 138 | } 139 | 140 | pub fn set_max_timerid(&mut self, max: u64) { 141 | self.max_timer_id = max; 142 | } 143 | 144 | fn get_next_timerid(&mut self) -> u64 { 145 | let mut timer_id; 146 | loop { 147 | timer_id = self.next_timer_id; 148 | if self.next_timer_id >= self.max_timer_id { 149 | self.next_timer_id = 1; 150 | } else { 151 | self.next_timer_id = self.next_timer_id + 1; 152 | } 153 | 154 | if !self.map.contains_key(&timer_id) { 155 | break; 156 | } 157 | } 158 | timer_id 159 | } 160 | 161 | /// 添加定时器元素 162 | /// # Examples 163 | /// 164 | /// ``` 165 | /// use algorithm::TimerRBTree; 166 | /// fn main() { 167 | /// let mut timer = TimerRBTree::new(); 168 | /// timer.add_timer(30); 169 | /// assert_eq!(timer.len(), 1); 170 | /// } 171 | pub fn add_timer(&mut self, val: T) -> u64 { 172 | let timer_id = self.get_next_timerid(); 173 | self.add_timer_by_id(timer_id, val); 174 | timer_id 175 | } 176 | 177 | pub fn add_timer_by_id(&mut self, timer_id: u64, val: T) { 178 | let when = val.when(); 179 | self.tree.insert(TreeKey(when, timer_id), val); 180 | self.map.insert(timer_id, when); 181 | } 182 | /// 删除指定的定时器,时间复杂度为O(logn), 183 | /// 184 | /// # Examples 185 | /// 186 | /// ``` 187 | /// use algorithm::TimerRBTree; 188 | /// fn main() { 189 | /// let mut timer = TimerRBTree::new(); 190 | /// let t = timer.add_timer(30); 191 | /// timer.del_timer(t); 192 | /// assert_eq!(timer.len(), 0); 193 | /// } 194 | pub fn del_timer(&mut self, timer_id: u64) -> Option { 195 | if let Some(when) = self.map.remove(&timer_id) { 196 | let tree = TreeKey(when, timer_id); 197 | self.tree.remove(&tree).map(|e| e) 198 | } else { 199 | None 200 | } 201 | } 202 | 203 | /// 获取指定的定时器,时间复杂度为O(log(n)) 204 | /// 205 | /// # Examples 206 | /// 207 | /// ``` 208 | /// use algorithm::TimerRBTree; 209 | /// fn main() { 210 | /// let mut timer = TimerRBTree::new(); 211 | /// let t = timer.add_timer(30); 212 | /// assert_eq!(timer.get_timer(&t), Some(&30)); 213 | /// } 214 | pub fn get_timer(&self, timer_id: &u64) -> Option<&T> { 215 | if let Some(when) = self.map.get(timer_id) { 216 | let tree = TreeKey(*when, *timer_id); 217 | self.tree.get(&tree).map(|e| e) 218 | } else { 219 | None 220 | } 221 | } 222 | 223 | /// 获取指定的定时器,时间复杂度为O(log(n)) 224 | /// 225 | /// # Examples 226 | /// 227 | /// ``` 228 | /// use algorithm::TimerRBTree; 229 | /// fn main() { 230 | /// let mut timer = TimerRBTree::new(); 231 | /// let t = timer.add_timer(30); 232 | /// *timer.get_mut_timer(&t).unwrap() = 33; 233 | /// let val = timer.update_deltatime(30).unwrap(); 234 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![33]); 235 | /// } 236 | pub fn get_mut_timer(&mut self, timer_id: &u64) -> Option<&mut T> { 237 | if let Some(when) = self.map.get(timer_id) { 238 | let tree = TreeKey(*when, *timer_id); 239 | self.tree.get_mut(&tree).map(|e| e) 240 | } else { 241 | None 242 | } 243 | } 244 | 245 | /// 取出时间轴最小的一个值 246 | pub fn tick_first(&self) -> Option { 247 | self.tree 248 | .get_first() 249 | .map(|(key, _)| Some(key.0)) 250 | .unwrap_or(None) 251 | } 252 | 253 | /// 判断到指定时间是否有小于该指定值的实例 254 | pub fn tick_time(&mut self, tm: u64) -> Option { 255 | if tm < self.tick_first().unwrap_or(tm + 1) { 256 | return None; 257 | } 258 | self.tree.pop_first().map(|(_, e)| e) 259 | } 260 | 261 | /// 计时器轮的递进时间 262 | /// 263 | /// # Examples 264 | /// 265 | /// ``` 266 | /// use algorithm::TimerRBTree; 267 | /// fn main() { 268 | /// let mut timer = TimerRBTree::new(); 269 | /// timer.add_timer(30); 270 | /// let val = timer.update_deltatime(30).unwrap(); 271 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![30]); 272 | /// } 273 | pub fn update_now(&mut self, now: u64) -> Option> { 274 | self.cur_step = now; 275 | let mut result = vec![]; 276 | loop { 277 | if let Some(val) = self.tick_first() { 278 | if self.cur_step < val { 279 | break; 280 | } 281 | result.push(self.tree.pop_first().map(|(k, e)| (k.1, e)).unwrap()); 282 | } else { 283 | break; 284 | } 285 | } 286 | Some(result) 287 | } 288 | /// 计时器轮的递进时间 289 | /// 290 | /// # Examples 291 | /// 292 | /// ``` 293 | /// use algorithm::TimerRBTree; 294 | /// fn main() { 295 | /// let mut timer = TimerRBTree::new(); 296 | /// timer.add_timer(30); 297 | /// let val = timer.update_deltatime(30).unwrap(); 298 | /// assert_eq!(val, vec![(1, 30)]); 299 | /// } 300 | pub fn update_deltatime(&mut self, delta: u64) -> Option> { 301 | self.update_now(self.cur_step.wrapping_add(delta)) 302 | } 303 | 304 | /// 计时器轮的递进时间 305 | /// 306 | /// # Examples 307 | /// 308 | /// ``` 309 | /// use algorithm::TimerRBTree; 310 | /// fn main() { 311 | /// let mut timer = TimerRBTree::new(); 312 | /// timer.add_timer(30); 313 | /// let mut idx = 0; 314 | /// let mut timer_id = 0; 315 | /// timer.update_deltatime_with_callback(30, &mut |_, id, v| { 316 | /// timer_id = id; 317 | /// idx = v; 318 | /// None 319 | /// }); 320 | /// assert_eq!(timer_id, 1); 321 | /// assert_eq!(idx, 30); 322 | /// } 323 | pub fn update_deltatime_with_callback(&mut self, delta: u64, f: &mut F) 324 | where 325 | F: FnMut(&mut Self, u64, T) -> Option<(u64, T)>, 326 | { 327 | self.update_now_with_callback(self.cur_step.wrapping_add(delta), f) 328 | } 329 | 330 | pub fn update_now_with_callback(&mut self, now: u64, f: &mut F) 331 | where 332 | F: FnMut(&mut Self, u64, T) -> Option<(u64, T)>, 333 | { 334 | if let Some(result) = self.update_now(now) { 335 | let mut collect_result = vec![]; 336 | for r in result.into_iter() { 337 | if let Some(v) = (*f)(self, r.0, r.1) { 338 | collect_result.push(v); 339 | } 340 | } 341 | for (timer_id, v) in collect_result.drain(..) { 342 | self.add_timer_by_id(timer_id, v); 343 | } 344 | } 345 | } 346 | } 347 | -------------------------------------------------------------------------------- /src/buf/binary_ref.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2022 - 2023 Wenmeng See the COPYRIGHT 2 | // file at the top-level directory of this distribution. 3 | // 4 | // Licensed under the Apache License, Version 2.0 , at your 6 | // option. This file may not be copied, modified, or distributed 7 | // except according to those terms. 8 | // 9 | // Author: tickbh 10 | // ----- 11 | // Created Date: 2023/09/14 12:21:02 12 | 13 | use std::fmt::Debug; 14 | use std::io::{self, Error}; 15 | use std::marker::PhantomData; 16 | use std::ops::Deref; 17 | use std::{borrow::Borrow, cmp, hash, io::Read, io::Result, slice}; 18 | 19 | use super::Binary; 20 | 21 | use super::Bt; 22 | 23 | static EMPTY_ARRAY: &[u8] = &[]; 24 | 25 | /// 二进制引用的封装, 只针对引用 26 | pub struct BinaryRef<'a> { 27 | ptr: *const u8, 28 | // 游标值, 可以得出当前指向的位置 29 | cursor: usize, 30 | // 标记值, 从上一次标记到现在的游标值, 可以得出偏移的对象 31 | mark: usize, 32 | // 长度值, 还剩下多少的长度 33 | len: usize, 34 | 35 | data: PhantomData<&'a ()>, 36 | } 37 | 38 | impl<'a> BinaryRef<'a> { 39 | pub fn new() -> BinaryRef<'a> { 40 | BinaryRef::from(EMPTY_ARRAY) 41 | } 42 | 43 | /// # Examples 44 | /// 45 | /// ``` 46 | /// use algorithm::buf::Binary; 47 | /// 48 | /// let b = Binary::from(&b"hello"[..]); 49 | /// assert_eq!(b.len(), 5); 50 | /// ``` 51 | /// 52 | pub fn len(&self) -> usize { 53 | self.len 54 | } 55 | 56 | /// Returns true if the `Binary` has a length of 0. 57 | /// 58 | /// # Examples 59 | /// 60 | /// ``` 61 | /// use algorithm::buf::Binary; 62 | /// 63 | /// let b = Binary::new(); 64 | /// assert!(b.is_empty()); 65 | /// ``` 66 | #[inline] 67 | pub const fn is_empty(&self) -> bool { 68 | self.len == 0 69 | } 70 | 71 | pub fn to_vec(&self) -> Vec { 72 | unsafe { slice::from_raw_parts(self.ptr, self.len).to_vec() } 73 | } 74 | 75 | #[inline] 76 | fn as_slice(&self) -> &[u8] { 77 | unsafe { slice::from_raw_parts(self.ptr, self.len) } 78 | } 79 | 80 | #[inline] 81 | unsafe fn inc_start(&mut self, by: usize) { 82 | if by == 0 { 83 | return; 84 | } 85 | // should already be asserted, but debug assert for tests 86 | debug_assert!(self.len >= by, "internal: inc_start out of bounds"); 87 | self.len -= by; 88 | self.ptr = self.ptr.add(by); 89 | self.cursor += by; 90 | } 91 | 92 | // #[inline] 93 | // unsafe fn sub_start(&mut self, by: usize) { 94 | // // should already be asserted, but debug assert for tests 95 | // debug_assert!(self.cursor >= by, "internal: inc_start out of bounds"); 96 | // self.len += by; 97 | // self.ptr = self.ptr.sub(by); 98 | // self.cursor -= by; 99 | // self.mark = std::cmp::min(self.mark, self.cursor); 100 | // } 101 | 102 | pub fn copy_from_slice(data: &'a [u8]) -> Self { 103 | data.into() 104 | } 105 | 106 | #[inline] 107 | pub fn into_slice_all(&self) -> Vec { 108 | self.to_vec() 109 | } 110 | } 111 | 112 | impl<'a> Clone for BinaryRef<'a> { 113 | fn clone(&self) -> Self { 114 | BinaryRef { 115 | ptr: self.ptr, 116 | cursor: self.cursor, 117 | mark: self.mark, 118 | len: self.len, 119 | data: self.data.clone(), 120 | } 121 | } 122 | } 123 | 124 | impl<'a> Drop for BinaryRef<'a> { 125 | fn drop(&mut self) {} 126 | } 127 | 128 | impl<'a> From<&'a str> for BinaryRef<'a> { 129 | fn from(value: &'a str) -> Self { 130 | BinaryRef::from(value.as_bytes()) 131 | } 132 | } 133 | 134 | impl<'a> From<&'a [u8]> for BinaryRef<'a> { 135 | fn from(value: &'a [u8]) -> Self { 136 | let len = value.len(); 137 | BinaryRef { 138 | ptr: value.as_ptr(), 139 | len, 140 | mark: 0, 141 | cursor: 0, 142 | data: PhantomData, 143 | } 144 | } 145 | } 146 | 147 | impl<'a> Bt for BinaryRef<'a> { 148 | fn remaining(&self) -> usize { 149 | self.len 150 | } 151 | 152 | fn chunk(&self) -> &[u8] { 153 | self.as_slice() 154 | } 155 | 156 | fn advance_chunk(&mut self, n: usize) -> &[u8] { 157 | let ret = &unsafe { slice::from_raw_parts(self.ptr, self.len) }[..n]; 158 | self.advance(n); 159 | ret 160 | } 161 | 162 | fn advance(&mut self, n: usize) { 163 | unsafe { 164 | self.inc_start(n); 165 | } 166 | } 167 | 168 | fn into_binary(self) -> Binary { 169 | Binary::from(self.chunk().to_vec()) 170 | } 171 | } 172 | 173 | impl<'a> Read for BinaryRef<'a> { 174 | #[inline(always)] 175 | fn read(&mut self, buf: &mut [u8]) -> Result { 176 | let left = self.remaining(); 177 | if left == 0 || buf.len() == 0 { 178 | return Err(Error::new(io::ErrorKind::WouldBlock, "")); 179 | } 180 | let read = std::cmp::min(left, buf.len()); 181 | unsafe { 182 | std::ptr::copy(&self.chunk()[0], &mut buf[0], read); 183 | } 184 | self.advance(read); 185 | Ok(read) 186 | } 187 | } 188 | 189 | impl<'a> Iterator for BinaryRef<'a> { 190 | type Item = u8; 191 | #[inline] 192 | fn next(&mut self) -> Option { 193 | self.get_next() 194 | } 195 | } 196 | 197 | impl<'a> Deref for BinaryRef<'a> { 198 | type Target = [u8]; 199 | 200 | #[inline] 201 | fn deref(&self) -> &[u8] { 202 | self.as_slice() 203 | } 204 | } 205 | 206 | impl<'a> Debug for BinaryRef<'a> { 207 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 208 | f.debug_struct("Binary") 209 | .field("ptr", &self.ptr) 210 | .field("cursor", &self.cursor) 211 | .field("mark", &self.mark) 212 | .field("len", &self.len) 213 | .finish() 214 | } 215 | } 216 | 217 | impl<'a> AsRef<[u8]> for BinaryRef<'a> { 218 | #[inline] 219 | fn as_ref(&self) -> &[u8] { 220 | self.as_slice() 221 | } 222 | } 223 | 224 | impl<'a> hash::Hash for BinaryRef<'a> { 225 | fn hash(&self, state: &mut H) 226 | where 227 | H: hash::Hasher, 228 | { 229 | self.as_slice().hash(state); 230 | } 231 | } 232 | 233 | impl<'a> Borrow<[u8]> for BinaryRef<'a> { 234 | fn borrow(&self) -> &[u8] { 235 | self.as_slice() 236 | } 237 | } 238 | 239 | impl<'a> PartialEq for BinaryRef<'a> { 240 | fn eq(&self, other: &BinaryRef) -> bool { 241 | self.as_slice() == other.as_slice() 242 | } 243 | } 244 | 245 | impl<'a> PartialOrd for BinaryRef<'a> { 246 | fn partial_cmp(&self, other: &BinaryRef) -> Option { 247 | self.as_slice().partial_cmp(other.as_slice()) 248 | } 249 | } 250 | 251 | impl<'a> Ord for BinaryRef<'a> { 252 | fn cmp(&self, other: &BinaryRef) -> cmp::Ordering { 253 | self.as_slice().cmp(other.as_slice()) 254 | } 255 | } 256 | 257 | impl<'a> Eq for BinaryRef<'a> {} 258 | 259 | impl<'a> PartialEq<[u8]> for BinaryRef<'a> { 260 | fn eq(&self, other: &[u8]) -> bool { 261 | self.as_slice() == other 262 | } 263 | } 264 | 265 | impl<'a> PartialOrd<[u8]> for BinaryRef<'a> { 266 | fn partial_cmp(&self, other: &[u8]) -> Option { 267 | self.as_slice().partial_cmp(other) 268 | } 269 | } 270 | 271 | impl<'a> PartialEq> for [u8] { 272 | fn eq(&self, other: &BinaryRef) -> bool { 273 | *other == *self 274 | } 275 | } 276 | 277 | impl<'a> PartialOrd> for [u8] { 278 | fn partial_cmp(&self, other: &BinaryRef) -> Option { 279 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) 280 | } 281 | } 282 | 283 | impl<'a> PartialEq for BinaryRef<'a> { 284 | fn eq(&self, other: &str) -> bool { 285 | self.as_slice() == other.as_bytes() 286 | } 287 | } 288 | 289 | impl<'a> PartialOrd for BinaryRef<'a> { 290 | fn partial_cmp(&self, other: &str) -> Option { 291 | self.as_slice().partial_cmp(other.as_bytes()) 292 | } 293 | } 294 | 295 | impl<'a> PartialEq> for str { 296 | fn eq(&self, other: &BinaryRef) -> bool { 297 | *other == *self 298 | } 299 | } 300 | 301 | impl<'a> PartialOrd> for str { 302 | fn partial_cmp(&self, other: &BinaryRef) -> Option { 303 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) 304 | } 305 | } 306 | 307 | impl<'a> PartialEq> for BinaryRef<'a> { 308 | fn eq(&self, other: &Vec) -> bool { 309 | *self == other[..] 310 | } 311 | } 312 | 313 | impl<'a> PartialOrd> for BinaryRef<'a> { 314 | fn partial_cmp(&self, other: &Vec) -> Option { 315 | self.as_slice().partial_cmp(&other[..]) 316 | } 317 | } 318 | 319 | impl<'a> PartialEq> for Vec { 320 | fn eq(&self, other: &BinaryRef) -> bool { 321 | *other == *self 322 | } 323 | } 324 | 325 | impl<'a> PartialOrd> for Vec { 326 | fn partial_cmp(&self, other: &BinaryRef) -> Option { 327 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) 328 | } 329 | } 330 | 331 | impl<'a> PartialEq for BinaryRef<'a> { 332 | fn eq(&self, other: &String) -> bool { 333 | *self == other[..] 334 | } 335 | } 336 | 337 | impl<'a> PartialOrd for BinaryRef<'a> { 338 | fn partial_cmp(&self, other: &String) -> Option { 339 | self.as_slice().partial_cmp(other.as_bytes()) 340 | } 341 | } 342 | 343 | impl<'a> PartialEq> for String { 344 | fn eq(&self, other: &BinaryRef) -> bool { 345 | *other == *self 346 | } 347 | } 348 | 349 | impl<'a> PartialOrd> for String { 350 | fn partial_cmp(&self, other: &BinaryRef) -> Option { 351 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) 352 | } 353 | } 354 | 355 | impl<'a> PartialEq> for &[u8] { 356 | fn eq(&self, other: &BinaryRef) -> bool { 357 | *other == *self 358 | } 359 | } 360 | 361 | impl<'a> PartialOrd> for &[u8] { 362 | fn partial_cmp(&self, other: &BinaryRef) -> Option { 363 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) 364 | } 365 | } 366 | 367 | impl<'a> PartialEq> for &str { 368 | fn eq(&self, other: &BinaryRef) -> bool { 369 | *other == *self 370 | } 371 | } 372 | 373 | impl<'a> PartialOrd> for &str { 374 | fn partial_cmp(&self, other: &BinaryRef) -> Option { 375 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) 376 | } 377 | } 378 | 379 | impl<'a, T: ?Sized> PartialEq<&'a T> for BinaryRef<'a> 380 | where 381 | BinaryRef<'a>: PartialEq, 382 | { 383 | fn eq(&self, other: &&'a T) -> bool { 384 | *self == **other 385 | } 386 | } 387 | 388 | impl<'a, T: ?Sized> PartialOrd<&'a T> for BinaryRef<'a> 389 | where 390 | BinaryRef<'a>: PartialOrd, 391 | { 392 | fn partial_cmp(&self, other: &&'a T) -> Option { 393 | self.partial_cmp(&**other) 394 | } 395 | } 396 | 397 | // impl From 398 | 399 | impl<'a> Default for BinaryRef<'a> { 400 | #[inline] 401 | fn default() -> BinaryRef<'a> { 402 | BinaryRef::new() 403 | } 404 | } 405 | 406 | #[cfg(test)] 407 | mod tests { 408 | use super::{BinaryRef, Bt}; 409 | 410 | #[test] 411 | fn binary_refs() { 412 | { 413 | let s = BinaryRef::from("aaaa"); 414 | let s1 = s.clone(); 415 | drop(s1); 416 | } 417 | { 418 | let v = vec![1, 2]; 419 | let mut b = BinaryRef::from(&v[..]); 420 | let x = b.get_u8(); 421 | assert!(x == 1); 422 | let b1 = b.clone(); 423 | drop(b1); 424 | } 425 | } 426 | } 427 | -------------------------------------------------------------------------------- /src/arr/circular_buffer.rs: -------------------------------------------------------------------------------- 1 | use std::{marker::PhantomData, ops::{Index, IndexMut}}; 2 | 3 | /// 循环的圆结构 4 | /// 如果数据满了之后将自动在结尾后续添加,并保持最大个数 5 | /// 6 | /// # Examples 7 | /// 8 | /// ``` 9 | /// use algorithm::CircularBuffer; 10 | /// fn main() { 11 | /// let mut circular = CircularBuffer::new(2); 12 | /// circular.push_back(1); 13 | /// circular.push_back(2); 14 | /// circular.push_back(3); 15 | /// assert_eq!(circular.len(), 2); 16 | /// assert_eq!(circular[0], 2); 17 | /// assert_eq!(circular[1], 3); 18 | /// } 19 | /// ``` 20 | pub struct CircularBuffer { 21 | arr: Vec, 22 | head: usize, 23 | tail: usize, 24 | len: usize, 25 | cap: usize, 26 | } 27 | 28 | impl CircularBuffer { 29 | pub fn new(cap: usize) -> Self { 30 | Self { 31 | arr: Vec::with_capacity(cap), 32 | head: 0, 33 | tail: cap - 1, 34 | len: 0, 35 | cap, 36 | } 37 | } 38 | 39 | /// 是否已经填满过所有的元素 40 | /// 41 | /// # Examples 42 | /// 43 | /// ``` 44 | /// use algorithm::CircularBuffer; 45 | /// fn main() { 46 | /// let mut circular = CircularBuffer::new(2); 47 | /// circular.push_back(1); 48 | /// assert_eq!(circular.is_inited(), false); 49 | /// circular.push_back(2); 50 | /// assert_eq!(circular.is_inited(), true); 51 | /// circular.pop_back(); 52 | /// assert_eq!(circular.is_inited(), true); 53 | /// } 54 | /// ``` 55 | pub fn is_inited(&self) -> bool { 56 | self.cap == self.arr.len() 57 | } 58 | 59 | /// 是否元素已满 60 | /// 61 | /// # Examples 62 | /// 63 | /// ``` 64 | /// use algorithm::CircularBuffer; 65 | /// fn main() { 66 | /// let mut circular = CircularBuffer::new(2); 67 | /// circular.push_back(1); 68 | /// assert_eq!(circular.is_full(), false); 69 | /// circular.push_back(2); 70 | /// assert_eq!(circular.is_full(), true); 71 | /// circular.pop_back(); 72 | /// assert_eq!(circular.is_full(), false); 73 | /// } 74 | /// ``` 75 | pub fn is_full(&self) -> bool { 76 | self.cap == self.len 77 | } 78 | 79 | /// 是否元素为空 80 | /// 81 | /// # Examples 82 | /// 83 | /// ``` 84 | /// use algorithm::CircularBuffer; 85 | /// fn main() { 86 | /// let mut circular = CircularBuffer::new(2); 87 | /// assert_eq!(circular.is_empty(), true); 88 | /// circular.push_back(1); 89 | /// assert_eq!(circular.is_empty(), false); 90 | /// } 91 | /// ``` 92 | pub fn is_empty(&self) -> bool { 93 | self.len == 0 94 | } 95 | 96 | /// 返回元素长度 97 | /// 98 | /// # Examples 99 | /// 100 | /// ``` 101 | /// use algorithm::CircularBuffer; 102 | /// fn main() { 103 | /// let mut circular = CircularBuffer::new(2); 104 | /// assert_eq!(circular.is_empty(), true); 105 | /// circular.push_back(1); 106 | /// circular.push_back(2); 107 | /// assert_eq!(circular.len(), 2); 108 | /// circular.push_front(1); 109 | /// assert_eq!(circular.len(), 2); 110 | /// circular.pop_front(); 111 | /// assert_eq!(circular.len(), 1); 112 | /// } 113 | /// ``` 114 | pub fn len(&self) -> usize { 115 | self.len 116 | } 117 | 118 | fn add_fix(&self, val: usize) -> usize { 119 | (val + 1) % self.cap 120 | } 121 | 122 | fn sub_fix(&self, val: usize) -> usize { 123 | if val == 0 { 124 | self.cap - 1 125 | } else { 126 | val - 1 127 | } 128 | } 129 | 130 | 131 | /// 在元素末尾添加元素 132 | /// 133 | /// # Examples 134 | /// 135 | /// ``` 136 | /// use algorithm::CircularBuffer; 137 | /// fn main() { 138 | /// let mut circular = CircularBuffer::new(2); 139 | /// assert_eq!(circular.is_empty(), true); 140 | /// circular.push_back(1); 141 | /// circular.push_back(2); 142 | /// assert_eq!(circular[0], 1); 143 | /// } 144 | /// ``` 145 | pub fn push_back(&mut self, val: T) { 146 | if self.is_inited() { 147 | self.tail = self.add_fix(self.tail); 148 | self.arr[self.tail] = val; 149 | self.head = self.add_fix(self.head); 150 | } else { 151 | if self.tail + 1 < self.arr.len() { 152 | self.tail = self.add_fix(self.tail); 153 | self.arr[self.tail] = val; 154 | } else { 155 | self.arr.push(val); 156 | self.tail = self.arr.len() - 1; 157 | } 158 | self.len += 1; 159 | } 160 | } 161 | 162 | /// 在元素前面添加元素 163 | /// 164 | /// # Examples 165 | /// 166 | /// ``` 167 | /// use algorithm::CircularBuffer; 168 | /// fn main() { 169 | /// let mut circular = CircularBuffer::new(2); 170 | /// assert_eq!(circular.is_empty(), true); 171 | /// circular.push_front(1); 172 | /// circular.push_front(2); 173 | /// assert_eq!(circular[0], 2); 174 | /// } 175 | /// ``` 176 | pub fn push_front(&mut self, val: T) { 177 | if self.is_inited() { 178 | self.head = self.sub_fix(self.head); 179 | self.arr[self.head] = val; 180 | self.tail = self.sub_fix(self.tail); 181 | } else { 182 | if self.head > 0 { 183 | self.head = self.sub_fix(self.head); 184 | self.arr[self.head] = val; 185 | } else { 186 | self.arr.insert(0, val); 187 | self.head = 0; 188 | self.tail = self.add_fix(self.tail); 189 | } 190 | self.len += 1; 191 | } 192 | } 193 | 194 | /// 将最前面的元素弹出 195 | /// 196 | /// # Examples 197 | /// 198 | /// ``` 199 | /// use algorithm::CircularBuffer; 200 | /// fn main() { 201 | /// let mut circular = CircularBuffer::new(2); 202 | /// assert_eq!(circular.is_empty(), true); 203 | /// circular.push_front(1); 204 | /// circular.push_front(2); 205 | /// assert_eq!(circular[0], 2); 206 | /// circular.pop_front(); 207 | /// assert_eq!(circular[0], 1); 208 | /// } 209 | /// ``` 210 | pub fn pop_front(&mut self) { 211 | debug_assert!(!self.is_empty()); 212 | self.head = self.add_fix(self.head); 213 | self.len -= 1; 214 | } 215 | 216 | /// 将最后面的元素弹出 217 | /// 218 | /// # Examples 219 | /// 220 | /// ``` 221 | /// use algorithm::CircularBuffer; 222 | /// fn main() { 223 | /// let mut circular = CircularBuffer::new(2); 224 | /// assert_eq!(circular.is_empty(), true); 225 | /// circular.push_back(1); 226 | /// circular.push_back(2); 227 | /// assert_eq!(circular[0], 1); 228 | /// circular.pop_back(); 229 | /// assert_eq!(circular[0], 1); 230 | /// } 231 | /// ``` 232 | pub fn pop_back(&mut self) { 233 | debug_assert!(!self.is_empty()); 234 | self.tail = self.sub_fix(self.tail); 235 | self.len -= 1; 236 | } 237 | 238 | /// 迭代器 239 | /// 240 | /// # Examples 241 | /// 242 | /// ``` 243 | /// use algorithm::CircularBuffer; 244 | /// fn main() { 245 | /// let mut circular = CircularBuffer::new(2); 246 | /// assert_eq!(circular.is_empty(), true); 247 | /// circular.push_back(1); 248 | /// circular.push_back(2); 249 | /// let val: Vec = circular.iter().map(|s| *s).collect(); 250 | /// assert_eq!(val, vec![1, 2]); 251 | /// let val: Vec = circular.iter().rev().map(|s| *s).collect(); 252 | /// assert_eq!(val, vec![2, 1]); 253 | /// } 254 | /// ``` 255 | pub fn iter(&self) -> Iter<'_, T> { 256 | Iter { 257 | arr: &self.arr, 258 | len: self.len, 259 | head: self.head, 260 | tail: self.tail, 261 | cap: self.cap, 262 | } 263 | } 264 | 265 | /// 迭代更改器 266 | /// 267 | /// # Examples 268 | /// 269 | /// ``` 270 | /// use algorithm::CircularBuffer; 271 | /// fn main() { 272 | /// let mut circular = CircularBuffer::new(2); 273 | /// assert_eq!(circular.is_empty(), true); 274 | /// circular.push_back(1); 275 | /// circular.push_back(2); 276 | /// let val: Vec = circular.iter_mut().map(|v| { *v *= 2; *v }).collect(); 277 | /// assert_eq!(val, vec![2, 4]); 278 | /// let val: Vec = circular.iter_mut().rev().map(|v| { *v *= 2; *v }).collect(); 279 | /// assert_eq!(val, vec![8, 4]); 280 | /// } 281 | /// ``` 282 | pub fn iter_mut(&mut self) -> IterMut<'_, T> { 283 | IterMut { 284 | arr: self.arr.as_mut_ptr(), 285 | len: self.len, 286 | head: self.head, 287 | tail: self.tail, 288 | cap: self.cap, 289 | _marker: PhantomData, 290 | } 291 | } 292 | } 293 | 294 | 295 | impl Index for CircularBuffer { 296 | type Output = T; 297 | 298 | #[inline] 299 | fn index(&self, index: usize) -> &T { 300 | if index < self.len { 301 | let ridx = (index + self.head) % self.cap; 302 | &self.arr[ridx] 303 | } else { 304 | panic!("index error"); 305 | } 306 | } 307 | } 308 | 309 | impl IndexMut for CircularBuffer { 310 | #[inline] 311 | fn index_mut(&mut self, index: usize) -> &mut T { 312 | if index < self.len { 313 | let ridx = (index + self.head) % self.cap; 314 | &mut self.arr[ridx] 315 | } else { 316 | panic!("index error"); 317 | } 318 | } 319 | } 320 | 321 | impl Clone for CircularBuffer { 322 | fn clone(&self) -> Self { 323 | Self { 324 | arr: self.arr.clone(), 325 | head: self.head, 326 | tail: self.tail, 327 | len: self.len, 328 | cap: self.cap, 329 | } 330 | } 331 | } 332 | 333 | pub struct Iter<'a, T: 'a> { 334 | len: usize, 335 | arr: &'a Vec, 336 | head: usize, 337 | tail: usize, 338 | cap: usize, 339 | } 340 | 341 | impl<'a, T> Iterator for Iter<'a, T> { 342 | type Item = &'a T; 343 | 344 | fn next(&mut self) -> Option { 345 | if self.len == 0 { 346 | return None; 347 | } 348 | let now = self.head; 349 | self.head = (self.head + 1) % self.cap; 350 | self.len -= 1; 351 | Some(&self.arr[now]) 352 | } 353 | 354 | fn size_hint(&self) -> (usize, Option) { 355 | (self.len, Some(self.len)) 356 | } 357 | } 358 | 359 | 360 | impl<'a, T> DoubleEndedIterator for Iter<'a, T> { 361 | fn next_back(&mut self) -> Option { 362 | if self.len == 0 { 363 | return None; 364 | } 365 | let now = self.tail; 366 | self.tail = (self.tail + self.cap - 1) % self.cap; 367 | self.len -= 1; 368 | Some(&self.arr[now]) 369 | } 370 | } 371 | 372 | pub struct IterMut<'a, T: 'a> { 373 | len: usize, 374 | arr: *mut T, 375 | head: usize, 376 | tail: usize, 377 | cap: usize, 378 | _marker: PhantomData<&'a mut T>, 379 | } 380 | 381 | impl<'a, T> Iterator for IterMut<'a, T> { 382 | type Item = &'a mut T; 383 | 384 | fn next(&mut self) -> Option { 385 | if self.len == 0 { 386 | return None; 387 | } 388 | let now = self.head; 389 | self.head = (self.head + 1) % self.cap; 390 | self.len -= 1; 391 | unsafe { 392 | let ptr = self.arr.add(now); 393 | return Some(&mut *ptr) 394 | } 395 | } 396 | 397 | fn size_hint(&self) -> (usize, Option) { 398 | (self.len, Some(self.len)) 399 | } 400 | } 401 | 402 | 403 | impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { 404 | fn next_back(&mut self) -> Option { 405 | if self.len == 0 { 406 | return None; 407 | } 408 | let now = self.tail; 409 | self.tail = (self.tail + self.cap - 1) % self.cap; 410 | self.len -= 1; 411 | unsafe { 412 | let ptr = self.arr.add(now); 413 | return Some(&mut *ptr) 414 | } 415 | } 416 | } 417 | 418 | 419 | 420 | impl PartialEq for CircularBuffer 421 | where 422 | T: Eq, 423 | { 424 | fn eq(&self, other: &CircularBuffer) -> bool { 425 | if self.len() != other.len() { 426 | return false; 427 | } 428 | 429 | self.iter().enumerate().all(|(idx, value)| &other[idx] == value) 430 | } 431 | } 432 | 433 | impl Eq for CircularBuffer 434 | where 435 | T: Eq, 436 | {} 437 | 438 | #[cfg(test)] 439 | mod tests { 440 | use super::CircularBuffer; 441 | 442 | #[test] 443 | fn test_iter() { 444 | let mut circular = CircularBuffer::new(2); 445 | assert_eq!(circular.is_empty(), true); 446 | circular.push_back(1); 447 | circular.push_back(2); 448 | let val: Vec = circular.iter().map(|s| *s).collect(); 449 | assert_eq!(val, vec![1, 2]); 450 | let val: Vec = circular.iter().rev().map(|s| *s).collect(); 451 | assert_eq!(val, vec![2, 1]); 452 | } 453 | } -------------------------------------------------------------------------------- /src/arr/skip_list.rs: -------------------------------------------------------------------------------- 1 | use std::{marker::PhantomData, ptr}; 2 | 3 | struct LevelType { 4 | pub forward: *mut SkipNode, 5 | pub span: usize, 6 | } 7 | 8 | impl Clone for LevelType { 9 | fn clone(&self) -> Self { 10 | Self { 11 | forward: self.forward.clone(), 12 | span: self.span.clone(), 13 | } 14 | } 15 | } 16 | 17 | impl LevelType { 18 | pub fn new() -> Self { 19 | LevelType { 20 | forward: ptr::null_mut(), 21 | span: 0, 22 | } 23 | } 24 | } 25 | 26 | pub struct SkipNode { 27 | pub score: T, 28 | pub backward: *mut SkipNode, 29 | levels: Vec>, 30 | } 31 | 32 | impl SkipNode { 33 | pub fn rank(&self) -> usize { 34 | let mut rank = 0; 35 | let mut backward = self.backward; 36 | unsafe { 37 | while !backward.is_null() { 38 | rank += (*backward).levels[0].span; 39 | backward = (*backward).backward; 40 | } 41 | } 42 | rank 43 | } 44 | } 45 | 46 | /// 跳表, 链表的一种扩展, 相对更复杂, 但是效率更高 47 | /// 48 | /// # Examples 49 | /// 50 | /// ``` 51 | /// use algorithm::SkipList; 52 | /// fn main() { 53 | /// let mut val = SkipList::new(); 54 | /// val.insert(1); 55 | /// val.insert(10); 56 | /// val.insert(100); 57 | /// assert_eq!(val.len(), 3); 58 | /// } 59 | /// ``` 60 | #[derive(Debug)] 61 | pub struct SkipList { 62 | length: usize, 63 | level: usize, 64 | 65 | header: *mut SkipNode, 66 | tail: *mut SkipNode, 67 | } 68 | 69 | const MAX_LEVEL: usize = 16; 70 | const PERCENT: u16 = 50; 71 | 72 | impl SkipList { 73 | pub fn new() -> Self { 74 | let mut sl = SkipList { 75 | length: 0, 76 | level: 1, 77 | header: ptr::null_mut(), 78 | tail: ptr::null_mut(), 79 | }; 80 | sl.clear(); 81 | sl 82 | } 83 | 84 | fn free_all(&mut self) { 85 | while !self.header.is_null() { 86 | unsafe { 87 | let next = (*self.header).levels[0].forward; 88 | Self::free_node(self.header); 89 | self.header = next; 90 | } 91 | } 92 | self.header = ptr::null_mut(); 93 | } 94 | 95 | /// 清除表内的所有内容 96 | /// 97 | /// # Examples 98 | /// 99 | /// ``` 100 | /// use algorithm::SkipList; 101 | /// fn main() { 102 | /// let mut val = SkipList::new(); 103 | /// val.insert(1); 104 | /// assert_eq!(val.len(), 1); 105 | /// val.clear(); 106 | /// assert_eq!(val.len(), 0); 107 | /// } 108 | /// ``` 109 | pub fn clear(&mut self) { 110 | self.free_all(); 111 | 112 | self.header = Self::make_node(MAX_LEVEL, T::default()); 113 | 114 | unsafe { 115 | for i in 0..MAX_LEVEL { 116 | (*self.header).levels[i].forward = ptr::null_mut(); 117 | (*self.header).levels[i].span = 0; 118 | } 119 | (*self.header).backward = ptr::null_mut(); 120 | } 121 | self.level = 1; 122 | self.length = 0; 123 | } 124 | 125 | fn make_node(level: usize, score: T) -> *mut SkipNode { 126 | assert!(level > 0); 127 | 128 | let levels = vec![LevelType::new(); level]; 129 | let node = SkipNode { 130 | score, 131 | backward: ptr::null_mut(), 132 | levels, 133 | }; 134 | Box::into_raw(Box::new(node)) 135 | } 136 | 137 | fn free_node(node: *mut SkipNode) { 138 | unsafe { 139 | let _ = Box::from_raw(node); 140 | } 141 | } 142 | 143 | fn rand_level() -> usize { 144 | let mut level = 1; 145 | while rand::random::() % 100 < PERCENT { 146 | level += 1; 147 | } 148 | level 149 | } 150 | 151 | /// 插入内容, 将根据score放至合适的排序中 152 | /// 153 | /// # Examples 154 | /// 155 | /// ``` 156 | /// use algorithm::SkipList; 157 | /// fn main() { 158 | /// let mut val = SkipList::new(); 159 | /// val.insert(1); 160 | /// assert_eq!(val.len(), 1); 161 | /// } 162 | /// ``` 163 | pub fn insert(&mut self, score: T) -> *mut SkipNode { 164 | let mut update: [*mut SkipNode; MAX_LEVEL] = [ptr::null_mut(); MAX_LEVEL]; 165 | let mut rank = [0; MAX_LEVEL]; 166 | let mut x = self.header; 167 | for i in (0..self.level).rev() { 168 | println!("i = {}, level = {}", i, self.level); 169 | rank[i] = if i == self.level - 1 { 0 } else { rank[i + 1] }; 170 | 171 | unsafe { 172 | while (*x).levels[i].forward != ptr::null_mut() 173 | && (*(*x).levels[i].forward).score < score 174 | { 175 | rank[i] += (*x).levels[i].span; 176 | x = (*x).levels[i].forward; 177 | } 178 | update[i] = x; 179 | } 180 | } 181 | 182 | let level = Self::rand_level(); 183 | println!("level == {}", level); 184 | if level > self.level { 185 | for i in self.level..level { 186 | println!("aaaa i = {}, level = {}", i, self.level); 187 | rank[i] = 0; 188 | update[i] = self.header; 189 | unsafe { 190 | (*update[i]).levels[i].span = self.length; 191 | } 192 | } 193 | self.level = level; 194 | } 195 | 196 | x = Self::make_node(level, score); 197 | unsafe { 198 | for i in 0..level { 199 | println!("i ==== {}", i); 200 | (*x).levels[i].forward = (*update[i]).levels[i].forward; 201 | (*update[i]).levels[i].forward = x; 202 | 203 | (*x).levels[i].span = (*update[i]).levels[i].span - (rank[0] - rank[i]); 204 | (*update[i]).levels[i].span = (rank[0] - rank[i]) + 1; 205 | } 206 | 207 | for i in level..self.level { 208 | (*update[i]).levels[i].span += 1; 209 | } 210 | 211 | (*x).backward = if update[0] == self.header { 212 | ptr::null_mut() 213 | } else { 214 | update[0] 215 | }; 216 | if (*x).levels[0].forward != ptr::null_mut() { 217 | (*(*x).levels[0].forward).backward = x; 218 | } else { 219 | self.tail = x; 220 | } 221 | 222 | self.length += 1; 223 | } 224 | x 225 | } 226 | 227 | /// 更新内容, 查找原值, 并更新新值 228 | /// 229 | /// # Examples 230 | /// 231 | /// ``` 232 | /// use algorithm::SkipList; 233 | /// fn main() { 234 | /// let mut val = SkipList::new(); 235 | /// val.insert(1); 236 | /// val.update(&1, 2); 237 | /// assert_eq!(val.len(), 1); 238 | /// } 239 | /// ``` 240 | pub fn update(&mut self, cur_score: &T, new_score: T) -> *mut SkipNode { 241 | let mut update: [*mut SkipNode; MAX_LEVEL] = [ptr::null_mut(); MAX_LEVEL]; 242 | let mut rank = [0; MAX_LEVEL]; 243 | let mut x = self.header; 244 | 245 | unsafe { 246 | for i in (0..self.level).rev() { 247 | while (*x).levels[i].forward != ptr::null_mut() 248 | && &(*(*x).levels[i].forward).score < cur_score 249 | { 250 | rank[i] += (*x).levels[i].span; 251 | x = (*x).levels[i].forward; 252 | } 253 | update[i] = x; 254 | } 255 | x = (*x).levels[0].forward; 256 | 257 | assert!(!x.is_null() && cur_score == &(*x).score); 258 | 259 | if ((*x).backward.is_null() || (*(*x).backward).score < new_score) 260 | && ((*x).levels[0].forward.is_null() || (*(*x).levels[0].forward).score < new_score) 261 | { 262 | (*x).score = new_score; 263 | return x; 264 | } 265 | 266 | self.remove_node(x, update); 267 | let ret = self.insert(new_score); 268 | Self::free_node(x); 269 | ret 270 | } 271 | } 272 | 273 | fn remove_node(&mut self, x: *mut SkipNode, update: [*mut SkipNode; MAX_LEVEL]) { 274 | unsafe { 275 | for i in 0..self.level { 276 | if (*update[i]).levels[i].forward == x { 277 | (*update[i]).levels[i].span += (*x).levels[i].span.saturating_sub(1); 278 | (*update[i]).levels[i].forward = (*x).levels[i].forward; 279 | } else { 280 | (*update[i]).levels[i].span = (*update[i]).levels[i].span.saturating_sub(1); 281 | } 282 | } 283 | 284 | if (*x).levels[0].forward != ptr::null_mut() { 285 | (*(*x).levels[0].forward).backward = (*x).backward; 286 | } else { 287 | self.tail = (*x).backward; 288 | } 289 | 290 | while self.level > 1 && (*self.header).levels[self.level - 1].forward.is_null() { 291 | self.level -= 1; 292 | } 293 | self.length -= 1; 294 | } 295 | } 296 | 297 | /// 获取排序值, 得到该值在序列中的排序 298 | /// 299 | /// # Examples 300 | /// 301 | /// ``` 302 | /// use algorithm::SkipList; 303 | /// fn main() { 304 | /// let mut val = SkipList::new(); 305 | /// val.insert(4); 306 | /// val.insert(2); 307 | /// val.insert(1); 308 | /// assert_eq!(val.get_rank(&1), 1); 309 | /// assert_eq!(val.get_rank(&4), 3); 310 | /// val.insert(3); 311 | /// assert_eq!(val.get_rank(&4), 4); 312 | /// } 313 | /// ``` 314 | pub fn get_rank(&mut self, score: &T) -> usize { 315 | let mut x = self.header; 316 | let mut rank = 0; 317 | for i in (0..self.level).rev() { 318 | unsafe { 319 | while !(*x).levels[i].forward.is_null() && &(*(*x).levels[i].forward).score <= score 320 | { 321 | rank += (*x).levels[i].span; 322 | x = (*x).levels[i].forward; 323 | } 324 | 325 | if &(*x).score == score { 326 | return rank; 327 | } 328 | } 329 | } 330 | 0 331 | } 332 | 333 | pub fn find_by_rank(&mut self, rank: usize) -> *mut SkipNode { 334 | let mut x = self.header; 335 | let mut traversed = 0; 336 | for i in (0..self.level).rev() { 337 | unsafe { 338 | while !(*x).levels[i].forward.is_null() && (traversed + (*x).levels[i].span) <= rank 339 | { 340 | traversed += (*x).levels[i].span; 341 | x = (*x).levels[i].forward; 342 | } 343 | if traversed == rank { 344 | return x; 345 | } 346 | } 347 | } 348 | ptr::null_mut() 349 | } 350 | 351 | /// 移除指定值 352 | /// 353 | /// # Examples 354 | /// 355 | /// ``` 356 | /// use algorithm::SkipList; 357 | /// fn main() { 358 | /// let mut val = SkipList::new(); 359 | /// val.insert(4); 360 | /// val.insert(2); 361 | /// val.insert(1); 362 | /// assert_eq!(val.len(), 3); 363 | /// val.remove(&3); 364 | /// assert_eq!(val.len(), 3); 365 | /// val.remove(&4); 366 | /// assert_eq!(val.len(), 2); 367 | /// } 368 | /// ``` 369 | pub fn remove(&mut self, score: &T) -> bool { 370 | let mut update: [*mut SkipNode; MAX_LEVEL] = [ptr::null_mut(); MAX_LEVEL]; 371 | let mut x = self.header; 372 | unsafe { 373 | for i in (0..self.level).rev() { 374 | while (*x).levels[i].forward != ptr::null_mut() 375 | && &(*(*x).levels[i].forward).score < score 376 | { 377 | x = (*x).levels[i].forward; 378 | } 379 | update[i] = x; 380 | } 381 | x = (*x).levels[0].forward; 382 | 383 | if !x.is_null() && score == &(*x).score { 384 | self.remove_node(x, update); 385 | Self::free_node(x); 386 | return true; 387 | } 388 | } 389 | return false; 390 | } 391 | 392 | /// 获取长度 393 | pub fn len(&self) -> usize { 394 | self.length 395 | } 396 | 397 | /// 遍历值 398 | /// 399 | /// # Examples 400 | /// 401 | /// ``` 402 | /// use algorithm::SkipList; 403 | /// fn main() { 404 | /// let mut val = SkipList::new(); 405 | /// val.insert(4); 406 | /// val.insert(2); 407 | /// val.insert(1); 408 | /// let mut iter = val.iter(); 409 | /// assert_eq!(iter.next(), Some((&1, 0))); 410 | /// assert_eq!(iter.next(), Some((&2, 1))); 411 | /// assert_eq!(iter.next(), Some((&4, 2))); 412 | /// assert_eq!(iter.next(), None); 413 | /// } 414 | /// ``` 415 | pub fn iter(&self) -> SkipIter<'_, T> { 416 | let first = unsafe { (*self.header).levels[0].forward }; 417 | SkipIter::new(self.length, first, self.tail) 418 | } 419 | } 420 | 421 | pub struct SkipIter<'a, T: 'a + Default + PartialEq + PartialOrd> { 422 | len: usize, 423 | header: *mut SkipNode, 424 | tail: *mut SkipNode, 425 | data: PhantomData<&'a ()>, 426 | } 427 | 428 | impl<'a, T: Default + PartialEq + PartialOrd> SkipIter<'a, T> { 429 | pub fn new(len: usize, header: *mut SkipNode, tail: *mut SkipNode) -> Self { 430 | Self { 431 | len, 432 | header, 433 | tail, 434 | data: PhantomData, 435 | } 436 | } 437 | } 438 | 439 | impl<'a, T: Default + PartialEq + PartialOrd> Iterator for SkipIter<'a, T> { 440 | type Item = (&'a T, usize); 441 | 442 | fn next(&mut self) -> Option { 443 | if self.len == 0 { 444 | return None; 445 | } 446 | 447 | self.len -= 1; 448 | 449 | unsafe { 450 | let node = self.header; 451 | self.header = (*self.header).levels[0].forward; 452 | return Some((&(*node).score, (*node).rank())); 453 | } 454 | } 455 | 456 | fn size_hint(&self) -> (usize, Option) { 457 | (self.len, Some(self.len)) 458 | } 459 | } 460 | 461 | impl<'a, T: Default + PartialEq + PartialOrd> DoubleEndedIterator for SkipIter<'a, T> { 462 | fn next_back(&mut self) -> Option { 463 | if self.len == 0 { 464 | return None; 465 | } 466 | 467 | self.len -= 1; 468 | 469 | unsafe { 470 | let node = self.tail; 471 | self.tail = (*self.tail).backward; 472 | return Some((&(*node).score, (*node).rank())); 473 | } 474 | } 475 | } 476 | 477 | impl Drop for SkipList { 478 | fn drop(&mut self) { 479 | self.clear(); 480 | } 481 | } 482 | -------------------------------------------------------------------------------- /src/map/bitmap.rs: -------------------------------------------------------------------------------- 1 | use std::{fmt::{Debug, Display}, ops::{BitAnd, BitOr, BitXor}}; 2 | 3 | /// 位图类,根据访问的位看是否被占用 4 | /// 解决经典的是否被占用的问题,但是相对占用大小会较大 5 | /// 6 | /// # Examples 7 | /// 8 | /// ``` 9 | /// use algorithm::BitMap; 10 | /// fn main() { 11 | /// let mut map = BitMap::new(10240); 12 | /// map.add_many(&vec![1, 2, 3, 4, 10]); 13 | /// assert!(map.contains(&1)); 14 | /// assert!(!map.contains(&5)); 15 | /// assert!(map.contains(&10)); 16 | /// map.add_range(7, 16); 17 | /// assert!(!map.contains(&6)); 18 | /// assert!(map.contains(&7)); 19 | /// assert!(map.contains(&16)); 20 | /// assert!(!map.contains(&17)); 21 | /// } 22 | /// ``` 23 | pub struct BitMap { 24 | entries: Vec, 25 | cap: usize, 26 | len: usize, 27 | max_key: usize, 28 | min_key: usize, 29 | } 30 | 31 | impl BitMap { 32 | pub fn new(cap: usize) -> Self { 33 | let len = cap / 8 + if cap % 8 == 0 { 0 } else { 1 }; 34 | Self { 35 | entries: vec![0; len], 36 | cap, 37 | len: 0, 38 | max_key: 0, 39 | min_key: 0, 40 | } 41 | } 42 | 43 | pub fn capacity(&self) -> usize { 44 | self.cap 45 | } 46 | 47 | pub fn len(&self) -> usize { 48 | self.len 49 | } 50 | 51 | pub fn is_empty(&self) -> bool { self.len == 0 } 52 | 53 | pub fn clear(&mut self) { 54 | self.entries.fill(0); 55 | self.len = 0; 56 | } 57 | 58 | /// 添加新的元素 59 | /// # Examples 60 | /// 61 | /// ``` 62 | /// use algorithm::BitMap; 63 | /// fn main() { 64 | /// let mut map = BitMap::new(10240); 65 | /// map.add(1); 66 | /// assert!(map.contains(&1)); 67 | /// assert!(map.len() == 1); 68 | /// } 69 | /// ``` 70 | pub fn add(&mut self, val: usize) -> bool { 71 | let pos = val / 8; 72 | let mask = 1 << val % 8; 73 | let mut success = false; 74 | if self.entries[pos] & mask == 0 { 75 | self.len += 1; 76 | self.max_key = self.max_key.max(val); 77 | self.min_key = self.min_key.min(val); 78 | success = true; 79 | } 80 | self.entries[pos] = self.entries[pos] | mask; 81 | success 82 | } 83 | 84 | /// 添加许多新的元素 85 | /// # Examples 86 | /// 87 | /// ``` 88 | /// use algorithm::BitMap; 89 | /// fn main() { 90 | /// let mut map = BitMap::new(10240); 91 | /// map.add_many(&vec![1, 2, 3, 4, 10]); 92 | /// assert!(map.contains(&1)); 93 | /// assert!(map.contains(&10)); 94 | /// assert!(map.len() == 5); 95 | /// } 96 | /// ``` 97 | pub fn add_many(&mut self, val: &[usize]) { 98 | for v in val { 99 | self.add(*v); 100 | } 101 | } 102 | 103 | /// 添加范围内的元素(包含头与结果),批量添加增加效率 104 | /// # Examples 105 | /// 106 | /// ``` 107 | /// use algorithm::BitMap; 108 | /// fn main() { 109 | /// let mut map = BitMap::new(10240); 110 | /// map.add_range(7, 16); 111 | /// assert!(!map.contains(&6)); 112 | /// assert!(map.contains(&7)); 113 | /// assert!(map.contains(&16)); 114 | /// assert!(!map.contains(&17)); 115 | /// assert!(map.len() == 10); 116 | /// } 117 | /// ``` 118 | pub fn add_range(&mut self, start: usize, end: usize) { 119 | for pos in start..end.min((start / 8 + 1) * 8) { 120 | self.add(pos); 121 | } 122 | for pos in (start / 8 + 1)..end / 8 { 123 | self.len += 8 - self.entries[pos].count_ones() as usize; 124 | self.entries[pos] = u8::MAX; 125 | } 126 | for pos in start.max((end / 8) * 8)..=end { 127 | self.add(pos); 128 | } 129 | 130 | self.min_key = self.min_key.min(start); 131 | self.max_key = self.max_key.max(end); 132 | } 133 | 134 | /// 删除元素 135 | /// # Examples 136 | /// 137 | /// ``` 138 | /// use algorithm::BitMap; 139 | /// fn main() { 140 | /// let mut map = BitMap::new(10240); 141 | /// map.add_range(7, 16); 142 | /// assert!(map.len() == 10); 143 | /// assert!(map.contains(&7)); 144 | /// assert!(map.remove(7)); 145 | /// assert!(!map.contains(&7)); 146 | /// assert!(map.len() == 9); 147 | /// } 148 | /// ``` 149 | pub fn remove(&mut self, val: usize) -> bool { 150 | let pos = val / 8; 151 | let mask = 1 << val % 8; 152 | let mut success = false; 153 | if self.entries[pos] & mask != 0 { 154 | self.len -= 1; 155 | success = true; 156 | } 157 | self.entries[pos] = self.entries[pos] & (u8::MAX - mask); 158 | success 159 | } 160 | 161 | /// 删除列表中元素 162 | /// # Examples 163 | /// 164 | /// ``` 165 | /// use algorithm::BitMap; 166 | /// fn main() { 167 | /// let mut map = BitMap::new(10240); 168 | /// map.add_range(7, 16); 169 | /// assert!(map.len() == 10); 170 | /// assert!(map.contains(&7)); 171 | /// assert!(map.remove(7)); 172 | /// assert!(!map.contains(&7)); 173 | /// assert!(map.len() == 9); 174 | /// } 175 | /// ``` 176 | pub fn remove_many(&mut self, val: &[usize]) { 177 | for v in val { 178 | self.remove(*v); 179 | } 180 | } 181 | 182 | /// 删除范围元素(包含头与尾) 183 | /// # Examples 184 | /// 185 | /// ``` 186 | /// use algorithm::BitMap; 187 | /// fn main() { 188 | /// let mut map = BitMap::new(10240); 189 | /// map.add_range(7, 16); 190 | /// assert!(map.len() == 10); 191 | /// map.remove_range(7, 15); 192 | /// assert!(map.len() == 1); 193 | /// assert!(map.contains(&16)); 194 | /// } 195 | /// ``` 196 | pub fn remove_range(&mut self, start: usize, end: usize) { 197 | for pos in start..end.min((start / 8 + 1) * 8) { 198 | self.remove(pos); 199 | } 200 | for pos in (start / 8 + 1)..end / 8 { 201 | self.len -= self.entries[pos].count_ones() as usize; 202 | self.entries[pos] = 0; 203 | } 204 | for pos in start.max((end / 8) * 8)..=end { 205 | self.remove(pos); 206 | } 207 | } 208 | 209 | /// 醒看是否包含 210 | /// # Examples 211 | /// 212 | /// ``` 213 | /// use algorithm::BitMap; 214 | /// fn main() { 215 | /// let mut map = BitMap::new(10240); 216 | /// map.add(7); 217 | /// assert!(map.contains(&7)); 218 | /// } 219 | /// ``` 220 | pub fn contains(&self, val: &usize) -> bool { 221 | let pos = val / 8; 222 | (self.entries[pos] & (1 << val % 8)) != 0 223 | } 224 | 225 | /// 迭代器,通过遍历进行循环,如果位图的容量非常大,可能效率相当低 226 | /// # Examples 227 | /// 228 | /// ``` 229 | /// use algorithm::BitMap; 230 | /// fn main() { 231 | /// let mut map = BitMap::new(10240); 232 | /// map.add(7); 233 | /// map.add_range(9, 12); 234 | /// map.add_many(&vec![20, 100, 300]); 235 | /// assert!(map.iter().collect::>() == vec![7, 9, 10, 11, 12, 20, 100, 300]); 236 | /// } 237 | /// ``` 238 | pub fn iter(&self) -> Iter<'_> { 239 | Iter { 240 | base: self, 241 | len: self.len, 242 | val: self.min_key, 243 | } 244 | } 245 | 246 | 247 | /// 是否保留,通过遍历进行循环,如果位图的容量非常大,可能效率相当低 248 | /// # Examples 249 | /// 250 | /// ``` 251 | /// use algorithm::BitMap; 252 | /// fn main() { 253 | /// let mut map = BitMap::new(10240); 254 | /// map.add_range(9, 16); 255 | /// map.retain(|v| v % 2 == 0); 256 | /// assert!(map.iter().collect::>() == vec![10, 12, 14, 16]); 257 | /// } 258 | /// ``` 259 | pub fn retain(&mut self, mut f: F) 260 | where 261 | F: FnMut(&usize) -> bool, 262 | { 263 | let mut oper = self.len; 264 | for i in self.min_key..=self.max_key { 265 | if oper == 0 { 266 | break; 267 | } 268 | if self.contains(&i) { 269 | oper -= 1; 270 | if !f(&i) { 271 | self.remove(i); 272 | } 273 | } 274 | } 275 | } 276 | 277 | /// 是否为子位图 278 | /// # Examples 279 | /// 280 | /// ``` 281 | /// use algorithm::BitMap; 282 | /// fn main() { 283 | /// let mut map = BitMap::new(10240); 284 | /// map.add_range(9, 16); 285 | /// let mut sub_map = BitMap::new(10240); 286 | /// sub_map.add_range(9, 12); 287 | /// assert!(map.contains_sub(&sub_map)); 288 | /// } 289 | /// ``` 290 | pub fn contains_sub(&self, other: &BitMap) -> bool { 291 | other.iter().all(|k| self.contains(&k)) 292 | } 293 | 294 | /// 取两个位图间的交集 295 | /// # Examples 296 | /// 297 | /// ``` 298 | /// use algorithm::BitMap; 299 | /// fn main() { 300 | /// let mut map = BitMap::new(10240); 301 | /// map.add_range(9, 16); 302 | /// let mut sub_map = BitMap::new(10240); 303 | /// sub_map.add_range(7, 12); 304 | /// let map = map.intersect(&sub_map); 305 | /// assert!(map.iter().collect::>() == vec![9, 10, 11, 12]); 306 | /// } 307 | /// ``` 308 | pub fn intersect(&self, other: &BitMap) -> BitMap { 309 | let mut map = BitMap::new(other.cap.max(self.cap)); 310 | let min = self.min_key.max(other.min_key); 311 | let max = self.max_key.min(other.max_key); 312 | for i in min..=max { 313 | if self.contains(&i) && other.contains(&i) { 314 | map.add(i); 315 | } 316 | } 317 | map 318 | } 319 | 320 | /// 取两个位图间的并集 321 | /// # Examples 322 | /// 323 | /// ``` 324 | /// use algorithm::BitMap; 325 | /// fn main() { 326 | /// let mut map = BitMap::new(10240); 327 | /// map.add_range(9, 16); 328 | /// let mut sub_map = BitMap::new(10240); 329 | /// sub_map.add_range(7, 12); 330 | /// let map = map.union(&sub_map); 331 | /// assert!(map.iter().collect::>() == vec![7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); 332 | /// } 333 | /// ``` 334 | pub fn union(&self, other: &BitMap) -> BitMap { 335 | let mut map = BitMap::new(other.cap.max(self.cap)); 336 | let min = self.min_key.min(other.min_key); 337 | let max = self.max_key.max(other.max_key); 338 | for i in min..=max { 339 | if self.contains(&i) || other.contains(&i) { 340 | map.add(i); 341 | } 342 | } 343 | map 344 | } 345 | 346 | 347 | /// 取两个位图间的异或并集 348 | /// # Examples 349 | /// 350 | /// ``` 351 | /// use algorithm::BitMap; 352 | /// fn main() { 353 | /// let mut map = BitMap::new(10240); 354 | /// map.add_range(9, 16); 355 | /// let mut sub_map = BitMap::new(10240); 356 | /// sub_map.add_range(7, 12); 357 | /// let map = map.union_xor(&sub_map); 358 | /// assert_eq!(map.iter().collect::>(), vec![7, 8, 13, 14, 15, 16]); 359 | /// } 360 | /// ``` 361 | pub fn union_xor(&self, other: &BitMap) -> BitMap { 362 | let mut map = BitMap::new(other.cap.max(self.cap)); 363 | let min = self.min_key.min(other.min_key); 364 | let max = self.max_key.max(other.max_key); 365 | for i in min..=max { 366 | if self.contains(&i) ^ other.contains(&i) { 367 | map.add(i); 368 | } 369 | } 370 | map 371 | } 372 | } 373 | 374 | impl BitAnd for &BitMap { 375 | type Output=BitMap; 376 | fn bitand(self, rhs: Self) -> Self::Output { 377 | self.intersect(rhs) 378 | } 379 | } 380 | 381 | impl BitOr for &BitMap { 382 | type Output=BitMap; 383 | fn bitor(self, rhs: Self) -> Self::Output { 384 | self.union(rhs) 385 | } 386 | } 387 | 388 | impl BitXor for &BitMap { 389 | type Output=BitMap; 390 | 391 | fn bitxor(self, rhs: Self) -> Self::Output { 392 | self.union_xor(rhs) 393 | } 394 | } 395 | 396 | impl Clone for BitMap { 397 | fn clone(&self) -> Self { 398 | Self { 399 | entries: self.entries.clone(), 400 | cap: self.cap, 401 | len: self.len, 402 | max_key: self.max_key, 403 | min_key: self.min_key, 404 | } 405 | } 406 | } 407 | 408 | 409 | impl FromIterator for BitMap { 410 | fn from_iter>(iter: T) -> BitMap { 411 | let vec = iter.into_iter().collect::>(); 412 | let mut cap = 1024; 413 | for v in &vec { 414 | cap = cap.max(*v); 415 | } 416 | let mut map = BitMap::new(cap); 417 | map.extend(vec); 418 | map 419 | } 420 | } 421 | 422 | impl PartialEq for BitMap { 423 | fn eq(&self, other: &Self) -> bool { 424 | if self.len() != other.len() { 425 | return false; 426 | } 427 | self.iter().all(|k| other.contains(&k)) 428 | } 429 | } 430 | 431 | impl Eq for BitMap {} 432 | 433 | impl Extend for BitMap { 434 | fn extend>(&mut self, iter: T) { 435 | let iter = iter.into_iter(); 436 | for v in iter { 437 | self.add(v); 438 | } 439 | } 440 | } 441 | 442 | pub struct Iter<'a> { 443 | base: &'a BitMap, 444 | len: usize, 445 | val: usize, 446 | } 447 | 448 | impl<'a> Iterator for Iter<'a> { 449 | type Item = usize; 450 | 451 | fn next(&mut self) -> Option { 452 | if self.len == 0 { 453 | return None; 454 | } 455 | 456 | for i in self.val..=self.base.max_key { 457 | if self.base.contains(&i) { 458 | self.len -= 1; 459 | self.val = i + 1; 460 | return Some(i); 461 | } 462 | } 463 | unreachable!() 464 | } 465 | 466 | fn size_hint(&self) -> (usize, Option) { 467 | (self.len, Some(self.len)) 468 | } 469 | } 470 | 471 | impl<'a> DoubleEndedIterator for Iter<'a> { 472 | fn next_back(&mut self) -> Option { 473 | if self.len == 0 { 474 | return None; 475 | } 476 | for i in (0..=(self.base.max_key - self.val)).rev() { 477 | if self.base.contains(&i) { 478 | self.len -= 1; 479 | self.val = self.base.max_key - i; 480 | return Some(i); 481 | } 482 | } 483 | unreachable!() 484 | } 485 | } 486 | 487 | impl Display for BitMap { 488 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 489 | f.write_fmt(format_args!("len:{}-val:{{", self.len))?; 490 | let mut iter = self.iter(); 491 | if let Some(v) = iter.next() { 492 | f.write_str(&v.to_string())?; 493 | } 494 | let mut sum = 1; 495 | while let Some(v) = iter.next() { 496 | f.write_fmt(format_args!(",{}", v))?; 497 | sum += 1; 498 | if sum > 0x100000 { 499 | break; 500 | } 501 | } 502 | f.write_str("}") 503 | } 504 | } 505 | 506 | impl Debug for BitMap { 507 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 508 | f.write_str(&format!("{}", self)) 509 | } 510 | } 511 | 512 | 513 | 514 | #[cfg(test)] 515 | mod tests { 516 | 517 | use super::BitMap; 518 | 519 | #[test] 520 | fn test_display() { 521 | let mut m = BitMap::new(10240); 522 | m.add_many(&vec![1, 3, 9]); 523 | assert_eq!(format!("{}", m), "len:3-val:{1,3,9}".to_string()); 524 | } 525 | } -------------------------------------------------------------------------------- /src/buf/binary.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2022 - 2023 Wenmeng See the COPYRIGHT 2 | // file at the top-level directory of this distribution. 3 | // 4 | // Licensed under the Apache License, Version 2.0 , at your 6 | // option. This file may not be copied, modified, or distributed 7 | // except according to those terms. 8 | // 9 | // Author: tickbh 10 | // ----- 11 | // Created Date: 2023/08/28 09:38:10 12 | 13 | use std::fmt::Debug; 14 | use std::io; 15 | use std::io::Error; 16 | use std::ops::Deref; 17 | use std::{ 18 | alloc::{dealloc, Layout}, 19 | borrow::Borrow, 20 | cell::RefCell, 21 | cmp, hash, 22 | io::Read, 23 | io::Result, 24 | rc::Rc, 25 | slice, 26 | sync::atomic::{AtomicUsize, Ordering}, 27 | }; 28 | 29 | use super::Bt; 30 | 31 | static EMPTY_ARRAY: &[u8] = &[]; 32 | const STATIC_TYPE: u8 = 1; 33 | const SHARED_TYPE: u8 = 2; 34 | 35 | /// 二进制的封装, 包括静态引用及共享引用对象, 仅支持写操作 36 | pub struct Binary { 37 | ptr: *const u8, 38 | // 共享引用计数 39 | counter: Rc>, 40 | // 游标值, 可以得出当前指向的位置 41 | cursor: usize, 42 | // 标记值, 从上一次标记到现在的游标值, 可以得出偏移的对象 43 | mark: usize, 44 | // 长度值, 还剩下多少的长度 45 | len: usize, 46 | // 对象虚表的引用函数 47 | vtable: &'static Vtable, 48 | } 49 | 50 | unsafe impl Sync for Binary {} 51 | 52 | unsafe impl Send for Binary {} 53 | 54 | pub struct Vtable { 55 | pub clone: unsafe fn(bin: &Binary) -> Binary, 56 | pub to_vec: unsafe fn(bin: &Binary) -> Vec, 57 | pub drop: unsafe fn(bin: &mut Binary), 58 | pub vtype: fn() -> u8, 59 | } 60 | 61 | const STATIC_VTABLE: Vtable = Vtable { 62 | clone: static_clone, 63 | to_vec: static_to_vec, 64 | drop: static_drop, 65 | vtype: || STATIC_TYPE, 66 | }; 67 | 68 | unsafe fn static_clone(bin: &Binary) -> Binary { 69 | let slice = slice::from_raw_parts(bin.ptr, bin.len); 70 | Binary::from_static(slice) 71 | } 72 | 73 | unsafe fn static_to_vec(bin: &Binary) -> Vec { 74 | let slice = slice::from_raw_parts(bin.ptr, bin.len); 75 | slice.to_vec() 76 | } 77 | 78 | unsafe fn static_drop(_bin: &mut Binary) { 79 | // nothing to drop for &'static [u8] 80 | } 81 | 82 | const SHARED_VTABLE: Vtable = Vtable { 83 | clone: shared_clone, 84 | to_vec: shared_to_vec, 85 | drop: shared_drop, 86 | vtype: || SHARED_TYPE, 87 | }; 88 | 89 | unsafe fn shared_clone(bin: &Binary) -> Binary { 90 | bin.counter.borrow_mut().fetch_add(1, Ordering::Relaxed); 91 | Binary { 92 | ptr: bin.ptr, 93 | counter: bin.counter.clone(), 94 | cursor: bin.cursor, 95 | mark: bin.mark, 96 | len: bin.len, 97 | vtable: bin.vtable, 98 | } 99 | } 100 | 101 | unsafe fn shared_to_vec(bin: &Binary) -> Vec { 102 | let slice = slice::from_raw_parts(bin.ptr, bin.len); 103 | slice.to_vec() 104 | } 105 | 106 | unsafe fn shared_drop(bin: &mut Binary) { 107 | if (*bin.counter).borrow_mut().fetch_sub(1, Ordering::Release) == 1 { 108 | let ori = bin.ptr.sub(bin.cursor); 109 | dealloc( 110 | ori as *mut u8, 111 | Layout::from_size_align(bin.cursor + bin.len, 1).unwrap(), 112 | ); 113 | } 114 | } 115 | impl Binary { 116 | pub fn new() -> Binary { 117 | Binary::from_static(EMPTY_ARRAY) 118 | } 119 | 120 | pub fn from_static(val: &'static [u8]) -> Binary { 121 | Binary { 122 | ptr: val.as_ptr(), 123 | counter: Rc::new(RefCell::new(AtomicUsize::new(0))), 124 | cursor: 0, 125 | mark: 0, 126 | len: val.len(), 127 | vtable: &STATIC_VTABLE, 128 | } 129 | } 130 | 131 | /// # Examples 132 | /// 133 | /// ``` 134 | /// use algorithm::buf::Binary; 135 | /// 136 | /// let b = Binary::from(&b"hello"[..]); 137 | /// assert_eq!(b.len(), 5); 138 | /// ``` 139 | /// 140 | pub fn len(&self) -> usize { 141 | self.len 142 | } 143 | 144 | /// Returns true if the `Binary` has a length of 0. 145 | /// 146 | /// # Examples 147 | /// 148 | /// ``` 149 | /// use algorithm::buf::Binary; 150 | /// 151 | /// let b = Binary::new(); 152 | /// assert!(b.is_empty()); 153 | /// ``` 154 | #[inline] 155 | pub const fn is_empty(&self) -> bool { 156 | self.len == 0 157 | } 158 | 159 | #[inline] 160 | pub fn to_vec(&self) -> Vec { 161 | unsafe { (self.vtable.to_vec)(self) } 162 | } 163 | 164 | /// 获取引用的数量 165 | /// 166 | /// # Examples 167 | /// 168 | /// ``` 169 | /// use algorithm::buf::Binary; 170 | /// 171 | /// let b = Binary::from(vec![1, 2, 3]); 172 | /// { 173 | /// let b1 = b.clone(); 174 | /// assert!(b1.get_refs() == 2); 175 | /// drop(b1); 176 | /// } 177 | /// assert!(b.get_refs() == 1); 178 | /// ``` 179 | pub fn get_refs(&self) -> usize { 180 | (*self.counter) 181 | .borrow() 182 | .load(std::sync::atomic::Ordering::SeqCst) 183 | } 184 | 185 | #[inline] 186 | fn as_slice(&self) -> &[u8] { 187 | unsafe { slice::from_raw_parts(self.ptr, self.len) } 188 | } 189 | 190 | #[inline] 191 | unsafe fn inc_start(&mut self, by: usize) { 192 | if by == 0 { 193 | return; 194 | } 195 | debug_assert!(self.len >= by, "internal: inc_start out of bounds"); 196 | self.len -= by; 197 | self.ptr = self.ptr.add(by); 198 | self.cursor += by; 199 | } 200 | 201 | #[inline] 202 | pub fn clear(&mut self) { 203 | unsafe { self.sub_start(self.cursor) } 204 | } 205 | 206 | #[inline] 207 | unsafe fn sub_start(&mut self, by: usize) { 208 | // should already be asserted, but debug assert for tests 209 | debug_assert!(self.cursor >= by, "internal: inc_start out of bounds"); 210 | self.len += by; 211 | self.ptr = self.ptr.sub(by); 212 | self.cursor -= by; 213 | self.mark = std::cmp::min(self.mark, self.cursor); 214 | } 215 | 216 | pub fn copy_from_slice(data: &[u8]) -> Self { 217 | data.to_vec().into() 218 | } 219 | 220 | #[inline] 221 | pub fn into_slice_all(&self) -> Vec { 222 | if (self.vtable.vtype)() == STATIC_TYPE { 223 | self.to_vec() 224 | } else { 225 | if (*self.counter).borrow().load(Ordering::SeqCst) == 1 { 226 | (*self.counter).borrow().fetch_add(1, Ordering::Relaxed); 227 | self.to_vec() 228 | } else { 229 | self.to_vec() 230 | } 231 | } 232 | } 233 | 234 | #[inline] 235 | pub fn into_slice(&self) -> Vec { 236 | if (self.vtable.vtype)() == STATIC_TYPE { 237 | self.to_vec()[self.cursor..(self.cursor + self.len)].to_vec() 238 | } else { 239 | if (*self.counter).borrow().load(Ordering::SeqCst) == 1 { 240 | (*self.counter).borrow().fetch_add(1, Ordering::Relaxed); 241 | self.to_vec()[self.cursor..(self.cursor + self.len)].to_vec() 242 | } else { 243 | self.to_vec()[self.cursor..(self.cursor + self.len)].to_vec() 244 | } 245 | } 246 | } 247 | } 248 | 249 | impl Clone for Binary { 250 | fn clone(&self) -> Self { 251 | unsafe { (self.vtable.clone)(self) } 252 | } 253 | } 254 | 255 | impl Drop for Binary { 256 | fn drop(&mut self) { 257 | unsafe { (self.vtable.drop)(self) } 258 | } 259 | } 260 | 261 | impl From<&'static str> for Binary { 262 | fn from(value: &'static str) -> Self { 263 | Binary::from_static(value.as_bytes()) 264 | } 265 | } 266 | 267 | impl From<&'static [u8]> for Binary { 268 | fn from(value: &'static [u8]) -> Self { 269 | Binary::from_static(value) 270 | } 271 | } 272 | 273 | impl From> for Binary { 274 | fn from(value: Box<[u8]>) -> Self { 275 | if value.len() == 0 { 276 | return Binary::new(); 277 | } 278 | let len = value.len(); 279 | let ptr = Box::into_raw(value) as *mut u8; 280 | Binary { 281 | ptr, 282 | len, 283 | mark: 0, 284 | cursor: 0, 285 | counter: Rc::new(RefCell::new(AtomicUsize::new(1))), 286 | vtable: &SHARED_VTABLE, 287 | } 288 | } 289 | } 290 | 291 | impl From> for Binary { 292 | fn from(value: Vec) -> Self { 293 | Binary::from(value.into_boxed_slice()) 294 | } 295 | } 296 | 297 | impl Bt for Binary { 298 | fn remaining(&self) -> usize { 299 | self.len 300 | } 301 | 302 | fn chunk(&self) -> &[u8] { 303 | self.as_slice() 304 | } 305 | 306 | fn advance_chunk(&mut self, n: usize) -> &[u8] { 307 | let ret = &unsafe { slice::from_raw_parts(self.ptr, self.len) }[..n]; 308 | self.advance(n); 309 | ret 310 | } 311 | 312 | fn advance(&mut self, n: usize) { 313 | unsafe { 314 | self.inc_start(n); 315 | } 316 | } 317 | 318 | fn into_binary(self) -> Binary { 319 | self 320 | } 321 | } 322 | 323 | impl Read for Binary { 324 | #[inline(always)] 325 | fn read(&mut self, buf: &mut [u8]) -> Result { 326 | let left = self.remaining(); 327 | if left == 0 || buf.len() == 0 { 328 | return Err(Error::new(io::ErrorKind::WouldBlock, "")); 329 | } 330 | let read = std::cmp::min(left, buf.len()); 331 | unsafe { 332 | std::ptr::copy(&self.chunk()[0], &mut buf[0], read); 333 | } 334 | self.advance(read); 335 | Ok(read) 336 | } 337 | } 338 | 339 | impl Iterator for Binary { 340 | type Item = u8; 341 | #[inline] 342 | fn next(&mut self) -> Option { 343 | self.get_next() 344 | } 345 | } 346 | 347 | impl Deref for Binary { 348 | type Target = [u8]; 349 | 350 | #[inline] 351 | fn deref(&self) -> &[u8] { 352 | self.as_slice() 353 | } 354 | } 355 | 356 | impl Debug for Binary { 357 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 358 | f.debug_struct("Binary") 359 | .field("ptr", &self.ptr) 360 | .field("counter", &self.counter) 361 | .field("cursor", &self.cursor) 362 | .field("mark", &self.mark) 363 | .field("len", &self.len) 364 | .finish() 365 | } 366 | } 367 | 368 | impl AsRef<[u8]> for Binary { 369 | #[inline] 370 | fn as_ref(&self) -> &[u8] { 371 | self.as_slice() 372 | } 373 | } 374 | 375 | impl hash::Hash for Binary { 376 | fn hash(&self, state: &mut H) 377 | where 378 | H: hash::Hasher, 379 | { 380 | self.as_slice().hash(state); 381 | } 382 | } 383 | 384 | impl Borrow<[u8]> for Binary { 385 | fn borrow(&self) -> &[u8] { 386 | self.as_slice() 387 | } 388 | } 389 | 390 | impl PartialEq for Binary { 391 | fn eq(&self, other: &Binary) -> bool { 392 | self.as_slice() == other.as_slice() 393 | } 394 | } 395 | 396 | impl PartialOrd for Binary { 397 | fn partial_cmp(&self, other: &Binary) -> Option { 398 | self.as_slice().partial_cmp(other.as_slice()) 399 | } 400 | } 401 | 402 | impl Ord for Binary { 403 | fn cmp(&self, other: &Binary) -> cmp::Ordering { 404 | self.as_slice().cmp(other.as_slice()) 405 | } 406 | } 407 | 408 | impl Eq for Binary {} 409 | 410 | impl PartialEq<[u8]> for Binary { 411 | fn eq(&self, other: &[u8]) -> bool { 412 | self.as_slice() == other 413 | } 414 | } 415 | 416 | impl PartialOrd<[u8]> for Binary { 417 | fn partial_cmp(&self, other: &[u8]) -> Option { 418 | self.as_slice().partial_cmp(other) 419 | } 420 | } 421 | 422 | impl PartialEq for [u8] { 423 | fn eq(&self, other: &Binary) -> bool { 424 | *other == *self 425 | } 426 | } 427 | 428 | impl PartialOrd for [u8] { 429 | fn partial_cmp(&self, other: &Binary) -> Option { 430 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) 431 | } 432 | } 433 | 434 | impl PartialEq for Binary { 435 | fn eq(&self, other: &str) -> bool { 436 | self.as_slice() == other.as_bytes() 437 | } 438 | } 439 | 440 | impl PartialOrd for Binary { 441 | fn partial_cmp(&self, other: &str) -> Option { 442 | self.as_slice().partial_cmp(other.as_bytes()) 443 | } 444 | } 445 | 446 | impl PartialEq for str { 447 | fn eq(&self, other: &Binary) -> bool { 448 | *other == *self 449 | } 450 | } 451 | 452 | impl PartialOrd for str { 453 | fn partial_cmp(&self, other: &Binary) -> Option { 454 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) 455 | } 456 | } 457 | 458 | impl PartialEq> for Binary { 459 | fn eq(&self, other: &Vec) -> bool { 460 | *self == other[..] 461 | } 462 | } 463 | 464 | impl PartialOrd> for Binary { 465 | fn partial_cmp(&self, other: &Vec) -> Option { 466 | self.as_slice().partial_cmp(&other[..]) 467 | } 468 | } 469 | 470 | impl PartialEq for Vec { 471 | fn eq(&self, other: &Binary) -> bool { 472 | *other == *self 473 | } 474 | } 475 | 476 | impl PartialOrd for Vec { 477 | fn partial_cmp(&self, other: &Binary) -> Option { 478 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) 479 | } 480 | } 481 | 482 | impl PartialEq for Binary { 483 | fn eq(&self, other: &String) -> bool { 484 | *self == other[..] 485 | } 486 | } 487 | 488 | impl PartialOrd for Binary { 489 | fn partial_cmp(&self, other: &String) -> Option { 490 | self.as_slice().partial_cmp(other.as_bytes()) 491 | } 492 | } 493 | 494 | impl PartialEq for String { 495 | fn eq(&self, other: &Binary) -> bool { 496 | *other == *self 497 | } 498 | } 499 | 500 | impl PartialOrd for String { 501 | fn partial_cmp(&self, other: &Binary) -> Option { 502 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) 503 | } 504 | } 505 | 506 | impl PartialEq for &[u8] { 507 | fn eq(&self, other: &Binary) -> bool { 508 | *other == *self 509 | } 510 | } 511 | 512 | impl PartialOrd for &[u8] { 513 | fn partial_cmp(&self, other: &Binary) -> Option { 514 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other) 515 | } 516 | } 517 | 518 | impl PartialEq for &str { 519 | fn eq(&self, other: &Binary) -> bool { 520 | *other == *self 521 | } 522 | } 523 | 524 | impl PartialOrd for &str { 525 | fn partial_cmp(&self, other: &Binary) -> Option { 526 | <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other) 527 | } 528 | } 529 | 530 | impl<'a, T: ?Sized> PartialEq<&'a T> for Binary 531 | where 532 | Binary: PartialEq, 533 | { 534 | fn eq(&self, other: &&'a T) -> bool { 535 | *self == **other 536 | } 537 | } 538 | 539 | impl<'a, T: ?Sized> PartialOrd<&'a T> for Binary 540 | where 541 | Binary: PartialOrd, 542 | { 543 | fn partial_cmp(&self, other: &&'a T) -> Option { 544 | self.partial_cmp(&**other) 545 | } 546 | } 547 | 548 | // impl From 549 | 550 | impl Default for Binary { 551 | #[inline] 552 | fn default() -> Binary { 553 | Binary::new() 554 | } 555 | } 556 | 557 | #[cfg(test)] 558 | mod tests { 559 | use super::Binary; 560 | 561 | #[test] 562 | fn binary_refs() { 563 | { 564 | let s = Binary::from("aaaa"); 565 | let s1 = s.clone(); 566 | assert!(s1.get_refs() == 0); 567 | drop(s1); 568 | assert!(s.get_refs() == 0); 569 | } 570 | { 571 | let b = Binary::from(vec![1]); 572 | let b1 = b.clone(); 573 | assert!(b1.get_refs() == 2); 574 | drop(b1); 575 | assert!(b.get_refs() == 1); 576 | } 577 | } 578 | } 579 | -------------------------------------------------------------------------------- /src/buf/binary_mut.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2022 - 2023 Wenmeng See the COPYRIGHT 2 | // file at the top-level directory of this distribution. 3 | // 4 | // Licensed under the Apache License, Version 2.0 , at your 6 | // option. This file may not be copied, modified, or distributed 7 | // except according to those terms. 8 | // 9 | // Author: tickbh 10 | // ----- 11 | // Created Date: 2023/08/28 09:38:10 12 | 13 | use std::{ 14 | cmp, 15 | fmt::{self, Debug}, 16 | hash, 17 | io::{self, Error, Read, Result, Write}, 18 | mem::MaybeUninit, 19 | ops::{Deref, DerefMut}, 20 | ptr, slice, usize, 21 | }; 22 | 23 | use super::{Binary, Bt}; 24 | 25 | use super::BtMut; 26 | 27 | /// 100k,当数据大于100k时,可以尝试重排当前的结构 28 | static RESORT_MEMORY_SIZE: usize = 102400; 29 | 30 | /// 二进制的封装, 可写可读 31 | pub struct BinaryMut { 32 | vec: Vec, 33 | // 游标值, 可以得出当前指向的位置 34 | cursor: usize, 35 | // 当前写入的位置 36 | wpos: usize, 37 | // 标记值, 从上一次标记到现在的游标值, 可以得出偏移的对象 38 | mark: usize, 39 | // 尝试重排的大小 40 | resort: usize, 41 | } 42 | 43 | impl Clone for BinaryMut { 44 | fn clone(&self) -> Self { 45 | Self { 46 | vec: self.vec.clone(), 47 | cursor: self.cursor.clone(), 48 | wpos: self.wpos.clone(), 49 | mark: self.mark.clone(), 50 | resort: self.resort.clone(), 51 | } 52 | } 53 | } 54 | 55 | impl BinaryMut { 56 | #[inline] 57 | pub fn with_capacity(n: usize) -> BinaryMut { 58 | BinaryMut::from_vec(Vec::with_capacity(n)) 59 | } 60 | 61 | /// 新建对象 62 | /// 63 | /// # Examples 64 | /// 65 | /// ``` 66 | /// use algorithm::buf::BinaryMut; 67 | /// 68 | /// let mut bytes = BinaryMut::new(); 69 | /// assert_eq!(0, bytes.len()); 70 | /// bytes.reserve(2); 71 | /// bytes.put_slice(b"xy"); 72 | /// assert_eq!(&b"xy"[..], &bytes[..]); 73 | /// ``` 74 | #[inline] 75 | pub fn new() -> BinaryMut { 76 | BinaryMut::with_capacity(0) 77 | } 78 | 79 | #[inline] 80 | pub(crate) fn from_vec(vec: Vec) -> BinaryMut { 81 | BinaryMut { 82 | wpos: vec.len(), 83 | vec, 84 | cursor: 0, 85 | mark: usize::MAX, 86 | resort: RESORT_MEMORY_SIZE, 87 | } 88 | } 89 | 90 | #[inline] 91 | pub fn into_slice_all(self) -> Vec { 92 | self.vec[self.cursor..self.wpos].into() 93 | } 94 | 95 | #[inline] 96 | pub fn as_slice(&self) -> &[u8] { 97 | &self.vec[self.cursor..self.wpos] 98 | } 99 | 100 | #[inline] 101 | fn as_slice_mut(&mut self) -> &mut [u8] { 102 | &mut self.vec[self.cursor..self.wpos] 103 | } 104 | 105 | #[inline] 106 | fn inc_start(&mut self, by: usize) { 107 | // should already be asserted, but debug assert for tests 108 | debug_assert!(self.remaining() >= by, "internal: inc_start out of bounds"); 109 | self.cursor += by; 110 | } 111 | 112 | /// 判断对象的长度 113 | /// 114 | /// # Examples 115 | /// 116 | /// ``` 117 | /// use algorithm::buf::BinaryMut; 118 | /// 119 | /// let b = BinaryMut::from(&b"hello"[..]); 120 | /// assert_eq!(b.len(), 5); 121 | /// ``` 122 | #[inline] 123 | pub fn len(&self) -> usize { 124 | self.wpos - self.cursor 125 | } 126 | 127 | #[inline] 128 | pub fn cursor(&self) -> usize { 129 | self.cursor 130 | } 131 | 132 | #[inline] 133 | pub fn clear(&mut self) { 134 | self.cursor = 0; 135 | self.wpos = 0; 136 | } 137 | /// 判断对象是否为空 138 | /// 139 | /// # Examples 140 | /// 141 | /// ``` 142 | /// use algorithm::buf::BinaryMut; 143 | /// 144 | /// let b = BinaryMut::with_capacity(64); 145 | /// assert!(b.is_empty()); 146 | /// ``` 147 | #[inline] 148 | pub fn is_empty(&self) -> bool { 149 | self.len() == 0 150 | } 151 | 152 | /// 返回对象大小的容量 153 | /// 154 | /// # Examples 155 | /// 156 | /// ``` 157 | /// use algorithm::buf::BinaryMut; 158 | /// 159 | /// let b = BinaryMut::with_capacity(64); 160 | /// assert_eq!(b.capacity(), 64); 161 | /// ``` 162 | #[inline] 163 | pub fn capacity(&self) -> usize { 164 | self.vec.capacity() 165 | } 166 | 167 | pub fn reserve(&mut self, additional: usize) { 168 | let len = self.vec.len(); 169 | let rem = self.vec.capacity() - len; 170 | if rem >= additional { 171 | return; 172 | } 173 | self.vec.reserve(additional) 174 | } 175 | 176 | pub fn put(&mut self, mut src: T) 177 | where 178 | Self: Sized, 179 | { 180 | while src.has_remaining() { 181 | let s = src.chunk(); 182 | let l = s.len(); 183 | self.extend_from_slice(s); 184 | src.advance(l); 185 | } 186 | } 187 | 188 | pub fn put_slice(&mut self, src: &[u8]) -> usize { 189 | self.extend_from_slice(src); 190 | src.len() 191 | } 192 | 193 | /// 将当前的数据转成不可写的对象Binary 194 | /// 195 | /// 196 | /// # Examples 197 | /// 198 | /// ``` 199 | /// use algorithm::buf::BinaryMut; 200 | /// 201 | /// let mut buf = BinaryMut::with_capacity(0); 202 | /// buf.extend_from_slice(b"aaabbb"); 203 | /// let bin = buf.freeze(); 204 | /// 205 | /// assert_eq!(b"aaabbb", &bin[..]); 206 | /// ``` 207 | #[inline] 208 | pub fn freeze(self) -> Binary { 209 | Binary::from(self.into_slice_all()) 210 | } 211 | 212 | pub fn copy_to_binary(&mut self) -> Binary { 213 | let binary = Binary::from(self.chunk().to_vec()); 214 | self.advance_all(); 215 | binary 216 | } 217 | 218 | /// 扩展bytes到`BinaryMut`, 将会自动扩展容量空间 219 | /// 220 | /// 221 | /// # Examples 222 | /// 223 | /// ``` 224 | /// use algorithm::buf::BinaryMut; 225 | /// 226 | /// let mut buf = BinaryMut::with_capacity(0); 227 | /// buf.extend_from_slice(b"aaabbb"); 228 | /// buf.extend_from_slice(b"cccddd"); 229 | /// 230 | /// assert_eq!(b"aaabbbcccddd", &buf[..]); 231 | /// ``` 232 | #[inline] 233 | pub fn extend_from_slice(&mut self, extend: &[u8]) { 234 | let cnt = extend.len(); 235 | self.reserve(cnt); 236 | 237 | unsafe { 238 | let dst = self.chunk_mut(); 239 | // Reserved above 240 | debug_assert!(dst.len() >= cnt); 241 | 242 | ptr::copy_nonoverlapping(extend.as_ptr(), dst.as_mut_ptr().cast(), cnt); 243 | } 244 | 245 | unsafe { 246 | self.advance_mut(cnt); 247 | } 248 | } 249 | 250 | pub fn get_resort(&self) -> usize { 251 | self.resort 252 | } 253 | 254 | pub fn set_resort(&mut self, resort: usize) { 255 | self.resort = resort; 256 | } 257 | 258 | /// 标记当前游标的位置, 如果在需要的情况下进行回退 259 | /// 注: 如果当前数据被写入的情况下游标可能失效 260 | /// 261 | /// # Examples 262 | /// 263 | /// ``` 264 | /// use algorithm::buf::{BinaryMut, Bt, BtMut}; 265 | /// 266 | /// let mut buf = BinaryMut::with_capacity(0); 267 | /// buf.put_u8(1); 268 | /// buf.put_u8(2); 269 | /// buf.put_u8(3); 270 | /// buf.put_u8(4); 271 | /// assert_eq!(1, buf.get_u8()); 272 | /// buf.mark(); 273 | /// assert_eq!(2, buf.get_u8()); 274 | /// assert_eq!(3, buf.get_u8()); 275 | /// buf.rewind_mark(); 276 | /// assert_eq!(2, buf.get_u8()); 277 | /// assert_eq!(3, buf.get_u8()); 278 | /// assert_eq!(4, buf.get_u8()); 279 | /// 280 | /// ``` 281 | pub fn mark(&mut self) { 282 | self.mark = self.cursor; 283 | } 284 | 285 | #[inline(always)] 286 | pub fn clear_mark(&mut self) { 287 | self.mark = usize::MAX; 288 | } 289 | 290 | pub fn rewind_mark(&mut self) -> bool { 291 | if self.mark == usize::MAX { 292 | false 293 | } else { 294 | if self.mark > self.wpos { 295 | self.clear_mark(); 296 | false 297 | } else { 298 | self.cursor = self.mark; 299 | true 300 | } 301 | } 302 | } 303 | 304 | /// 获取可读且已初始化的数据 305 | /// 306 | /// # Examples 307 | /// 308 | /// ``` 309 | /// use algorithm::buf::{BinaryMut, Bt, BtMut}; 310 | /// 311 | /// let mut buf = BinaryMut::with_capacity(0); 312 | /// assert!(buf.data_mut().len() != 0); 313 | /// 314 | /// ``` 315 | pub fn data_mut(&mut self) -> &mut [u8] { 316 | if self.wpos + 128 > self.vec.len() { 317 | self.vec.resize(self.wpos + 128, 0); 318 | } 319 | &mut self.vec[self.wpos..] 320 | } 321 | 322 | #[inline] 323 | pub unsafe fn try_resort_memory(&mut self) { 324 | if self.vec.len() < self.resort || self.cursor < self.resort / 2 { 325 | return; 326 | } 327 | let left = self.remaining(); 328 | // 当时数据占一半, 不做处理 329 | if left * 2 > self.vec.len() { 330 | return; 331 | } 332 | if left == 0 { 333 | self.clear(); 334 | } else { 335 | std::ptr::copy( 336 | self.vec.as_ptr().add(self.cursor), 337 | self.vec.as_mut_ptr(), 338 | left, 339 | ); 340 | self.cursor = 0; 341 | self.wpos = left; 342 | self.clear_mark(); 343 | } 344 | } 345 | } 346 | 347 | impl From> for BinaryMut { 348 | fn from(value: Vec) -> Self { 349 | BinaryMut::from_vec(value) 350 | } 351 | } 352 | 353 | impl Drop for BinaryMut { 354 | fn drop(&mut self) {} 355 | } 356 | 357 | impl Bt for BinaryMut { 358 | fn remaining(&self) -> usize { 359 | std::cmp::min(self.wpos, self.vec.len()) - self.cursor 360 | } 361 | 362 | fn chunk(&self) -> &[u8] { 363 | self.as_slice() 364 | } 365 | 366 | fn advance(&mut self, n: usize) { 367 | self.inc_start(n); 368 | } 369 | 370 | fn advance_chunk(&mut self, n: usize) -> &[u8] { 371 | let cursor = self.cursor; 372 | self.inc_start(n); 373 | let ret = &{ 374 | let end = std::cmp::min(self.wpos, cursor + n); 375 | &self.vec[cursor..end] 376 | }[..n]; 377 | ret 378 | } 379 | 380 | fn into_binary(self) -> Binary { 381 | Binary::from(self.chunk().to_vec()) 382 | } 383 | } 384 | 385 | unsafe impl BtMut for BinaryMut { 386 | fn remaining_mut(&self) -> usize { 387 | usize::MAX - self.len() 388 | } 389 | 390 | unsafe fn advance_mut(&mut self, cnt: usize) { 391 | self.wpos += cnt; 392 | if self.wpos > self.vec.len() { 393 | self.vec.set_len(self.wpos); 394 | } 395 | self.try_resort_memory(); 396 | } 397 | 398 | fn chunk_mut(&mut self) -> &mut [MaybeUninit] { 399 | if self.wpos == self.vec.capacity() { 400 | self.reserve(128); 401 | } 402 | unsafe { 403 | slice::from_raw_parts_mut( 404 | self.vec.as_mut_ptr().add(self.wpos) as *mut MaybeUninit, 405 | self.vec.capacity() - self.wpos, 406 | ) 407 | } 408 | } 409 | } 410 | 411 | impl AsRef<[u8]> for BinaryMut { 412 | #[inline] 413 | fn as_ref(&self) -> &[u8] { 414 | self.as_slice() 415 | } 416 | } 417 | 418 | impl Deref for BinaryMut { 419 | type Target = [u8]; 420 | 421 | #[inline] 422 | fn deref(&self) -> &[u8] { 423 | self.as_ref() 424 | } 425 | } 426 | 427 | impl AsMut<[u8]> for BinaryMut { 428 | #[inline] 429 | fn as_mut(&mut self) -> &mut [u8] { 430 | self.as_slice_mut() 431 | } 432 | } 433 | 434 | impl DerefMut for BinaryMut { 435 | #[inline] 436 | fn deref_mut(&mut self) -> &mut [u8] { 437 | self.as_mut() 438 | } 439 | } 440 | 441 | impl<'a> From<&'a [u8]> for BinaryMut { 442 | fn from(src: &'a [u8]) -> BinaryMut { 443 | BinaryMut::from_vec(src.to_vec()) 444 | } 445 | } 446 | 447 | impl<'a> From<&'a str> for BinaryMut { 448 | fn from(src: &'a str) -> BinaryMut { 449 | BinaryMut::from(src.as_bytes()) 450 | } 451 | } 452 | 453 | impl From for BinaryMut { 454 | fn from(src: String) -> BinaryMut { 455 | BinaryMut::from_vec(src.into_bytes()) 456 | } 457 | } 458 | 459 | impl From for Binary { 460 | fn from(src: BinaryMut) -> Binary { 461 | src.freeze() 462 | } 463 | } 464 | 465 | impl From for BinaryMut { 466 | fn from(src: Binary) -> BinaryMut { 467 | BinaryMut::from(src.into_slice()) 468 | } 469 | } 470 | 471 | impl PartialEq for BinaryMut { 472 | fn eq(&self, other: &BinaryMut) -> bool { 473 | self.as_slice() == other.as_slice() 474 | } 475 | } 476 | 477 | impl PartialOrd for BinaryMut { 478 | fn partial_cmp(&self, other: &BinaryMut) -> Option { 479 | self.as_slice().partial_cmp(other.as_slice()) 480 | } 481 | } 482 | 483 | impl Ord for BinaryMut { 484 | fn cmp(&self, other: &BinaryMut) -> cmp::Ordering { 485 | self.as_slice().cmp(other.as_slice()) 486 | } 487 | } 488 | 489 | impl Eq for BinaryMut {} 490 | 491 | impl Default for BinaryMut { 492 | #[inline] 493 | fn default() -> BinaryMut { 494 | BinaryMut::new() 495 | } 496 | } 497 | 498 | impl hash::Hash for BinaryMut { 499 | fn hash(&self, state: &mut H) 500 | where 501 | H: hash::Hasher, 502 | { 503 | let s: &[u8] = self.as_ref(); 504 | s.hash(state); 505 | } 506 | } 507 | 508 | impl Iterator for BinaryMut { 509 | type Item = u8; 510 | #[inline] 511 | fn next(&mut self) -> Option { 512 | self.get_next() 513 | } 514 | } 515 | 516 | impl fmt::Write for BinaryMut { 517 | #[inline] 518 | fn write_str(&mut self, s: &str) -> fmt::Result { 519 | if self.remaining_mut() >= s.len() { 520 | self.put_slice(s.as_bytes()); 521 | Ok(()) 522 | } else { 523 | Err(fmt::Error) 524 | } 525 | } 526 | 527 | #[inline] 528 | fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { 529 | fmt::write(self, args) 530 | } 531 | } 532 | 533 | impl TryInto for BinaryMut { 534 | type Error = io::Error; 535 | 536 | fn try_into(self) -> std::result::Result { 537 | Ok(String::from_utf8_lossy(&self.chunk()).to_string()) 538 | } 539 | } 540 | 541 | impl Read for BinaryMut { 542 | #[inline(always)] 543 | fn read(&mut self, buf: &mut [u8]) -> Result { 544 | let left = self.remaining(); 545 | if left == 0 || buf.len() == 0 { 546 | return Err(Error::new(io::ErrorKind::WouldBlock, "")); 547 | } 548 | let read = std::cmp::min(left, buf.len()); 549 | unsafe { 550 | std::ptr::copy(&self.chunk()[0], &mut buf[0], read); 551 | } 552 | self.advance(read); 553 | Ok(read) 554 | } 555 | } 556 | 557 | impl Write for BinaryMut { 558 | #[inline(always)] 559 | fn write(&mut self, buf: &[u8]) -> Result { 560 | self.put_slice(buf); 561 | Ok(buf.len()) 562 | } 563 | 564 | fn flush(&mut self) -> Result<()> { 565 | Ok(()) 566 | } 567 | } 568 | 569 | // impl Write for &mut BinaryMut { 570 | // #[inline(always)] 571 | // fn write(&mut self, buf: &[u8]) -> Result { 572 | // self.put_slice(buf); 573 | // Ok(buf.len()) 574 | // } 575 | 576 | // fn flush(&mut self) -> Result<()> { 577 | // Ok(()) 578 | // } 579 | // } 580 | 581 | impl Debug for BinaryMut { 582 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 583 | f.debug_struct("BinaryMut") 584 | .field("ptr", &self.vec) 585 | .field("cursor", &self.cursor) 586 | .field("wpos", &self.wpos) 587 | .field("mark", &self.mark) 588 | .finish() 589 | } 590 | } 591 | 592 | unsafe impl Sync for BinaryMut {} 593 | 594 | unsafe impl Send for BinaryMut {} 595 | 596 | #[cfg(test)] 597 | mod tests {} 598 | -------------------------------------------------------------------------------- /src/map/roaring_bitmap.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::fmt::{Debug, Display}; 3 | use std::ops::{BitAnd, BitOr, BitXor}; 4 | 5 | use crate::BitMap; 6 | 7 | 8 | const TAIL_BIT: usize = 16; 9 | const TAIL_NUM: usize = 0x10000; 10 | 11 | #[derive(Clone)] 12 | enum TailContainer { 13 | Array(Vec), 14 | Bitmap(BitMap), 15 | } 16 | 17 | impl TailContainer { 18 | fn new() -> Self { 19 | Self::Array(vec![]) 20 | } 21 | 22 | fn try_move(&mut self) { 23 | let val = match self { 24 | TailContainer::Array(v) if v.len() >= 4096 => { 25 | v.drain(..).collect::>() 26 | } 27 | _ => { 28 | return; 29 | } 30 | 31 | }; 32 | let mut map = BitMap::new(TAIL_NUM); 33 | for v in val { 34 | map.add(v as usize); 35 | } 36 | *self = TailContainer::Bitmap(map); 37 | } 38 | 39 | pub fn add(&mut self, val: u16) -> bool { 40 | self.try_move(); 41 | match self { 42 | TailContainer::Array(vec) => { 43 | if let Err(s) = vec.binary_search(&val) { 44 | vec.insert(s, val); 45 | true 46 | } else { 47 | false 48 | } 49 | }, 50 | TailContainer::Bitmap(hash) => hash.add(val as usize) 51 | } 52 | } 53 | 54 | pub fn remove(&mut self, val: u16) -> bool { 55 | match self { 56 | TailContainer::Array(vec) => { 57 | if let Ok(s) = vec.binary_search(&val) { 58 | vec.remove(s); 59 | true 60 | } else { 61 | false 62 | } 63 | }, 64 | TailContainer::Bitmap(hash) => hash.remove(val as usize) 65 | } 66 | } 67 | 68 | pub fn next(&self, val: u16) -> Option { 69 | match self { 70 | TailContainer::Array(vec) => { 71 | match vec.binary_search(&val) { 72 | Ok(s) => { return Some(vec[s]) }, 73 | Err(s) => { 74 | if s == vec.len() { 75 | return None; 76 | } 77 | return Some(vec[s]); 78 | } 79 | } 80 | }, 81 | TailContainer::Bitmap(hash) => { 82 | for i in val..=65535u16 { 83 | if hash.contains(&(i as usize)) { 84 | return Some(i); 85 | } 86 | } 87 | return None; 88 | } 89 | } 90 | } 91 | 92 | 93 | pub fn next_back(&self, val: u16) -> Option { 94 | match self { 95 | TailContainer::Array(vec) => { 96 | match vec.binary_search(&val) { 97 | Ok(s) => { return Some(vec[s]) }, 98 | Err(s) => { 99 | if s == 0 { 100 | return None; 101 | } 102 | return Some(vec[s - 1]); 103 | } 104 | } 105 | }, 106 | TailContainer::Bitmap(hash) => { 107 | for i in (0..=val).rev() { 108 | if hash.contains(&(i as usize)) { 109 | return Some(i); 110 | } 111 | } 112 | return None; 113 | } 114 | } 115 | } 116 | 117 | pub fn contains(&self, val: u16) -> bool { 118 | match self { 119 | TailContainer::Array(vec) => { 120 | if let Ok(_) = vec.binary_search(&val) { 121 | true 122 | } else { 123 | false 124 | } 125 | }, 126 | TailContainer::Bitmap(hash) => hash.contains(&(val as usize)) 127 | } 128 | } 129 | } 130 | 131 | /// 位图类RoaringBitMap,根据访问的位看是否被占用 132 | /// 本质上是将大块的bitmap分成各个小块,其中每个小块在需要存储数据的时候才会存在 133 | /// 解决经典的是否被占用的问题,不会一次性分配大内存 134 | /// 头部以val / 65536做为索引键值, 尾部分为Array及HashSet结构 135 | /// 当元素个数小于4096时以有序array做为索引, 当>4096以HashSet做为存储 136 | /// 137 | /// # Examples 138 | /// 139 | /// ``` 140 | /// use algorithm::RoaringBitMap; 141 | /// fn main() { 142 | /// let mut map = RoaringBitMap::new(); 143 | /// map.add_many(&vec![1, 2, 3, 4, 10]); 144 | /// assert!(map.contains(&1)); 145 | /// assert!(!map.contains(&5)); 146 | /// assert!(map.contains(&10)); 147 | /// map.add_range(7, 16); 148 | /// assert!(!map.contains(&6)); 149 | /// assert!(map.contains(&7)); 150 | /// assert!(map.contains(&16)); 151 | /// assert!(!map.contains(&17)); 152 | /// } 153 | /// ``` 154 | pub struct RoaringBitMap { 155 | map: HashMap, 156 | len: usize, 157 | max_key: usize, 158 | min_key: usize, 159 | } 160 | 161 | impl RoaringBitMap { 162 | pub fn new() -> Self { 163 | Self { 164 | map: HashMap::new(), 165 | len: 0, 166 | max_key: 0, 167 | min_key: 0, 168 | } 169 | } 170 | 171 | pub fn len(&self) -> usize { 172 | self.len 173 | } 174 | 175 | pub fn is_empty(&self) -> bool { self.len == 0 } 176 | 177 | pub fn clear(&mut self) { 178 | self.map.clear(); 179 | self.len = 0; 180 | } 181 | 182 | /// 添加新的元素 183 | /// # Examples 184 | /// 185 | /// ``` 186 | /// use algorithm::RoaringBitMap; 187 | /// fn main() { 188 | /// let mut map = RoaringBitMap::new(); 189 | /// map.add(1); 190 | /// assert!(map.contains(&1)); 191 | /// assert!(map.len() == 1); 192 | /// } 193 | /// ``` 194 | pub fn add(&mut self, val: usize) { 195 | let head = val >> 16; 196 | let tail = (val % TAIL_NUM) as u16; 197 | if self.map.entry(head).or_insert(TailContainer::new()).add(tail) { 198 | self.len += 1; 199 | self.min_key = self.min_key.min(val); 200 | self.max_key = self.max_key.max(val); 201 | } 202 | } 203 | 204 | /// 添加许多新的元素 205 | /// # Examples 206 | /// 207 | /// ``` 208 | /// use algorithm::RoaringBitMap; 209 | /// fn main() { 210 | /// let mut map = RoaringBitMap::new(); 211 | /// map.add_many(&vec![1, 2, 3, 4, 10]); 212 | /// assert!(map.contains(&1)); 213 | /// assert!(map.contains(&10)); 214 | /// assert!(map.len() == 5); 215 | /// } 216 | /// ``` 217 | pub fn add_many(&mut self, val: &[usize]) { 218 | for v in val { 219 | self.add(*v); 220 | } 221 | } 222 | 223 | /// 添加范围内的元素(包含头与结果),批量添加增加效率 224 | /// # Examples 225 | /// 226 | /// ``` 227 | /// use algorithm::RoaringBitMap; 228 | /// fn main() { 229 | /// let mut map = RoaringBitMap::new(); 230 | /// map.add_range(7, 16); 231 | /// assert!(!map.contains(&6)); 232 | /// assert!(map.contains(&7)); 233 | /// assert!(map.contains(&16)); 234 | /// assert!(!map.contains(&17)); 235 | /// assert!(map.len() == 10); 236 | /// } 237 | /// ``` 238 | pub fn add_range(&mut self, start: usize, end: usize) { 239 | for i in start..=end { 240 | self.add(i); 241 | } 242 | } 243 | 244 | /// 删除元素 245 | /// # Examples 246 | /// 247 | /// ``` 248 | /// use algorithm::RoaringBitMap; 249 | /// fn main() { 250 | /// let mut map = RoaringBitMap::new(); 251 | /// map.add_range(7, 16); 252 | /// assert!(map.len() == 10); 253 | /// assert!(map.contains(&7)); 254 | /// assert!(map.remove(7)); 255 | /// assert!(!map.contains(&7)); 256 | /// assert!(map.len() == 9); 257 | /// } 258 | /// ``` 259 | pub fn remove(&mut self, val: usize) -> bool { 260 | let head = val >> 16; 261 | let tail = (val % TAIL_NUM) as u16; 262 | if let Some(map) = self.map.get_mut(&head) { 263 | if map.remove(tail) { 264 | self.len -= 1; 265 | return true; 266 | } 267 | } 268 | false 269 | } 270 | 271 | /// 删除列表中元素 272 | /// # Examples 273 | /// 274 | /// ``` 275 | /// use algorithm::RoaringBitMap; 276 | /// fn main() { 277 | /// let mut map = RoaringBitMap::new(); 278 | /// map.add_range(7, 16); 279 | /// assert!(map.len() == 10); 280 | /// assert!(map.contains(&7)); 281 | /// assert!(map.remove(7)); 282 | /// assert!(!map.contains(&7)); 283 | /// assert!(map.len() == 9); 284 | /// } 285 | /// ``` 286 | pub fn remove_many(&mut self, val: &[usize]) { 287 | for v in val { 288 | self.remove(*v); 289 | } 290 | } 291 | 292 | /// 删除范围元素(包含头与尾) 293 | /// # Examples 294 | /// 295 | /// ``` 296 | /// use algorithm::RoaringBitMap; 297 | /// fn main() { 298 | /// let mut map = RoaringBitMap::new(); 299 | /// map.add_range(7, 16); 300 | /// assert!(map.len() == 10); 301 | /// map.remove_range(7, 15); 302 | /// assert!(map.len() == 1); 303 | /// assert!(map.contains(&16)); 304 | /// } 305 | /// ``` 306 | pub fn remove_range(&mut self, start: usize, end: usize) { 307 | for i in start..=end { 308 | self.remove(i); 309 | } 310 | } 311 | 312 | /// 醒看是否包含 313 | /// # Examples 314 | /// 315 | /// ``` 316 | /// use algorithm::RoaringBitMap; 317 | /// fn main() { 318 | /// let mut map = RoaringBitMap::new(); 319 | /// map.add(7); 320 | /// assert!(map.contains(&7)); 321 | /// } 322 | /// ``` 323 | pub fn contains(&self, val: &usize) -> bool { 324 | let head = val >> 16; 325 | let tail = (val % TAIL_NUM) as u16; 326 | if let Some(map) = self.map.get(&head) { 327 | map.contains(tail) 328 | } else { 329 | false 330 | } 331 | } 332 | 333 | /// 迭代器,通过遍历进行循环,如果位图的容量非常大,可能效率相当低 334 | /// # Examples 335 | /// 336 | /// ``` 337 | /// use algorithm::RoaringBitMap; 338 | /// fn main() { 339 | /// let mut map = RoaringBitMap::new(); 340 | /// map.add(7); 341 | /// map.add_range(9, 12); 342 | /// map.add_many(&vec![20, 100, 300]); 343 | /// assert!(map.iter().collect::>() == vec![7, 9, 10, 11, 12, 20, 100, 300]); 344 | /// } 345 | /// ``` 346 | pub fn iter(&self) -> Iter<'_> { 347 | Iter { 348 | base: self, 349 | len: self.len, 350 | min_val: self.min_key, 351 | max_val: self.max_key, 352 | } 353 | } 354 | 355 | 356 | /// 是否保留,通过遍历进行循环,如果位图的容量非常大,可能效率相当低 357 | /// # Examples 358 | /// 359 | /// ``` 360 | /// use algorithm::RoaringBitMap; 361 | /// fn main() { 362 | /// let mut map = RoaringBitMap::new(); 363 | /// map.add_range(9, 16); 364 | /// map.retain(|v| v % 2 == 0); 365 | /// assert!(map.iter().collect::>() == vec![10, 12, 14, 16]); 366 | /// } 367 | /// ``` 368 | pub fn retain(&mut self, mut f: F) 369 | where 370 | F: FnMut(&usize) -> bool, 371 | { 372 | let mut oper = self.len; 373 | for i in self.min_key..=self.max_key { 374 | if oper == 0 { 375 | break; 376 | } 377 | if self.contains(&i) { 378 | oper -= 1; 379 | if !f(&i) { 380 | self.remove(i); 381 | } 382 | } 383 | } 384 | } 385 | 386 | /// 是否为子位图 387 | /// # Examples 388 | /// 389 | /// ``` 390 | /// use algorithm::RoaringBitMap; 391 | /// fn main() { 392 | /// let mut map = RoaringBitMap::new(); 393 | /// map.add_range(9, 16); 394 | /// let mut sub_map = RoaringBitMap::new(); 395 | /// sub_map.add_range(9, 12); 396 | /// assert!(map.contains_sub(&sub_map)); 397 | /// } 398 | /// ``` 399 | pub fn contains_sub(&self, other: &RoaringBitMap) -> bool { 400 | other.iter().all(|k| self.contains(&k)) 401 | } 402 | 403 | /// 取两个位图间的交集 404 | /// # Examples 405 | /// 406 | /// ``` 407 | /// use algorithm::RoaringBitMap; 408 | /// fn main() { 409 | /// let mut map = RoaringBitMap::new(); 410 | /// map.add_range(9, 16); 411 | /// let mut sub_map = RoaringBitMap::new(); 412 | /// sub_map.add_range(7, 12); 413 | /// let map = map.intersect(&sub_map); 414 | /// assert!(map.iter().collect::>() == vec![9, 10, 11, 12]); 415 | /// } 416 | /// ``` 417 | pub fn intersect(&self, other: &RoaringBitMap) -> RoaringBitMap { 418 | let mut map = RoaringBitMap::new(); 419 | let mut from = self.min_key.max(other.min_key); 420 | let end = self.max_key.min(other.max_key); 421 | while from <= end { 422 | let head_from = from >> TAIL_BIT; 423 | let next_from = (head_from + 1) * TAIL_NUM; 424 | if self.map.contains_key(&head_from) && other.map.contains_key(&head_from) { 425 | for i in from..next_from.min(end+1) { 426 | if self.contains(&i) && other.contains(&i) { 427 | map.add(i); 428 | } 429 | } 430 | } 431 | from = next_from; 432 | } 433 | map 434 | } 435 | 436 | /// 取两个位图间的并集 437 | /// # Examples 438 | /// 439 | /// ``` 440 | /// use algorithm::RoaringBitMap; 441 | /// fn main() { 442 | /// let mut map = RoaringBitMap::new(); 443 | /// map.add_range(9, 16); 444 | /// let mut sub_map = RoaringBitMap::new(); 445 | /// sub_map.add_range(7, 12); 446 | /// let map = map.union(&sub_map); 447 | /// assert!(map.iter().collect::>() == vec![7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); 448 | /// } 449 | /// ``` 450 | pub fn union(&self, other: &RoaringBitMap) -> RoaringBitMap { 451 | let mut map = RoaringBitMap::new(); 452 | let mut from = self.min_key.min(other.min_key); 453 | let end = self.max_key.max(other.max_key); 454 | while from <= end { 455 | let head_from = from >> TAIL_BIT; 456 | let next_from = (head_from + 1) * TAIL_NUM; 457 | if self.map.contains_key(&head_from) || other.map.contains_key(&head_from) { 458 | for i in from..next_from.min(end+1) { 459 | if self.contains(&i) || other.contains(&i) { 460 | map.add(i); 461 | } 462 | } 463 | } 464 | from = next_from; 465 | } 466 | map 467 | } 468 | 469 | /// 取两个位图间的异或并集 470 | /// # Examples 471 | /// 472 | /// ``` 473 | /// use algorithm::RoaringBitMap; 474 | /// fn main() { 475 | /// let mut map = RoaringBitMap::new(); 476 | /// map.add_range(9, 16); 477 | /// let mut sub_map = RoaringBitMap::new(); 478 | /// sub_map.add_range(7, 12); 479 | /// let map = map.union_xor(&sub_map); 480 | /// assert!(map.iter().collect::>() == vec![7, 8, 13, 14, 15, 16]); 481 | /// } 482 | /// ``` 483 | pub fn union_xor(&self, other: &RoaringBitMap) -> RoaringBitMap { 484 | let mut map = RoaringBitMap::new(); 485 | let mut from = self.min_key.min(other.min_key); 486 | let end = self.max_key.max(other.max_key); 487 | while from <= end { 488 | let head_from = from >> TAIL_BIT; 489 | let next_from = (head_from + 1) * TAIL_NUM; 490 | if self.map.contains_key(&head_from) || other.map.contains_key(&head_from) { 491 | for i in from..next_from.min(end+1) { 492 | if self.contains(&i) ^ other.contains(&i) { 493 | map.add(i); 494 | } 495 | } 496 | } 497 | from = next_from; 498 | } 499 | map 500 | } 501 | } 502 | 503 | impl BitAnd for &RoaringBitMap { 504 | type Output=RoaringBitMap; 505 | fn bitand(self, rhs: Self) -> Self::Output { 506 | self.intersect(rhs) 507 | } 508 | } 509 | 510 | impl BitOr for &RoaringBitMap { 511 | type Output=RoaringBitMap; 512 | fn bitor(self, rhs: Self) -> Self::Output { 513 | self.union(rhs) 514 | } 515 | } 516 | 517 | impl BitXor for &RoaringBitMap { 518 | type Output=RoaringBitMap; 519 | 520 | fn bitxor(self, rhs: Self) -> Self::Output { 521 | self.union_xor(rhs) 522 | } 523 | } 524 | 525 | impl Clone for RoaringBitMap { 526 | fn clone(&self) -> Self { 527 | Self { 528 | map: self.map.clone(), 529 | len: self.len, 530 | max_key: self.max_key, 531 | min_key: self.min_key, 532 | } 533 | } 534 | } 535 | 536 | 537 | impl FromIterator for RoaringBitMap { 538 | fn from_iter>(iter: T) -> RoaringBitMap { 539 | let vec = iter.into_iter().collect::>(); 540 | let mut cap = 1024; 541 | for v in &vec { 542 | cap = cap.max(*v); 543 | } 544 | let mut map = RoaringBitMap::new(); 545 | map.extend(vec); 546 | map 547 | } 548 | } 549 | 550 | impl PartialEq for RoaringBitMap { 551 | fn eq(&self, other: &Self) -> bool { 552 | if self.len() != other.len() { 553 | return false; 554 | } 555 | self.iter().all(|k| other.contains(&k)) 556 | } 557 | } 558 | 559 | impl Eq for RoaringBitMap {} 560 | 561 | impl Extend for RoaringBitMap { 562 | fn extend>(&mut self, iter: T) { 563 | let iter = iter.into_iter(); 564 | for v in iter { 565 | self.add(v); 566 | } 567 | } 568 | } 569 | 570 | pub struct Iter<'a> { 571 | base: &'a RoaringBitMap, 572 | len: usize, 573 | min_val: usize, 574 | max_val: usize, 575 | } 576 | 577 | impl<'a> Iterator for Iter<'a> { 578 | type Item = usize; 579 | 580 | fn next(&mut self) -> Option { 581 | if self.len == 0 { 582 | return None; 583 | } 584 | 585 | while self.min_val <= self.base.max_key { 586 | let head = self.min_val >> 16; 587 | if !self.base.map.contains_key(&head) { 588 | self.min_val = (head + 1) * TAIL_NUM; 589 | continue; 590 | } 591 | let tail = (self.min_val % TAIL_NUM) as u16; 592 | let container = self.base.map.get(&head).expect("ok"); 593 | if let Some(i) = container.next(tail) { 594 | self.min_val = head * TAIL_NUM + i as usize + 1; 595 | self.len -= 1; 596 | return Some(head * TAIL_NUM + i as usize); 597 | } else { 598 | self.min_val = (head + 1) * TAIL_NUM; 599 | continue; 600 | } 601 | } 602 | unreachable!() 603 | } 604 | 605 | fn size_hint(&self) -> (usize, Option) { 606 | (self.len, Some(self.len)) 607 | } 608 | } 609 | 610 | impl<'a> DoubleEndedIterator for Iter<'a> { 611 | fn next_back(&mut self) -> Option { 612 | if self.len == 0 { 613 | return None; 614 | } 615 | 616 | loop { 617 | let head = self.max_val >> 16; 618 | if !self.base.map.contains_key(&head) { 619 | self.max_val = (head * TAIL_NUM).saturating_sub(1); 620 | continue; 621 | } 622 | let tail = (self.max_val % TAIL_NUM) as u16; 623 | let container = self.base.map.get(&head).expect("ok"); 624 | if let Some(i) = container.next_back(tail) { 625 | self.max_val = (head * TAIL_NUM + i as usize).saturating_sub(1); 626 | self.len -= 1; 627 | return Some(head * TAIL_NUM + i as usize); 628 | } else { 629 | self.max_val = (head * TAIL_NUM).saturating_sub(1); 630 | continue; 631 | } 632 | } 633 | } 634 | } 635 | 636 | impl Display for RoaringBitMap { 637 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 638 | f.write_fmt(format_args!("len:{}-val:{{", self.len))?; 639 | let mut iter = self.iter(); 640 | if let Some(v) = iter.next() { 641 | f.write_str(&v.to_string())?; 642 | } 643 | let mut sum = 1; 644 | while let Some(v) = iter.next() { 645 | f.write_fmt(format_args!(",{}", v))?; 646 | sum += 1; 647 | if sum > 0x100000 { 648 | break; 649 | } 650 | } 651 | f.write_str("}") 652 | } 653 | } 654 | 655 | impl Debug for RoaringBitMap { 656 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 657 | f.write_str(&format!("{}", self)) 658 | } 659 | } 660 | 661 | 662 | 663 | #[cfg(test)] 664 | mod tests { 665 | 666 | use super::RoaringBitMap; 667 | 668 | #[test] 669 | fn test_display() { 670 | let mut m = RoaringBitMap::new(); 671 | m.add_many(&vec![1, 3, 9, 10240000111]); 672 | assert_eq!(format!("{}", m), "len:4-val:{1,3,9,10240000111}".to_string()); 673 | } 674 | 675 | #[test] 676 | fn test_nextback() { 677 | let mut m = RoaringBitMap::new(); 678 | m.add_many(&vec![1, 3, 9, 10240000111]); 679 | let vec = m.iter().rev().collect::>(); 680 | assert_eq!(vec, vec![10240000111, 9, 3, 1]); 681 | } 682 | } -------------------------------------------------------------------------------- /src/arr/fix_vec.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::Ordering; 2 | 3 | #[derive(Debug)] 4 | struct FixedVecNode { 5 | prev: usize, 6 | next: usize, 7 | data: T, 8 | } 9 | 10 | /// 指定位置的序列, 每个位置上指向上向的位置, 相当于模拟指针 11 | /// 可以根据坐标位置获取相应数据, 亦可以操作上级及下级位置 12 | /// 13 | /// # Examples 14 | /// 15 | /// ``` 16 | /// use algorithm::FixedVec; 17 | /// fn main() { 18 | /// let mut val = FixedVec::new(2); 19 | /// val.insert_head(1); 20 | /// val.insert_head(2); 21 | /// assert_eq!(val.len(), 2); 22 | /// assert_eq!(val.head(), Some(&2)); 23 | /// assert_eq!(val.tail(), Some(&1)); 24 | /// assert_eq!(val.insert_head(3), None); 25 | /// } 26 | /// ``` 27 | #[derive(Debug)] 28 | pub struct FixedVec { 29 | capacity: usize, 30 | nodes: Vec>>, 31 | // 存储空闲位置, 用O(1)的时间复杂度取出空闲位置 32 | free: Vec, 33 | head: usize, 34 | tail: usize, 35 | } 36 | 37 | impl FixedVec { 38 | #[inline] 39 | pub fn new(capacity: usize) -> Self { 40 | Self { 41 | capacity, 42 | nodes: Vec::new(), 43 | free: Vec::new(), 44 | head: usize::MAX, 45 | tail: usize::MAX, 46 | } 47 | } 48 | 49 | #[inline] 50 | pub fn with_memory(capacity: usize, mut reserve: usize) -> Self { 51 | reserve = reserve.min(capacity); 52 | Self { 53 | capacity, 54 | nodes: Vec::with_capacity(reserve), 55 | free: Vec::new(), 56 | head: usize::MAX, 57 | tail: usize::MAX, 58 | } 59 | } 60 | 61 | /// 获取容量 62 | #[inline] 63 | pub fn capacity(&self) -> usize { 64 | self.capacity 65 | } 66 | 67 | /// 返回长度 68 | #[inline] 69 | pub fn len(&self) -> usize { 70 | self.nodes.len() - self.free.len() 71 | } 72 | 73 | #[inline] 74 | pub fn is_empty(&self) -> bool { 75 | self.len() == 0 76 | } 77 | 78 | #[inline] 79 | pub fn is_full(&self) -> bool { 80 | self.len() == self.capacity() 81 | } 82 | 83 | /// 清除数据 84 | /// # Examples 85 | /// 86 | /// ``` 87 | /// use algorithm::FixedVec; 88 | /// fn main() { 89 | /// let mut val = FixedVec::new(2); 90 | /// val.insert_head(1); 91 | /// assert_eq!(val.len(), 1); 92 | /// val.clear(); 93 | /// assert_eq!(val.len(), 0); 94 | /// } 95 | /// ``` 96 | pub fn clear(&mut self) { 97 | self.nodes.clear(); 98 | self.free.clear(); 99 | self.head = usize::MAX; 100 | self.tail = usize::MAX; 101 | } 102 | 103 | fn next(&mut self) -> Option { 104 | if self.is_full() { 105 | None 106 | } else if self.free.is_empty() { 107 | let len = self.len(); 108 | self.nodes.push(None); 109 | Some(len) 110 | } else { 111 | self.free.pop() 112 | } 113 | } 114 | 115 | #[inline] 116 | fn node_ref(&self, idx: usize) -> Option<&FixedVecNode> { 117 | self.nodes.get(idx).and_then(|node| node.as_ref()) 118 | } 119 | 120 | #[inline] 121 | pub fn get(&self, idx: usize) -> Option<&T> { 122 | self.node_ref(idx).map(|node| &node.data) 123 | } 124 | 125 | #[inline] 126 | fn node_mut(&mut self, idx: usize) -> Option<&mut FixedVecNode> { 127 | self.nodes.get_mut(idx).and_then(|node| node.as_mut()) 128 | } 129 | 130 | #[inline] 131 | pub fn get_mut(&mut self, idx: usize) -> Option<&mut T> { 132 | self.node_mut(idx).map(|node| &mut node.data) 133 | } 134 | /// 获取头部的坐标位置 135 | /// 136 | /// # Examples 137 | /// 138 | /// ``` 139 | /// use algorithm::FixedVec; 140 | /// fn main() { 141 | /// let mut val = FixedVec::new(2); 142 | /// val.insert_tail(1); 143 | /// assert_eq!(val.head_idx(), 0); 144 | /// } 145 | /// ``` 146 | #[inline] 147 | pub fn head_idx(&self) -> usize { 148 | self.head 149 | } 150 | /// 获取头部首位数据 151 | /// 152 | /// # Examples 153 | /// 154 | /// ``` 155 | /// use algorithm::FixedVec; 156 | /// fn main() { 157 | /// let mut val = FixedVec::new(2); 158 | /// val.insert_tail(1); 159 | /// assert_eq!(val.head(), Some(&1)); 160 | /// } 161 | /// ``` 162 | #[inline] 163 | pub fn head(&self) -> Option<&T> { 164 | self.node_ref(self.head).map(|node| &node.data) 165 | } 166 | 167 | /// 获取头部首位可变数据 168 | /// 169 | /// # Examples 170 | /// 171 | /// ``` 172 | /// use algorithm::FixedVec; 173 | /// fn main() { 174 | /// let mut val = FixedVec::new(2); 175 | /// val.insert_tail(1); 176 | /// assert_eq!(val.head_mut(), Some(&mut 1)); 177 | /// } 178 | /// ``` 179 | #[inline] 180 | pub fn head_mut(&mut self) -> Option<&mut T> { 181 | self.node_mut(self.head).map(|node| &mut node.data) 182 | } 183 | 184 | /// 获取尾部的坐标位置 185 | /// 186 | /// # Examples 187 | /// 188 | /// ``` 189 | /// use algorithm::FixedVec; 190 | /// fn main() { 191 | /// let mut val = FixedVec::new(2); 192 | /// val.insert_tail(1); 193 | /// assert_eq!(val.tail_idx(), 0); 194 | /// } 195 | /// ``` 196 | #[inline] 197 | pub fn tail_idx(&self) -> usize { 198 | self.tail 199 | } 200 | 201 | /// 获取尾部首位数据 202 | /// 203 | /// # Examples 204 | /// 205 | /// ``` 206 | /// use algorithm::FixedVec; 207 | /// fn main() { 208 | /// let mut val = FixedVec::new(2); 209 | /// val.insert_head(1); 210 | /// assert_eq!(val.tail(), Some(&1)); 211 | /// } 212 | /// ``` 213 | #[inline] 214 | pub fn tail(&self) -> Option<&T> { 215 | self.node_ref(self.tail).map(|node| &node.data) 216 | } 217 | 218 | /// 获取尾部首位可变数据 219 | /// 220 | /// # Examples 221 | /// 222 | /// ``` 223 | /// use algorithm::FixedVec; 224 | /// fn main() { 225 | /// let mut val = FixedVec::new(2); 226 | /// val.insert_head(1); 227 | /// assert_eq!(val.tail_mut(), Some(&mut 1)); 228 | /// } 229 | /// ``` 230 | #[inline] 231 | pub fn tail_mut(&mut self) -> Option<&mut T> { 232 | self.node_mut(self.tail).map(|node| &mut node.data) 233 | } 234 | 235 | /// 从头部插入新的数据 236 | /// 237 | /// # Examples 238 | /// 239 | /// ``` 240 | /// use algorithm::FixedVec; 241 | /// fn main() { 242 | /// let mut val = FixedVec::new(2); 243 | /// assert_eq!(val.insert_head(1), Some((0, &mut 1))); 244 | /// assert_eq!(val.insert_head(2), Some((1, &mut 2))); 245 | /// assert_eq!(val.insert_head(3), None); 246 | /// } 247 | /// ``` 248 | pub fn insert_head(&mut self, data: T) -> Option<(usize, &mut T)> { 249 | let idx = self.next()?; 250 | if let Some(head) = self.node_mut(self.head) { 251 | head.prev = idx; 252 | } 253 | if self.node_ref(self.tail).is_none() { 254 | self.tail = idx; 255 | } 256 | let node = self.nodes.get_mut(idx).unwrap().insert(FixedVecNode { 257 | prev: usize::MAX, 258 | next: self.head, 259 | data, 260 | }); 261 | self.head = idx; 262 | Some((idx, &mut node.data)) 263 | } 264 | /// 从头部插入新的数据 265 | /// 266 | /// # Examples 267 | /// 268 | /// ``` 269 | /// use algorithm::FixedVec; 270 | /// fn main() { 271 | /// let mut val = FixedVec::new(2); 272 | /// assert_eq!(val.insert_tail(1), Some((0, &mut 1))); 273 | /// assert_eq!(val.insert_tail(2), Some((1, &mut 2))); 274 | /// assert_eq!(val.insert_tail(3), None); 275 | /// } 276 | /// ``` 277 | pub fn insert_tail(&mut self, data: T) -> Option<(usize, &mut T)> { 278 | let idx = self.next()?; 279 | if let Some(tail) = self.node_mut(self.tail) { 280 | tail.next = idx; 281 | } 282 | if self.node_ref(self.head).is_none() { 283 | self.head = idx; 284 | } 285 | let node = self.nodes.get_mut(idx).unwrap().insert(FixedVecNode { 286 | prev: self.tail, 287 | next: usize::MAX, 288 | data, 289 | }); 290 | self.tail = idx; 291 | Some((idx, &mut node.data)) 292 | } 293 | 294 | /// 从头部弹出数据 295 | /// 296 | /// # Examples 297 | /// 298 | /// ``` 299 | /// use algorithm::FixedVec; 300 | /// fn main() { 301 | /// let mut val = FixedVec::new(2); 302 | /// val.insert_tail(1); 303 | /// val.insert_tail(2); 304 | /// assert_eq!(val.pop_head(), Some(1)); 305 | /// } 306 | /// ``` 307 | #[inline] 308 | pub fn pop_head(&mut self) -> Option { 309 | self.remove(self.head) 310 | } 311 | /// 从尾部弹出数据 312 | /// 313 | /// # Examples 314 | /// 315 | /// ``` 316 | /// use algorithm::FixedVec; 317 | /// fn main() { 318 | /// let mut val = FixedVec::new(2); 319 | /// val.insert_head(1); 320 | /// val.insert_head(2); 321 | /// assert_eq!(val.pop_tail(), Some(1)); 322 | /// } 323 | /// ``` 324 | #[inline] 325 | pub fn pop_tail(&mut self) -> Option { 326 | self.remove(self.tail) 327 | } 328 | 329 | /// 删除指定位置数据 330 | /// 331 | /// # Examples 332 | /// 333 | /// ``` 334 | /// use algorithm::FixedVec; 335 | /// fn main() { 336 | /// let mut val = FixedVec::new(2); 337 | /// val.insert_head(1); 338 | /// val.insert_head(2); 339 | /// assert_eq!(val.remove(1), Some(2)); 340 | /// assert_eq!(val.len(), 1); 341 | /// assert_eq!(val.tail_idx(), 0); 342 | /// } 343 | /// ``` 344 | pub fn remove(&mut self, idx: usize) -> Option { 345 | let node = self.nodes.get_mut(idx)?.take()?; 346 | if let Some(prev) = self.node_mut(node.prev) { 347 | prev.next = node.next; 348 | } else { 349 | self.head = node.next; 350 | } 351 | if let Some(next) = self.node_mut(node.next) { 352 | next.prev = node.prev; 353 | } else { 354 | self.tail = node.prev; 355 | } 356 | self.free.push(idx); 357 | Some(node.data) 358 | } 359 | 360 | 361 | /// 迭代器 362 | /// 363 | /// # Examples 364 | /// 365 | /// ``` 366 | /// use algorithm::FixedVec; 367 | /// fn main() { 368 | /// let mut val = FixedVec::new(5); 369 | /// val.insert_head(1); 370 | /// val.insert_head(2); 371 | /// val.insert_head(3); 372 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![3, 2, 1]); 373 | /// } 374 | /// ``` 375 | #[inline] 376 | pub fn iter(&self) -> FixedVecIter<'_, T> { 377 | FixedVecIter { 378 | list: self, 379 | head: self.head, 380 | tail: self.tail, 381 | len: self.len(), 382 | } 383 | } 384 | 385 | /// 迭代器 386 | /// 387 | /// # Examples 388 | /// 389 | /// ``` 390 | /// use algorithm::FixedVec; 391 | /// fn main() { 392 | /// let mut val = FixedVec::new(5); 393 | /// val.insert_head(1); 394 | /// val.insert_head(2); 395 | /// val.insert_head(3); 396 | /// let _ = val.iter_mut().map(|(_, v)| *v = *v * 2).collect::>(); 397 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![6, 4, 2]); 398 | /// } 399 | /// ``` 400 | #[inline] 401 | pub fn iter_mut(&mut self) -> FixedVecIterMut<'_, T> { 402 | FixedVecIterMut { 403 | head: self.head, 404 | tail: self.tail, 405 | len: self.len(), 406 | list: self, 407 | } 408 | // let head = self.head; 409 | // let tail = self.tail; 410 | // let len = self.len(); 411 | // FixedVecIterMut::new(self, head, tail, len) 412 | } 413 | 414 | fn reorder(&mut self) { 415 | if self.is_empty() { 416 | return; 417 | } 418 | 419 | let len = self.len(); 420 | let mut current = 0; 421 | while current < len { 422 | let head = self.head; 423 | let head_data = self.pop_head().unwrap(); 424 | if head != current { 425 | debug_assert!(current < head, "{} < {}", current, head); 426 | if let Some(current_node) = self.nodes[current].take() { 427 | if let Some(node) = self.node_mut(current_node.prev) { 428 | node.next = head; 429 | } else { 430 | self.head = head; 431 | } 432 | if let Some(node) = self.node_mut(current_node.next) { 433 | node.prev = head; 434 | } else { 435 | self.tail = head; 436 | } 437 | self.nodes[head] = Some(current_node); 438 | } 439 | } 440 | self.nodes[current] = Some(FixedVecNode { 441 | prev: current.wrapping_sub(1), 442 | next: current + 1, 443 | data: head_data, 444 | }); 445 | current += 1; 446 | } 447 | self.head = 0; 448 | self.nodes[len - 1].as_mut().unwrap().next = usize::MAX; 449 | self.tail = len - 1; 450 | self.free.clear(); 451 | self.free.extend((len..self.nodes.len()).rev()); 452 | } 453 | 454 | 455 | /// 重置设置大小 456 | /// 457 | /// # Examples 458 | /// 459 | /// ``` 460 | /// use algorithm::FixedVec; 461 | /// fn main() { 462 | /// let mut val = FixedVec::new(5); 463 | /// val.insert_head(1); 464 | /// val.insert_head(2); 465 | /// val.resize(3); 466 | /// assert_eq!(val.len(), 2); 467 | /// assert_eq!(val.head_idx(), 0); 468 | /// assert_eq!(val.tail_idx(), 1); 469 | /// assert_eq!(val.tail(), Some(&1)); 470 | /// } 471 | /// ``` 472 | pub fn resize(&mut self, capacity: usize) { 473 | let len = self.len(); 474 | let cap = self.capacity(); 475 | if capacity < len { 476 | return; 477 | } 478 | match capacity.cmp(&cap) { 479 | Ordering::Less => { 480 | self.reorder(); 481 | self.nodes.truncate(capacity); 482 | self.free.clear(); 483 | self.free.extend(len..self.nodes.len()); 484 | self.capacity = capacity; 485 | } 486 | Ordering::Equal => {} 487 | Ordering::Greater => { 488 | self.capacity = capacity; 489 | } 490 | }; 491 | debug_assert_eq!(self.len(), len); 492 | debug_assert_eq!(self.capacity(), capacity); 493 | } 494 | 495 | pub fn retain(&mut self, mut f: F) 496 | where 497 | F: FnMut(&T) -> bool, 498 | { 499 | let mut head = self.head; 500 | while head != usize::MAX { 501 | let node = self.node_ref(head).unwrap(); 502 | let next = node.next; 503 | if !f(&node.data) { 504 | self.remove(head); 505 | } 506 | head = next; 507 | } 508 | } 509 | 510 | pub fn retain_mut(&mut self, mut f: F) 511 | where 512 | F: FnMut(&mut T) -> bool, 513 | { 514 | let mut head = self.head; 515 | while head != usize::MAX { 516 | let node = self.node_mut(head).unwrap(); 517 | let next = node.next; 518 | if !f(&mut node.data) { 519 | self.remove(head); 520 | } 521 | head = next; 522 | } 523 | } 524 | 525 | /// 将指定位置挪到最前面 526 | /// 527 | /// # Examples 528 | /// 529 | /// ``` 530 | /// use algorithm::FixedVec; 531 | /// fn main() { 532 | /// let mut val = FixedVec::new(5); 533 | /// val.insert_head(1); 534 | /// val.insert_head(2); 535 | /// val.insert_head(3); 536 | /// assert_eq!(val.get(1), Some(&2)); 537 | /// assert_eq!(val.head(), Some(&3)); 538 | /// val.move_head(1); 539 | /// assert_eq!(val.head(), Some(&2)); 540 | /// assert_eq!(val.get(1), Some(&2)); 541 | /// assert_eq!(val.tail(), Some(&1)); 542 | /// } 543 | /// ``` 544 | #[inline] 545 | pub fn move_head(&mut self, idx: usize) -> Option<&mut T> { 546 | let node = self.nodes.get_mut(idx)?.take()?; 547 | if let Some(prev) = self.node_mut(node.prev) { 548 | prev.next = node.next; 549 | } else { 550 | self.head = node.next; 551 | } 552 | if let Some(next) = self.node_mut(node.next) { 553 | next.prev = node.prev; 554 | } else { 555 | self.tail = node.prev; 556 | } 557 | 558 | if let Some(head) = self.node_mut(self.head) { 559 | head.prev = idx; 560 | } 561 | if self.node_ref(self.tail).is_none() { 562 | self.tail = idx; 563 | } 564 | 565 | let node = self.nodes.get_mut(idx).unwrap().insert(FixedVecNode { 566 | prev: usize::MAX, 567 | next: self.head, 568 | data: node.data, 569 | }); 570 | self.head = idx; 571 | Some(&mut node.data) 572 | } 573 | 574 | 575 | /// 将指定位置挪到最后面 576 | /// 577 | /// # Examples 578 | /// 579 | /// ``` 580 | /// use algorithm::FixedVec; 581 | /// fn main() { 582 | /// let mut val = FixedVec::new(5); 583 | /// val.insert_tail(1); 584 | /// val.insert_tail(2); 585 | /// val.insert_tail(3); 586 | /// assert_eq!(val.get(1), Some(&2)); 587 | /// assert_eq!(val.tail(), Some(&3)); 588 | /// val.move_tail(1); 589 | /// assert_eq!(val.tail(), Some(&2)); 590 | /// assert_eq!(val.get(1), Some(&2)); 591 | /// assert_eq!(val.head(), Some(&1)); 592 | /// } 593 | /// ``` 594 | #[inline] 595 | pub fn move_tail(&mut self, idx: usize) -> Option<&mut T> { 596 | let node = self.nodes.get_mut(idx)?.take()?; 597 | if let Some(prev) = self.node_mut(node.prev) { 598 | prev.next = node.next; 599 | } else { 600 | self.head = node.next; 601 | } 602 | if let Some(next) = self.node_mut(node.next) { 603 | next.prev = node.prev; 604 | } else { 605 | self.tail = node.prev; 606 | } 607 | 608 | if let Some(tail) = self.node_mut(self.tail) { 609 | tail.next = idx; 610 | } 611 | if self.node_ref(self.head).is_none() { 612 | self.head = idx; 613 | } 614 | 615 | let node = self.nodes.get_mut(idx).unwrap().insert(FixedVecNode { 616 | prev: self.tail, 617 | next: usize::MAX, 618 | data: node.data, 619 | }); 620 | self.tail = idx; 621 | Some(&mut node.data) 622 | } 623 | } 624 | 625 | #[derive(Debug)] 626 | pub struct FixedVecIter<'a, T> { 627 | list: &'a FixedVec, 628 | head: usize, 629 | tail: usize, 630 | len: usize, 631 | } 632 | 633 | impl<'a, T> Clone for FixedVecIter<'a, T> { 634 | fn clone(&self) -> Self { 635 | Self { 636 | list: self.list, 637 | head: self.head, 638 | tail: self.tail, 639 | len: self.len, 640 | } 641 | } 642 | } 643 | 644 | impl<'a, T> Iterator for FixedVecIter<'a, T> { 645 | type Item = (usize, &'a T); 646 | 647 | fn next(&mut self) -> Option { 648 | if self.len > 0 { 649 | let head = self.head; 650 | let node = self.list.node_ref(head).unwrap(); 651 | self.head = node.next; 652 | self.len -= 1; 653 | Some((head, &node.data)) 654 | } else { 655 | None 656 | } 657 | } 658 | 659 | fn size_hint(&self) -> (usize, Option) { 660 | (self.len, Some(self.len)) 661 | } 662 | } 663 | 664 | impl<'a, T> DoubleEndedIterator for FixedVecIter<'a, T> { 665 | fn next_back(&mut self) -> Option { 666 | if self.len > 0 { 667 | let tail = self.tail; 668 | let node = self.list.node_ref(tail).unwrap(); 669 | self.tail = node.prev; 670 | self.len -= 1; 671 | Some((tail, &node.data)) 672 | } else { 673 | None 674 | } 675 | } 676 | } 677 | 678 | #[derive(Debug)] 679 | pub struct FixedVecIterMut<'a, T> { 680 | list: &'a mut FixedVec, 681 | head: usize, 682 | tail: usize, 683 | len: usize, 684 | } 685 | 686 | impl<'a, T> Iterator for FixedVecIterMut<'a, T> { 687 | type Item = (usize, &'a mut T); 688 | 689 | fn next(&mut self) -> Option { 690 | if self.len > 0 { 691 | let head = self.head; 692 | let node = unsafe { 693 | core::mem::transmute::<&'_ mut FixedVecNode, &'a mut FixedVecNode>(self.list.node_mut(head).unwrap()) 694 | }; 695 | self.head = node.next; 696 | self.len -= 1; 697 | Some((head, &mut node.data)) 698 | } else { 699 | None 700 | } 701 | } 702 | 703 | fn size_hint(&self) -> (usize, Option) { 704 | (self.len, Some(self.len)) 705 | } 706 | } 707 | 708 | impl<'a, T> DoubleEndedIterator for FixedVecIterMut<'a, T> { 709 | fn next_back(&mut self) -> Option { 710 | if self.len > 0 { 711 | let tail = self.tail; 712 | let node = unsafe { 713 | core::mem::transmute::<&'_ mut FixedVecNode, &'a mut FixedVecNode>(self.list.node_mut(tail).unwrap()) 714 | }; 715 | self.tail = node.prev; 716 | self.len -= 1; 717 | Some((tail, &mut node.data)) 718 | } else { 719 | None 720 | } 721 | } 722 | } -------------------------------------------------------------------------------- /src/timer/timer_wheel.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | fmt::{self, Display}, 3 | ptr, u64, 4 | }; 5 | 6 | use super::Timer; 7 | 8 | struct Entry { 9 | val: T, 10 | when: u64, 11 | id: u64, 12 | } 13 | /// 单轮结构 14 | pub struct OneTimerWheel { 15 | /// 当时指针指向的位置,如秒针指向3点钟方向 16 | index: u64, 17 | /// 当前结构的容量,比如60s可能有30个槽,每个都是2秒 18 | capation: u64, 19 | /// 当前槽的个数 20 | num: u64, 21 | /// 当前结构步长,如分钟就表示60s的 22 | step: u64, 23 | /// 修正步长,当前的步长*基础步长 24 | fix_step: u64, 25 | /// 当前槽位容纳的元素 26 | slots: Vec>>, 27 | /// 当前轮结构的父轮,如当前是分的,那父轮为时轮 28 | parent: *mut OneTimerWheel, 29 | /// 当前轮结构的子轮,如当前是分的,那父轮为秒轮 30 | child: *mut OneTimerWheel, 31 | /// 当前轮的名字,辅助定位 32 | name: &'static str, 33 | } 34 | 35 | impl OneTimerWheel { 36 | pub fn new(num: u64, step: u64, one_step: u64, name: &'static str) -> Self { 37 | let mut slots = vec![]; 38 | for _ in 0..num { 39 | slots.push(Vec::new()); 40 | } 41 | Self { 42 | index: 0, 43 | capation: num * step, 44 | num, 45 | step, 46 | fix_step: one_step * step, 47 | slots, 48 | parent: ptr::null_mut(), 49 | child: ptr::null_mut(), 50 | name, 51 | } 52 | } 53 | 54 | pub fn clear(&mut self) { 55 | for idx in 0..self.num as usize { 56 | self.slots[idx].clear(); 57 | } 58 | } 59 | 60 | pub fn append(&mut self, next: *mut OneTimerWheel) { 61 | if self.child.is_null() { 62 | unsafe { 63 | (*next).parent = self; 64 | self.child = next; 65 | } 66 | } else { 67 | unsafe { 68 | (*self.child).append(next); 69 | } 70 | } 71 | } 72 | 73 | fn add_timer(&mut self, entry: Entry) { 74 | let offset = entry.when; 75 | self.add_timer_with_offset(entry, offset); 76 | } 77 | 78 | fn del_timer(&mut self, timer_id: u64) -> Option { 79 | for i in 0..self.num as usize { 80 | let mut found_idx = None; 81 | for (idx, val) in self.slots[i].iter().enumerate() { 82 | if val.id == timer_id { 83 | found_idx = Some(idx); 84 | break; 85 | } 86 | } 87 | if let Some(idx) = found_idx { 88 | return Some(self.slots[i].remove(idx).val); 89 | } 90 | } 91 | None 92 | } 93 | 94 | fn get_timer(&self, timer_id: &u64) -> Option<&T> { 95 | for i in 0..self.num as usize { 96 | for val in self.slots[i].iter() { 97 | if &val.id == timer_id { 98 | return Some(&val.val); 99 | } 100 | } 101 | } 102 | None 103 | } 104 | 105 | fn get_mut_timer(&mut self, timer_id: &u64) -> Option<&mut T> { 106 | for i in 0..self.num as usize { 107 | let mut found_idx = None; 108 | let v = &mut self.slots[i]; 109 | for (idx, val) in v.iter().enumerate() { 110 | if &val.id == timer_id { 111 | found_idx = Some(idx); 112 | break; 113 | } 114 | } 115 | if let Some(idx) = found_idx { 116 | return Some(&mut self.slots[i][idx].val); 117 | } 118 | } 119 | None 120 | } 121 | 122 | fn add_step_timer(&mut self, entry: Entry) { 123 | let offset = entry.when % self.num; 124 | self.add_timer_with_offset(entry, offset); 125 | } 126 | 127 | fn add_timer_with_offset(&mut self, entry: Entry, offset: u64) { 128 | if offset > self.capation { 129 | let index = (self.index + self.num - 1) % self.num; 130 | self.slots[index as usize].push(entry); 131 | } else if offset <= self.fix_step && !self.child.is_null() { 132 | unsafe { 133 | (*self.child).add_timer_with_offset(entry, offset); 134 | } 135 | } else { 136 | // 当前偏差值还在自己的容纳范围之前,做容错,排在最后处理位 137 | let index = (offset - 1) / self.fix_step; 138 | let index = (index + self.index) % self.num; 139 | self.slots[index as usize].push(entry); 140 | } 141 | } 142 | 143 | pub fn update_index( 144 | &mut self, 145 | offset: u64, 146 | remainder: u64, 147 | result: &mut Vec<(u64, T)>, 148 | ) -> (u64, u64) { 149 | let next = self.index + offset; 150 | let mut all = 0; 151 | for idx in self.index..next { 152 | if all > self.num { 153 | break; 154 | } 155 | all += 1; 156 | let idx = idx % self.num; 157 | let list = &mut self.slots[idx as usize]; 158 | for val in list.drain(..) { 159 | result.push((val.id, val.val)); 160 | } 161 | } 162 | self.index = next % self.num; 163 | if !self.child.is_null() { 164 | unsafe { 165 | let list = &mut self.slots[self.index as usize]; 166 | for mut val in list.drain(..) { 167 | val.when = (val.when % self.step).saturating_sub(remainder); 168 | if val.when == 0 { 169 | result.push((val.id, val.val)); 170 | } else { 171 | (*self.child).add_step_timer(val); 172 | } 173 | } 174 | } 175 | } 176 | (next / self.num, next % self.num + remainder) 177 | } 178 | } 179 | 180 | /// 计时器轮,模拟时钟格式组成的高效计时器 181 | /// 182 | /// 时间轮是一个环形的数据结构,可以想象成一个时钟的面,被分成多个格子 183 | /// 184 | /// 每个格子代表一段时间,这段时间越短,定时器的精度就越高。 185 | /// 186 | /// 每个格子用一个Vec存储放在该格子上的延时任务。 187 | /// 188 | /// Mark: 在Rust中双向链表中暂未提供元素关键列表的接口,这里改用Vec,删除时会额外移动Vec值 189 | /// 190 | /// # Examples 191 | /// 192 | /// ``` 193 | /// use algorithm::TimerWheel; 194 | /// fn main() { 195 | /// let mut timer = TimerWheel::new(); 196 | /// timer.append_timer_wheel(60, "SecondWheel"); 197 | /// timer.append_timer_wheel(60, "MinuteWheel"); 198 | /// timer.append_timer_wheel(12, "HourWheel"); 199 | /// timer.add_timer(30); 200 | /// assert_eq!(timer.get_delay_id(), 30); 201 | /// timer.add_timer(149); 202 | /// assert_eq!(timer.get_delay_id(), 30); 203 | /// let t = timer.add_timer(600); 204 | /// assert_eq!(timer.get_delay_id(), 30); 205 | /// timer.add_timer(1); 206 | /// assert_eq!(timer.get_delay_id(), 1); 207 | /// timer.del_timer(t); 208 | /// timer.add_timer(150); 209 | /// assert_eq!(timer.get_delay_id(), 1); 210 | /// let val = timer.update_deltatime(30).unwrap(); 211 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![1, 30]); 212 | /// timer.add_timer(2); 213 | /// let val = timer.update_deltatime(119).unwrap(); 214 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![2, 149]); 215 | /// let val = timer.update_deltatime(1).unwrap(); 216 | /// assert_eq!(val.iter().map(|(_, v)| *v).collect::>(), vec![150]); 217 | /// assert!(timer.is_empty()); 218 | /// } 219 | /// ``` 220 | pub struct TimerWheel { 221 | /// 时轮的最大轮,以时钟为例就是时针 222 | greatest: *mut OneTimerWheel, 223 | /// 时轮的最小轮,以时钟为例就是秒针 224 | lessest: *mut OneTimerWheel, 225 | /// 时轮的最小间隔,以时间为例就是秒 226 | one_step: u64, 227 | /// 维护定时器id 228 | next_timer_id: u64, 229 | /// 限制最大的timer id 230 | max_timer_id: u64, 231 | /// 离的最近的id 232 | delay_id: u64, 233 | /// 总共的递进步长,缓存优化触发 234 | all_deltatime: u64, 235 | /// 当时时轮里的元素个数 236 | len: usize, 237 | } 238 | 239 | impl TimerWheel { 240 | /// 创建一个计时器轮 241 | /// # Examples 242 | /// 243 | /// ``` 244 | /// use algorithm::TimerWheel; 245 | /// fn main() { 246 | /// let mut timer = TimerWheel::::new(); 247 | /// assert!(timer.is_empty()); 248 | /// } 249 | /// ``` 250 | pub fn new() -> Self { 251 | Self { 252 | greatest: ptr::null_mut(), 253 | lessest: ptr::null_mut(), 254 | next_timer_id: 1, 255 | max_timer_id: u64::MAX, 256 | delay_id: 0, 257 | one_step: 1, 258 | all_deltatime: 0, 259 | len: 0, 260 | } 261 | } 262 | 263 | /// 获取计时器轮的长度 264 | /// # Examples 265 | /// 266 | /// ``` 267 | /// use algorithm::TimerWheel; 268 | /// fn main() { 269 | /// let mut timer = TimerWheel::::new(); 270 | /// timer.append_timer_wheel(60, "SecondWheel"); 271 | /// assert!(timer.is_empty()); 272 | /// timer.add_timer(1); 273 | /// assert_eq!(timer.len(), 1); 274 | /// let t = timer.add_timer(2); 275 | /// assert_eq!(timer.len(), 2); 276 | /// timer.del_timer(t); 277 | /// assert_eq!(timer.len(), 1); 278 | /// } 279 | /// ``` 280 | pub fn len(&self) -> usize { 281 | self.len 282 | } 283 | 284 | /// 是否为空 285 | /// # Examples 286 | /// 287 | /// ``` 288 | /// use algorithm::TimerWheel; 289 | /// fn main() { 290 | /// let mut timer = TimerWheel::::new(); 291 | /// assert!(timer.is_empty()); 292 | /// } 293 | /// ``` 294 | pub fn is_empty(&self) -> bool { 295 | self.len == 0 296 | } 297 | 298 | /// 清除所有的槽位 299 | /// # Examples 300 | /// 301 | /// ``` 302 | /// use algorithm::TimerWheel; 303 | /// fn main() { 304 | /// let mut timer = TimerWheel::::new(); 305 | /// timer.append_timer_wheel(60, "SecondWheel"); 306 | /// assert!(timer.is_empty()); 307 | /// timer.add_timer(1); 308 | /// timer.add_timer(2); 309 | /// assert_eq!(timer.len(), 2); 310 | /// timer.clear(); 311 | /// assert_eq!(timer.len(), 0); 312 | /// } 313 | /// ``` 314 | pub fn clear(&mut self) { 315 | let mut wheel = self.lessest; 316 | while !wheel.is_null() { 317 | unsafe { 318 | (*wheel).clear(); 319 | wheel = (*wheel).parent; 320 | } 321 | } 322 | self.len = 0; 323 | } 324 | 325 | pub fn get_one_step(&self) -> u64 { 326 | self.one_step 327 | } 328 | 329 | pub fn set_one_step(&mut self, step: u64) { 330 | self.one_step = step.max(1); 331 | } 332 | /// 添加计时器轮, 设置槽位和精度值, 名字用来辅助 333 | /// 334 | /// # Examples 335 | /// 336 | /// ``` 337 | /// use algorithm::TimerWheel; 338 | /// fn main() { 339 | /// let mut timer = TimerWheel::new(); 340 | /// timer.append_timer_wheel(60, "SecondWheel"); 341 | /// timer.append_timer_wheel(60, "MinuteWheel"); 342 | /// timer.append_timer_wheel(12, "HourWheel"); 343 | /// timer.add_timer(30); 344 | /// } 345 | pub fn append_timer_wheel(&mut self, slots: u64, name: &'static str) { 346 | debug_assert!(self.len == 0, "必须时轮为空才可改变时轮"); 347 | let step = if self.greatest.is_null() { 348 | self.one_step 349 | } else { 350 | let mut now_step = 1; 351 | let mut node = self.greatest; 352 | unsafe { 353 | while !node.is_null() { 354 | now_step *= (*node).capation; 355 | node = (*node).child; 356 | } 357 | } 358 | now_step / self.one_step 359 | }; 360 | let one = Box::into_raw(Box::new(OneTimerWheel::new(slots, step, self.one_step, name))); 361 | self.delay_id = self.delay_id.max(slots * step); 362 | if self.lessest.is_null() { 363 | self.lessest = one; 364 | self.greatest = one; 365 | } else { 366 | unsafe { 367 | let child = self.greatest; 368 | (*one).append(child); 369 | self.greatest = one; 370 | } 371 | } 372 | } 373 | 374 | /// 计时器轮的递进时间 375 | /// 376 | /// # Examples 377 | /// 378 | /// ``` 379 | /// use algorithm::TimerWheel; 380 | /// fn main() { 381 | /// let mut timer = TimerWheel::new(); 382 | /// timer.append_timer_wheel(60, "SecondWheel"); 383 | /// timer.add_timer(30); 384 | /// let val = timer.update_deltatime(30).unwrap(); 385 | /// assert_eq!(val, vec![(1, 30)]); 386 | /// } 387 | pub fn update_deltatime(&mut self, delta: u64) -> Option> { 388 | debug_assert!(self.one_step > 0); 389 | self.update_now(self.all_deltatime.wrapping_add(delta)) 390 | } 391 | 392 | /// 计时器轮的递进时间 393 | /// 394 | /// # Examples 395 | /// 396 | /// ``` 397 | /// use algorithm::TimerWheel; 398 | /// fn main() { 399 | /// let mut timer = TimerWheel::new(); 400 | /// timer.append_timer_wheel(60, "SecondWheel"); 401 | /// timer.add_timer(30); 402 | /// let val = timer.update_deltatime(30).unwrap(); 403 | /// assert_eq!(val, vec![(1, 30)]); 404 | /// } 405 | pub fn update_now(&mut self, now: u64) -> Option> { 406 | debug_assert!(self.one_step > 0); 407 | self.all_deltatime = now; 408 | let mut offset = self.all_deltatime / self.one_step; 409 | if offset < self.delay_id { 410 | return None; 411 | } 412 | 413 | self.all_deltatime -= offset * self.one_step; 414 | let mut remainder = 0; 415 | let mut result = vec![]; 416 | let mut wheel = self.lessest; 417 | while !wheel.is_null() { 418 | unsafe { 419 | (offset, remainder) = (*wheel).update_index(offset, remainder, &mut result); 420 | if offset == 0 { 421 | break; 422 | } 423 | wheel = (*wheel).parent; 424 | } 425 | } 426 | self.calc_delay_id(); 427 | self.len -= result.len(); 428 | Some(result) 429 | } 430 | 431 | /// 计时器轮的递进时间 432 | /// 433 | /// # Examples 434 | /// 435 | /// ``` 436 | /// use algorithm::TimerWheel; 437 | /// fn main() { 438 | /// let mut timer = TimerWheel::new(); 439 | /// timer.append_timer_wheel(60, "SecondWheel"); 440 | /// timer.add_timer(30); 441 | /// let mut idx = 0; 442 | /// timer.update_deltatime_with_callback(30, &mut |_, id, v| { 443 | /// idx = v; 444 | /// None 445 | /// }); 446 | /// assert_eq!(idx, 30); 447 | /// } 448 | pub fn update_deltatime_with_callback(&mut self, delta: u64, f: &mut F) 449 | where 450 | F: FnMut(&mut Self, u64, T) -> Option<(u64, T)>, 451 | { 452 | debug_assert!(self.one_step > 0); 453 | self.update_now_with_callback(self.all_deltatime.wrapping_add(delta), f); 454 | } 455 | 456 | /// 计时器轮的递进时间 457 | /// 458 | /// # Examples 459 | /// 460 | /// ``` 461 | /// use algorithm::TimerWheel; 462 | /// fn main() { 463 | /// let mut timer = TimerWheel::new(); 464 | /// timer.append_timer_wheel(60, "SecondWheel"); 465 | /// timer.add_timer(30); 466 | /// let mut idx = 0; 467 | /// timer.update_deltatime_with_callback(30, &mut |_, _, v| { 468 | /// idx = v; 469 | /// None 470 | /// }); 471 | /// assert_eq!(idx, 30); 472 | /// } 473 | pub fn update_now_with_callback(&mut self, now: u64, f: &mut F) 474 | where 475 | F: FnMut(&mut Self, u64, T) -> Option<(u64, T)>, 476 | { 477 | debug_assert!(self.one_step > 0); 478 | if let Some(result) = self.update_now(now) { 479 | let mut collect_result = vec![]; 480 | for r in result.into_iter() { 481 | if let Some(v) = (*f)(self, r.0, r.1) { 482 | collect_result.push(v); 483 | } 484 | } 485 | for (timer_id, val) in collect_result.drain(..) { 486 | self.add_timer_by_id(timer_id, val); 487 | } 488 | } 489 | } 490 | 491 | /// 计算下一个delay_id, 根据容器的密度稀疏有关 492 | /// 密度高的基本为O(1)的复杂度, 最差情况为O(n)的复杂度 493 | /// 总刻度数以时钟为计秒轮遍历60次,分轮遍历60次,时轮遍历12次,即最高遍历132次 494 | /// 495 | /// # Examples 496 | /// 497 | /// ``` 498 | /// use algorithm::TimerWheel; 499 | /// fn main() { 500 | /// let mut timer = TimerWheel::new(); 501 | /// timer.append_timer_wheel(60, "SecondWheel"); 502 | /// timer.add_timer(30); 503 | /// assert_eq!(timer.get_delay_id(), 30); 504 | /// } 505 | pub fn calc_delay_id(&mut self) { 506 | let mut next_delay_id = 0; 507 | let mut wheel = self.lessest; 508 | 'outer: while !wheel.is_null() { 509 | unsafe { 510 | let (step, index, cap) = ((*wheel).step, (*wheel).index, (*wheel).num); 511 | for i in 0..cap { 512 | let index = (index + i) % cap; 513 | if !(*wheel).slots[index as usize].is_empty() { 514 | next_delay_id = (i + 1) * step; 515 | break 'outer; 516 | } 517 | } 518 | next_delay_id = cap * step; 519 | wheel = (*wheel).parent; 520 | } 521 | } 522 | self.delay_id = next_delay_id / self.one_step; 523 | } 524 | 525 | /// 删除指定的定时器,时间复杂度为O(n), 526 | /// 该模型删除不具备优势,需要频繁删除请选用其它时间框架 527 | /// 528 | /// # Examples 529 | /// 530 | /// ``` 531 | /// use algorithm::TimerWheel; 532 | /// fn main() { 533 | /// let mut timer = TimerWheel::new(); 534 | /// timer.append_timer_wheel(60, "SecondWheel"); 535 | /// let t = timer.add_timer(30); 536 | /// timer.del_timer(t); 537 | /// assert_eq!(timer.len(), 0); 538 | /// } 539 | pub fn del_timer(&mut self, timer_id: u64) -> Option { 540 | let mut wheel = self.lessest; 541 | while !wheel.is_null() { 542 | unsafe { 543 | if let Some(v) = (*wheel).del_timer(timer_id) { 544 | self.len -= 1; 545 | return Some(v); 546 | } 547 | wheel = (*wheel).parent; 548 | } 549 | } 550 | None 551 | } 552 | 553 | /// 获取指定的定时器,时间复杂度为O(n) 554 | /// 该模型获取不具备优势,需要频繁获取请选用其它时间框架 555 | /// 556 | /// # Examples 557 | /// 558 | /// ``` 559 | /// use algorithm::TimerWheel; 560 | /// fn main() { 561 | /// let mut timer = TimerWheel::new(); 562 | /// timer.append_timer_wheel(60, "SecondWheel"); 563 | /// let t = timer.add_timer(30); 564 | /// assert_eq!(timer.get_timer(&t), Some(&30)); 565 | /// } 566 | pub fn get_timer(&self, timer_id: &u64) -> Option<&T> { 567 | let mut wheel = self.lessest; 568 | while !wheel.is_null() { 569 | unsafe { 570 | if let Some(v) = (*wheel).get_timer(timer_id) { 571 | return Some(v); 572 | } 573 | wheel = (*wheel).parent; 574 | } 575 | } 576 | None 577 | } 578 | 579 | /// 获取指定的定时器,时间复杂度为O(n) 580 | /// 该模型获取不具备优势,需要频繁获取请选用其它时间框架 581 | /// 582 | /// # Examples 583 | /// 584 | /// ``` 585 | /// use algorithm::TimerWheel; 586 | /// fn main() { 587 | /// let mut timer = TimerWheel::new(); 588 | /// timer.append_timer_wheel(60, "SecondWheel"); 589 | /// let t = timer.add_timer(30); 590 | /// *timer.get_mut_timer(&t).unwrap() = 33; 591 | /// let val = timer.update_deltatime(30).unwrap(); 592 | /// assert_eq!(val, vec![(1, 33)]); 593 | /// } 594 | pub fn get_mut_timer(&mut self, timer_id: &u64) -> Option<&mut T> { 595 | let mut wheel = self.lessest; 596 | while !wheel.is_null() { 597 | unsafe { 598 | if let Some(v) = (*wheel).get_mut_timer(timer_id) { 599 | return Some(v); 600 | } 601 | wheel = (*wheel).parent; 602 | } 603 | } 604 | None 605 | } 606 | 607 | pub fn get_max_timerid(&self) -> u64 { 608 | self.max_timer_id 609 | } 610 | 611 | pub fn set_max_timerid(&mut self, max: u64) { 612 | self.max_timer_id = max; 613 | } 614 | 615 | fn get_next_timerid(&mut self) -> u64 { 616 | let timer_id = self.next_timer_id; 617 | if self.next_timer_id >= self.max_timer_id { 618 | self.next_timer_id = 1; 619 | } else { 620 | self.next_timer_id = self.next_timer_id + 1; 621 | } 622 | timer_id 623 | } 624 | 625 | /// 添加定时器元素, 时间复杂度为O(1) 626 | /// # Examples 627 | /// 628 | /// ``` 629 | /// use algorithm::TimerWheel; 630 | /// fn main() { 631 | /// let mut timer = TimerWheel::new(); 632 | /// timer.append_timer_wheel(60, "SecondWheel"); 633 | /// timer.add_timer(30); 634 | /// } 635 | pub fn add_timer(&mut self, val: T) -> u64 { 636 | debug_assert!(!self.greatest.is_null(), "必须设置时轮才能添加元素"); 637 | let timer_id: u64 = self.get_next_timerid(); 638 | self.add_timer_by_id(timer_id, val); 639 | timer_id 640 | } 641 | 642 | pub fn add_timer_by_id(&mut self, timer_id: u64, mut val: T) { 643 | let entry = Entry { 644 | when: val.when_mut().max(1), 645 | val, 646 | id: timer_id, 647 | }; 648 | self.delay_id = self.delay_id.min(entry.when / self.one_step); 649 | unsafe { 650 | (*self.greatest).add_timer(entry); 651 | } 652 | self.len += 1; 653 | } 654 | 655 | /// 获取下一个延时 656 | /// # Examples 657 | /// 658 | /// ``` 659 | /// use algorithm::TimerWheel; 660 | /// fn main() { 661 | /// let mut timer = TimerWheel::new(); 662 | /// timer.append_timer_wheel(60, "SecondWheel"); 663 | /// timer.add_timer(30); 664 | /// assert_eq!(timer.get_delay_id(), 30); 665 | /// } 666 | pub fn get_delay_id(&self) -> u64 { 667 | self.delay_id 668 | } 669 | } 670 | 671 | impl Display for TimerWheel { 672 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 673 | f.write_str("TimerWheel {\r\n")?; 674 | let mut wheel = self.greatest; 675 | while !wheel.is_null() { 676 | unsafe { 677 | f.write_fmt(format_args!( 678 | "{}, slots: {}, step: {}", 679 | (*wheel).name, 680 | (*wheel).slots.len(), 681 | (*wheel).step 682 | ))?; 683 | wheel = (*wheel).child; 684 | } 685 | } 686 | f.write_str("}") 687 | } 688 | } 689 | 690 | impl Drop for TimerWheel { 691 | fn drop(&mut self) { 692 | let mut wheel = self.greatest; 693 | while !wheel.is_null() { 694 | unsafe { 695 | let val = *Box::from_raw(wheel); 696 | wheel = val.child; 697 | } 698 | } 699 | } 700 | } 701 | --------------------------------------------------------------------------------