Skip to content

Commit

Permalink
Fix method add_coded_symbol to allow user to create coded symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
automainint committed Apr 12, 2024
1 parent a088cc2 commit a597401
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dec.add_symbol(bytes([4]))
while True:
s = enc.produce_next_coded_symbol()
print("coded: " + str(s.data[0]) + ", " + str(s.hash) + ", " + str(s.count))
dec.add_coded_symbol(s)
dec.add_coded_symbol(bytes([s.data[0]]), s.hash, s.count)
dec.try_decode()
if dec.decoded():
break
Expand Down
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def example():
while True:
s = enc.produce_next_coded_symbol()
print("coded: " + str(s.data[0]) + ", " + str(s.hash) + ", " + str(s.count))
dec.add_coded_symbol(s)
dec.add_coded_symbol(bytes([s.data[0]]), s.hash, s.count)
dec.try_decode()
if dec.decoded():
break
Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ macro_rules! instant {

fn add_symbol(&mut self, bytes: &[u8]) -> PyResult<()> {
if bytes.len() > $max_size || bytes.len() != self.symbol_size {
return Err(PyTypeError::new_err("invalid byte array size"))
return Err(PyTypeError::new_err("invalid byte array size"));
}
self.dec.add_symbol(&$Sym {
bytes : core::array::from_fn(|i| if i < self.symbol_size { bytes[i] } else { 0 }),
Expand All @@ -156,16 +156,19 @@ macro_rules! instant {
Ok(())
}

fn add_coded_symbol(&mut self, sym: &$Coded) -> PyResult<()> {
fn add_coded_symbol(&mut self, data: &[u8], hash: u64, count: i64) -> PyResult<()> {
if data.len() != $max_size && data.len() != self.symbol_size {
return Err(PyTypeError::new_err("invalid data size"));
}
self.dec.add_coded_symbol(&CodedSymbol::<$Sym> {
symbol : $Sym {
bytes : sym.data,
bytes : core::array::from_fn(|i| if i < self.symbol_size { data[i] } else { 0 }),
size : self.symbol_size,
hash_type : self.hash_type,
hash_key : self.hash_key,
},
hash : sym.hash,
count : sym.count,
hash : hash,
count : count,
});
Ok(())
}
Expand Down

0 comments on commit a597401

Please sign in to comment.