blob: d4962f34cb0c91268b36edcdfc80f1ed2cbd2bb4 (
plain) (
blame)
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
39
40
41
42
43
44
45
46
47
48
49
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2005-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection.mutable
object LinkedHashSet {
/** The empty map of this type */
def empty[A] = new LinkedHashSet[A]
/** The canonical factory for this type
*/
def apply[A](elems: A*) = empty[A] ++ elems
}
@serializable
class LinkedHashSet[A] extends Set[A] with FlatHashTable[A] {
private var ordered = List[A]()
def contains(elem: A): Boolean = containsEntry(elem)
def +=(elem: A) { add(elem) }
def add(elem: A): Boolean = {
if (addEntry(elem)) {
ordered = elem :: ordered
true
} else false
}
def -=(elem: A) { remove(elem) }
def remove(elem: A) : Boolean = removeEntry(elem) match {
case None => false
case Some(elem) => ordered = ordered.filter(_ != elem); true
}
override def clear() {
ordered = Nil
super.clear()
}
override def clone(): Set[A] = new LinkedHashSet[A] ++ this
override def elements = ordered.reverse.elements
}
|