eygraber
09/06/2018, 4:28 AMpublic operator fun <T> Set<T>.minus(elements: Iterable<T>): Set<T> {
val other = elements.convertToSetForSetOperationWith(this)
if (other.isEmpty())
return this.toSet()
if (other is Set)
return this.filterNotTo(LinkedHashSet<T>()) { it in other }
val result = LinkedHashSet<T>(this)
result.removeAll(other)
return result
}
My request is that an initial capacity get added to the LinkedHashSet
that is returned if other
is a Set
. I know there's no way to know what the value would be, but it could certainly be approximated at better than the default by doing LinkedHashSet<T>(this.size - other.size)
. That way, in the "best case" where everything in other
is in the set, the capacity will be correct, and in the "worst case", where nothing in other
is in the set, the capacity will have to grow to the size of the set, but we'll likely be closer to that than the default (16).
Am I missing something / is this not necessary, or is this worth filing a bug over?karelpeeters
09/06/2018, 5:54 AMgildor
09/06/2018, 6:15 AMeygraber
09/06/2018, 7:35 AMother.size > this.size
, if this
and other
are large, and most of this
ends up getting filtered out there'll be a lot of wasted memory, etc...