1- /// A buffer for packets indexed by a u64 sequence number.
1+ /// A buffer indexed by a u64 sequence number.
22///
33/// This behaves idential to a BTreeMap<u64, T>, but is optimized for the case where
44/// the indices are mostly contiguous, with occasional gaps.
55///
66/// It will have large memory overhead if the indices are very sparse, so it
7- /// should not be used as a general-purpose map.
7+ /// should not be used as a general-purpose sorted map.
88///
99/// # Internals
1010///
2525/// happen every O(n.next_power_of_two()) operations, so amortized complexity is
2626/// still O(1).
2727#[ derive( Debug , Clone ) ]
28- pub struct PacketBuf < T > {
28+ pub struct SortedIndexBuffer < T > {
2929 /// The underlying data buffer. Size is a power of two, 2 "pages".
3030 data : Vec < Option < T > > ,
3131 /// The minimum valid index (inclusive).
@@ -34,14 +34,14 @@ pub struct PacketBuf<T> {
3434 max : u64 ,
3535}
3636
37- impl < T > Default for PacketBuf < T > {
37+ impl < T > Default for SortedIndexBuffer < T > {
3838 fn default ( ) -> Self {
3939 Self :: new ( )
4040 }
4141}
4242
43- impl < T > PacketBuf < T > {
44- /// Create a new PacketBuffer with the given initial capacity.
43+ impl < T > SortedIndexBuffer < T > {
44+ /// Create a new SortedIndexBuffer with the given initial capacity.
4545 pub fn with_capacity ( capacity : usize ) -> Self {
4646 let data = Vec :: with_capacity ( capacity) ;
4747 Self {
@@ -51,7 +51,7 @@ impl<T> PacketBuf<T> {
5151 }
5252 }
5353
54- /// Create a new, empty PacketBuffer .
54+ /// Create a new, empty SortedIndexBuffer .
5555 pub fn new ( ) -> Self {
5656 Self :: with_capacity ( 0 )
5757 }
@@ -134,13 +134,15 @@ impl<T> PacketBuf<T> {
134134 . iter ( )
135135 . position ( |slot| slot. is_some ( ) )
136136 . map ( |p| p + start)
137- . unwrap_or ( end) as u64 + base;
137+ . unwrap_or ( end) as u64
138+ + base;
138139 let max1 = self . data [ start..end]
139140 . iter ( )
140141 . rev ( )
141142 . position ( |slot| slot. is_some ( ) )
142143 . map ( |p| end - p)
143- . unwrap_or ( start + 1 ) as u64 + base;
144+ . unwrap_or ( start + 1 ) as u64
145+ + base;
144146 self . resize ( min1, max1) ;
145147 self . check_invariants ( ) ;
146148 }
@@ -199,7 +201,7 @@ impl<T> PacketBuf<T> {
199201
200202 /// Convert range bounds into an inclusive start and exclusive end, clipped to the current
201203 /// bounds.
202- ///
204+ ///
203205 /// The resulting range may be empty, which has to be handled by the caller.
204206 #[ inline]
205207 fn clip_bounds < R : std:: ops:: RangeBounds < u64 > > ( & self , range : R ) -> ( u64 , u64 ) {
@@ -374,9 +376,11 @@ impl<T> PacketBuf<T> {
374376 & mut self . data
375377 }
376378
377- /// Check that the invariants of the PacketBuffer hold.
379+ /// Check that the invariants of the SortedIndexBuffer hold.
378380 ///
379381 /// This should be called after each public &mut method.
382+ ///
383+ /// It is a noop in release builds.
380384 fn check_invariants ( & self ) {
381385 if self . is_empty ( ) {
382386 // for the empty buffer, min and max must be zero
@@ -390,6 +394,8 @@ impl<T> PacketBuf<T> {
390394 }
391395
392396 /// Same as `check_invariants`, but also checks that the unused parts of the buffer are empty.
397+ ///
398+ /// This is more expensive, so only used in tests.
393399 #[ cfg( test) ]
394400 fn check_invariants_expensive ( & self ) {
395401 self . check_invariants ( ) ;
@@ -456,7 +462,7 @@ mod tests {
456462 fn test_usage ( ) {
457463 let elements = lag_permute ( 0 ..10000 , 100 ) . collect :: < Vec < _ > > ( ) ;
458464 let mut reference = BTreeMap :: < u64 , u64 > :: new ( ) ;
459- let mut pb = PacketBuf :: < u64 > :: default ( ) ;
465+ let mut pb = SortedIndexBuffer :: < u64 > :: default ( ) ;
460466 let d = 100 ;
461467 let add = elements
462468 . iter ( )
@@ -482,7 +488,7 @@ mod tests {
482488
483489 #[ test]
484490 fn test_range_iterators ( ) {
485- let mut pb = PacketBuf :: default ( ) ;
491+ let mut pb = SortedIndexBuffer :: default ( ) ;
486492 for i in 0 ..100 {
487493 pb. insert ( i, i * 10 ) ;
488494 }
@@ -504,10 +510,9 @@ mod tests {
504510 assert_eq ! ( pairs, ( 10 ..20 ) . map( |i| ( i, i * 10 ) ) . collect:: <Vec <_>>( ) ) ;
505511 }
506512
507-
508513 #[ test]
509514 fn test_retain ( ) {
510- let mut pb = PacketBuf :: default ( ) ;
515+ let mut pb = SortedIndexBuffer :: default ( ) ;
511516 for i in 0 ..100 {
512517 pb. insert ( i, i * 10 ) ;
513518 }
@@ -526,14 +531,14 @@ mod tests {
526531 }
527532 pb. check_invariants_expensive ( ) ;
528533
529- pb. retain ( |_, _| false ) ;
534+ pb. retain ( |_, _| false ) ;
530535 assert ! ( pb. is_empty( ) ) ;
531536 pb. check_invariants_expensive ( ) ;
532537 }
533538
534539 #[ test]
535540 fn test_retain_range ( ) {
536- let mut pb = PacketBuf :: default ( ) ;
541+ let mut pb = SortedIndexBuffer :: default ( ) ;
537542 for i in 0 ..100 {
538543 pb. insert ( i, i * 10 ) ;
539544 }
@@ -591,7 +596,7 @@ mod tests {
591596 proptest ! {
592597 #[ test]
593598 fn test_insert_remove_get( ops in prop:: collection:: vec( op_strategy( ) , 0 ..1000 ) ) {
594- let mut pb = PacketBuf :: default ( ) ;
599+ let mut pb = SortedIndexBuffer :: default ( ) ;
595600 let mut reference = BTreeMap :: new( ) ;
596601
597602 for op in ops {
0 commit comments