Skip to content

Commit 67f43a1

Browse files
committed
Changed split_at and split_at_mut methods in BitSlice and BitSliceMut to take self as parameter and to return BitSlice<'a, S> and BitSliceMut<'a, S> respectively where 'a is the lifetime of the data inside BitSlice and BitSliceMut respectively, added reborrow() method on BitSlice and BitSliceMut to be able to read the original slice before splitting and added integration tests for a real usecase where this feature was needed.
1 parent 4ecc8ff commit 67f43a1

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

src/bit_slice.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a, S: BitStorage + 'a> BitSlice<'a, S> {
3838
self.capacity
3939
}
4040

41-
pub fn split_at(&self, index: usize) -> (BitSlice<S>, BitSlice<S>) {
41+
pub fn split_at(self, index: usize) -> (BitSlice<'a, S>, BitSlice<'a, S>) {
4242
self.panic_index_not_on_storage_bound(index);
4343
let data_index = S::compute_data_index(index);
4444
let (capacity_left, capacity_right) = self.compute_capacities(index);
@@ -51,6 +51,14 @@ impl<'a, S: BitStorage + 'a> BitSlice<'a, S> {
5151
}
5252
}
5353

54+
pub fn reborrow<'b>(&'b self) -> BitSlice<'b, S> {
55+
BitSlice {
56+
pointer: self.pointer,
57+
capacity: self.capacity,
58+
phantom: self.phantom
59+
}
60+
}
61+
5462
pub fn iter(&self) -> Iter<S> {
5563
Iter {
5664
pointer: self.pointer,
@@ -342,6 +350,38 @@ mod tests {
342350
slice.split_at(4);
343351
}
344352

353+
#[test]
354+
fn test_reborrow() {
355+
let mut vec_8_32: BitVector<u8> = BitVector::with_capacity(32, false);
356+
357+
vec_8_32.set(1, true);
358+
vec_8_32.set(3, true);
359+
vec_8_32.set(5, true);
360+
vec_8_32.set(7, true);
361+
vec_8_32.set(11, true);
362+
vec_8_32.set(13, true);
363+
364+
let (_, slice) = vec_8_32.split_at(0);
365+
let reborrow = slice.reborrow();
366+
367+
assert_eq!(reborrow[0], false);
368+
assert_eq!(reborrow[1], true);
369+
assert_eq!(reborrow[2], false);
370+
assert_eq!(reborrow[3], true);
371+
assert_eq!(reborrow[4], false);
372+
assert_eq!(reborrow[5], true);
373+
assert_eq!(reborrow[6], false);
374+
assert_eq!(reborrow[7], true);
375+
assert_eq!(reborrow[8], false);
376+
assert_eq!(reborrow[9], false);
377+
assert_eq!(reborrow[10], false);
378+
assert_eq!(reborrow[11], true);
379+
assert_eq!(reborrow[12], false);
380+
assert_eq!(reborrow[13], true);
381+
assert_eq!(reborrow[14], false);
382+
assert_eq!(reborrow[15], false);
383+
}
384+
345385
#[test]
346386
fn test_iter() {
347387
let mut vec_8_4 = BitVector::<u8>::with_capacity(4, false);

src/bit_slice_mut.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a, S: BitStorage + 'a> BitSliceMut<'a, S> {
4848
self.capacity
4949
}
5050

51-
pub fn split_at(&self, index: usize) -> (BitSlice<S>, BitSlice<S>) {
51+
pub fn split_at(self, index: usize) -> (BitSlice<'a, S>, BitSlice<'a, S>) {
5252
self.panic_index_not_on_storage_bound(index);
5353
let data_index = S::compute_data_index(index);
5454
let (capacity_left, capacity_right) = self.compute_capacities(index);
@@ -61,7 +61,7 @@ impl<'a, S: BitStorage + 'a> BitSliceMut<'a, S> {
6161
}
6262
}
6363

64-
pub fn split_at_mut(&mut self, index: usize) -> (BitSliceMut<S>, BitSliceMut<S>) {
64+
pub fn split_at_mut(self, index: usize) -> (BitSliceMut<'a, S>, BitSliceMut<'a, S>) {
6565
self.panic_index_not_on_storage_bound(index);
6666
let data_index = S::compute_data_index(index);
6767
let (capacity_left, capacity_right) = self.compute_capacities(index);
@@ -74,6 +74,14 @@ impl<'a, S: BitStorage + 'a> BitSliceMut<'a, S> {
7474
}
7575
}
7676

77+
pub fn reborrow<'b>(&'b self) -> BitSliceMut<'b, S> {
78+
BitSliceMut {
79+
pointer: self.pointer,
80+
capacity: self.capacity,
81+
phantom: self.phantom
82+
}
83+
}
84+
7785
pub fn iter(&self) -> Iter<S> {
7886
Iter {
7987
pointer: self.pointer,
@@ -501,6 +509,7 @@ mod tests {
501509
slice.set(15, true);
502510

503511
{
512+
let slice = slice.reborrow();
504513
let (mut left, mut right) = slice.split_at_mut(8);
505514

506515
assert_eq!(left[0], true);
@@ -562,10 +571,42 @@ mod tests {
562571
#[should_panic]
563572
fn test_split_at_mut_not_on_storage_bound() {
564573
let mut vec_8_32: BitVector<u8> = BitVector::with_capacity(32, false);
565-
let mut slice = create_bitslice_mut_u8_16_from_bitvector_u8_32(&mut vec_8_32);
574+
let slice = create_bitslice_mut_u8_16_from_bitvector_u8_32(&mut vec_8_32);
566575
slice.split_at_mut(4);
567576
}
568577

578+
#[test]
579+
fn test_reborrow() {
580+
let mut vec_8_32: BitVector<u8> = BitVector::with_capacity(32, false);
581+
582+
vec_8_32.set(1, true);
583+
vec_8_32.set(3, true);
584+
vec_8_32.set(5, true);
585+
vec_8_32.set(7, true);
586+
vec_8_32.set(11, true);
587+
vec_8_32.set(13, true);
588+
589+
let (_, slice) = vec_8_32.split_at(0);
590+
let reborrow = slice.reborrow();
591+
592+
assert_eq!(reborrow[0], false);
593+
assert_eq!(reborrow[1], true);
594+
assert_eq!(reborrow[2], false);
595+
assert_eq!(reborrow[3], true);
596+
assert_eq!(reborrow[4], false);
597+
assert_eq!(reborrow[5], true);
598+
assert_eq!(reborrow[6], false);
599+
assert_eq!(reborrow[7], true);
600+
assert_eq!(reborrow[8], false);
601+
assert_eq!(reborrow[9], false);
602+
assert_eq!(reborrow[10], false);
603+
assert_eq!(reborrow[11], true);
604+
assert_eq!(reborrow[12], false);
605+
assert_eq!(reborrow[13], true);
606+
assert_eq!(reborrow[14], false);
607+
assert_eq!(reborrow[15], false);
608+
}
609+
569610
#[test]
570611
fn test_iter() {
571612
let mut vec_8_4 = BitVector::<u8>::with_capacity(4, false);

tests/lib.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate bit_vector;
22
extern crate crossbeam;
33

4-
use bit_vector::BitVector;
4+
use bit_vector::{BitVector,BitSlice,BitSliceMut,BitStorage};
55

66
#[test]
77
fn test_parallel_immutable() {
@@ -110,7 +110,7 @@ fn test_parallel_mutable() {
110110
let mut vec: BitVector<u8> = BitVector::with_capacity(32, false);
111111

112112
{
113-
let (mut left, mut right) = vec.split_at_mut(16);
113+
let (left, right) = vec.split_at_mut(16);
114114

115115
let (mut first, mut second) = left.split_at_mut(8);
116116
let (mut third, mut fourth) = right.split_at_mut(8);
@@ -207,4 +207,54 @@ fn test_parallel_mutable() {
207207
assert_eq!(vec[29], false);
208208
assert_eq!(vec[30], false);
209209
assert_eq!(vec[31], true);
210+
}
211+
212+
#[test]
213+
fn test_reborrow_immutable() {
214+
let mut vector: BitVector<u32> = BitVector::with_capacity(1000, false);
215+
let indices = vec![128, 224, 320, 416, 512, 608, 704, 800, 928];
216+
217+
let slices = split_into_bit_slices(&mut vector, &indices);
218+
assert_eq!(slices.len(), 10);
219+
}
220+
221+
fn split_into_bit_slices<'a, S: BitStorage>(bit_vector: &'a mut BitVector<S>, indices: &[usize]) -> Vec<BitSlice<'a, S>> {
222+
let mut bit_slices = vec![];
223+
224+
bit_slices.push(bit_vector.split_at(0).1);
225+
let mut split_indices = 0;
226+
for index in indices {
227+
let last_slice = bit_slices.pop().unwrap();
228+
let (new_slice, remainder) = last_slice.split_at(index - split_indices);
229+
split_indices = *index;
230+
bit_slices.push(new_slice);
231+
bit_slices.push(remainder);
232+
}
233+
234+
bit_slices
235+
}
236+
237+
#[test]
238+
fn test_reborrow_mutable() {
239+
let mut vector: BitVector<u32> = BitVector::with_capacity(1000, false);
240+
let indices = vec![128, 224, 320, 416, 512, 608, 704, 800, 928];
241+
242+
let slices = split_into_bit_slices_mut(&mut vector, &indices);
243+
assert_eq!(slices.len(), 10);
244+
}
245+
246+
fn split_into_bit_slices_mut<'a, S: BitStorage>(bit_vector: &'a mut BitVector<S>, indices: &[usize]) -> Vec<BitSliceMut<'a, S>> {
247+
let mut bit_slices = vec![];
248+
249+
bit_slices.push(bit_vector.split_at_mut(0).1);
250+
let mut split_indices = 0;
251+
for index in indices {
252+
let last_slice = bit_slices.pop().unwrap();
253+
let (new_slice, remainder) = last_slice.split_at_mut(index - split_indices);
254+
split_indices = *index;
255+
bit_slices.push(new_slice);
256+
bit_slices.push(remainder);
257+
}
258+
259+
bit_slices
210260
}

0 commit comments

Comments
 (0)