blob: 481c8667dca60e8d01d90a49faeead612e6187bc (
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
50
51
52
53
54
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2005-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection.mutable
import generic._
//!!! todo make inherit from HashSet.
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] ++ collection.Iterable.fromOld(elems)
}
@serializable
class LinkedHashSet[A] extends Set[A] with MutableSetTemplate[A, LinkedHashSet[A]] with FlatHashTable[A] {
override def empty = LinkedHashSet.empty
override def size = super.size
private var ordered = List[A]()
def contains(elem: A): Boolean = containsEntry(elem)
def put(elem: A): Boolean =
if (addEntry(elem)) {
ordered = elem :: ordered
true
} else false
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 elements = Iterator.fromOld(ordered.reverse.elements)
}
|