pub struct NodeField<T: Clone> { /* private fields */ }Expand description
Used to allocate space on the heap, read from that space, and write to it.
Implementations§
Source§impl<T: Clone> NodeField<T>
impl<T: Clone> NodeField<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new NodeField which can store data of type T
§Example
//Stores i32s
let mut storage = NodeField::<i32>::new();Sourcepub fn data<I: Indexable>(&self, index: I) -> Result<&T, AccessError>
pub fn data<I: Indexable>(&self, index: I) -> Result<&T, AccessError>
Returns an immutable reference to the data stored at the requested index, or an AccessError if there is a problem.
Sourcepub fn mut_data<I: Indexable>(
&mut self,
index: I,
) -> Result<&mut T, AccessError>
pub fn mut_data<I: Indexable>( &mut self, index: I, ) -> Result<&mut T, AccessError>
Returns a mutable reference to the data stored at the requested index, or an AccessError if there is a problem.
Sourcepub fn add_ref<I: Indexable>(&mut self, index: I) -> Result<(), AccessError>
pub fn add_ref<I: Indexable>(&mut self, index: I) -> Result<(), AccessError>
Tells the NodeField that something else references the data at index.
So long as the NodeField thinks there is at least one reference, the data won’t be freed.
Failure to properly track references will lead to either freeing data you wanted or leaking data you didn’t.
Sourcepub fn remove_ref<I: Indexable>(
&mut self,
index: I,
) -> Result<Option<T>, AccessError>
pub fn remove_ref<I: Indexable>( &mut self, index: I, ) -> Result<Option<T>, AccessError>
Tells the NodeField that something no longer references the data at index.
If calling this function renders the refcount 0, the data will be freed and returned.
Failure to properly track references will lead to either freeing data you wanted or leaking data you didn’t.
Sourcepub fn status<I: Indexable>(&self, index: I) -> Result<usize, AccessError>
pub fn status<I: Indexable>(&self, index: I) -> Result<usize, AccessError>
Returns the number of references the data at index has or an AccessError if the request has a problem
Sourcepub fn push(&mut self, data: T) -> usize
pub fn push(&mut self, data: T) -> usize
Pushes data into the NodeField, returning the index it was stored at.
Once you recieve the index the data was stored at, it is your responsibility to manage its references. The data will start with one reference.
Sourcepub fn replace<I: Indexable>(
&mut self,
index: I,
new_data: T,
) -> Result<T, AccessError>
pub fn replace<I: Indexable>( &mut self, index: I, new_data: T, ) -> Result<T, AccessError>
Replaces the data at index with new_data, returning the replaced data on success and an AccessError on failure.
You may not replace an index which is currently free.
Sourcepub fn next_allocated(&self) -> usize
pub fn next_allocated(&self) -> usize
Returns the next index which will be allocated on a NodeField::push call
Sourcepub fn repair_allocator(&mut self)
pub fn repair_allocator(&mut self)
Travels through each slot in the vec checks whether it currently contains data, updating the allocator accordingly.
Sourcepub fn defrag(&mut self) -> HashMap<usize, usize>
pub fn defrag(&mut self) -> HashMap<usize, usize>
Travels through memory and re-arranges slots so that they are contiguous in memory, with no free slots in between occupied ones. The hashmap returned can be used to remap your references to their new locations. (Key:Old, Value:New)
Slots at the back of memory will be placed in the first free slot, until the above condition is met.
This operation is O(n) to the number of slots in memory.
Sourcepub fn trim(&mut self) -> HashMap<usize, usize>
pub fn trim(&mut self) -> HashMap<usize, usize>
NodeField::defrags the memory, then shrinks the internal memory Vec to the size of the block of occupied memory.
Sourcepub fn allocation_map(&self) -> &Vec<usize>
pub fn allocation_map(&self) -> &Vec<usize>
Returns a reference to the current bitfield of allocated/free memory slots