Skip to content

Instantly share code, notes, and snippets.

@ArcticLight
Last active August 29, 2015 14:07
Show Gist options
  • Save ArcticLight/23a9a1b3279692bb6590 to your computer and use it in GitHub Desktop.
Save ArcticLight/23a9a1b3279692bb6590 to your computer and use it in GitHub Desktop.
ClobberTable for Syntax Tables
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;
}
}
@hawkw
Copy link

hawkw commented Oct 10, 2014

        // ... snip ...

    public ClobberTable(parent = null) { //Java supports this, right?
        this.parent = parent
        my = new HashTable<K, T>();
    }

        // ... snip ...

    public ClobberTable<K, T> fork() {
        ClobberTable child = new ClobberTable<K, T>(parent=this);
        return child;
    }
}

@hawkw
Copy link

hawkw commented Oct 10, 2014

Pretty neat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment