Skip to content

Commit

Permalink
bitarray remove pub on struct
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Apr 12, 2024
1 parent c8ba40a commit edbeee8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
23 changes: 19 additions & 4 deletions src/data_structures/src/bit_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const BYTES_IN_BYTES31_MINUS_ONE: usize = BYTES_IN_BYTES31 - 1;

#[derive(Clone, Drop)]
pub struct BitArray {
pub data: Array<bytes31>,
pub current: felt252,
pub read_pos: usize,
pub write_pos: usize,
data: Array<bytes31>,
current: felt252,
read_pos: usize,
write_pos: usize,
}

impl BitArrayDefaultImpl of Default<BitArray> {
Expand All @@ -24,6 +24,9 @@ impl BitArrayDefaultImpl of Default<BitArray> {
}

pub trait BitArrayTrait {
fn new(data: Array<bytes31>, current: felt252, read_pos: usize, write_pos: usize,) -> BitArray;
fn current(self: @BitArray) -> felt252;
fn data(self: BitArray) -> Array<bytes31>;
/// Appends a single bit to the BitArray
/// # Arguments
/// `bit` - either true or false, representing a single bit to be appended
Expand Down Expand Up @@ -121,6 +124,18 @@ pub trait BitArrayTrait {
}

impl BitArrayImpl of BitArrayTrait {
fn new(data: Array<bytes31>, current: felt252, read_pos: usize, write_pos: usize,) -> BitArray {
BitArray { data, current, read_pos, write_pos, }
}

fn current(self: @BitArray) -> felt252 {
*self.current
}

fn data(self: BitArray) -> Array<bytes31> {
self.data
}

fn append_bit(ref self: BitArray, bit: bool) {
let (byte_number, bit_offset) = DivRem::div_rem(
self.write_pos, 8_usize.try_into().unwrap()
Expand Down
11 changes: 3 additions & 8 deletions src/data_structures/src/tests/bit_array_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ fn test_append_bit() {
.try_into()
.unwrap();
let expected: Array<bytes31> = array![val, val,];
assert!(ba.data == expected, "illegal array");
assert(
ba.current == 0xa * one_shift_left_bytes_felt252(30) * shift_bit(4).into(),
ba.current() == 0xa * one_shift_left_bytes_felt252(30) * shift_bit(4).into(),
'expected current 0xa'
);
assert!(ba.data() == expected, "illegal array");
}

#[test]
Expand Down Expand Up @@ -330,10 +330,5 @@ fn test_serde_ser_deser() {
// helpers
fn sample_bit_array() -> BitArray {
let sample: felt252 = BoundedInt::<u128>::max().into() - 1;
BitArray {
data: array![],
current: sample * one_shift_left_bytes_felt252(15),
read_pos: 0,
write_pos: 8 * 16,
}
BitArrayTrait::new(array![], sample * one_shift_left_bytes_felt252(15), 0, 8 * 16)
}

0 comments on commit edbeee8

Please sign in to comment.