Last active
August 29, 2015 14:07
-
-
Save ArcticLight/23a9a1b3279692bb6590 to your computer and use it in GitHub Desktop.
ClobberTable for Syntax Tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClobberTable<K, T> { | |
ClobberTable parent; | |
HashTable<K, T> my; | |
ArrayList<K> whites; | |
public ClobberTable() { | |
parent = null; | |
my = new HashTable<K, T>(); | |
whites = new ArrayList<K>(); | |
} | |
public void put(K a, T b) { | |
my.put(a,b); | |
if(whites.contains(a)) whites.remove(a); | |
} | |
public T get(K a) { | |
if(whites.contains(a)) return null; | |
if(my.hasKey(a)) { return my.get(a); } | |
if(parent != null && parent.hasKey(a)) return parent.get(a); | |
return null; | |
} | |
public void remove(K a) { | |
if(my.contains(a)) { | |
my.remove(a); | |
} | |
else if (parent.contains(a)) { | |
whites.add(a); | |
} | |
} | |
public boolean hasKey() { | |
if(my.hasKey(a) || (parent != null && parent.hasKey(a))) return true; | |
} | |
public ClobberTable<K, T> fork() { | |
ClobberTable child = new ClobberTable<K, T>(); | |
child.parent = this; | |
return child; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty neat.