@@ -2,6 +2,7 @@ use crate::HashMap;
22
33use crate :: RBTree ;
44use std:: cmp:: Ordering ;
5+ use std:: u64;
56use std:: vec;
67
78use super :: Timer ;
@@ -52,12 +53,12 @@ impl PartialOrd for TreeKey {
5253/// timer.add_timer(150);
5354/// assert_eq!(timer.tick_first(), Some(1));
5455/// let val = timer.update_deltatime(30).unwrap();
55- /// assert_eq!(val, vec![1, 30]);
56+ /// assert_eq!(val.iter().map(|(_, v)| *v).collect::<Vec<usize>>() , vec![1, 30]);
5657/// timer.add_timer(2);
5758/// let val = timer.update_deltatime(119).unwrap();
58- /// assert_eq!(val, vec![2, 149]);
59+ /// assert_eq!(val.iter().map(|(_, v)| *v).collect::<Vec<usize>>() , vec![2, 149]);
5960/// let val = timer.update_deltatime(1).unwrap();
60- /// assert_eq!(val, vec![150]);
61+ /// assert_eq!(val.iter().map(|(_, v)| *v).collect::<Vec<usize>>() , vec![150]);
6162/// assert!(timer.is_empty());
6263/// }
6364/// ```
@@ -71,6 +72,8 @@ pub struct TimerRBTree<T: Timer> {
7172
7273 /// id记录
7374 next_timer_id : u64 ,
75+ /// max id
76+ max_timer_id : u64 ,
7477}
7578
7679impl < T : Timer > TimerRBTree < T > {
@@ -79,7 +82,8 @@ impl<T: Timer> TimerRBTree<T> {
7982 tree : RBTree :: new ( ) ,
8083 map : HashMap :: new ( ) ,
8184 cur_step : 0 ,
82- next_timer_id : 0 ,
85+ next_timer_id : 1 ,
86+ max_timer_id : u64:: MAX ,
8387 }
8488 }
8589
@@ -126,7 +130,32 @@ impl<T: Timer> TimerRBTree<T> {
126130 self . tree . clear ( ) ;
127131 self . map . clear ( ) ;
128132 self . cur_step = 0 ;
129- self . next_timer_id = 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
130159 }
131160
132161 /// 添加定时器元素
@@ -140,14 +169,16 @@ impl<T: Timer> TimerRBTree<T> {
140169 /// assert_eq!(timer.len(), 1);
141170 /// }
142171 pub fn add_timer ( & mut self , val : T ) -> u64 {
143- let timer_id = self . next_timer_id ;
144- self . next_timer_id = self . next_timer_id . wrapping_add ( 1 ) ;
172+ let timer_id = self . get_next_timerid ( ) ;
173+ self . _add_timer ( timer_id, val) ;
174+ timer_id
175+ }
176+
177+ fn _add_timer ( & mut self , timer_id : u64 , val : T ) {
145178 let when = val. when ( ) ;
146179 self . tree . insert ( TreeKey ( when, timer_id) , val) ;
147180 self . map . insert ( timer_id, when) ;
148- timer_id
149181 }
150-
151182 /// 删除指定的定时器,时间复杂度为O(logn),
152183 ///
153184 /// # Examples
@@ -200,7 +231,7 @@ impl<T: Timer> TimerRBTree<T> {
200231 /// let t = timer.add_timer(30);
201232 /// *timer.get_mut_timer(&t).unwrap() = 33;
202233 /// let val = timer.update_deltatime(30).unwrap();
203- /// assert_eq!(val, vec![33]);
234+ /// assert_eq!(val.iter().map(|(_, v)| *v).collect::<Vec<usize>>() , vec![33]);
204235 /// }
205236 pub fn get_mut_timer ( & mut self , timer_id : & u64 ) -> Option < & mut T > {
206237 if let Some ( when) = self . map . get ( timer_id) {
@@ -237,17 +268,17 @@ impl<T: Timer> TimerRBTree<T> {
237268 /// let mut timer = TimerRBTree::new();
238269 /// timer.add_timer(30);
239270 /// let val = timer.update_deltatime(30).unwrap();
240- /// assert_eq!(val, vec![30]);
271+ /// assert_eq!(val.iter().map(|(_, v)| *v).collect::<Vec<usize>>() , vec![30]);
241272 /// }
242- pub fn update_now ( & mut self , now : u64 ) -> Option < Vec < T > > {
273+ pub fn update_now ( & mut self , now : u64 ) -> Option < Vec < ( u64 , T ) > > {
243274 self . cur_step = now;
244275 let mut result = vec ! [ ] ;
245276 loop {
246277 if let Some ( val) = self . tick_first ( ) {
247278 if self . cur_step < val {
248279 break ;
249280 }
250- result. push ( self . tree . pop_first ( ) . map ( |( _ , e) | e ) . unwrap ( ) ) ;
281+ result. push ( self . tree . pop_first ( ) . map ( |( k , e) | ( k . 1 , e ) ) . unwrap ( ) ) ;
251282 } else {
252283 break ;
253284 }
@@ -264,9 +295,9 @@ impl<T: Timer> TimerRBTree<T> {
264295 /// let mut timer = TimerRBTree::new();
265296 /// timer.add_timer(30);
266297 /// let val = timer.update_deltatime(30).unwrap();
267- /// assert_eq!(val, vec![30 ]);
298+ /// assert_eq!(val, vec![(1, 30) ]);
268299 /// }
269- pub fn update_deltatime ( & mut self , delta : u64 ) -> Option < Vec < T > > {
300+ pub fn update_deltatime ( & mut self , delta : u64 ) -> Option < Vec < ( u64 , T ) > > {
270301 self . update_now ( self . cur_step . wrapping_add ( delta) )
271302 }
272303
@@ -280,32 +311,35 @@ impl<T: Timer> TimerRBTree<T> {
280311 /// let mut timer = TimerRBTree::new();
281312 /// timer.add_timer(30);
282313 /// let mut idx = 0;
283- /// timer.update_deltatime_with_callback(30, &mut |_, v| {
314+ /// let mut timer_id = 0;
315+ /// timer.update_deltatime_with_callback(30, &mut |_, id, v| {
316+ /// timer_id = id;
284317 /// idx = v;
285318 /// None
286319 /// });
320+ /// assert_eq!(timer_id, 1);
287321 /// assert_eq!(idx, 30);
288322 /// }
289323 pub fn update_deltatime_with_callback < F > ( & mut self , delta : u64 , f : & mut F )
290324 where
291- F : FnMut ( & mut Self , T ) -> Option < T > ,
325+ F : FnMut ( & mut Self , u64 , T ) -> Option < ( u64 , T ) > ,
292326 {
293327 self . update_now_with_callback ( self . cur_step . wrapping_add ( delta) , f)
294328 }
295329
296330 pub fn update_now_with_callback < F > ( & mut self , now : u64 , f : & mut F )
297331 where
298- F : FnMut ( & mut Self , T ) -> Option < T > ,
332+ F : FnMut ( & mut Self , u64 , T ) -> Option < ( u64 , T ) > ,
299333 {
300334 if let Some ( result) = self . update_now ( now) {
301335 let mut collect_result = vec ! [ ] ;
302336 for r in result. into_iter ( ) {
303- if let Some ( v) = ( * f) ( self , r) {
337+ if let Some ( v) = ( * f) ( self , r. 0 , r . 1 ) {
304338 collect_result. push ( v) ;
305339 }
306340 }
307- for v in collect_result. drain ( ..) {
308- self . add_timer ( v) ;
341+ for ( timer_id , v ) in collect_result. drain ( ..) {
342+ self . _add_timer ( timer_id , v) ;
309343 }
310344 }
311345 }
0 commit comments