From 09b867d0be5378f249a3dc4722527ed2e0233645 Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Thu, 4 Feb 2021 17:12:40 +0100 Subject: Experiment with incremental island having only one awake island. --- src/data/coarena.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/data') diff --git a/src/data/coarena.rs b/src/data/coarena.rs index 78cbfa7..6cdfbde 100644 --- a/src/data/coarena.rs +++ b/src/data/coarena.rs @@ -29,6 +29,27 @@ impl Coarena { .and_then(|(gg, t)| if g == *gg { Some(t) } else { None }) } + pub fn ensure_element_exists(&mut self, index: Index, default: T) -> &mut T + where + T: Clone, + { + let (i1, g1) = index.into_raw_parts(); + + let elt1 = { + if self.data.len() <= i1 { + self.data.resize(i1 + 1, (u32::MAX as u64, default.clone())); + } + + &mut self.data[i1] + }; + + if elt1.0 != g1 { + *elt1 = (g1, default); + } + + &mut elt1.1 + } + /// Ensure that elements at the two given indices exist in this coarena, and return their reference. /// /// Missing elements are created automatically and initialized with the `default` value. @@ -69,3 +90,16 @@ impl Coarena { (&mut elt1.1, &mut elt2.1) } } + +impl std::ops::Index for Coarena { + type Output = T; + fn index(&self, id: Index) -> &T { + self.get(id).expect("Index out of bounds.") + } +} + +impl std::ops::IndexMut for Coarena { + fn index_mut(&mut self, id: Index) -> &mut T { + self.get_mut(id).expect("Index out of bounds.") + } +} -- cgit