Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 38 lines (36 sloc) 1.493 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/* sbt -- Simple Build Tool
* Copyright 2009, 2010 Mark Harrah
*/
package xsbt.boot

import Pre._
import scala.collection.{Iterable, Iterator}
import scala.collection.immutable.List

// preserves iteration order
sealed class ListMap[K,V] private(backing: List[(K,V)]) extends Iterable[(K,V)] // use Iterable because Traversable.toStream loops
{
import ListMap.remove
def update(k: K, v: V) = this.+( (k,v) )
def +(pair: (K,V)) = copy(pair :: remove(backing,pair._1))
def -(k: K) = copy(remove(backing,k))
def get(k: K): Option[V] = backing.find(_._1 == k).map(_._2)
def keys: List[K] = backing.reverse.map(_._1)
def apply(k: K): V = getOrError(get(k), "Key " + k + " not found")
def contains(k: K): Boolean = get(k).isDefined
def iterator = backing.reverse.iterator
override def isEmpty: Boolean = backing.isEmpty
override def toList = backing.reverse
override def toSeq = toList
protected def copy(newBacking: List[(K,V)]): ListMap[K,V] = new ListMap(newBacking)
def default(defaultF: K => V): ListMap[K,V] =
new ListMap[K,V](backing) {
override def apply(k: K) = super.get(k).getOrElse(defaultF(k))
override def copy(newBacking: List[(K,V)]) = super.copy(newBacking).default(defaultF)
}
override def toString = backing.mkString("ListMap(",",",")")
}
object ListMap
{
def apply[K,V](pairs: (K,V)*) = new ListMap[K,V](pairs.toList.distinct)
def empty[K,V] = new ListMap[K,V](Nil)
private def remove[K,V](backing: List[(K,V)], k: K) = backing.filter(_._1 != k)
}
Something went wrong with that request. Please try again.