From 9f69dbb4cadaa6befdd6b7301fcc49c287ec2fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Sat, 12 Oct 2019 12:13:50 +0200 Subject: [PATCH] experiment with KeyConvertable which has compress undcompress function set a tem OWN_PER_PARENT const because i couldn't figure out childs correctly experiment with a first get implementation, and choose to make trav return a ResultLayer on current layer not one below --- worldsim/src/lodstore/newdata.rs | 167 +++++++++++++++++++++---------- 1 file changed, 116 insertions(+), 51 deletions(-) diff --git a/worldsim/src/lodstore/newdata.rs b/worldsim/src/lodstore/newdata.rs index 3e4fea3f74..9fad8da497 100644 --- a/worldsim/src/lodstore/newdata.rs +++ b/worldsim/src/lodstore/newdata.rs @@ -18,6 +18,7 @@ use vek::*; traits: - IndexStore: Every layer must implement this for either KEY = usize or KEY = LodPos and INDEX is often u16/u32. depending on the store of the parent detail. It is accessed by parent layer to store the index when a detail is added or removed. + Every IndexStore has a parent, and a constant OWN_PER_PARENT which says, how many details fit into one element of the parent. - DetailStore: Every layer must implement this for either KEY = usize or KEY = LodPos, independent from the parent. This is used to store the actual detail of every layer. - Nestable: All layers, except the lowest one implement this trait. It links the below layer to interact with the child layer. @@ -38,19 +39,32 @@ use vek::*; pub type LodPos = LodIndex; -pub trait IndexStore { +pub trait KeyConvertable { type KEY; - type INDEX: Copy; + type INDEX; + fn uncompress(&self, index: &Self::INDEX) -> Self::KEY; + fn compress(&self, key: &Self::KEY) -> Self::INDEX; +} + +pub trait IndexStore { + type PARENT_DETAIL: DetailStore; + type INDEX: Copy; + const OWN_PER_PARENT: usize; // theoretically this can be auto calculated in a new trait, however rust ist detecting conflicting implementations + + fn load(&self, key: Self::PARENT_DETAIL::KEY) -> Self::INDEX; + fn store(&mut self, key: Self::PARENT_DETAIL::KEY, index: Self::INDEX); +} +// Every IndexStore practivally needs this, but i want to autocalculte OWN_PER_PARENT so this needs to be an extra trait +pub trait IndexStoreLevel: IndexStore { + - fn load(&mut self, key: Self::KEY) -> Self::INDEX; - fn store(&mut self, key: Self::KEY, index: Self::INDEX); } pub trait DetailStore { type KEY; type DETAIL; const LEVEL: u8; - fn load(&mut self, key: Self::KEY) -> &Self::DETAIL; + fn load(&self, key: Self::KEY) -> &Self::DETAIL; fn load_mut(&mut self, key: Self::KEY) -> &mut Self::DETAIL; fn store(&mut self, key: Self::KEY, detail: Self::DETAIL); } @@ -70,10 +84,26 @@ pub trait Materializeable { fn mat(self) -> T; } -pub struct LayerResult<'a, N: IndexStore + DetailStore, PK> { +//CK is childs key of IndexStore, its needed for Traversable, but IndexStore cannot be a dependency, because of last node which acets materializeable but not Traversable +pub struct LayerResult<'a, N: DetailStore> { child: &'a N, wanted: LodPos, - index: PK, + key: N::KEY, +} + +//TODO: optimize this self away! vtable +//TODO: arrr and the clone +impl KeyConvertable { + fn uncompress(&self, index: &u16) -> usize { index.clone() as usize } + fn compress(&self, key: &usize) -> u16 { key.clone() as u16 } +} +impl KeyConvertable { + fn uncompress(&self, index: &u32) -> usize { index.clone() as usize } + fn compress(&self, key: &usize) -> u32 { key.clone() as u32 } +} +impl KeyConvertable { + fn uncompress(&self, index: &LodPos) -> LodPos { index.clone() } + fn compress(&self, key: &LodPos) -> LodPos { key.clone() } } //####################################################### @@ -107,66 +137,66 @@ pub struct HashNoneNestLayer { } #[rustfmt::skip] -impl IndexStore for VecVecLayer { - type KEY = usize; type INDEX=PI; - fn load(&mut self, key: usize) -> PI { *self.index.get(key).unwrap() } - fn store(&mut self, key: usize, index: PI) { self.index.insert(key, index); } -} -#[rustfmt::skip] -impl + DetailStore, T, PI: Copy, const L: u8> IndexStore for VecVecNestLayer { - type KEY = usize; type INDEX=PI; - fn load(&mut self, key: usize) -> PI { *self.index.get(key).unwrap() } - fn store(&mut self, key: usize, index: PI) { self.index.insert(key, index); } -} -#[rustfmt::skip] -impl IndexStore for VecHashLayer { - type KEY = LodPos; type INDEX=PI; - fn load(&mut self, key: LodPos) -> PI { *self.index.get(&key).unwrap() } - fn store(&mut self, key: LodPos, index: PI) { self.index.insert(key, index); } -} -#[rustfmt::skip] -impl + DetailStore, T, PI: Copy, const L: u8> IndexStore for VecHashNestLayer { - type KEY = LodPos; type INDEX=PI; - fn load(&mut self, key: LodPos) -> PI { *self.index.get(&key).unwrap() } - fn store(&mut self, key: LodPos, index: PI) { self.index.insert(key, index); } -} - -#[rustfmt::skip] -impl DetailStore for VecVecLayer { - type KEY = usize; type DETAIL=T; const LEVEL: u8 = { L }; - fn load(&mut self, key: usize) -> &T { self.detail.get(key).unwrap() } +impl DetailStore for VecVecLayer { + type PARENT_DETAIL = usize; type DETAIL=T; const LEVEL: u8 = { L }; + fn load(&self, key: usize) -> &T { self.detail.get(key).unwrap() } fn load_mut(&mut self, key: usize) -> &mut T { self.detail.get_mut(key).unwrap() } fn store(&mut self, key: usize, detail: T) { self.detail.insert(key, detail); } } #[rustfmt::skip] impl + DetailStore, T, PI: Copy, const L: u8> DetailStore for VecVecNestLayer { - type KEY = usize; type DETAIL=T; const LEVEL: u8 = { L }; - fn load(&mut self, key: usize) -> &T { self.detail.get(key).unwrap() } + type PARENT_DETAIL = usize; type DETAIL=T; const LEVEL: u8 = { L }; + fn load(&self, key: usize) -> &T { self.detail.get(key).unwrap() } fn load_mut(&mut self, key: usize) -> &mut T { self.detail.get_mut(key).unwrap() } fn store(&mut self, key: usize, detail: T) { self.detail.insert(key, detail); } } #[rustfmt::skip] impl DetailStore for VecHashLayer { type KEY = usize; type DETAIL=T; const LEVEL: u8 = { L }; - fn load(&mut self, key: usize) -> &T { self.detail.get(key).unwrap() } + fn load(&self, key: usize) -> &T { self.detail.get(key).unwrap() } fn load_mut(&mut self, key: usize) -> &mut T { self.detail.get_mut(key).unwrap() } fn store(&mut self, key: usize, detail: T) { self.detail.insert(key, detail); } } #[rustfmt::skip] impl + DetailStore, T, PI: Copy, const L: u8> DetailStore for VecHashNestLayer { type KEY = usize; type DETAIL=T; const LEVEL: u8 = { L }; - fn load(&mut self, key: usize) -> &T { self.detail.get(key).unwrap() } + fn load(&self, key: usize) -> &T { self.detail.get(key).unwrap() } fn load_mut(&mut self, key: usize) -> &mut T { self.detail.get_mut(key).unwrap() } fn store(&mut self, key: usize, detail: T) { self.detail.insert(key, detail); } } #[rustfmt::skip] impl + DetailStore, T, const L: u8> DetailStore for HashNoneNestLayer { type KEY = LodPos; type DETAIL=T; const LEVEL: u8 = { L }; - fn load(&mut self, key: LodPos) -> &T { self.detail.get(&key).unwrap() } + fn load(&self, key: LodPos) -> &T { self.detail.get(&key).unwrap() } fn load_mut(&mut self, key: LodPos) -> &mut T { self.detail.get_mut(&key).unwrap() } fn store(&mut self, key: LodPos, detail: T) { self.detail.insert(key, detail); } } +#[rustfmt::skip] +impl IndexStore for VecVecLayer { + type KEY = usize; type INDEX=PI; const OWN_PER_PARENT: usize = 4096; //TODO: calculate these correctly + fn load(&self, key: usize) -> PI { *self.index.get(key).unwrap() } + fn store(&mut self, key: usize, index: PI) { self.index.insert(key, index); } +} +#[rustfmt::skip] +impl + DetailStore, T, PI: Copy, const L: u8> IndexStore for VecVecNestLayer { + type KEY = usize; type INDEX=PI; const OWN_PER_PARENT: usize = 32768; + fn load(&self, key: usize) -> PI { *self.index.get(key).unwrap() } + fn store(&mut self, key: usize, index: PI) { self.index.insert(key, index); } +} +#[rustfmt::skip] +impl IndexStore for VecHashLayer { + type KEY = LodPos; type INDEX=PI; const OWN_PER_PARENT: usize = 1337; + fn load(&self, key: LodPos) -> PI { *self.index.get(&key).unwrap() } + fn store(&mut self, key: LodPos, index: PI) { self.index.insert(key, index); } +} +#[rustfmt::skip] +impl + DetailStore, T, PI: Copy, const L: u8> IndexStore for VecHashNestLayer { + type KEY = LodPos; type INDEX=PI; const OWN_PER_PARENT: usize = 4096; + fn load(&self, key: LodPos) -> PI { *self.index.get(&key).unwrap() } + fn store(&mut self, key: LodPos, index: PI) { self.index.insert(key, index); } +} + #[rustfmt::skip] impl + DetailStore, T, PI: Copy, const L: u8> Nestable for VecVecNestLayer { type NESTED=N; @@ -185,25 +215,60 @@ impl + DetailStore, T, const L: u8> Nestable for Has //####################################################### -impl + DetailStore, T, const L: u8> HashNoneNestLayer { - pub fn trav<'a>(&'a self, index: LodPos) -> LayerResult<'a, N, usize> { +impl + DetailStore, T, const L: u8> HashNoneNestLayer +{ + pub fn trav<'a>(&'a self, pos: LodPos) -> LayerResult<'a, Self> { LayerResult { - child: &self.nested, - wanted: index, - index: 0, + child: self, + wanted: pos, + key: pos.align_to_layer_id(Self::LEVEL), } } } -impl<'a, N: IndexStore + DetailStore + Nestable, PK> - Traversable::KEY>> for LayerResult<'a, N, PK> +/*impl<'a, N: DetailStore + Nestable> +Traversable::KEY>> for LayerResult<'a, N, ::KEY> +where N::NESTED: IndexStore, + ::KEY: Copy, + ::KEY: KeyConvertable::KEY, INDEX=::INDEX> { - fn get(self) -> LayerResult<'a, N::NESTED, ::KEY> { - unimplemented!(); + fn get(self) -> LayerResult<'a, N::NESTED, ::KEY> { + println!("{}", N::LEVEL); + let child = self.child.nested(); + let key = self.key; + //let index = self.index.align_to_layer_id(N::LEVEL); + LayerResult { + child, + wanted: self.wanted, + key: key.uncompress(&IndexStore::load(child, key)), + } + } +}*/ + + +impl<'a, N: DetailStore + Nestable> +Traversable> for LayerResult<'a, N> +where N::NESTED: IndexStore, + ::KEY: Copy, + ::KEY: KeyConvertable::KEY, INDEX=::INDEX>, +// ERROR PRATISCH GESEHEN GILT DIE FOLGENDE ZEILE IMMER, aber das sieht der compiler nur wenn wir auf den structs arbeiten, +// AUF DEN TRAITS WEIS ER ES NICHT! DAS IST KAKA + ::KEY == ::KEY +{ + fn get(self) -> LayerResult<'a, N::NESTED> { + println!("{}", N::LEVEL); + let child = self.child.nested(); + let key = self.key; + //let index = self.index.align_to_layer_id(N::LEVEL); + LayerResult { + child, + wanted: self.wanted, + key: key.uncompress(&IndexStore::load(child, key)), + } } } -impl<'a, N: IndexStore + DetailStore, PK> Materializeable for LayerResult<'a, N, PK> { +impl<'a, N: IndexStore + DetailStore> Materializeable for LayerResult<'a, N> { fn mat(self) -> N::DETAIL { unimplemented!(); } @@ -244,7 +309,7 @@ mod tests { }; let i = LodPos::new(Vec3::new(0, 1, 2)); let y = x.trav(i); - let ttc = y.get().get(); + let ttc = y.get().get().get(); let tt = ttc.mat(); } }