Created
February 4, 2009 18:08
-
-
Save okomok/58241 to your computer and use it in GitHub Desktop.
rejected memoize
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
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