Skip to content

Commit

Permalink
Set bit error.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Nov 13, 2020
1 parent 151bb2a commit fd75933
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 66 deletions.
10 changes: 5 additions & 5 deletions rust/arrow/src/array/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ impl<T: ArrowPrimitiveType> BufferBuilderTrait<T> for BufferBuilder<T> {
self.reserve(n);
if T::DATA_TYPE == DataType::Boolean {
if n != 0 && v != T::default_value() {
unsafe {
bit_util::set_bits_raw(
let data = unsafe {
std::slice::from_raw_parts_mut(
self.buffer.raw_data_mut(),
self.len,
self.len + n,
self.buffer.capacity(),
)
}
};
(self.len..self.len + n).for_each(|i| bit_util::set_bit(data, i))
}
self.len += n;
} else {
Expand Down
2 changes: 2 additions & 0 deletions rust/arrow/src/array/equal_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ mod tests {
"#,
)
.unwrap();
println!("{:?}", arrow_array);
println!("{:?}", json_array);
assert!(arrow_array.eq(&json_array));
assert!(json_array.eq(&arrow_array));

Expand Down
61 changes: 0 additions & 61 deletions rust/arrow/src/util/bit_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,6 @@ pub unsafe fn unset_bit_raw(data: *mut u8, i: usize) {
*data.add(i >> 3) ^= BIT_MASK[i & 7];
}

/// Sets bits in the non-inclusive range `start..end` for `data`
///
/// # Safety
///
/// Note this doesn't do any bound checking, for performance reason. The caller is
/// responsible to guarantee that both `start` and `end` are within bounds.
#[inline]
pub unsafe fn set_bits_raw(data: *mut u8, start: usize, end: usize) {
let start_byte = (start >> 3) as isize;
let end_byte = (end >> 3) as isize;

let start_offset = (start & 7) as u8;
let end_offset = (end & 7) as u8;

// All set apart from lowest `start_offset` bits
let start_mask = !((1 << start_offset) - 1);
// All clear apart from lowest `end_offset` bits
let end_mask = (1 << end_offset) - 1;

if start_byte == end_byte {
*data.offset(start_byte) |= start_mask & end_mask;
} else {
*data.offset(start_byte) |= start_mask;
for i in (start_byte + 1)..end_byte {
*data.offset(i) = 0xFF;
}
*data.offset(end_byte) |= end_mask;
}
}

/// Returns the number of 1-bits in `data`
#[inline]
pub fn count_set_bits(data: &[u8]) -> usize {
Expand Down Expand Up @@ -334,37 +304,6 @@ mod tests {
}
}

#[test]
fn test_set_bits_raw() {
const NUM_BYTE: usize = 64;
const NUM_BLOCKS: usize = 12;
const MAX_BLOCK_SIZE: usize = 32;
let mut buf = vec![0; NUM_BYTE];

let mut expected = Vec::with_capacity(NUM_BYTE * 8);
expected.resize(NUM_BYTE * 8, false);

let mut rng = seedable_rng();

for _ in 0..NUM_BLOCKS {
let start = rng.gen_range(0, NUM_BYTE * 8 - MAX_BLOCK_SIZE);
let end = start + rng.gen_range(1, MAX_BLOCK_SIZE);
unsafe {
set_bits_raw(buf.as_mut_ptr(), start, end);
}
for i in start..end {
expected[i] = true;
}
}

let raw_ptr = buf.as_ptr();
for (i, b) in expected.iter().enumerate() {
unsafe {
assert_eq!(*b, get_bit_raw(raw_ptr, i));
}
}
}

#[test]
fn test_get_set_bit_roundtrip() {
const NUM_BYTES: usize = 10;
Expand Down

0 comments on commit fd75933

Please sign in to comment.