summaryrefslogtreecommitdiff
path: root/src/library/scalax/collection/immutable/DefaultSet.scala
blob: a9ce3fb3df593573f82ab003271313141984c61b (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
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2003-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id: HashSet.scala 16884 2009-01-09 16:52:09Z cunei $

package scalax.collection.immutable

import generic.SetTemplate

/** A default implementation of immutable sets.
 *  This is currently implemented as a proxy for an immutable HashSet,
 *  except that its builder returns specialized representations EmptySet,Set1,..., Set4
 *  for sets of size <= 4.
 */
class DefaultSet[A] private (hset: HashSet[A])
  extends Set[A]
     with SetTemplate[Set, A] {

  def this() = this(new HashSet[A])

  def contains(elem: A): Boolean = hset.contains(elem)

  def + (elem: A): Set[A] = hset + elem

  /** Keeps underlying HashSet representation, but switches back to EmptySet if
   *  result does not contain any elements
   */
  def - (elem: A): Set[A] = {
    val hset1 = hset - elem
    if (hset1.isEmpty) new EmptySet[A]
    else new DefaultSet(hset)
  }

  def size: Int = hset.size

  def elements: Iterator[A] = hset.elements

  override def foreach(f: A => Unit): Unit = hset.foreach(f)
}