Buggy attempt at using slices
This commit is contained in:
parent
a49d3e6888
commit
d0e2587b85
100
src/main.rs
100
src/main.rs
|
@ -9,83 +9,79 @@ struct DenseItem<T> {
|
||||||
value: T
|
value: T
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use arrays+slices instead of vecs
|
// 'sparse' is the sparse list tracking the indices of densely-stored items
|
||||||
|
// 'dense' is the dense list containing the actual items
|
||||||
|
// 'n' is the total number of densely-stored items
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct SparseSet<T> {
|
struct SparseSet<'s, 'd, T> {
|
||||||
sparse: Vec<Option<usize>>,
|
sparse: &'s mut [Option<usize>],
|
||||||
dense: Vec<DenseItem<T>>
|
dense: &'d mut [Option<DenseItem<T>>],
|
||||||
|
n: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SparseSet<T> {
|
impl<'s, 'd, T> SparseSet<'s, 'd, T> {
|
||||||
pub fn new() -> SparseSet<T> {
|
// A new sparse set can be created with two mutable slices: one for the sparse and one for the dense slices
|
||||||
|
pub fn new(sparse_slice: &'s mut [Option<usize>], dense_slice: &'d mut [Option<DenseItem<T>>], n: usize) -> SparseSet<'s, 'd, T> {
|
||||||
SparseSet {
|
SparseSet {
|
||||||
sparse: vec![],
|
sparse: sparse_slice,
|
||||||
dense: vec![]
|
dense: dense_slice,
|
||||||
|
n: n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add an item to the dense vector and record its index in the sparse vector
|
// Add an item to the set at a target sparse location
|
||||||
fn insert(&mut self, value: T) {
|
fn insert(&mut self, target_sparse_index: usize, value: T) {
|
||||||
|
// Create the item, first of all
|
||||||
let item = DenseItem {
|
let item = DenseItem {
|
||||||
index: self.dense.len(),
|
index: target_sparse_index,
|
||||||
value: value
|
value: value
|
||||||
};
|
};
|
||||||
self.dense.push(item);
|
|
||||||
let dense_index = self.dense.len() - 1;
|
// Insert the item into the dense slice at index N
|
||||||
self.sparse.push(Some(dense_index));
|
self.dense[self.n] = Some(item);
|
||||||
|
|
||||||
|
// Write N to the sparse slice at the target index
|
||||||
|
self.sparse[target_sparse_index] = Some(self.n);
|
||||||
|
|
||||||
|
// Increase N by one, lastly
|
||||||
|
self.n += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove an item at the supplied target index from the set
|
// Remove an item at the supplied target index from the set
|
||||||
// Replaces the selected target with the last item in the dense vector
|
// Replaces the selected target with the last item in the dense vector
|
||||||
fn remove(&mut self, target_sparse_index: usize) -> Result<(), SparseSetError> {
|
fn remove(&mut self, target_sparse_index: usize) -> Result<(), SparseSetError> {
|
||||||
// Find the dense index of the target (need to decide what to do from there)
|
// Get the last dense item and clear its former dense entry
|
||||||
|
let last_item = self.dense[self.n - 1].as_ref().unwrap();
|
||||||
|
let last_item_sparse_index = last_item.index;
|
||||||
|
self.dense[self.n - 1] = None;
|
||||||
|
|
||||||
|
// Replace the target dense item with the last dense item
|
||||||
let target_dense_index = self.sparse[target_sparse_index].unwrap();
|
let target_dense_index = self.sparse[target_sparse_index].unwrap();
|
||||||
|
self.dense[target_dense_index] = Some(last_item);
|
||||||
|
|
||||||
// If the target points to the last item in the dense set, this'll be easier
|
// Update the (former) last dense item's corresponding dense index key to point to its new location (where the target used to be)
|
||||||
if target_dense_index + 1 == self.dense.len() {
|
self.sparse[last_item_sparse_index] = Some(target_dense_index);
|
||||||
// Remove the target from the dense set
|
|
||||||
self.dense.pop();
|
|
||||||
|
|
||||||
// Overwrite the target's sparse index
|
// Clear the target's sparse entry
|
||||||
self.sparse[target_sparse_index] = None;
|
self.sparse[target_sparse_index] = None;
|
||||||
} else {
|
|
||||||
// Get the last item in the dense set as a source to switch with
|
// Decrement N
|
||||||
// Change its index to the target sparse index, as it's replacing the target
|
self.n -= 1;
|
||||||
let source_item = match self.dense.pop() {
|
|
||||||
Some(item) => { item },
|
|
||||||
None => { return Err(SparseSetError) }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Redirect the source item's sparse index to replace the target sparse index
|
|
||||||
self.sparse[source_item.index] = Some(target_sparse_index);
|
|
||||||
|
|
||||||
// Assign the source item to the target's place in the dense set
|
|
||||||
self.dense[target_dense_index] = source_item;
|
|
||||||
|
|
||||||
// Overwrite the target's sparse index
|
|
||||||
self.sparse[target_dense_index] = None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut test_set: SparseSet<f32> = SparseSet::new();
|
// Create the arrays for our sparse set
|
||||||
test_set.insert(69.0);
|
let mut dense_array: [Option<DenseItem<f32>>; 8] = [None; 8];
|
||||||
test_set.insert(420.0);
|
let mut sparse_array: [Option<usize>; 8] = [None; 8];
|
||||||
test_set.insert(9001.0);
|
|
||||||
|
let mut test_set: SparseSet<f32> = SparseSet::new(&mut sparse_array, &mut dense_array, 0);
|
||||||
|
test_set.insert(0, 69.0);
|
||||||
|
test_set.insert(4, 420.0);
|
||||||
|
test_set.insert(7, 9001.0);
|
||||||
println!("{:?}", test_set);
|
println!("{:?}", test_set);
|
||||||
test_set.remove(1);
|
test_set.remove(1);
|
||||||
println!("{:?}", test_set);
|
println!("{:?}", test_set);
|
||||||
|
|
||||||
// Create a mutable array (contiguous memory!)
|
|
||||||
let mut array: [Option<DenseItem<f32>>; 8] = [None; 8];
|
|
||||||
|
|
||||||
// Create a mutable slice of that whole array
|
|
||||||
let slice: &[Option<DenseItem<f32>>] = &mut array[..];
|
|
||||||
|
|
||||||
for item in slice {
|
|
||||||
println!("{:?}", item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue