Skip to content

Instantly share code, notes, and snippets.

@riking
Last active July 15, 2020 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riking/3e2aafc6315e10b1e296e0f0ad2688e3 to your computer and use it in GitHub Desktop.
Save riking/3e2aafc6315e10b1e296e0f0ad2688e3 to your computer and use it in GitHub Desktop.
hash array mapped table immutable overlay node
enum HAMTOverlay<K, V, Hash> {
Empty(),
FullTable(&[Self; 32]),
SingleKey(Hash::Hash, K, V, Rc<Self>),
SingleSubtree(Hash::Hash, Rc<Self>, Rc<Self>),
}
impl ... for SingleKey {
fn lookupWithHash(&self, k: K, h: Hash::Hash) -> Option<V> {
if h == self.0 && k == self.1 {
Some(self.2)
} else {
self.3.lookupWithHash(k, h)
}
}
}
impl ... for SingleSubtree {
fn lookupWithHash(&self, k: K, h: Hash::Hash) -> Option<V> {
if hashPrefixMatches(self.0, h) {
self.1.lookupWithHash(k, removeHashPrefix(h))
} else {
self.2.lookupWithHash(k, h)
}
}
}
impl ... for FullTable {
fn lookupWithHash(&self, k: K, h: hash::Hash) -> Option<V> {
self.0[getHashPrefix(h)].lookupWithHash(k, removeHashPrefix(h))
}
}
impl ... for Empty {
fn lookupWithHash(&self, k: K, h: hash::Hash) -> Option<V> {
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment