From: Ben Pfaff Date: Sat, 21 Dec 2024 20:19:38 +0000 (-0800) Subject: work on dictionary reading X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3e95332d83571342ff3fbb104a1e572f905bc68;p=pspp work on dictionary reading --- diff --git a/rust/pspp/src/cooked.rs b/rust/pspp/src/cooked.rs index 7bfba78ecb..4d46dc4ead 100644 --- a/rust/pspp/src/cooked.rs +++ b/rust/pspp/src/cooked.rs @@ -552,7 +552,7 @@ pub fn decode( } for dict_index in dict_indexes { - let mut variable = &dictionary.variables[dict_index]; + let variable = dictionary.variables.get_index_mut2(dict_index).unwrap(); for ValueLabel { value, label } in record.labels.iter().cloned() { let value = match value { raw::Value::Number(number) => Value::Number(number.map(|n| n.into())), @@ -560,6 +560,7 @@ pub fn decode( string.0[..variable.width.as_string_width().unwrap()].into() } }; + variable.value_labels.insert(value, label); } } } diff --git a/rust/pspp/src/dictionary.rs b/rust/pspp/src/dictionary.rs index d4fdd6c458..e8dcec2f20 100644 --- a/rust/pspp/src/dictionary.rs +++ b/rust/pspp/src/dictionary.rs @@ -159,8 +159,17 @@ impl Ord for Value { } } +impl Hash for Value { + fn hash(&self, state: &mut H) { + match self { + Value::Number(number) => number.map(|x| OrderedFloat(x)).hash(state), + Value::String(string) => string.hash(state), + } + } +} + impl Value { - fn sysmis() -> Self { + pub fn sysmis() -> Self { Self::Number(None) } } diff --git a/rust/pspp/src/identifier.rs b/rust/pspp/src/identifier.rs index 2d5c0317ec..c6909fd58a 100644 --- a/rust/pspp/src/identifier.rs +++ b/rust/pspp/src/identifier.rs @@ -3,7 +3,7 @@ use std::{ cmp::Ordering, fmt::{Debug, Display, Formatter, Result as FmtResult}, hash::{Hash, Hasher}, - ops::Deref, + ops::{Deref, DerefMut}, }; use encoding_rs::{EncoderResult, Encoding, UTF_8}; @@ -392,3 +392,12 @@ where &self.0 } } + +impl DerefMut for ByIdentifier +where + T: HasIdentifier + Clone, +{ + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/rust/pspp/src/main.rs b/rust/pspp/src/main.rs index 5fb57135f7..62ab24337b 100644 --- a/rust/pspp/src/main.rs +++ b/rust/pspp/src/main.rs @@ -153,8 +153,8 @@ fn dissect( } let headers = Headers::new(decoded_records, &|e| eprintln!("{e}"))?; let (dictionary, metadata) = decode(headers, encoding, |e| eprintln!("{e}"))?; - println!("{dictionary:?}"); - println!("{metadata:?}"); + println!("{dictionary:#?}"); + println!("{metadata:#?}"); } } diff --git a/rust/pspp/src/raw.rs b/rust/pspp/src/raw.rs index c9b04773ff..5b2a3eb55f 100644 --- a/rust/pspp/src/raw.rs +++ b/rust/pspp/src/raw.rs @@ -1553,9 +1553,10 @@ impl ValueLabelRecord, RawString> { let index_offset = r.stream_position()?; let mut dict_indexes = Vec::with_capacity(n as usize); let mut invalid_indexes = Vec::new(); + let valid_range = 1..=var_types.len(); for _ in 0..n { let index: u32 = endian.parse(read_bytes(r)?); - if index == 0 || index as usize > var_types.len() { + if valid_range.contains(&(index as usize)) { dict_indexes.push(index); } else { invalid_indexes.push(index);