use ux::u48;
#[derive(Clone, Debug)]
pub struct ConstTable {
pub strings: Vec<String>,
pub floats: Vec<f64>,
pub ints: Vec<i64>,
}
impl Default for ConstTable {
fn default() -> Self {
Self::new()
}
}
impl ConstTable {
pub fn new() -> Self {
ConstTable {
strings: Vec::new(),
floats: Vec::new(),
ints: Vec::new(),
}
}
pub fn add_string(&mut self, s: String) -> u48 {
if let Some((i, _)) = self.strings.iter().enumerate().find(|(_, v)| *v == &s) {
return u48::new(i as u64);
}
self.strings.push(s);
u48::new((self.strings.len() - 1) as u64)
}
pub fn add_float(&mut self, f: f64) -> u48 {
if let Some((i, _)) = self.floats.iter().enumerate().find(|(_, v)| *v == &f) {
return u48::new(i as u64);
}
self.floats.push(f);
u48::new((self.floats.len() - 1) as u64)
}
pub fn add_int(&mut self, i: i64) -> u48 {
if let Some((i, _)) = self.ints.iter().enumerate().find(|(_, v)| *v == &i) {
return u48::new(i as u64);
}
self.ints.push(i);
u48::new((self.ints.len() - 1) as u64)
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
fn write_string(bytes: &mut Vec<u8>, s: &String) {
let str_bytes = s.as_bytes();
let len = str_bytes.len() as u32;
bytes.extend_from_slice(&len.to_le_bytes());
bytes.extend_from_slice(str_bytes);
}
fn write_f64(bytes: &mut Vec<u8>, f: f64) {
bytes.extend_from_slice(&f.to_le_bytes());
}
fn write_i64(bytes: &mut Vec<u8>, i: i64) {
bytes.extend_from_slice(&i.to_le_bytes());
}
let string_count = self.strings.len() as u32;
bytes.extend_from_slice(&string_count.to_le_bytes());
for s in &self.strings {
write_string(&mut bytes, s);
}
let float_count = self.floats.len() as u32;
bytes.extend_from_slice(&float_count.to_le_bytes());
for f in &self.floats {
write_f64(&mut bytes, *f);
}
let int_count = self.ints.len() as u32;
bytes.extend_from_slice(&int_count.to_le_bytes());
for i in &self.ints {
write_i64(&mut bytes, *i);
}
bytes
}
pub fn from_bytes(bytes: &[u8]) -> Self {
let mut offset = 0;
fn read_string(bytes: &[u8]) -> (usize, String) {
let len = u32::from_le_bytes(bytes[0..4].try_into().unwrap()) as usize;
let s = String::from_utf8(bytes[4..4 + len].to_vec()).unwrap();
(4 + len, s)
}
fn read_f64(bytes: &[u8]) -> (usize, f64) {
let f = f64::from_le_bytes(bytes[0..8].try_into().unwrap());
(8, f)
}
fn read_i64(bytes: &[u8]) -> (usize, i64) {
let i = i64::from_le_bytes(bytes[0..8].try_into().unwrap());
(8, i)
}
let string_count =
u32::from_le_bytes(bytes[offset..offset + 4].try_into().unwrap()) as usize;
offset += 4;
let mut strings = Vec::with_capacity(string_count);
for _ in 0..string_count {
let (len, s) = read_string(&bytes[offset..]);
strings.push(s);
offset += len;
}
let float_count =
u32::from_le_bytes(bytes[offset..offset + 4].try_into().unwrap()) as usize;
offset += 4;
let mut floats = Vec::with_capacity(float_count);
for _ in 0..float_count {
let (len, f) = read_f64(&bytes[offset..]);
floats.push(f);
offset += len;
}
let int_count = u32::from_le_bytes(bytes[offset..offset + 4].try_into().unwrap()) as usize;
offset += 4;
let mut ints = Vec::with_capacity(int_count);
for _ in 0..int_count {
let (len, i) = read_i64(&bytes[offset..]);
ints.push(i);
offset += len;
}
ConstTable {
strings,
floats,
ints,
}
}
}