Skip to content

Instantly share code, notes, and snippets.

@schmmd
Created November 16, 2011 06:41
Show Gist options
  • Save schmmd/1369439 to your computer and use it in GitHub Desktop.
Save schmmd/1369439 to your computer and use it in GitHub Desktop.
Trie
import collection.mutable.SetLike
class Trie[A] extends Set[List[A]] {
type S = List[A]
type This = Trie[A]
sealed abstract class AbstractLayer
case class Layer(val map: Map[A, Layer]) extends AbstractLayer
case class Entry(val seq: S) extends AbstractLayer
case object Empty extends AbstractLayer
var layers: AbstractLayer = Empty
def contains(elem: S): Boolean = {
def contains(layer: AbstractLayer, elem: S): Boolean = elem match {
case head :: tail =>
layer match {
case Layer(map) => map.get(head).map(contains(_, tail)).getOrElse(false)
case Entry(seq) => true
case Empty => false
}
case Nil => false
}
contains(layers, elem)
}
def iterator: Iterator[S] = null
def += (elem: S): this.type = this
def -= (elem: S): this.type = this
def + (elem: S): This = this
def - (elem: S): This = this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment