blob: af02c455b8bc74bca54033e4123117e355fe2af1 (
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
55
56
57
58
59
60
61
62
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2005-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.collection
package mutable
import generic._
/** Todo: this has O(n) cost for element removal.
* Should be rewritten to be more efficient.
* @since 2.2
*/
@serializable
class LinkedHashSet[A] extends Set[A]
with GenericSetTemplate[A, LinkedHashSet]
with SetLike[A, LinkedHashSet[A]]
with FlatHashTable[A]
{
override def companion: GenericCompanion[LinkedHashSet] = LinkedHashSet
protected val ordered = new ListBuffer[A]
override def size = super.size
def contains(elem: A): Boolean = containsEntry(elem)
def += (elem: A): this.type = { add(elem); this }
def -= (elem: A): this.type = { remove(elem); this }
override def add(elem: A): Boolean =
if (addEntry(elem)) { ordered += elem; true }
else false
override def remove(elem: A): Boolean =
removeEntry(elem) match {
case None => false
case _ => ordered -= elem; true
}
override def clear() {
ordered.clear()
super.clear()
}
override def iterator = ordered.iterator
override def foreach[U](f: A => U) = ordered foreach f
}
/** Factory object for `LinkedHashSet` class */
object LinkedHashSet extends SetFactory[LinkedHashSet] {
implicit def builderFactory[A]: BuilderFactory[A, LinkedHashSet[A], Coll] = setBuilderFactory[A]
override def empty[A]: LinkedHashSet[A] = new LinkedHashSet[A]
}
|