Skip to content

Commit b74d9ef

Browse files
committed
Implemented split method
1 parent cb34041 commit b74d9ef

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,22 @@ pub trait BitVector:
418418
high
419419
}
420420

421+
/// Split the bit vector at the specified index and return a tuple of bit vectors
422+
/// containing the higher and lower bits.
423+
///
424+
/// ```
425+
/// use bva::{BitVector, Bv};
426+
///
427+
/// let a = Bv::from(0b101010101u64);
428+
/// let (hi, lo) = a.split(4);
429+
/// assert_eq!(lo, Bv::from(0b0101u64));
430+
/// assert_eq!(hi, Bv::from(0b10101u64));
431+
/// ```
432+
fn split(mut self, index: usize) -> (Self, Self) {
433+
let high = self.split_off(index);
434+
(high, self)
435+
}
436+
421437
/// Push a bit at the end of the bit vector as the most significant bit.
422438
/// Will panic if there is not enough capacity and it is a fixed variant.
423439
///

src/tests/bitvector.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,38 @@ fn split_off_bv() {
520520
split_off_inner::<Bv>(256);
521521
}
522522

523+
fn split_inner<B: BitVector>(max_capacity: usize) {
524+
for capacity in 0..max_capacity {
525+
let bv: B = random_bv(capacity);
526+
for split in 0..capacity {
527+
let (hi, lo) = bv.clone().split(split);
528+
assert_eq!(hi.len() + lo.len(), bv.len());
529+
for i in 0..split {
530+
assert_eq!(lo.get(i), bv.get(i));
531+
}
532+
for i in split..bv.len() {
533+
assert_eq!(hi.get(i - split), bv.get(i));
534+
}
535+
}
536+
}
537+
}
538+
539+
#[test]
540+
fn split_bvf() {
541+
bvf_inner_unroll_cap!(split_inner, {u8, u16, u32, u64}, {1, 2, 3, 4});
542+
bvf_inner_unroll_cap!(split_inner, {u128}, {1, 2});
543+
}
544+
545+
#[test]
546+
fn split_bvd() {
547+
split_inner::<Bvd>(256);
548+
}
549+
550+
#[test]
551+
fn split_bv() {
552+
split_inner::<Bv>(256);
553+
}
554+
523555
fn push_pop_inner<B: BitVector>(max_capacity: usize) {
524556
let mut rng = thread_rng();
525557
for capacity in 0..max_capacity {

0 commit comments

Comments
 (0)