Skip to content

Commit

Permalink
chore: Fixed MAX_PRIME_NUMBERS to match Panda, and other fixes
Browse files Browse the repository at this point in the history
Not sure if it matters that Panda stores its hash value as a *signed*
integer and then casts into an unsigned integer when done, while we were
keeping it as an unsigned integer from the beginning.
  • Loading branch information
maxrdz committed Feb 21, 2024
1 parent 96285bc commit adb2761
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion libdonet/src/dcarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl DCArrayTypeInterface for DCArrayType {
// TODO!
//hashgen.add_int(self.array_range.unwrap().min.value.integer)
} else {
hashgen.add_int(u32::from(self.array_size))
hashgen.add_int(i32::from(self.array_size))
}
}

Expand Down
2 changes: 1 addition & 1 deletion libdonet/src/dcfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl DCFieldInterface for DCField {
// The field ID is added to the hash here, since we need to ensure
// the hash code comes out different in the DC_MULTIPLE_INHERITANCE case.
if globals::DC_MULTIPLE_INHERITANCE {
hashgen.add_int(u32::from(self.field_id));
hashgen.add_int(i32::from(self.field_id));
}
}

Expand Down
4 changes: 2 additions & 2 deletions libdonet/src/dcfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ impl DCFileInterface for DCFile {
if globals::DC_VIRTUAL_INHERITANCE {
// Just to change the hash output in this case.
if globals::DC_SORT_INHERITANCE_BY_FILE {
hashgen.add_int(1_u32);
hashgen.add_int(1_i32);
} else {
hashgen.add_int(2_u32);
hashgen.add_int(2_i32);
}
}
hashgen.add_int(self.get_num_dclasses().try_into().unwrap());
Expand Down
6 changes: 3 additions & 3 deletions libdonet/src/dckeyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
/// This is a flag bitmask for historical keywords.
/// Panda uses a C/C++ 'int' for this, which is stored
/// as 4 bytes in modern 32-bit and 64-bit C/C++ compilers.
pub type HistoricalFlag = u32;
pub type HistoricalFlag = i32;

#[derive(Debug, PartialEq, Eq)]
pub struct DCKeyword {
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Default for DCKeywordList {
Self {
keywords: vec![],
kw_name_2_keyword: MultiMap::new(),
flags: 0_u32,
flags: 0_i32,
}
}
}
Expand Down Expand Up @@ -241,6 +241,6 @@ impl DCKeywordListInterface for DCKeywordList {
fn clear_keywords(&mut self) {
self.keywords.clear();
self.kw_name_2_keyword.clear();
self.flags = 0_u32;
self.flags = 0_i32;
}
}
2 changes: 1 addition & 1 deletion libdonet/src/dclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl DClassInterface for DClass {
let new_ptr: Arc<Mutex<DClass>> = parent_ptr.clone();
let mut parent: MutexGuard<'_, DClass> = new_ptr.deref().lock().unwrap();

hashgen.add_int(u32::from(parent.get_dclass_id()));
hashgen.add_int(i32::from(parent.get_dclass_id()));
}

if let Some(constructor_ptr) = &self.constructor {
Expand Down
2 changes: 1 addition & 1 deletion libdonet/src/dcnumeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl DCNumericTypeInterface for DCNumericType {

fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
self.base_type.generate_hash(hashgen);
hashgen.add_int(u32::from(self.divisor));
hashgen.add_int(i32::from(self.divisor));

if self.has_modulus() {
// unsafe block required for accessing unions
Expand Down
2 changes: 1 addition & 1 deletion libdonet/src/dctype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl DCTypeDefinitionInterface for DCTypeDefinition {

/// Generates the hash for this DC Type element.
fn generate_hash(&self, hashgen: &mut DCHashGenerator) {
hashgen.add_int(u32::from(self.data_type.clone() as u8));
hashgen.add_int(i32::from(self.data_type.clone() as u8));

if self.alias.is_some() {
hashgen.add_string(self.alias.clone().unwrap())
Expand Down
2 changes: 1 addition & 1 deletion libdonet/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ cfg_if! {
pub static DC_MULTIPLE_INHERITANCE: bool = true;
pub static DC_VIRTUAL_INHERITANCE: bool = true;
pub static DC_SORT_INHERITANCE_BY_FILE: bool = false;
pub static MAX_PRIME_NUMBERS: u16 = 1000;
pub static MAX_PRIME_NUMBERS: u16 = 10000;

// DC Parser Return Types
pub type ParseError = (Option<(DCToken, Span)>, &'static str);
Expand Down
12 changes: 6 additions & 6 deletions libdonet/src/hashgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct PrimeNumberGenerator {
}

pub struct DCHashGenerator {
hash: DCFileHash,
hash: i32,
index: u16,
primes: PrimeNumberGenerator,
}
Expand Down Expand Up @@ -64,7 +64,7 @@ impl PrimeNumberGenerator {
impl Default for DCHashGenerator {
fn default() -> Self {
Self {
hash: 0_u32,
hash: 0_i32,
index: 0_u16,
primes: PrimeNumberGenerator::new(),
}
Expand All @@ -76,17 +76,17 @@ impl DCHashGenerator {
Self::default()
}
/// Adds another integer to the hash so far.
pub fn add_int(&mut self, number: u32) {
pub fn add_int(&mut self, number: i32) {
assert!(self.index < MAX_PRIME_NUMBERS);
self.hash += u32::from(self.primes.get_prime(self.index)) * number;
self.hash += i32::from(self.primes.get_prime(self.index)) * number;
self.index = (self.index + 1) % MAX_PRIME_NUMBERS;
}

/// Adds a blob to the hash, by breaking it down into a sequence of integers.
pub fn add_blob(&mut self, blob: Vec<u8>) {
self.add_int(blob.len().try_into().unwrap());
for byte in blob.into_iter() {
self.add_int(u32::from(byte));
self.add_int(i32::from(byte));
}
}
/// Adds a string to the hash, by breaking it down into a sequence of integers.
Expand All @@ -95,7 +95,7 @@ impl DCHashGenerator {
}

pub const fn get_hash(&self) -> DCFileHash {
self.hash
self.hash as u32
}
}

Expand Down

0 comments on commit adb2761

Please sign in to comment.