bitmap4rust/
lib.rs

1// Create a bitmap which can represent "bitmap_size" number of blocks 
2pub fn bitmap_create(bitmap_size: &mut u64) -> Vec<u8> {
3    if *bitmap_size % 8 != 0 {
4        *bitmap_size = (*bitmap_size + *bitmap_size % 8) / 8;
5    }
6    else {
7        *bitmap_size = *bitmap_size / 8;
8    }
9    let mut _bitmap: Vec<u8> = Vec::with_capacity(*bitmap_size as usize);
10    for _i in 0..(*bitmap_size) {
11        _bitmap.push(0);
12    }
13    return _bitmap;
14}
15
16// Clears the bit corresponding to the block number given by "bitno"
17pub fn clear_bit(bitmap: &mut Vec<u8>, bitno: u64) -> i32{
18    if bitno > ((bitmap.len() as u64) * 8) {
19        return -1;
20    }
21    let index: u64 = bitno / 8;
22    let bit_index: u32 = (bitno % 8) as u32;
23    let val: u8 = u8::pow(2, bit_index);
24    (* bitmap)[index as usize] = (* bitmap)[index as usize] & !(val);
25    return 0;
26}
27
28// Sets the bit corresponding to the block number given by "bitno"
29pub fn set_bit(bitmap: &mut Vec<u8>, bitno: u64) -> i32{
30    if bitno > ((bitmap.len() as u64) * 8) {
31        return -1;
32    }
33    let index: u64 = bitno / 8;
34    let bit_index: u32 = (bitno % 8) as u32;
35    let val: u8 = u8::pow(2, bit_index);
36    (* bitmap)[index as usize] = (* bitmap)[index as usize] | (val);
37    return 0;
38}
39
40// Fetches the block number of the first occupied block
41pub fn get_first_set_bit(bitmap: &mut Vec<u8>) -> i64{
42    let mut _val: u8 = 0;
43    for index in 0..bitmap.len() {
44        for bit_index in 0..8 {
45            _val = u8::pow(2, bit_index);
46            if (bitmap[index] & _val) > 0 {
47                return ((index as u64) * 8 + (bit_index as u64)) as i64;
48            }
49        }
50    }
51    return -1;
52}
53
54// Fetches the block number of the first free block
55pub fn get_first_unset_bit(bitmap: &mut Vec<u8>) -> i64{
56    let mut _val: u8 = 0;
57    for index in 0..bitmap.len() {
58        for bit_index in 0..8 {
59            _val = u8::pow(2, bit_index);
60            if (bitmap[index] & _val) == 0{
61                return ((index as u64) * 8 + (bit_index as u64)) as i64;
62            }
63        }
64    }
65    return -1;
66}
67
68// Checks the bit status of the bit corresponding to the block number given by "bitno"
69pub fn check_bit(bitmap: &mut Vec<u8>, bitno: u64) -> i32 {
70    if bitno > ((bitmap.len() as u64) * 8) {
71        return -1;
72    }
73    let index: u64 = bitno / 8;
74    let bit_index: u32 = (bitno % 8) as u32;
75    return (((bitmap[index as usize] >> bit_index) as u32) & 1) as i32;
76}