1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::collections::{BTreeMap, LinkedList};
pub struct TimeoutSet<T>{
time_list: BTreeMap<u64, LinkedList<T>>,
}
impl <T> Default for TimeoutSet<T> {
fn default() -> Self {
Self::new()
}
}
impl <T> TimeoutSet <T> {
pub fn new() -> Self{
Self{
time_list:BTreeMap::new(),
}
}
pub fn add(&mut self,time:u64,val:T) {
match self.time_list.get_mut(&time) {
Some(list) => {
list.push_back(val);
},
None => {
let mut list = LinkedList::new();
list.push_back(val);
self.time_list.insert(time,list);
}
}
}
fn get_timeout_keys(&self,time:u64) -> LinkedList<u64> {
let mut times= LinkedList::new();
for (t, _) in self.time_list.iter() {
if *t <= time {
times.push_back(*t)
}
else{
break;
}
}
times
}
pub fn get_timeout_values(&self,time:u64) -> LinkedList<&T> {
let mut list = LinkedList::new();
for (t, values) in self.time_list.iter() {
if *t <= time {
for item in values {
list.push_back(item);
}
}
else{
break;
}
}
list
}
pub fn timeout(&mut self,time:u64) -> LinkedList<T> {
let mut list = LinkedList::new();
let times= self.get_timeout_keys(time);
let mut time_list = &mut self.time_list;
for t in times {
if let Some(mut values)=time_list.remove(&t){
list.append(&mut values)
}
}
list
}
pub fn clear(&mut self) {
self.time_list.clear();
}
}