mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
implement a DeltaStorage System similar to DataStore, however i stil need to make duplicate non modifyable and mutable implementations, even though i only need mut, and this should somehow be simplified. also the borrow checker is still quite chaotic
This commit is contained in:
parent
7a1d04f320
commit
ff0d44d015
@ -19,27 +19,34 @@ use std::marker::PhantomData;
|
||||
However i belive that most algorithms only change every Value once.
|
||||
*/
|
||||
|
||||
pub trait Delta: Layer {}
|
||||
pub trait DeltaStore: Layer {
|
||||
type DETAIL;
|
||||
fn store(&mut self, pos: LodPos, value: Option<Self::DETAIL>);
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct VecDelta<T, const L: u8> {
|
||||
pub detail: Vec<(LodPos, Option<T>)>,
|
||||
}
|
||||
#[derive(Default, Clone)]
|
||||
pub struct VecNestDelta<D: Delta, T, const L: u8> {
|
||||
pub struct VecNestDelta<D: DeltaStore, T, const L: u8> {
|
||||
pub detail: Vec<(LodPos, Option<T>)>,
|
||||
pub child: D,
|
||||
}
|
||||
|
||||
pub struct DeltaWriter<'a, C: EntryLayer<'a> + DetailStore, D: EntryLayer<'a> + Delta> {
|
||||
pub struct DeltaWriter<'a, C: EntryLayer<'a> + DetailStore, D: EntryLayer<'a> + DeltaStore> {
|
||||
pub delta: &'a mut D,
|
||||
pub data: &'a mut C,
|
||||
}
|
||||
|
||||
pub struct VecDataIter<'a, D: Delta> {
|
||||
pub struct VecDeltaIter<'a, D: DeltaStore> {
|
||||
pub( super ) layer: &'a D,
|
||||
}
|
||||
|
||||
pub struct VecDeltaIterMut<'a, D: DeltaStore> {
|
||||
pub( super ) layer: &'a mut D,
|
||||
}
|
||||
|
||||
pub struct DataWriterIter<'a, DT: 'a, CT: 'a> {
|
||||
pub( super ) delta_iter: DT,
|
||||
pub( super ) data_iter: CT,
|
||||
@ -48,14 +55,24 @@ pub struct DataWriterIter<'a, DT: 'a, CT: 'a> {
|
||||
|
||||
//#######################################################
|
||||
|
||||
impl<'a, C: DetailStore + EntryLayer<'a>, D: Delta + EntryLayer<'a>> DeltaWriter<'a, C, D> {
|
||||
impl<'a, C: DetailStore + EntryLayer<'a>, D: DeltaStore + EntryLayer<'a>> DeltaWriter<'a, C, D> {
|
||||
pub fn new(delta: &'a mut D, data: &'a mut C) -> Self {
|
||||
DeltaWriter { delta, data }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const L: u8> Delta for VecDelta<T, { L }> {}
|
||||
impl<C: Delta, T, const L: u8> Delta for VecNestDelta<C, T, { L }> {}
|
||||
impl<T, const L: u8> DeltaStore for VecDelta<T, { L }> {
|
||||
type DETAIL = T;
|
||||
fn store(&mut self, pos: LodPos, value: Option<Self::DETAIL>) {
|
||||
self.detail.push((pos, value));
|
||||
}
|
||||
}
|
||||
impl<C: DeltaStore, T, const L: u8> DeltaStore for VecNestDelta<C, T, { L }> {
|
||||
type DETAIL = T;
|
||||
fn store(&mut self, pos: LodPos, value: Option<Self::DETAIL>) {
|
||||
self.detail.push((pos, value));
|
||||
}
|
||||
}
|
||||
|
||||
//#######################################################
|
||||
|
||||
@ -83,10 +100,10 @@ mod tests {
|
||||
let mut x = ExampleData::default();
|
||||
let mut d = ExampleDelta::default();
|
||||
{
|
||||
let w = DeltaWriter::new(&mut d, &mut x);
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
let i = LodPos::xyz(0, 1, 2);
|
||||
if false {
|
||||
let y = w.trav(i);
|
||||
let y = w.trav_mut(i);
|
||||
let ttc = y.get().get().get();
|
||||
let _tt = ttc.mat();
|
||||
}
|
||||
@ -98,9 +115,71 @@ mod tests {
|
||||
let mut x = gen_simple_example();
|
||||
let mut d = ExampleDelta::default();
|
||||
{
|
||||
let w = DeltaWriter::new(&mut d, &mut x);
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
let i = LodPos::xyz(0, 0, 0);
|
||||
assert_eq!(*w.trav(i).get().get().get().mat(), 7_i8);
|
||||
assert_eq!(*w.trav_mut(i).get().get().get().mat(), 7_i8);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mut_first_element() {
|
||||
let mut x = gen_simple_example();
|
||||
let mut d = ExampleDelta::default();
|
||||
//assert_eq!(x.detail_index.len(),1);
|
||||
assert_eq!(d.detail.len(),0);
|
||||
assert_eq!(d.child.detail.len(),0);
|
||||
assert_eq!(d.child.child.detail.len(),0);
|
||||
assert_eq!(d.child.child.child.detail.len(),0);
|
||||
let i = LodPos::xyz(0, 0, 0);
|
||||
{
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
assert_eq!(*w.trav_mut(i).get().get().get().mat(), 7_i8);
|
||||
}
|
||||
{
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
w.trav_mut(i).get().get().get().store(123);
|
||||
}
|
||||
{ //TODO: this shouldnt be necessary but somehow it is...
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
assert_eq!(*w.trav_mut(i).get().get().get().mat(), 123_i8);
|
||||
assert_eq!(d.detail.len(),0);
|
||||
assert_eq!(d.child.detail.len(),0);
|
||||
assert_eq!(d.child.child.detail.len(),0);
|
||||
assert_eq!(d.child.child.child.detail.len(),1);
|
||||
//assert_eq!(x.detail_index.len(),1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mut_multiple_elements() {
|
||||
let mut x = gen_simple_example();
|
||||
let mut d = ExampleDelta::default();
|
||||
let i = LodPos::xyz(0, 0, 0);
|
||||
{
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
assert_eq!(*w.trav_mut(i).get().get().get().mat(), 7_i8);
|
||||
}
|
||||
{
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
w.trav_mut(i).get().get().get().store(123);
|
||||
}
|
||||
{
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
w.trav_mut(LodPos::xyz(0, 0, 1)).get().get().get().store(111);
|
||||
}
|
||||
{
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
w.trav_mut(LodPos::xyz(0, 0, 2)).get().get().get().store(112);
|
||||
}
|
||||
{
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
w.trav_mut(LodPos::xyz(0, 0, 3)).get().get().get().store(111);
|
||||
}
|
||||
{ //TODO: this shouldnt be necessary but somehow it is...
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
let i = LodPos::xyz(0, 0, 0);
|
||||
assert_eq!(*w.trav_mut(i).get().get().get().mat(), 123_i8);
|
||||
assert_eq!(x.detail_index.len(),1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -109,9 +188,9 @@ mod tests {
|
||||
let mut x = gen_simple_example();
|
||||
let mut d = ExampleDelta::default();
|
||||
{
|
||||
let w = DeltaWriter::new(&mut d, &mut x);
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
let access = LodPos::xyz(0, 0, 0);
|
||||
b.iter(|| w.trav(access));
|
||||
b.iter(|| w.trav_mut(access));
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,9 +199,9 @@ mod tests {
|
||||
let mut x = gen_simple_example();
|
||||
let mut d = ExampleDelta::default();
|
||||
{
|
||||
let w = DeltaWriter::new(&mut d, &mut x);
|
||||
let mut w = DeltaWriter::new(&mut d, &mut x);
|
||||
let access = LodPos::xyz(0, 0, 0);
|
||||
b.iter(|| w.trav(access).get().get().get().mat());
|
||||
b.iter(|| w.trav_mut(access).get().get().get().mat());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
16
worldsim/src/lodstore/deltalizeable.rs
Normal file
16
worldsim/src/lodstore/deltalizeable.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use super::lodpos::{LodPos};
|
||||
use super::delta::{DeltaStore, VecDeltaIterMut};
|
||||
|
||||
pub trait Deltalizeable {
|
||||
type DELTA: DeltaStore;
|
||||
fn store(self, pos: LodPos, value: Option<<Self::DELTA as DeltaStore>::DETAIL>);
|
||||
}
|
||||
|
||||
///////////////// delta types
|
||||
|
||||
impl<'a, D: DeltaStore> Deltalizeable for VecDeltaIterMut<'a, D> {
|
||||
type DELTA = D;
|
||||
fn store(self, pos: LodPos, value: Option<D::DETAIL>) {
|
||||
self.layer.store(pos, value);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
use super::delta::Delta;
|
||||
use super::delta::DeltaStore;
|
||||
|
||||
/*
|
||||
traits:
|
||||
@ -7,12 +7,12 @@ use super::delta::Delta;
|
||||
*/
|
||||
|
||||
pub trait DrillDownable {
|
||||
type DELTA: Delta;
|
||||
type DELTA: DeltaStore;
|
||||
fn drill_down(detail: Self) -> Self::DELTA;
|
||||
}
|
||||
|
||||
pub trait DrillUpable {
|
||||
type DELTA: Delta;
|
||||
type DELTA: DeltaStore;
|
||||
fn drill_up(detail: Self) -> Self::DELTA;
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::index::ToOptionUsize;
|
||||
use super::lodpos::LodPos;
|
||||
use super::data::{HashNestLayer, DetailStore, HashIter, HashIterMut};
|
||||
use super::delta::{VecNestDelta, Delta, VecDataIter, DataWriterIter, DeltaWriter};
|
||||
use super::delta::{VecNestDelta, DeltaStore, VecDeltaIter, VecDeltaIterMut, DataWriterIter, DeltaWriter};
|
||||
use super::traversable::Traversable;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@ -40,26 +40,26 @@ for HashNestLayer<C, T, I, { L }>
|
||||
|
||||
///////////////// delta types
|
||||
|
||||
impl<'a, D: 'a + Delta, T: 'a, const L: u8> EntryLayer<'a> for VecNestDelta<D, T, { L }> {
|
||||
type TRAV = VecDataIter<'a, VecNestDelta<D, T, { L }>>;
|
||||
type TRAV_MUT = VecDataIter<'a, VecNestDelta<D, T, { L }>>;
|
||||
impl<'a, D: 'a + DeltaStore, T: 'a, const L: u8> EntryLayer<'a> for VecNestDelta<D, T, { L }> {
|
||||
type TRAV = VecDeltaIter<'a, VecNestDelta<D, T, { L }>>;
|
||||
type TRAV_MUT = VecDeltaIterMut<'a, VecNestDelta<D, T, { L }>>;
|
||||
|
||||
fn trav(&'a self, _pos: LodPos) -> Self::TRAV {
|
||||
VecDataIter { layer: self }
|
||||
VecDeltaIter { layer: self }
|
||||
}
|
||||
fn trav_mut(&'a mut self, _pos: LodPos) -> Self::TRAV_MUT {
|
||||
VecDataIter { layer: self }
|
||||
VecDeltaIterMut { layer: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C: DetailStore + EntryLayer<'a>, D: Delta + EntryLayer<'a>> EntryLayer<'a>
|
||||
impl<'a, C: DetailStore + EntryLayer<'a>, D: DeltaStore + EntryLayer<'a>> EntryLayer<'a>
|
||||
for DeltaWriter<'a, C, D>
|
||||
where
|
||||
<<C as EntryLayer<'a>>::TRAV as Traversable>::TRAV_CHILD: Traversable,
|
||||
<<D as EntryLayer<'a>>::TRAV as Traversable>::TRAV_CHILD: Traversable,
|
||||
{
|
||||
type TRAV = DataWriterIter<'a, D::TRAV, C::TRAV>;
|
||||
type TRAV_MUT = DataWriterIter<'a, D::TRAV, C::TRAV>;
|
||||
type TRAV_MUT = DataWriterIter<'a, D::TRAV_MUT, C::TRAV_MUT>;
|
||||
|
||||
fn trav(&'a self, pos: LodPos) -> DataWriterIter<D::TRAV, C::TRAV> {
|
||||
DataWriterIter {
|
||||
@ -69,10 +69,10 @@ for DeltaWriter<'a, C, D>
|
||||
}
|
||||
}
|
||||
|
||||
fn trav_mut(&'a mut self, pos: LodPos) -> DataWriterIter<D::TRAV, C::TRAV> {
|
||||
fn trav_mut(&'a mut self, pos: LodPos) -> DataWriterIter<D::TRAV_MUT, C::TRAV_MUT> {
|
||||
DataWriterIter {
|
||||
delta_iter: self.delta.trav(pos),
|
||||
data_iter: self.data.trav(pos),
|
||||
delta_iter: self.delta.trav_mut(pos),
|
||||
data_iter: self.data.trav_mut(pos),
|
||||
_a: PhantomData::<&'a ()>::default(),
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::index::ToOptionUsize;
|
||||
use super::lodpos::{two_pow_u, LodPos};
|
||||
use super::data::{VecLayer, HashLayer, VecNestLayer, HashNestLayer, DetailStore};
|
||||
use super::delta::{VecDelta, VecNestDelta, Delta};
|
||||
use super::delta::{VecDelta, VecNestDelta, DeltaStore};
|
||||
use vek::Vec3;
|
||||
|
||||
pub trait Layer {
|
||||
@ -25,19 +25,19 @@ pub trait ParentLayer: Layer {
|
||||
///////////////// data types
|
||||
|
||||
impl<T, const L: u8> Layer for VecLayer<T, { L }> {
|
||||
type KEY = (usize);
|
||||
type KEY = usize;
|
||||
const LEVEL: u8 = { L };
|
||||
}
|
||||
impl<T, const L: u8> Layer for HashLayer<T, { L }> {
|
||||
type KEY = (LodPos);
|
||||
type KEY = LodPos;
|
||||
const LEVEL: u8 = { L };
|
||||
}
|
||||
impl<C: DetailStore, T, I: ToOptionUsize, const L: u8> Layer for VecNestLayer<C, T, I, { L }> {
|
||||
type KEY = (usize);
|
||||
type KEY = usize;
|
||||
const LEVEL: u8 = { L };
|
||||
}
|
||||
impl<C: DetailStore, T, I: ToOptionUsize, const L: u8> Layer for HashNestLayer<C, T, I, { L }> {
|
||||
type KEY = (LodPos);
|
||||
type KEY = LodPos;
|
||||
const LEVEL: u8 = { L };
|
||||
}
|
||||
|
||||
@ -67,15 +67,15 @@ impl<C: DetailStore, T, I: ToOptionUsize, const L: u8> ParentLayer
|
||||
///////////////// delta types
|
||||
|
||||
impl<T, const L: u8> Layer for VecDelta<T, { L }> {
|
||||
type KEY = (usize);
|
||||
type KEY = usize;
|
||||
const LEVEL: u8 = { L };
|
||||
}
|
||||
impl<D: Delta, T, const L: u8> Layer for VecNestDelta<D, T, { L }> {
|
||||
type KEY = (usize);
|
||||
impl<D: DeltaStore, T, const L: u8> Layer for VecNestDelta<D, T, { L }> {
|
||||
type KEY = usize;
|
||||
const LEVEL: u8 = { L };
|
||||
}
|
||||
|
||||
impl<D: Delta, T, const L: u8> ParentLayer for VecNestDelta<D, T, { L }> {
|
||||
impl<D: DeltaStore, T, const L: u8> ParentLayer for VecNestDelta<D, T, { L }> {
|
||||
type CHILD = D;
|
||||
fn child(&self) -> &Self::CHILD {
|
||||
&self.child
|
||||
|
@ -1,6 +1,7 @@
|
||||
use super::lodpos::{LodPos};
|
||||
use super::data::{DetailStore, HashIter, VecIter, HashIterMut, VecIterMut};
|
||||
use super::delta::DataWriterIter;
|
||||
use super::delta::{DataWriterIter};
|
||||
use super::deltalizeable::{Deltalizeable};
|
||||
|
||||
/*
|
||||
|
||||
@ -32,7 +33,8 @@ type MAT_CHILD = L::DETAIL;
|
||||
fn mat(self) -> &'a L::DETAIL {
|
||||
DetailStore::load(self.layer, self.layer_lod)
|
||||
}
|
||||
fn store(self, mat: L::DETAIL) {
|
||||
fn store(self, _mat: L::DETAIL) {
|
||||
unimplemented!("only call on mut Iter");
|
||||
//DetailStore::save(self.layer, self.layer_key, mat)
|
||||
}
|
||||
}
|
||||
@ -44,7 +46,7 @@ impl<'a, L: DetailStore<KEY = LodPos>> Materializeable<'a> for HashIterMut<'a, L
|
||||
DetailStore::load(self.layer, self.layer_lod)
|
||||
}
|
||||
fn store(self, mat: L::DETAIL) {
|
||||
//DetailStore::save(self.layer, self.layer_key, mat)
|
||||
DetailStore::save(self.layer, self.layer_lod, mat)
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,7 +54,8 @@ impl<'a, L: DetailStore<KEY = usize>> Materializeable<'a> for VecIter<'a, L> {
|
||||
type MAT_CHILD = L::DETAIL;
|
||||
|
||||
fn mat(self) -> &'a L::DETAIL { DetailStore::load(self.layer, self.layer_key) }
|
||||
fn store(self, mat: L::DETAIL) {
|
||||
fn store(self, _mat: L::DETAIL) {
|
||||
unimplemented!("only call on mut Iter");
|
||||
//DetailStore::save(self.layer, self.layer_key, mat)
|
||||
}
|
||||
}
|
||||
@ -68,13 +71,19 @@ impl<'a, L: DetailStore<KEY = usize>> Materializeable<'a> for VecIterMut<'a, L>
|
||||
|
||||
///////////////// delta types
|
||||
|
||||
impl<'a, DT, CT: Materializeable<'a>> Materializeable<'a> for DataWriterIter<'a, DT, CT> {
|
||||
impl<'a, DT: Deltalizeable, CT: Materializeable<'a>> Materializeable<'a> for DataWriterIter<'a, DT, CT> {
|
||||
type MAT_CHILD = CT::MAT_CHILD;
|
||||
|
||||
fn mat(self) -> &'a CT::MAT_CHILD {
|
||||
self.data_iter.mat()
|
||||
}
|
||||
fn store(self, mat: CT::MAT_CHILD) {
|
||||
//self.delta_iter.register(LodPos::xyz(2,2,2,), mat);
|
||||
|
||||
//<DT as Deltalizeable>::DELTA::store(self.delta_iter,LodPos::xyz(2,2,2), None);
|
||||
self.delta_iter.store(LodPos::xyz(2,2,2), None);
|
||||
println!("saaave");
|
||||
self.data_iter.store(mat);
|
||||
//self.data_iter.store(mat)
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ pub mod lodarea;
|
||||
pub mod layer;
|
||||
pub mod traversable;
|
||||
pub mod materializeable;
|
||||
pub mod deltalizeable;
|
||||
pub mod entrylayer;
|
||||
pub mod data;
|
||||
pub mod delta;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::index::ToOptionUsize;
|
||||
use super::lodpos::{multily_with_2_pow_n, relative_to_1d, LodPos};
|
||||
use super::data::{DetailStore, IndexStore, HashIter, VecIter, HashIterMut, VecIterMut};
|
||||
use super::delta::{Delta, VecDataIter, DataWriterIter};
|
||||
use super::delta::{DeltaStore, VecDeltaIter, VecDeltaIterMut, DataWriterIter};
|
||||
#[allow(unused_imports)] //not unsued, cargo is just to stupud to detect that
|
||||
use super::layer::{Layer, ParentLayer};
|
||||
use std::marker::PhantomData;
|
||||
@ -123,19 +123,32 @@ impl<'a, L: DetailStore<KEY = usize> + IndexStore> Traversable for VecIterMut<'a
|
||||
|
||||
///////////////// delta types
|
||||
|
||||
impl<'a, D: Delta + ParentLayer> Traversable for VecDataIter<'a, D>
|
||||
impl<'a, D: DeltaStore + ParentLayer> Traversable for VecDeltaIter<'a, D>
|
||||
where
|
||||
D::CHILD: Delta,
|
||||
D::CHILD: DeltaStore,
|
||||
{
|
||||
type TRAV_CHILD = VecDataIter<'a, D::CHILD>;
|
||||
type TRAV_CHILD = VecDeltaIter<'a, D::CHILD>;
|
||||
|
||||
fn get(self) -> VecDataIter<'a, D::CHILD> {
|
||||
VecDataIter {
|
||||
fn get(self) -> VecDeltaIter<'a, D::CHILD> {
|
||||
VecDeltaIter {
|
||||
layer: self.layer.child(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, D: DeltaStore + ParentLayer> Traversable for VecDeltaIterMut<'a, D>
|
||||
where
|
||||
D::CHILD: DeltaStore,
|
||||
{
|
||||
type TRAV_CHILD = VecDeltaIterMut<'a, D::CHILD>;
|
||||
|
||||
fn get(self) -> VecDeltaIterMut<'a, D::CHILD> {
|
||||
VecDeltaIterMut {
|
||||
layer: self.layer.child_mut(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, DT: Traversable, CT: Traversable> Traversable for DataWriterIter<'a, DT, CT> {
|
||||
type TRAV_CHILD = DataWriterIter<'a, DT::TRAV_CHILD, CT::TRAV_CHILD>;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user