Refactoring for new types Map and Set, renaming of DisplacementSet and its methods. Builds, not tested.
This commit is contained in:
parent
bae7248471
commit
d73227b907
3 changed files with 353 additions and 229 deletions
|
|
@ -197,6 +197,10 @@ impl<T,C> Traverse<C> for IdNode<T> where C: Allocates<Self> {
|
|||
IdNode::traverse(next,context)
|
||||
}
|
||||
}
|
||||
|
||||
fn collect(self) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
|
|||
|
|
@ -1,2 +1,10 @@
|
|||
pub mod gc;
|
||||
pub mod map;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn simple() {
|
||||
|
||||
}
|
||||
}
|
||||
194
src/map.rs
194
src/map.rs
|
|
@ -18,8 +18,9 @@ Notes:
|
|||
- Values would only need to be shuffled around closer to their 'home' during a resize.
|
||||
- It is unlikely the displacement would large value without a resize amending that problem.
|
||||
*/
|
||||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
use crate::gc::{AnyId, Id};
|
||||
use std::fmt::Debug;
|
||||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
|
||||
type Displacement = u8;
|
||||
|
||||
|
|
@ -70,70 +71,71 @@ impl Hashes for AnyId {
|
|||
}
|
||||
|
||||
pub trait TableEntry: Equivalent<Self> + Hashes {
|
||||
fn new_vacant() -> Self;
|
||||
const VACANT: Self;
|
||||
fn is_vacant(&self) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Entry<E> {
|
||||
#[derive(Debug)]
|
||||
pub struct Entry<E: TableEntry> {
|
||||
pub entry: E,
|
||||
home: usize,
|
||||
displacement: Displacement,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Map<E: TableEntry + Clone> {
|
||||
impl<E: TableEntry> Entry<E> {
|
||||
const VACANT: Entry<E> = Entry {
|
||||
entry: E::VACANT,
|
||||
home: 0,
|
||||
displacement: 0,
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DisplacementSet<E: TableEntry> {
|
||||
// default representing vacancy!
|
||||
pub map: Vec<Entry<E>>, // todo: make this private and use an iterator
|
||||
map_bounds: std::ops::Range<usize>,
|
||||
map_count: usize, // number of elements in table
|
||||
}
|
||||
|
||||
impl<E: TableEntry + Clone> Map<E> {
|
||||
fn exchange_table(&mut self, len: usize) {
|
||||
impl<E: TableEntry> DisplacementSet<E> {
|
||||
fn exchange(&mut self, len: usize) {
|
||||
let old = std::mem::replace(
|
||||
&mut self.map,
|
||||
vec![
|
||||
Entry {
|
||||
entry: E::new_vacant(),
|
||||
home: 0,
|
||||
displacement: 0,
|
||||
};
|
||||
len
|
||||
],
|
||||
(0..len).map(|_| Entry::VACANT).collect(), // todo: I don't like that this may go over all memory
|
||||
);
|
||||
self.map_bounds = self.table_lower()..self.table_upper();
|
||||
self.map_bounds = self.lower()..self.upper();
|
||||
for Entry { entry, .. } in old {
|
||||
if !entry.is_vacant() {
|
||||
self.set_table(entry)
|
||||
self.set(entry)
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn resize_table(&mut self, len: usize) {
|
||||
self.exchange_table(len.max(self.map_count + (self.map_count / 3)));
|
||||
self.map_bounds = 0..self.table_upper();
|
||||
pub fn resize(&mut self, len: usize) {
|
||||
self.exchange(len.max(self.map_count + (self.map_count / 3)));
|
||||
self.map_bounds = 0..self.upper();
|
||||
}
|
||||
fn table_upper(&self) -> usize {
|
||||
fn upper(&self) -> usize {
|
||||
(self.map.len() / 4) * 3
|
||||
}
|
||||
fn table_lower(&self) -> usize {
|
||||
fn lower(&self) -> usize {
|
||||
self.map.len() / 3
|
||||
}
|
||||
fn ensure_table(&mut self) {
|
||||
if self.map_count + 1 > self.table_upper() {
|
||||
fn ensure(&mut self) {
|
||||
if self.map_count + 1 > self.upper() {
|
||||
// free space is short
|
||||
self.exchange_table((self.map.len() + 4) * 2);
|
||||
} else if self.map_count < self.table_lower() {
|
||||
self.exchange((self.map.len() + 4) * 2);
|
||||
} else if self.map_count < self.lower() {
|
||||
// too much free space
|
||||
self.exchange_table(self.map.len() / 2)
|
||||
self.exchange(self.map.len() / 2)
|
||||
}
|
||||
}
|
||||
pub fn set_table(&mut self, entry: E) {
|
||||
pub fn set(&mut self, entry: E) {
|
||||
#[cfg(feature = "assertions")]
|
||||
assert!(!entry.is_vacant());
|
||||
if !self.map_bounds.contains(&(self.map_count + 1)) {
|
||||
// make sure the table is appropriately sized
|
||||
self.ensure_table();
|
||||
self.ensure();
|
||||
}
|
||||
let range = self.map.len();
|
||||
let home = entry.hashed() as usize % range;
|
||||
|
|
@ -197,11 +199,11 @@ impl<E: TableEntry + Clone> Map<E> {
|
|||
// must resize
|
||||
#[cfg(feature = "messages")]
|
||||
eprintln!("\n\tResizing...");
|
||||
self.resize_table((self.map.len() + 4) * 2);
|
||||
self.set_table(entry);
|
||||
self.resize((self.map.len() + 4) * 2);
|
||||
self.set(entry);
|
||||
}
|
||||
}
|
||||
pub fn rem_table<K>(&mut self, index: K)
|
||||
pub fn remove<K>(&mut self, index: K)
|
||||
where
|
||||
E: Equivalent<K>,
|
||||
K: Hashes,
|
||||
|
|
@ -212,7 +214,7 @@ impl<E: TableEntry + Clone> Map<E> {
|
|||
let range = self.map.len();
|
||||
let home = index.hashed() as usize % range;
|
||||
if self.map[home].entry.matches(&index) {
|
||||
self.map[home].entry = E::new_vacant();
|
||||
self.map[home].entry = E::VACANT;
|
||||
self.map_count -= 1;
|
||||
} else {
|
||||
if self.map[home].displacement != Displacement::MAX {
|
||||
|
|
@ -220,7 +222,7 @@ impl<E: TableEntry + Clone> Map<E> {
|
|||
for i in 1..self.map[home].displacement {
|
||||
let neighbour = &mut self.map[(home + i as usize) % range]; // todo: maybe mod slows this down
|
||||
if neighbour.entry.matches(&index) {
|
||||
neighbour.entry = E::new_vacant();
|
||||
neighbour.entry = E::VACANT;
|
||||
self.map_count -= 1;
|
||||
if i == self.map[home].displacement {
|
||||
self.map[home].displacement = largest
|
||||
|
|
@ -237,7 +239,7 @@ impl<E: TableEntry + Clone> Map<E> {
|
|||
for k in 0..self.map.len() {
|
||||
let neighbour = &mut self.map[k];
|
||||
if neighbour.entry.matches(&index) {
|
||||
neighbour.entry = E::new_vacant();
|
||||
neighbour.entry = E::VACANT;
|
||||
self.map_count -= 1;
|
||||
#[cfg(feature = "assertions")]
|
||||
{
|
||||
|
|
@ -261,7 +263,7 @@ impl<E: TableEntry + Clone> Map<E> {
|
|||
}
|
||||
}
|
||||
}
|
||||
pub fn get_table<K>(&mut self, index: K) -> Option<&E>
|
||||
pub fn get<K>(&mut self, index: K) -> Option<&E>
|
||||
where
|
||||
E: Equivalent<K>,
|
||||
K: Hashes,
|
||||
|
|
@ -283,8 +285,8 @@ impl<E: TableEntry + Clone> Map<E> {
|
|||
None
|
||||
}
|
||||
}
|
||||
pub fn new() -> Map<E> {
|
||||
Map {
|
||||
pub fn new() -> DisplacementSet<E> {
|
||||
DisplacementSet {
|
||||
map: Vec::new(),
|
||||
map_count: 0,
|
||||
map_bounds: (0..0).into(),
|
||||
|
|
@ -292,6 +294,116 @@ impl<E: TableEntry + Clone> Map<E> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct TableEntry
|
||||
#[derive(Clone)]
|
||||
pub struct MapEntry<K: Hashes + PartialEq, V>(pub Option<(K, V)>);
|
||||
|
||||
pub type Table<K,T> = Map<>
|
||||
impl<K: PartialEq + Hashes, V> Equivalent<K> for MapEntry<K, V> {
|
||||
fn matches(&self, other: &K) -> bool {
|
||||
&self.0.as_ref().unwrap().0 == other
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Hashes + PartialEq, V> Hashes for MapEntry<K, V> {
|
||||
fn hashed(&self) -> Hashed {
|
||||
self.0.as_ref().unwrap().0.hashed()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Hashes + PartialEq, V> PartialEq for MapEntry<K, V> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0.as_ref().unwrap().0 == other.0.as_ref().unwrap().0
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Hashes + PartialEq, V> TableEntry for MapEntry<K, V> {
|
||||
const VACANT: Self = MapEntry(None);
|
||||
fn is_vacant(&self) -> bool {
|
||||
self.0.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Map<K: Hashes + PartialEq, V>(pub DisplacementSet<MapEntry<K, V>>);
|
||||
|
||||
impl<K: Hashes + PartialEq, V> Map<K, V> {
|
||||
pub fn resize(&mut self, len: usize) {
|
||||
self.0.resize(len)
|
||||
}
|
||||
pub fn set(&mut self, index: K, value: V) {
|
||||
self.0.set(MapEntry(Some((index, value))));
|
||||
}
|
||||
pub fn remove(&mut self, index: K) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
pub fn get(&mut self, index: K) -> Option<&V> {
|
||||
if let Some(ref entry) = self.0.get(index) {
|
||||
Some(&entry.0.as_ref().unwrap().1)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn new() -> Map<K, V> {
|
||||
Map(DisplacementSet::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Debug + Hashes + PartialEq, V: Debug> Debug for Map<K, V> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_fmt(format_args!("Map")) // todo: debug and print the entire thing?
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub struct SetEntry<E: Hashes + PartialEq>(Option<E>);
|
||||
|
||||
pub struct Set<E: Hashes + PartialEq>(DisplacementSet<SetEntry<E>>);
|
||||
|
||||
impl<E: Hashes + PartialEq> Hashes for SetEntry<E> {
|
||||
fn hashed(&self) -> Hashed {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Hashes + PartialEq> TableEntry for SetEntry<E> {
|
||||
const VACANT: Self = SetEntry(None);
|
||||
fn is_vacant(&self) -> bool {
|
||||
self.0.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Hashes + PartialEq> Equivalent<E> for SetEntry<E> {
|
||||
fn matches(&self, other: &E) -> bool {
|
||||
self.0.as_ref().unwrap() == other
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, E: Hashes + PartialEq + Equivalent<K>> Equivalent<K> for SetEntry<E> {
|
||||
fn matches(&self, other: &K) -> bool {
|
||||
self.0.unwrap().matches(other)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Hashes + PartialEq> Set<E> {
|
||||
pub fn resize(&mut self, len: usize) {
|
||||
self.0.resize(len)
|
||||
}
|
||||
pub fn set(&mut self, element: E) {
|
||||
self.0.set(SetEntry(Some(element)));
|
||||
}
|
||||
pub fn remove(&mut self, element: E) {
|
||||
self.0.remove(element);
|
||||
}
|
||||
pub fn get<K>(&mut self, element: K) -> Option<&E>
|
||||
where
|
||||
E: Equivalent<K>,
|
||||
K: Hashes,
|
||||
{
|
||||
if let Some(ref entry) = self.0.get(element) {
|
||||
Some(&entry.0.as_ref().unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
pub fn new() -> Set<E> {
|
||||
Set(DisplacementSet::new())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue