pub struct ByteBox { /* private fields */ }
Expand description
A hash table implementation that stores key-value pairs as byte vectors. Uses separate chaining to handle hash collisions.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
bytebox.insert(b"key1", b"value1");
bytebox.insert(b"key2", b"value2");
assert_eq!(bytebox.get(b"key1"), Some(&b"value1"[..]));
assert_eq!(bytebox.len(), 2);
Implementations§
Source§impl ByteBox
impl ByteBox
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new ByteBox
with a default initial capacity of 16 cells.
§Examples
use bytesbox::ByteBox;
let bytebox = ByteBox::new();
assert_eq!(bytebox.len(), 0);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs stored in the ByteBox
.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
assert_eq!(bytebox.len(), 0);
bytebox.insert(b"key", b"value");
assert_eq!(bytebox.len(), 1);
Sourcepub fn allocation(&self) -> usize
pub fn allocation(&self) -> usize
Returns the current allocation size (number of cells) of the ByteBox
.
§Examples
use bytesbox::ByteBox;
let bytebox = ByteBox::new();
assert_eq!(bytebox.allocation(), 16);
Sourcepub fn insert(&mut self, key: &[u8], value: &[u8]) -> bool
pub fn insert(&mut self, key: &[u8], value: &[u8]) -> bool
Inserts a key-value pair into the ByteBox
.
If the key already exists, its value is updated. If the load factor exceeds the threshold after insertion, the table is resized.
§Arguments
key
- A byte slice representing the key.value
- A byte slice representing the value.
§Returns
true
if a new key-value pair was inserted.false
if an existing key was updated.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
assert!(bytebox.insert(b"key1", b"value1"));
assert!(!bytebox.insert(b"key1", b"value2"));
assert_eq!(bytebox.get(b"key1"), Some(&b"value2"[..]));
Sourcepub fn insert_primitive<T: BytesPrimitives>(&mut self, key: &[u8], value: T)
pub fn insert_primitive<T: BytesPrimitives>(&mut self, key: &[u8], value: T)
Inserts a key and a primitive value into the ByteBox
.
The primitive value is converted to its byte representation using the BytesPrimitives
trait.
§Type Parameters
T
- A type that implements theBytesPrimitives
trait.
§Arguments
key
- A byte slice representing the key.value
- A primitive value that can be converted into bytes.
§Examples
use bytesbox::ByteBox;
use bytesbox::primitives::BytesPrimitives;
let mut bytebox = ByteBox::new();
bytebox.insert_primitive(b"number", 42u32);
assert_eq!(bytebox.get(b"number"), Some(&b"42"[..]));
Sourcepub fn get(&self, key: &[u8]) -> Option<&[u8]>
pub fn get(&self, key: &[u8]) -> Option<&[u8]>
Retrieves the value associated with the given key.
§Arguments
key
- A byte slice representing the key to look up.
§Returns
Some(&[u8])
containing the value if the key exists.None
if the key does not exist in theByteBox
.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
bytebox.insert(b"key", b"value");
assert_eq!(bytebox.get(b"key"), Some(&b"value"[..]));
assert_eq!(bytebox.get(b"nonexistent"), None);
Sourcepub fn remove(&mut self, key: &[u8]) -> Option<Vec<u8>>
pub fn remove(&mut self, key: &[u8]) -> Option<Vec<u8>>
Removes the key-value pair associated with the given key from the ByteBox
.
§Arguments
key
- A byte slice representing the key to remove.
§Returns
Some(Vec<u8>)
containing the removed value if the key existed.None
if the key was not found.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
bytebox.insert(b"key", b"value");
assert_eq!(bytebox.remove(b"key"), Some(b"value".to_vec()));
assert_eq!(bytebox.remove(b"key"), None);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all key-value pairs from the ByteBox
, resetting it to an empty state.
This method retains the current capacity of the hash table, allowing it to be reused
without the overhead of reallocating memory. All existing entries are removed, and
the length (len
) is set to zero.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
bytebox.insert(b"key1", b"value1");
bytebox.insert(b"key2", b"value2");
assert_eq!(bytebox.len(), 2);
bytebox.clear();
assert_eq!(bytebox.len(), 0);
assert_eq!(bytebox.get(b"key1"), None);
assert_eq!(bytebox.get(b"key2"), None);
Sourcepub fn iter(&self) -> ByteBoxIterator<'_> ⓘ
pub fn iter(&self) -> ByteBoxIterator<'_> ⓘ
Provides an iterator over the ByteBox
that allows for iteration using for
loops.
This enables the use of ByteBox
in contexts where an iterator is expected.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
bytebox.insert(b"key1", b"value1");
bytebox.insert(b"key2", b"value2");
for (key, value) in bytebox.iter() {
println!("{:?}: {:?}", key, value);
}