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)
|
IdNode::traverse(next,context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn collect(self) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,10 @@
|
||||||
pub mod gc;
|
pub mod gc;
|
||||||
pub mod map;
|
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.
|
- 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.
|
- 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 crate::gc::{AnyId, Id};
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||||
|
|
||||||
type Displacement = u8;
|
type Displacement = u8;
|
||||||
|
|
||||||
|
|
@ -70,70 +71,71 @@ impl Hashes for AnyId {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait TableEntry: Equivalent<Self> + Hashes {
|
pub trait TableEntry: Equivalent<Self> + Hashes {
|
||||||
fn new_vacant() -> Self;
|
const VACANT: Self;
|
||||||
fn is_vacant(&self) -> bool;
|
fn is_vacant(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug)]
|
||||||
pub struct Entry<E> {
|
pub struct Entry<E: TableEntry> {
|
||||||
pub entry: E,
|
pub entry: E,
|
||||||
home: usize,
|
home: usize,
|
||||||
displacement: Displacement,
|
displacement: Displacement,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
impl<E: TableEntry> Entry<E> {
|
||||||
pub struct Map<E: TableEntry + Clone> {
|
const VACANT: Entry<E> = Entry {
|
||||||
|
entry: E::VACANT,
|
||||||
|
home: 0,
|
||||||
|
displacement: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct DisplacementSet<E: TableEntry> {
|
||||||
// default representing vacancy!
|
// default representing vacancy!
|
||||||
pub map: Vec<Entry<E>>, // todo: make this private and use an iterator
|
pub map: Vec<Entry<E>>, // todo: make this private and use an iterator
|
||||||
map_bounds: std::ops::Range<usize>,
|
map_bounds: std::ops::Range<usize>,
|
||||||
map_count: usize, // number of elements in table
|
map_count: usize, // number of elements in table
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: TableEntry + Clone> Map<E> {
|
impl<E: TableEntry> DisplacementSet<E> {
|
||||||
fn exchange_table(&mut self, len: usize) {
|
fn exchange(&mut self, len: usize) {
|
||||||
let old = std::mem::replace(
|
let old = std::mem::replace(
|
||||||
&mut self.map,
|
&mut self.map,
|
||||||
vec![
|
(0..len).map(|_| Entry::VACANT).collect(), // todo: I don't like that this may go over all memory
|
||||||
Entry {
|
|
||||||
entry: E::new_vacant(),
|
|
||||||
home: 0,
|
|
||||||
displacement: 0,
|
|
||||||
};
|
|
||||||
len
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
self.map_bounds = self.table_lower()..self.table_upper();
|
self.map_bounds = self.lower()..self.upper();
|
||||||
for Entry { entry, .. } in old {
|
for Entry { entry, .. } in old {
|
||||||
if !entry.is_vacant() {
|
if !entry.is_vacant() {
|
||||||
self.set_table(entry)
|
self.set(entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn resize_table(&mut self, len: usize) {
|
pub fn resize(&mut self, len: usize) {
|
||||||
self.exchange_table(len.max(self.map_count + (self.map_count / 3)));
|
self.exchange(len.max(self.map_count + (self.map_count / 3)));
|
||||||
self.map_bounds = 0..self.table_upper();
|
self.map_bounds = 0..self.upper();
|
||||||
}
|
}
|
||||||
fn table_upper(&self) -> usize {
|
fn upper(&self) -> usize {
|
||||||
(self.map.len() / 4) * 3
|
(self.map.len() / 4) * 3
|
||||||
}
|
}
|
||||||
fn table_lower(&self) -> usize {
|
fn lower(&self) -> usize {
|
||||||
self.map.len() / 3
|
self.map.len() / 3
|
||||||
}
|
}
|
||||||
fn ensure_table(&mut self) {
|
fn ensure(&mut self) {
|
||||||
if self.map_count + 1 > self.table_upper() {
|
if self.map_count + 1 > self.upper() {
|
||||||
// free space is short
|
// free space is short
|
||||||
self.exchange_table((self.map.len() + 4) * 2);
|
self.exchange((self.map.len() + 4) * 2);
|
||||||
} else if self.map_count < self.table_lower() {
|
} else if self.map_count < self.lower() {
|
||||||
// too much free space
|
// 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")]
|
#[cfg(feature = "assertions")]
|
||||||
assert!(!entry.is_vacant());
|
assert!(!entry.is_vacant());
|
||||||
if !self.map_bounds.contains(&(self.map_count + 1)) {
|
if !self.map_bounds.contains(&(self.map_count + 1)) {
|
||||||
// make sure the table is appropriately sized
|
// make sure the table is appropriately sized
|
||||||
self.ensure_table();
|
self.ensure();
|
||||||
}
|
}
|
||||||
let range = self.map.len();
|
let range = self.map.len();
|
||||||
let home = entry.hashed() as usize % range;
|
let home = entry.hashed() as usize % range;
|
||||||
|
|
@ -197,11 +199,11 @@ impl<E: TableEntry + Clone> Map<E> {
|
||||||
// must resize
|
// must resize
|
||||||
#[cfg(feature = "messages")]
|
#[cfg(feature = "messages")]
|
||||||
eprintln!("\n\tResizing...");
|
eprintln!("\n\tResizing...");
|
||||||
self.resize_table((self.map.len() + 4) * 2);
|
self.resize((self.map.len() + 4) * 2);
|
||||||
self.set_table(entry);
|
self.set(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn rem_table<K>(&mut self, index: K)
|
pub fn remove<K>(&mut self, index: K)
|
||||||
where
|
where
|
||||||
E: Equivalent<K>,
|
E: Equivalent<K>,
|
||||||
K: Hashes,
|
K: Hashes,
|
||||||
|
|
@ -212,7 +214,7 @@ impl<E: TableEntry + Clone> Map<E> {
|
||||||
let range = self.map.len();
|
let range = self.map.len();
|
||||||
let home = index.hashed() as usize % range;
|
let home = index.hashed() as usize % range;
|
||||||
if self.map[home].entry.matches(&index) {
|
if self.map[home].entry.matches(&index) {
|
||||||
self.map[home].entry = E::new_vacant();
|
self.map[home].entry = E::VACANT;
|
||||||
self.map_count -= 1;
|
self.map_count -= 1;
|
||||||
} else {
|
} else {
|
||||||
if self.map[home].displacement != Displacement::MAX {
|
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 {
|
for i in 1..self.map[home].displacement {
|
||||||
let neighbour = &mut self.map[(home + i as usize) % range]; // todo: maybe mod slows this down
|
let neighbour = &mut self.map[(home + i as usize) % range]; // todo: maybe mod slows this down
|
||||||
if neighbour.entry.matches(&index) {
|
if neighbour.entry.matches(&index) {
|
||||||
neighbour.entry = E::new_vacant();
|
neighbour.entry = E::VACANT;
|
||||||
self.map_count -= 1;
|
self.map_count -= 1;
|
||||||
if i == self.map[home].displacement {
|
if i == self.map[home].displacement {
|
||||||
self.map[home].displacement = largest
|
self.map[home].displacement = largest
|
||||||
|
|
@ -237,7 +239,7 @@ impl<E: TableEntry + Clone> Map<E> {
|
||||||
for k in 0..self.map.len() {
|
for k in 0..self.map.len() {
|
||||||
let neighbour = &mut self.map[k];
|
let neighbour = &mut self.map[k];
|
||||||
if neighbour.entry.matches(&index) {
|
if neighbour.entry.matches(&index) {
|
||||||
neighbour.entry = E::new_vacant();
|
neighbour.entry = E::VACANT;
|
||||||
self.map_count -= 1;
|
self.map_count -= 1;
|
||||||
#[cfg(feature = "assertions")]
|
#[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
|
where
|
||||||
E: Equivalent<K>,
|
E: Equivalent<K>,
|
||||||
K: Hashes,
|
K: Hashes,
|
||||||
|
|
@ -283,8 +285,8 @@ impl<E: TableEntry + Clone> Map<E> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn new() -> Map<E> {
|
pub fn new() -> DisplacementSet<E> {
|
||||||
Map {
|
DisplacementSet {
|
||||||
map: Vec::new(),
|
map: Vec::new(),
|
||||||
map_count: 0,
|
map_count: 0,
|
||||||
map_bounds: (0..0).into(),
|
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