@@ -29,6 +29,21 @@ pub struct SkipNode<T> {
29
29
levels : Vec < LevelType < T > > ,
30
30
}
31
31
32
+ /// 跳表, 链表的一种扩展, 相对更复杂, 但是效率更高
33
+ ///
34
+ /// # Examples
35
+ ///
36
+ /// ```
37
+ /// use algorithm::SkipList;
38
+ /// fn main() {
39
+ /// let mut val = SkipList::new();
40
+ /// val.insert(1);
41
+ /// val.insert(10);
42
+ /// val.insert(100);
43
+ /// assert_eq!(val.len(), 3);
44
+ /// }
45
+ /// ```
46
+ #[ derive( Debug ) ]
32
47
pub struct SkipList < T : Default + PartialEq + PartialOrd > {
33
48
length : usize ,
34
49
level : usize ,
@@ -37,8 +52,8 @@ pub struct SkipList<T: Default + PartialEq + PartialOrd> {
37
52
tail : * mut SkipNode < T > ,
38
53
}
39
54
40
- const MAX_LEVEL : usize = 32 ;
41
- const PERCENT : u16 = 25 ;
55
+ const MAX_LEVEL : usize = 16 ;
56
+ const PERCENT : u16 = 50 ;
42
57
43
58
impl < T : Default + PartialEq + PartialOrd > SkipList < T > {
44
59
pub fn new ( ) -> Self {
@@ -52,7 +67,7 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
52
67
sl
53
68
}
54
69
55
- pub fn free_all ( & mut self ) {
70
+ fn free_all ( & mut self ) {
56
71
while !self . header . is_null ( ) {
57
72
unsafe {
58
73
let next = ( * self . header ) . levels [ 0 ] . forward ;
@@ -63,6 +78,20 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
63
78
self . header = ptr:: null_mut ( ) ;
64
79
}
65
80
81
+ /// 清除表内的所有内容
82
+ ///
83
+ /// # Examples
84
+ ///
85
+ /// ```
86
+ /// use algorithm::SkipList;
87
+ /// fn main() {
88
+ /// let mut val = SkipList::new();
89
+ /// val.insert(1);
90
+ /// assert_eq!(val.len(), 1);
91
+ /// val.clear();
92
+ /// assert_eq!(val.len(), 0);
93
+ /// }
94
+ /// ```
66
95
pub fn clear ( & mut self ) {
67
96
self . free_all ( ) ;
68
97
@@ -105,16 +134,25 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
105
134
level
106
135
}
107
136
137
+ /// 插入内容, 将根据score放至合适的排序中
138
+ ///
139
+ /// # Examples
140
+ ///
141
+ /// ```
142
+ /// use algorithm::SkipList;
143
+ /// fn main() {
144
+ /// let mut val = SkipList::new();
145
+ /// val.insert(1);
146
+ /// assert_eq!(val.len(), 1);
147
+ /// }
148
+ /// ```
108
149
pub fn insert ( & mut self , score : T ) -> * mut SkipNode < T > {
109
150
let mut update: [ * mut SkipNode < T > ; MAX_LEVEL ] = [ ptr:: null_mut ( ) ; MAX_LEVEL ] ;
110
151
let mut rank = [ 0 ; MAX_LEVEL ] ;
111
152
let mut x = self . header ;
112
- for i in ( 0 ..self . level as usize ) . rev ( ) {
113
- rank[ i] = if i == ( self . level - 1 ) as usize {
114
- 0
115
- } else {
116
- rank[ i + 1 ]
117
- } ;
153
+ for i in ( 0 ..self . level ) . rev ( ) {
154
+ println ! ( "i = {}, level = {}" , i, self . level) ;
155
+ rank[ i] = if i == self . level - 1 { 0 } else { rank[ i + 1 ] } ;
118
156
119
157
unsafe {
120
158
while ( * x) . levels [ i] . forward != ptr:: null_mut ( )
@@ -128,8 +166,10 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
128
166
}
129
167
130
168
let level = Self :: rand_level ( ) ;
169
+ println ! ( "level == {}" , level) ;
131
170
if level > self . level {
132
- for i in level as usize ..self . level as usize {
171
+ for i in self . level ..level {
172
+ println ! ( "aaaa i = {}, level = {}" , i, self . level) ;
133
173
rank[ i] = 0 ;
134
174
update[ i] = self . header ;
135
175
unsafe {
@@ -142,6 +182,7 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
142
182
x = Self :: make_node ( level, score) ;
143
183
unsafe {
144
184
for i in 0 ..level {
185
+ println ! ( "i ==== {}" , i) ;
145
186
( * x) . levels [ i] . forward = ( * update[ i] ) . levels [ i] . forward ;
146
187
( * update[ i] ) . levels [ i] . forward = x;
147
188
@@ -169,6 +210,19 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
169
210
x
170
211
}
171
212
213
+ /// 更新内容, 查找原值, 并更新新值
214
+ ///
215
+ /// # Examples
216
+ ///
217
+ /// ```
218
+ /// use algorithm::SkipList;
219
+ /// fn main() {
220
+ /// let mut val = SkipList::new();
221
+ /// val.insert(1);
222
+ /// val.update(&1, 2);
223
+ /// assert_eq!(val.len(), 1);
224
+ /// }
225
+ /// ```
172
226
pub fn update ( & mut self , cur_score : & T , new_score : T ) -> * mut SkipNode < T > {
173
227
let mut update: [ * mut SkipNode < T > ; MAX_LEVEL ] = [ ptr:: null_mut ( ) ; MAX_LEVEL ] ;
174
228
let mut rank = [ 0 ; MAX_LEVEL ] ;
@@ -186,11 +240,10 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
186
240
}
187
241
x = ( * x) . levels [ 0 ] . forward ;
188
242
189
- assert ! ( x != ptr :: null_mut ( ) && cur_score == & ( * x) . score) ;
243
+ assert ! ( !x . is_null ( ) && cur_score == & ( * x) . score) ;
190
244
191
- if ( ( * x) . backward != ptr:: null_mut ( ) || ( * ( * x) . backward ) . score < new_score)
192
- && ( ( * x) . levels [ 0 ] . forward != ptr:: null_mut ( )
193
- || ( * ( * x) . levels [ 0 ] . forward ) . score < new_score)
245
+ if ( ( * x) . backward . is_null ( ) || ( * ( * x) . backward ) . score < new_score)
246
+ && ( ( * x) . levels [ 0 ] . forward . is_null ( ) || ( * ( * x) . levels [ 0 ] . forward ) . score < new_score)
194
247
{
195
248
( * x) . score = new_score;
196
249
return x;
@@ -207,10 +260,10 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
207
260
unsafe {
208
261
for i in 0 ..self . level {
209
262
if ( * update[ i] ) . levels [ i] . forward == x {
210
- ( * update[ i] ) . levels [ i] . span += ( * x) . levels [ i] . span - 1 ;
263
+ ( * update[ i] ) . levels [ i] . span += ( * x) . levels [ i] . span . saturating_sub ( 1 ) ;
211
264
( * update[ i] ) . levels [ i] . forward = ( * x) . levels [ i] . forward ;
212
265
} else {
213
- ( * update[ i] ) . levels [ i] . span -= 1 ;
266
+ ( * update[ i] ) . levels [ i] . span = ( * update [ i ] ) . levels [ i ] . span . saturating_sub ( 1 ) ;
214
267
}
215
268
}
216
269
@@ -227,6 +280,23 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
227
280
}
228
281
}
229
282
283
+ /// 获取排序值, 得到该值在序列中的排序
284
+ ///
285
+ /// # Examples
286
+ ///
287
+ /// ```
288
+ /// use algorithm::SkipList;
289
+ /// fn main() {
290
+ /// let mut val = SkipList::new();
291
+ /// val.insert(4);
292
+ /// val.insert(2);
293
+ /// val.insert(1);
294
+ /// assert_eq!(val.get_rank(&1), 1);
295
+ /// assert_eq!(val.get_rank(&4), 3);
296
+ /// val.insert(3);
297
+ /// assert_eq!(val.get_rank(&4), 4);
298
+ /// }
299
+ /// ```
230
300
pub fn get_rank ( & mut self , score : & T ) -> usize {
231
301
let mut x = self . header ;
232
302
let mut rank = 0 ;
@@ -246,7 +316,7 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
246
316
0
247
317
}
248
318
249
- fn find_by_rank ( & mut self , rank : usize ) -> * mut SkipNode < T > {
319
+ pub fn find_by_rank ( & mut self , rank : usize ) -> * mut SkipNode < T > {
250
320
let mut x = self . header ;
251
321
let mut traversed = 0 ;
252
322
for i in ( 0 ..self . level ) . rev ( ) {
@@ -264,7 +334,25 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
264
334
ptr:: null_mut ( )
265
335
}
266
336
267
- pub fn erase ( & mut self , score : & T ) -> bool {
337
+ /// 移除指定值
338
+ ///
339
+ /// # Examples
340
+ ///
341
+ /// ```
342
+ /// use algorithm::SkipList;
343
+ /// fn main() {
344
+ /// let mut val = SkipList::new();
345
+ /// val.insert(4);
346
+ /// val.insert(2);
347
+ /// val.insert(1);
348
+ /// assert_eq!(val.len(), 3);
349
+ /// val.remove(&3);
350
+ /// assert_eq!(val.len(), 3);
351
+ /// val.remove(&4);
352
+ /// assert_eq!(val.len(), 2);
353
+ /// }
354
+ /// ```
355
+ pub fn remove ( & mut self , score : & T ) -> bool {
268
356
let mut update: [ * mut SkipNode < T > ; MAX_LEVEL ] = [ ptr:: null_mut ( ) ; MAX_LEVEL ] ;
269
357
let mut x = self . header ;
270
358
unsafe {
@@ -287,7 +375,93 @@ impl<T: Default + PartialEq + PartialOrd> SkipList<T> {
287
375
return false ;
288
376
}
289
377
378
+ /// 获取长度
290
379
pub fn len ( & self ) -> usize {
291
380
self . length
292
381
}
382
+
383
+ /// 遍历值
384
+ ///
385
+ /// # Examples
386
+ ///
387
+ /// ```
388
+ /// use algorithm::SkipList;
389
+ /// fn main() {
390
+ /// let mut val = SkipList::new();
391
+ /// val.insert(4);
392
+ /// val.insert(2);
393
+ /// val.insert(1);
394
+ /// let mut iter = val.iter();
395
+ /// assert_eq!(iter.next(), Some(&1));
396
+ /// assert_eq!(iter.next(), Some(&2));
397
+ /// assert_eq!(iter.next(), Some(&4));
398
+ /// assert_eq!(iter.next(), None);
399
+ /// }
400
+ /// ```
401
+ pub fn iter ( & self ) -> Iter < ' _ , T > {
402
+ let first = unsafe { ( * self . header ) . levels [ 0 ] . forward } ;
403
+ Iter :: new ( self . length , first, self . tail )
404
+ }
405
+ }
406
+
407
+ pub struct Iter < ' a , T : ' a + Default + PartialEq + PartialOrd > {
408
+ len : usize ,
409
+ header : * mut SkipNode < T > ,
410
+ tail : * mut SkipNode < T > ,
411
+ data : PhantomData < & ' a ( ) > ,
412
+ }
413
+
414
+ impl < ' a , T : Default + PartialEq + PartialOrd > Iter < ' a , T > {
415
+ pub fn new ( len : usize , header : * mut SkipNode < T > , tail : * mut SkipNode < T > ) -> Self {
416
+ Self {
417
+ len,
418
+ header,
419
+ tail,
420
+ data : PhantomData ,
421
+ }
422
+ }
423
+ }
424
+
425
+ impl < ' a , T : Default + PartialEq + PartialOrd > Iterator for Iter < ' a , T > {
426
+ type Item = & ' a T ;
427
+
428
+ fn next ( & mut self ) -> Option < Self :: Item > {
429
+ if self . len == 0 {
430
+ return None ;
431
+ }
432
+
433
+ self . len -= 1 ;
434
+
435
+ unsafe {
436
+ let node = self . header ;
437
+ self . header = ( * self . header ) . levels [ 0 ] . forward ;
438
+ return Some ( & ( * node) . score ) ;
439
+ }
440
+ }
441
+
442
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
443
+ ( self . len , Some ( self . len ) )
444
+ }
445
+ }
446
+
447
+ impl < ' a , T : Default + PartialEq + PartialOrd > DoubleEndedIterator for Iter < ' a , T > {
448
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
449
+ if self . len == 0 {
450
+ return None ;
451
+ }
452
+
453
+ self . len -= 1 ;
454
+
455
+ unsafe {
456
+ let node = self . tail ;
457
+ self . tail = ( * self . tail ) . backward ;
458
+ return Some ( & ( * node) . score ) ;
459
+ }
460
+ }
461
+ }
462
+
463
+ impl < T : Default + PartialEq + PartialOrd > Drop for SkipList < T > {
464
+ fn drop ( & mut self ) {
465
+ self . clear ( ) ;
466
+ }
293
467
}
0 commit comments