Package com.github.tommyettinger.ds
Class CaseInsensitiveSet
- All Implemented Interfaces:
EnhancedCollection<CharSequence>,Iterable<CharSequence>,Collection<CharSequence>,Set<CharSequence>
A custom variant on ObjectSet that always uses CharSequence keys and compares them as case-insensitive.
This uses a fairly complex, quite-optimized hashing function because it needs to hash CharSequences rather
often, and to do so ignoring case means
This is very similar to
String.hashCode() won't work, plus not all CharSequences
implement hashCode() themselves (such as StringBuilder). User code similar to this can often get away
with a simple polynomial hash (the typical Java kind, used by String and Arrays), or if more speed is needed,
one with some of these
optimizations by Richard Startin. If you don't want to write or benchmark a hash function (which is quite
understandable), Utilities.hashCodeIgnoreCase(CharSequence) can get a case-insensitive hash of any
CharSequence, as a long. It does this without allocating new Strings all over, where many case-insensitive
algorithms do allocate quite a lot, but it does this by handling case incorrectly for the Georgian alphabet.
If I see Georgian text in-the-wild, I may reconsider, but I don't think that particular alphabet is in
widespread use. There's also Utilities.equalsIgnoreCase(CharSequence, CharSequence) for equality
comparisons that are similarly case-insensitive, except for Georgian.
This is very similar to
FilteredStringSet when its editor
is Character.toUpperCase(char) or Casing.caseUp(char).
FilteredStringSet works with Strings rather than CharSequences, which
may be more convenient, and allows filtering some characters out of hashing and equality comparisons. If you want a
case-insensitive set that ignores any non-letter characters in a String, then CaseInsensitiveSet won't do,
but new FilteredStringSet<>(CharPredicates.IS_LETTER, Casing::caseUp will work. Note that GWT only
handles Character.isLetter(char) for ASCII letters; CharPredicates in this library provides cross-platform
predicates that use CharBitSet to store their data, and the library RegExodus offers replacements in
Category for other Unicode categories, such as upper-case letters, currency symbols, decimal digits, and so on.-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectSet
ObjectSet.ObjectSetIterator<T> -
Field Summary
Fields inherited from class com.github.tommyettinger.ds.ObjectSet
hashMultiplier, iterator1, iterator2, keyTable, loadFactor, mask, shift, size, threshold -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new set with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().CaseInsensitiveSet(int initialCapacity) Creates a new set with the specified initial capacity a load factor ofUtilities.getDefaultLoadFactor().CaseInsensitiveSet(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor.Creates a new set that contains all distinct elements inset.CaseInsensitiveSet(ObjectSet<? extends CharSequence> set) Creates a new set identical to the specified set.CaseInsensitiveSet(CharSequence[] array) Creates a new set containing all the items in the given array.CaseInsensitiveSet(CharSequence[] array, int offset, int length) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).CaseInsensitiveSet(Collection<? extends CharSequence> coll) Creates a new set that contains all distinct elements incoll.CaseInsensitiveSet(Iterator<? extends CharSequence> coll) Creates a new instance containing the items in the specified iterator. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal.inthashCode()static CaseInsensitiveSetCallsObjectSet.parse(String, String, PartialParser, boolean)with brackets set to false.static CaseInsensitiveSetCreates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static CaseInsensitiveSetCreates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.protected intReturns an index >= 0 and <=ObjectSet.maskfor the specifieditem, mixed.protected voidresize(int newSize) static CaseInsensitiveSetwith()Constructs an empty set.static CaseInsensitiveSetwith(CharSequence item) Creates a new CaseInsensitiveSet that holds only the given item, but can be resized.static CaseInsensitiveSetwith(CharSequence... varargs) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.static CaseInsensitiveSetwith(CharSequence item0, CharSequence item1) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.static CaseInsensitiveSetwith(CharSequence item0, CharSequence item1, CharSequence item2) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.static CaseInsensitiveSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.static CaseInsensitiveSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.static CaseInsensitiveSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.static CaseInsensitiveSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.static CaseInsensitiveSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6, CharSequence item7) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.Methods inherited from class com.github.tommyettinger.ds.ObjectSet
add, addAll, addAll, addAll, addAll, addResize, appendTo, clear, clear, contains, containsAll, containsAll, containsAll, containsAny, containsAny, containsAnyIterable, ensureCapacity, equals, first, get, getHashMultiplier, getLoadFactor, getTableSize, isEmpty, iterator, locateKey, notEmpty, parse, parse, parse, remove, removeAll, removeAll, removeAll, retainAll, setHashMultiplier, setLoadFactor, shrink, size, toArray, toArray, toString, truncate, with, with, with, with, with, with, with, with, withMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface com.github.tommyettinger.ds.EnhancedCollection
add, add, add, addAll, addAllIterable, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAllIterable, containsAny, removeAll, removeAllIterable, removeEach, removeEach, removeEach, removeEachIterable, toString, toString, toStringMethods inherited from interface java.util.Set
spliterator
-
Constructor Details
-
CaseInsensitiveSet
public CaseInsensitiveSet()Creates a new set with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
CaseInsensitiveSet
public CaseInsensitiveSet(int initialCapacity) Creates a new set with the specified initial capacity a load factor ofUtilities.getDefaultLoadFactor(). This set will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.
-
CaseInsensitiveSet
public CaseInsensitiveSet(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.loadFactor- what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
-
CaseInsensitiveSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
CaseInsensitiveSet
Creates a new set identical to the specified set.- Parameters:
set- an ObjectSet or one of its subclasses
-
CaseInsensitiveSet
Creates a new set that contains all distinct elements incoll.- Parameters:
coll- a Collection implementation, such as an ObjectList
-
CaseInsensitiveSet
Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).- Parameters:
array- an array to draw items fromoffset- the first index in array to draw an item fromlength- how many items to take from array; bounds-checking is the responsibility of the using code
-
CaseInsensitiveSet
Creates a new set containing all the items in the given array.- Parameters:
array- an array that will be used in full, except for duplicate items
-
CaseInsensitiveSet
Creates a new set that contains all distinct elements inset.- Parameters:
set- another CaseInsensitiveSet
-
-
Method Details
-
place
Description copied from class:ObjectSetReturns an index >= 0 and <=ObjectSet.maskfor the specifieditem, mixed.- Overrides:
placein classObjectSet<CharSequence>- Parameters:
item- a non-null Object; its hashCode() method should be used by most implementations- Returns:
- an index between 0 and
ObjectSet.mask(both inclusive)
-
equate
Description copied from class:ObjectSetCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal. This is used by the rest of this class to determine whether two keys are considered equal. Normally, this returnsleft.equals(right), but subclasses can override it to use reference equality, fuzzy equality, deep array equality, or any other custom definition of equality. Usually,ObjectSet.place(Object)is also overridden if this method is.- Overrides:
equatein classObjectSet<CharSequence>- Parameters:
left- must be non-null; typically a key being compared, but not necessarilyright- may be null; typically a key being compared, but can often be null for an empty key slot, or some other type- Returns:
- true if left and right are considered equal for the purposes of this class
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<CharSequence>- Specified by:
hashCodein interfaceSet<CharSequence>- Overrides:
hashCodein classObjectSet<CharSequence>
-
resize
protected void resize(int newSize) - Overrides:
resizein classObjectSet<CharSequence>
-
with
Constructs an empty set. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.- Returns:
- a new set containing nothing
-
with
Creates a new CaseInsensitiveSet that holds only the given item, but can be resized.- Parameters:
item- one CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given item
-
with
Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given items
-
with
Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence itemitem2- a CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given items
-
with
public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence itemitem2- a CharSequence itemitem3- a CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given items
-
with
public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence itemitem2- a CharSequence itemitem3- a CharSequence itemitem4- a CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given items
-
with
public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence itemitem2- a CharSequence itemitem3- a CharSequence itemitem4- a CharSequence itemitem5- a CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given items
-
with
public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence itemitem2- a CharSequence itemitem3- a CharSequence itemitem4- a CharSequence itemitem5- a CharSequence itemitem6- a CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given items
-
with
public static CaseInsensitiveSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6, CharSequence item7) Creates a new CaseInsensitiveSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence itemitem2- a CharSequence itemitem3- a CharSequence itemitem4- a CharSequence itemitem5- a CharSequence itemitem6- a CharSequence item- Returns:
- a new CaseInsensitiveSet that holds the given items
-
with
Creates a new CaseInsensitiveSet that holds only the given items, but can be resized. This overload will only be used when varargs are used and there are 9 or more arguments.- Parameters:
varargs- a CharSequence varargs or CharSequence array; remember that varargs allocate- Returns:
- a new CaseInsensitiveSet that holds the given items
-
parse
CallsObjectSet.parse(String, String, PartialParser, boolean)with brackets set to false.- Parameters:
str- a String that will be parsed in fulldelimiter- the delimiter between items in str- Returns:
- a new collection parsed from str
-
parse
Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true). Each item is expected to be separated bydelimiter.- Parameters:
str- a String that will be parsed in full (depending on brackets)delimiter- the delimiter between items in strbrackets- if true, the first and last chars in str will be ignored- Returns:
- a new collection parsed from str
-
parse
Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.- Parameters:
str- a String that will have the given section parseddelimiter- the delimiter between items in stroffset- the first position to parse in str, inclusivelength- how many chars to parse, starting from offset- Returns:
- a new collection parsed from str
-