bit_map full

This commit is contained in:
Moysan Gabriel 2023-04-26 11:45:16 +02:00
parent 88d8d9cd58
commit 364723984e

View File

@ -71,7 +71,7 @@ impl BitMap {
let position_modulo = which%BITS_IN_WORD; let position_modulo = which%BITS_IN_WORD;
let left_shift = BITS_IN_WORD-position_modulo-1; let left_shift = BITS_IN_WORD-position_modulo-1;
self.map[which / BITS_IN_WORD] |= (1u32 << left_shift); self.map[which / BITS_IN_WORD] |= 1u32 << left_shift;
} }
/// return true if the "nth" bit is set. /// return true if the "nth" bit is set.
@ -107,9 +107,11 @@ impl BitMap {
pub fn clear(&mut self, which: usize) { pub fn clear(&mut self, which: usize) {
assert!(which < self.num_bits, "index out of range"); assert!(which < self.num_bits, "index out of range");
let word_idx = which / BITS_IN_WORD; let position_modulo = which%BITS_IN_WORD;
let bit_idx = which % BITS_IN_WORD; let left_shift = BITS_IN_WORD-position_modulo-1;
self.map[word_idx] &= !(1 << bit_idx); let mask : u32 = 0xFFFFFFFF^(1u32 << left_shift);
self.map[which / BITS_IN_WORD] &= mask;
} }
/// Return the number of clear bits in the bitmap. /// Return the number of clear bits in the bitmap.
@ -137,32 +139,6 @@ impl BitMap {
println!(); println!();
} }
/*
///Initialize the contents of a bitmap from a Nachos file.
///
/// ### Parameters
/// - **file** is the place to read the bitmap from
pub fn fetch_from(&mut self, file_path: &str) -> std::io::Result<&Vec<u32>> {
// Ouvre le fichier en mode lecture seule
let mut file = File::open(file_path)?;
// Lit les octets du fichier dans un buffer
let mut buffer = vec![0; self.num_words * std::mem::size_of::<u32>()];
file.read_exact(&mut buffer)?;
// Convertit les octets en vecteur de u32
self.map = vec![0; self.num_words];
for i in 0..self.num_words {
let j = i * std::mem::size_of::<u32>();
let bytes = [
buffer[j], buffer[j+1], buffer[j+2], buffer[j+3]
];
self.map[i] = u32::from_le_bytes(bytes);
}
Ok(&self.map)
}
*/
///Initialize the contents of a bitmap from a Nachos file. ///Initialize the contents of a bitmap from a Nachos file.
/// ///
@ -208,18 +184,32 @@ impl BitMap {
/// ### Paramenters /// ### Paramenters
/// - **file** is the place to write the bitmap to /// - **file** is the place to write the bitmap to
pub fn write_back(&mut self, file_path: &str) -> std::io::Result<()> { pub fn write_back(&mut self, file_path: &str) -> std::io::Result<()> {
// Encapsule le vecteur dans un std::io::Cursor pour l'utiliser comme un std::io::Write
let mut cursor = Cursor::new(Vec::<u8>::new()); let mut buf : Vec<u8> = Vec::new();
for u32_val in self.map.as_mut_slice() {
cursor.write_all(&u32_val.to_le_bytes())?; for val in self.map.iter(){
let a : u32 = (*val>>24)&0x000000ff;
let b : u32 = (*val>>16)&0x000000ff;
let c : u32 = (*val>>8)&0x000000ff;
let d : u32 = *val&0x000000ff;
buf.push(a as u8);
buf.push(b as u8);
buf.push(c as u8);
buf.push(d as u8);
} }
// Ouvre le fichier en mode écriture // Ouvre le fichier en mode écriture, si absent on le creer
let mut file = File::create(file_path)?; let mut file = File::options()
.read(false)
.write(true)
.create(true)
.open(file_path)
.expect("bitmap::write_back, Unable to create or open File");
// Écrit les données dans le fichier // Écrit les données dans le fichier
file.write_all(&cursor.into_inner())?; file.write_all(&buf);
file.flush();//histoire d'etre sur
Ok(()) Ok(())
} }
@ -244,9 +234,25 @@ mod tests {
use std::io::{Seek, SeekFrom}; use std::io::{Seek, SeekFrom};
use super::*; use super::*;
#[test]
fn test_num_clear() {
let mut bit_map : BitMap = BitMap::init_bitmap(128);
let index_to_set : [usize ; 16] = [0,5,10,15,18,25,28,31,45,70,74,88,99,101,102,127];
for val in index_to_set.iter(){
bit_map.mark(*val);
}
assert!(bit_map.num_bits == 128);
assert!(bit_map.num_words == 4);
assert!((bit_map.num_clear() as usize) == bit_map.num_bits - index_to_set.len());
}
#[test] #[test]
fn test_fetch_from_file_smaller_than_map() { fn test_fetch_from_file_smaller_than_map() {
//println!("\n\n TEST FETCH FROM\n");
//On ecrit la séquence 0x AB CD EF 10 20 2E 3E 4F //On ecrit la séquence 0x AB CD EF 10 20 2E 3E 4F
let values : [u8 ; 8] = [0xAB, 0xCD, 0xEF, 0x10, 0x20, 0x2E, 0x3E, 0x4F]; let values : [u8 ; 8] = [0xAB, 0xCD, 0xEF, 0x10, 0x20, 0x2E, 0x3E, 0x4F];
let values_for_test : [u32 ; 2] = [0xABCDEF10, 0x202E3E4F]; let values_for_test : [u32 ; 2] = [0xABCDEF10, 0x202E3E4F];
@ -284,6 +290,7 @@ mod tests {
} }
#[test] #[test]
fn test_fetch_from_map_smaller_than_file() { fn test_fetch_from_map_smaller_than_file() {
//println!("\n\n TEST FETCH FROM\n"); //println!("\n\n TEST FETCH FROM\n");
@ -303,22 +310,21 @@ mod tests {
//1 bit donnera lieu via init_bit_map a une map de 32 bit soit 1 mot //1 bit donnera lieu via init_bit_map a une map de 32 bit soit 1 mot
let mut bit_map : BitMap = BitMap::init_bitmap(1); let mut bit_map : BitMap = BitMap::init_bitmap(1);
//println!("\nnombre de bit de la map {} \nnombre de mot {}\n", bit_map.num_bits, bit_map.num_words);
bit_map.fetch_from("test_fetch_bitmap_2"); bit_map.fetch_from("test_fetch_bitmap_2");
assert!(bit_map.num_bits == 32); assert!(bit_map.num_bits == 32);
assert!(bit_map.num_words == 1); assert!(bit_map.num_words == 1);
assert!(bit_map.map[0] == 0xFE59EF10); assert!(bit_map.map[0] == 0xFE59EF10);
//println!("\n\n data loaded into map \n"); /*println!("\n\n data loaded into map \n");
//print for debug //print for debug
/*for val in bit_map.map.iter(){ for val in bit_map.map.iter(){
println!("{:08X?}", *val) println!("{:08X?}", *val)
}*/ }*/
} }
#[test] #[test]
fn test_mark() { fn test_mark() {
@ -352,6 +358,7 @@ mod tests {
} }
#[test] #[test]
fn test_test() { fn test_test() {
@ -376,4 +383,83 @@ mod tests {
} }
} }
#[test]
fn test_clear() {
let mut bit_map : BitMap = BitMap::init_bitmap(128);
let index_to_set : [usize ; 16] = [0,5,10,15,18,25,28,31,45,70,74,88,99,101,102,127];
//mise a 1
for val in index_to_set.iter(){
bit_map.mark(*val);
}
assert!(bit_map.num_bits == 128);
assert!(bit_map.num_words == 4);
for i in 0..bit_map.num_bits {
if index_to_set.contains(&i) {
assert!( bit_map.test(i) == true);
}
else{
assert!( bit_map.test(i) == false) ;
}
}
//on met a 0 partout dans bit_map pour tester que le clear ne transforme pas 0 en 1
for i in 0..bit_map.num_bits {
bit_map.clear(i);
assert!( bit_map.test(i) == false);
}
}
#[test]
fn test_find() {
let mut bit_map : BitMap = BitMap::init_bitmap(128);
assert!(bit_map.num_bits == 128);
assert!(bit_map.num_words == 4);
for i in 0..bit_map.num_bits {
let j = bit_map.find();
assert!(i == (j as usize));
}
for j in 0..bit_map.num_bits{
assert!(bit_map.test(j) == true);
}
}
#[test]
fn test_write_back() {
let mut bit_map : BitMap = BitMap::init_bitmap(128);
let mut bit_map_from_file : BitMap = BitMap::init_bitmap(128);
let index_to_set : [usize ; 16] = [1,5,10,15,18,25,28,31,45,70,74,88,99,101,102,127];
for val in index_to_set.iter(){
bit_map.mark(*val);
}
assert!(bit_map.num_bits == 128);
assert!(bit_map.num_words == 4);
assert!(bit_map_from_file.num_bits == 128);
assert!(bit_map_from_file.num_words == 4);
bit_map.write_back("test_bit_map_write_back");
bit_map_from_file.fetch_from("test_bit_map_write_back");
for i in 0..(bit_map.num_words){
assert!(bit_map_from_file.map[i] == bit_map.map[i]);
}
}
} }