]> git.lizzy.rs Git - rust.git/commitdiff
Added population count assertion in reserve. Cleaned up wrap_index.
authorColin Sherratt <colin.sherratt@gmail.com>
Wed, 12 Nov 2014 01:22:07 +0000 (20:22 -0500)
committerColin Sherratt <colin.sherratt@gmail.com>
Fri, 14 Nov 2014 08:41:08 +0000 (03:41 -0500)
src/libcollections/ring_buf.rs

index 1024a2828b3ddcc1ba9da29d3682ac6b4fc22dd6..359dffa08532138adebe75e185b207f40495b49c 100644 (file)
@@ -98,6 +98,10 @@ unsafe fn buffer_write(&mut self, off: uint, t: T) {
     /// Returns true iff the buffer is at capacity
     #[inline]
     fn is_full(&self) -> bool { self.cap - self.len() == 1 }
+
+    /// Returns the index in the underlying buffer for a given logical element index.
+    #[inline]
+    fn wrap_index(&self, idx: uint) -> uint { wrap_index(idx, self.cap) }
 }
 
 impl<T> RingBuf<T> {
@@ -149,7 +153,7 @@ pub fn with_capacity(n: uint) -> RingBuf<T> {
     #[unstable = "matches collection reform specification, waiting for dust to settle"]
     pub fn get(&self, i: uint) -> Option<&T> {
         if i < self.len() {
-            let idx = wrap_index(self.tail + i, self.cap);
+            let idx = self.wrap_index(self.tail + i);
             unsafe { Some(&*self.ptr.offset(idx as int)) }
         } else {
             None
@@ -179,7 +183,7 @@ pub fn get(&self, i: uint) -> Option<&T> {
     #[unstable = "matches collection reform specification, waiting for dust to settle"]
     pub fn get_mut(&mut self, i: uint) -> Option<&mut T> {
         if i < self.len() {
-            let idx = wrap_index(self.tail + i, self.cap);
+            let idx = self.wrap_index(self.tail + i);
             unsafe { Some(&mut *self.ptr.offset(idx as int)) }
         } else {
             None
@@ -208,8 +212,8 @@ pub fn get_mut(&mut self, i: uint) -> Option<&mut T> {
     pub fn swap(&mut self, i: uint, j: uint) {
         assert!(i < self.len());
         assert!(j < self.len());
-        let ri = wrap_index(self.tail + i, self.cap);
-        let rj = wrap_index(self.tail + j, self.cap);
+        let ri = self.wrap_index(self.tail + i);
+        let rj = self.wrap_index(self.tail + j);
         unsafe {
             ptr::swap(self.ptr.offset(ri as int), self.ptr.offset(rj as int))
         }
@@ -334,6 +338,7 @@ pub fn reserve(&mut self, additional: uint) {
             }
             debug_assert!(self.head < self.cap);
             debug_assert!(self.tail < self.cap);
+            debug_assert!(self.cap.count_ones() == 1);
         }
     }
 
@@ -549,7 +554,7 @@ pub fn pop_front(&mut self) -> Option<T> {
             None
         } else {
             let tail = self.tail;
-            self.tail = wrap_index(self.tail + 1, self.cap);
+            self.tail = self.wrap_index(self.tail + 1);
             unsafe { Some(self.buffer_read(tail)) }
         }
     }
@@ -573,7 +578,7 @@ pub fn push_front(&mut self, t: T) {
             debug_assert!(!self.is_full());
         }
 
-        self.tail = wrap_index(self.tail - 1, self.cap);
+        self.tail = self.wrap_index(self.tail - 1);
         let tail = self.tail;
         unsafe { self.buffer_write(tail, t); }
     }
@@ -604,7 +609,7 @@ pub fn push_back(&mut self, t: T) {
         }
 
         let head = self.head;
-        self.head = wrap_index(self.head + 1, self.cap);
+        self.head = self.wrap_index(self.head + 1);
         unsafe { self.buffer_write(head, t) }
     }
 
@@ -633,7 +638,7 @@ pub fn pop_back(&mut self) -> Option<T> {
         if self.is_empty() {
             None
         } else {
-            self.head = wrap_index(self.head - 1, self.cap);
+            self.head = self.wrap_index(self.head - 1);
             let head = self.head;
             unsafe { Some(self.buffer_read(head)) }
         }
@@ -644,9 +649,7 @@ pub fn pop_back(&mut self) -> Option<T> {
 #[inline]
 fn wrap_index(index: uint, size: uint) -> uint {
     // size is always a power of 2
-    let idx = index & (size - 1);
-    debug_assert!(idx < size);
-    idx
+    index & (size - 1)
 }
 
 /// Calculate the number of elements left to be read in the buffer