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
trueif a new key-value pair was inserted.falseif 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 theBytesPrimitivestrait.
§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.Noneif 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.Noneif 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 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);
}sourcepub fn view_table(&self)
pub fn view_table(&self)
Provides a detailed, colored visualization of the hash table.
This function prints the structure of the ByteBox, including each cell and its entries.
§Examples
use bytesbox::ByteBox;
let mut bytebox = ByteBox::new();
bytebox.insert(b"key", b"value");
bytebox.view_table();Trait Implementations§
Auto Trait Implementations§
impl Freeze for ByteBox
impl RefUnwindSafe for ByteBox
impl Send for ByteBox
impl Sync for ByteBox
impl Unpin for ByteBox
impl UnwindSafe for ByteBox
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)