Is there a more efficient `Set<Char>` implem...
# stdlib
r
Is there a more efficient
Set<Char>
implementation than
setOf(' ', '\n', ...)
for checking if an input character is in a particular set of characters?
h
I don't think so. Only if your "particular set" exactly matches a standard character class, the builtins like
isWhitespace()
might be better.
m
Copy code
val chars = "abcd".toCharArray().sortedArray()
val acceptable = chars.binarySearch('x') < 0
👍 3
r
Nice, thanks.
y
If it's from a IO source, okio has an options mechanism.
m
I should note that I interpreted your request as for a more memory efficient implementation. There are more efficient ways to do it than that if you can sacrifice a bit of memory, e.g. just associate a 64k bitmap and do bit testing (with chars outside the BMP in a side table or something).
Or even linear scan. The best way depends a lot on the details of how big your sets are, what resource you want to optimize etc.
👍 1
e
cf. https://javadoc.io/doc/com.google.guava/guava/latest/com/google/common/base/CharMatcher.html which has a number of different implementations for size=0, size=1, size=2, size<1024 with a custom primitive hashmap, or
java.util.BitSet
for everything else
👍 1
m
@ephemient nice, thanks for that! I'd forgotten Guava had a CharMatcher. Since starting to use Kotlin I've hardly used Guava but it's got some nice stuff in it that's probably too bulky for the Kotlin stdlib.
r
That is nice, thanks!