Skip to content

Instantly share code, notes, and snippets.

@okomok
Created February 4, 2009 18:08
Show Gist options
  • Save okomok/58241 to your computer and use it in GitHub Desktop.
Save okomok/58241 to your computer and use it in GitHub Desktop.
rejected memoize
private[mada] object Memoize {
def apply[A](p: Peg[A]): Peg[A] = new MemoizePeg(p)
}
private[mada] class MemoizePeg[A](override val self: Peg[A]) extends PegProxy[A] {
val memoTable = new scala.collection.jcl.HashMap[TripleKey[A], Int]
override def parse(v: Vector[A], start: Int, end: Int) = {
val key = new TripleKey(v, start, end)
val value = memoTable.get(key)
if (value.isEmpty) {
val cur = self.parse(v, start, end)
memoTable.put(key, cur)
cur
} else {
value.get
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment