You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current implementation of mutable.LinkedHashSet uses a ListBuffer as a companion of a standard HashSet. Consequently, a call to remove() actually calls both HashSet and ListBuffer's remove() methods, ListBuffer's being in O(n ).
An full O(1) implementation is expected and doable (for example, by storing double-linked cells in the HashSet, but something even better might exists).
I have not checked yet whether the immutable version is also affected.
The text was updated successfully, but these errors were encountered:
Julien Vion (scand1sk) said (edited on May 24, 2011 1:43:48 PM UTC):
Here is a tentative implementation, which can probably be made much better (by avoiding the instantiation of a wrapper object on every Set operation, for instance).
{code:title=MyLinkedHashSet.scala}
package scala.collection
package mutable
import collection.generic._
class MyLinkedHashSet[A] extends Set[A]
with GenericSetTemplate[A, MyLinkedHashSet]
with SetLike[A, MyLinkedHashSet[A]] {
override def companion: GenericCompanion[MyLinkedHashSet] = MyLinkedHashSet
def iterator = new Iterator[A] {
var current = firstCell
def hasNext = current != null
def next = {
val ret = current;
current = current.next;
ret.content;
}
}
override def foreach[U](f: A => U) = iterator foreach f
}
class Cell[A](val content: A) {
var next: Cell[A] = null;
var prev: Cell[A] = null;
override val hashCode = content.hashCode
override def equals(obj: Any) = obj match {
case c: Cell[A] => content == c.content
case _ => false
}
}
Current implementation of mutable.LinkedHashSet uses a ListBuffer as a companion of a standard HashSet. Consequently, a call to remove() actually calls both HashSet and ListBuffer's remove() methods, ListBuffer's being in O(n ).
An full O(1) implementation is expected and doable (for example, by storing double-linked cells in the HashSet, but something even better might exists).
I have not checked yet whether the immutable version is also affected.
The text was updated successfully, but these errors were encountered: