Skip to content

Commit

Permalink
refactor: use Self::
Browse files Browse the repository at this point in the history
  • Loading branch information
detectivekim committed Jun 21, 2024
1 parent f09b952 commit d2f8cdd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod libraries {
pub mod order_id;
pub mod packed_u256;
pub mod tick;
pub mod tick_bitmap;
pub mod significant_bit;
}

Expand Down
92 changes: 44 additions & 48 deletions src/libraries/tick_bitmap.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,95 +13,91 @@ pub struct TickBitmap {
pub low: Felt252Dict<u128>,
}

fn _split(tick: Tick) -> (u32, u32) {
let mut value: u32 = ~(tick.value + 0x800000).try_into().unwrap();
value = ~value + 0x800000;
let b0b1 = (value & 0xffff00) / 256;
let b2 = value & 0xff;
(b0b1, b2)
}

fn _to_tick(raw: felt252) -> Tick {
let value: u32 = (~(raw - 0x800000).try_into().unwrap()) & 0xffffff;
let value: i32 = value.try_into().unwrap();
Tick { value }
}

fn _get(ref bitmap: TickBitmap, key: felt252) -> u256 {
bitmap.hi.get(key).into() * TWO_POW_128 + bitmap.low.get(key).into()
}

fn _set(ref bitmap: TickBitmap, key: felt252, value: u256) {
bitmap.hi.insert(key, (value / TWO_POW_128).try_into().unwrap());
bitmap.low.insert(key, (value % TWO_POW_128).try_into().unwrap());
}

fn _is_empty(ref bitmap: TickBitmap) -> bool {
_get(ref bitmap, B0_BITMAP_KEY) == 0
}

#[generate_trait]
impl TickBitmapImpl of TickBitmapTrait {
fn has(ref bitmap: TickBitmap, tick: Tick) -> bool {
let (b0b1, b2) = _split(tick);
let (b0b1, b2) = Self::_split(tick);
let mask: u256 = fast_power(2, (b2)).into();
let value = _get(ref bitmap, b0b1.into());
let value = Self::_get(ref bitmap, b0b1.into());
value & mask == mask
}

fn is_empty(ref bitmap: TickBitmap) -> bool {
_get(ref bitmap, B0_BITMAP_KEY) == 0
Self::_get(ref bitmap, B0_BITMAP_KEY) == 0
}

fn highest(ref bitmap: TickBitmap) -> Tick {
assert(_is_empty(ref bitmap), 'EmptyError');
assert(Self::is_empty(ref bitmap), 'EmptyError');

let b0: u32 = SignificantBitImpl::least_significant_bit(_get(ref bitmap, B0_BITMAP_KEY))
let b0: u32 = SignificantBitImpl::least_significant_bit(Self::_get(ref bitmap, B0_BITMAP_KEY))
.into();
let b0b1: u32 = (b0 * 256)
| SignificantBitImpl::least_significant_bit(_get(ref bitmap, (~b0).into())).into();
let b2: u32 = SignificantBitImpl::least_significant_bit(_get(ref bitmap, b0b1.into()))
| SignificantBitImpl::least_significant_bit(Self::_get(ref bitmap, (~b0).into())).into();
let b2: u32 = SignificantBitImpl::least_significant_bit(Self::_get(ref bitmap, b0b1.into()))
.into();
_to_tick(((b0b1 * 256) + b2).into())
Self::_to_tick(((b0b1 * 256) + b2).into())
}

fn set(ref bitmap: TickBitmap, tick: Tick) {
let (b0b1, b2) = _split(tick);
let (b0b1, b2) = Self::_split(tick);
let mut mask: u256 = fast_power(2, (b2)).into();
let mut b2Bitmap = _get(ref bitmap, b0b1.into());
let mut b2Bitmap = Self::_get(ref bitmap, b0b1.into());
assert(b2Bitmap & mask == 0, 'AlreadyExistsError');

_set(ref bitmap, b0b1.into(), b2Bitmap | mask);
Self::_set(ref bitmap, b0b1.into(), b2Bitmap | mask);
if b2Bitmap == 0 {
mask = fast_power(2, (b0b1 & 0xff)).into();
let b1BitmapKey = ~(b0b1 / 256);
let mut b1Bitmap = _get(ref bitmap, b1BitmapKey.into());
_set(ref bitmap, b1BitmapKey.into(), b1Bitmap | mask);
let mut b1Bitmap = Self::_get(ref bitmap, b1BitmapKey.into());
Self::_set(ref bitmap, b1BitmapKey.into(), b1Bitmap | mask);
if b1Bitmap == 0 {
_set(
Self::_set(
ref bitmap,
B0_BITMAP_KEY,
_get(ref bitmap, B0_BITMAP_KEY) | fast_power(2, (~b1BitmapKey)).into()
Self::_get(ref bitmap, B0_BITMAP_KEY) | fast_power(2, (~b1BitmapKey)).into()
);
}
}
}

fn clear(ref bitmap: TickBitmap, tick: Tick) {
let (b0b1, b2) = _split(tick);
let (b0b1, b2) = Self::_split(tick);
let mut mask: u256 = fast_power(2, (b2)).into();
let mut b2Bitmap = _get(ref bitmap, b0b1.into());
_set(ref bitmap, b0b1.into(), b2Bitmap & (~mask));
let mut b2Bitmap = Self::_get(ref bitmap, b0b1.into());
Self::_set(ref bitmap, b0b1.into(), b2Bitmap & (~mask));
if b2Bitmap == mask {
mask = fast_power(2, (b0b1 & 0xff)).into();
let b1BitmapKey = ~(b0b1 / 256);
let mut b1Bitmap = _get(ref bitmap, b1BitmapKey.into());
_set(ref bitmap, b1BitmapKey.into(), b1Bitmap & (~mask));
let mut b1Bitmap = Self::_get(ref bitmap, b1BitmapKey.into());
Self::_set(ref bitmap, b1BitmapKey.into(), b1Bitmap & (~mask));
if mask == b1Bitmap {
mask = fast_power(2, (~b1BitmapKey)).into();
_set(ref bitmap, B0_BITMAP_KEY, _get(ref bitmap, B0_BITMAP_KEY) & (~mask));
Self::_set(ref bitmap, B0_BITMAP_KEY, Self::_get(ref bitmap, B0_BITMAP_KEY) & (~mask));
}
}
}

fn _split(tick: Tick) -> (u32, u32) {
let mut value: u32 = ~(tick.value + 0x800000).try_into().unwrap();
value = ~value + 0x800000;
let b0b1 = (value & 0xffff00) / 256;
let b2 = value & 0xff;
(b0b1, b2)
}

fn _to_tick(raw: felt252) -> Tick {
let value: u32 = (~(raw - 0x800000).try_into().unwrap()) & 0xffffff;
let value: i32 = value.try_into().unwrap();
Tick { value }
}

fn _get(ref bitmap: TickBitmap, key: felt252) -> u256 {
bitmap.hi.get(key).into() * TWO_POW_128 + bitmap.low.get(key).into()
}

fn _set(ref bitmap: TickBitmap, key: felt252, value: u256) {
bitmap.hi.insert(key, (value / TWO_POW_128).try_into().unwrap());
bitmap.low.insert(key, (value % TWO_POW_128).try_into().unwrap());
}
}

0 comments on commit d2f8cdd

Please sign in to comment.