Package com.github.tommyettinger.ds
Class CaseInsensitiveOrderedSet
java.lang.Object
com.github.tommyettinger.ds.ObjectSet<T>
com.github.tommyettinger.ds.ObjectOrderedSet<CharSequence>
com.github.tommyettinger.ds.CaseInsensitiveOrderedSet
- All Implemented Interfaces:
Arrangeable,EnhancedCollection<CharSequence>,Ordered<CharSequence>,Iterable<CharSequence>,Collection<CharSequence>,Set<CharSequence>
A custom variant on ObjectOrderedSet 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
This is also 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
CaseInsensitiveSet,
except that this class maintains insertion order and can be sorted with ObjectOrderedSet.sort(), ObjectOrderedSet.sort(Comparator), etc.
Note that because each CharSequence is stored in here in its
original form (not modified to make it ignore case), the sorted order might be different than you expect.
Utilities.compareIgnoreCase(CharSequence, CharSequence) can be used to sort this as case-insensitive.
This is also very similar to
FilteredStringOrderedSet when its editor
is Character.toUpperCase(char) or Casing.caseUp(char).
FilteredStringOrderedSet 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 CaseInsensitiveOrderedSet won't do,
but new FilteredStringOrderedSet<>(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.ObjectOrderedSet
ObjectOrderedSet.ObjectOrderedSetIterator<K>Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectSet
ObjectSet.ObjectSetIterator<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Ordered
Ordered.OfBoolean, Ordered.OfByte, Ordered.OfChar, Ordered.OfDouble, Ordered.OfFloat, Ordered.OfInt, Ordered.OfLong, Ordered.OfShort -
Field Summary
Fields inherited from class com.github.tommyettinger.ds.ObjectOrderedSet
itemsFields 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().CaseInsensitiveOrderedSet(int initialCapacity) Creates a new set with the specified initial capacity a load factor ofUtilities.getDefaultLoadFactor().CaseInsensitiveOrderedSet(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor.CaseInsensitiveOrderedSet(int initialCapacity, float loadFactor, OrderType ordering) CaseInsensitiveOrderedSet(int initialCapacity, OrderType ordering) Creates a new set that contains all distinct elements inset.CaseInsensitiveOrderedSet(ObjectOrderedSet<? extends CharSequence> set, OrderType ordering) CaseInsensitiveOrderedSet(ObjectSet<? extends CharSequence> set) Creates a new set identical to the specified set.CaseInsensitiveOrderedSet(ObjectSet<? extends CharSequence> set, OrderType ordering) Creates a new set that contains all distinct elements inset.CaseInsensitiveOrderedSet(Ordered<CharSequence> other, int offset, int count) Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.CaseInsensitiveOrderedSet(Ordered<CharSequence> other, int offset, int count, OrderType ordering) Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.CaseInsensitiveOrderedSet(OrderType ordering) CaseInsensitiveOrderedSet(CharSequence[] array) Creates a new set containing all the items in the given array.CaseInsensitiveOrderedSet(CharSequence[] array, int offset, int length) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).CaseInsensitiveOrderedSet(CharSequence[] array, int offset, int length, OrderType ordering) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).CaseInsensitiveOrderedSet(CharSequence[] items, OrderType ordering) Creates a new set that contains all distinct elements initems.CaseInsensitiveOrderedSet(Collection<? extends CharSequence> coll) Creates a new set that contains all distinct elements incoll.CaseInsensitiveOrderedSet(Collection<? extends CharSequence> coll, OrderType ordering) Creates a new set that contains all distinct elements incoll.CaseInsensitiveOrderedSet(Iterator<? extends CharSequence> coll) Creates a new instance containing the items in the specified iterator.CaseInsensitiveOrderedSet(Iterator<? extends CharSequence> coll, OrderType ordering) Creates a new instance containing the items in the specified iterator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanprotected booleanCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal.intGets the current hashMultiplier, used inplace(Object)to mix hash codes.inthashCode()static CaseInsensitiveOrderedSetCallsObjectOrderedSet.parse(String, String, PartialParser, boolean)with brackets set to false.static CaseInsensitiveOrderedSetCreates 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 CaseInsensitiveOrderedSetCreates 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) voidsetHashMultiplier(int hashMultiplier) Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001).static CaseInsensitiveOrderedSetwith()Constructs an empty set.static CaseInsensitiveOrderedSetwith(CharSequence item) Creates a new CaseInsensitiveOrderedSet that holds only the given item, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence... varargs) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence item0, CharSequence item1) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence item0, CharSequence item1, CharSequence item2) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.static CaseInsensitiveOrderedSetwith(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6, CharSequence item7) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.Methods inherited from class com.github.tommyettinger.ds.ObjectOrderedSet
add, add, addAll, addAll, alter, alterAt, clear, clear, ensureCapacity, first, getAt, iterator, order, parse, parse, parse, remove, removeAt, removeRange, sort, sort, toString, toString, truncate, with, with, with, with, with, with, with, with, withMethods inherited from class com.github.tommyettinger.ds.ObjectSet
addAll, addAll, addAll, addAll, addResize, appendTo, contains, containsAll, containsAll, containsAll, containsAny, containsAny, containsAnyIterable, get, getLoadFactor, getTableSize, isEmpty, locateKey, notEmpty, removeAll, removeAll, removeAll, retainAll, setLoadFactor, shrink, size, toArray, toArrayMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffle, sizeMethods 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, toStringMethods inherited from interface com.github.tommyettinger.ds.Ordered
getOrderType, random, random, reverse, selectRanked, selectRankedIndex, shuffle, swapMethods inherited from interface java.util.Set
spliterator
-
Constructor Details
-
CaseInsensitiveOrderedSet
-
CaseInsensitiveOrderedSet
-
CaseInsensitiveOrderedSet
-
CaseInsensitiveOrderedSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to thisordering-
-
CaseInsensitiveOrderedSet
-
CaseInsensitiveOrderedSet
Creates a new set that contains all distinct elements inset.- Parameters:
set-ordering-
-
CaseInsensitiveOrderedSet
Creates a new set that contains all distinct elements incoll.- Parameters:
coll-ordering-
-
CaseInsensitiveOrderedSet
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 codeordering-
-
CaseInsensitiveOrderedSet
Creates a new set that contains all distinct elements initems.- Parameters:
items- an array that will be used in full, except for duplicate itemsordering-
-
CaseInsensitiveOrderedSet
public CaseInsensitiveOrderedSet(Ordered<CharSequence> other, int offset, int count, OrderType ordering) Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.- Parameters:
other- another Ordered of the same typeoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from otherordering-
-
CaseInsensitiveOrderedSet
public CaseInsensitiveOrderedSet()Creates a new set with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
CaseInsensitiveOrderedSet
public CaseInsensitiveOrderedSet(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.
-
CaseInsensitiveOrderedSet
public CaseInsensitiveOrderedSet(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
-
CaseInsensitiveOrderedSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
CaseInsensitiveOrderedSet
Creates a new set identical to the specified set.- Parameters:
set- an ObjectSet or one of its subclasses
-
CaseInsensitiveOrderedSet
Creates a new set that contains all distinct elements incoll.- Parameters:
coll- a Collection implementation, such as an ObjectList
-
CaseInsensitiveOrderedSet
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
-
CaseInsensitiveOrderedSet
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
-
CaseInsensitiveOrderedSet
Creates a new set that contains all distinct elements inset.- Parameters:
set- another CaseInsensitiveOrderedSet
-
CaseInsensitiveOrderedSet
Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.- Parameters:
other- another Ordered of CharSequenceoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from other
-
-
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)
-
getHashMultiplier
public int getHashMultiplier()Gets the current hashMultiplier, used inplace(Object)to mix hash codes. IfsetHashMultiplier(int)is never called, the hashMultiplier will always be drawn fromUtilities.GOOD_MULTIPLIERS, with the index equal to64 - shift.- Overrides:
getHashMultiplierin classObjectSet<CharSequence>- Returns:
- any int; the value isn't used internally, but may be used by subclasses to identify something
-
setHashMultiplier
public void setHashMultiplier(int hashMultiplier) Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001). This can be any negative, odd int, but should almost always be drawn fromUtilities.GOOD_MULTIPLIERSor something like it.- Overrides:
setHashMultiplierin classObjectSet<CharSequence>- Parameters:
hashMultiplier- any int; will be made odd if even.
-
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 classObjectOrderedSet<CharSequence>
-
equals
- Specified by:
equalsin interfaceCollection<CharSequence>- Specified by:
equalsin interfaceSet<CharSequence>- Overrides:
equalsin 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 CaseInsensitiveOrderedSet that holds only the given item, but can be resized.- Parameters:
item- one CharSequence item- Returns:
- a new CaseInsensitiveOrderedSet that holds the given item
-
with
Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence item- Returns:
- a new CaseInsensitiveOrderedSet that holds the given items
-
with
public static CaseInsensitiveOrderedSet with(CharSequence item0, CharSequence item1, CharSequence item2) Creates a new CaseInsensitiveOrderedSet that holds only the given items, but can be resized.- Parameters:
item0- a CharSequence itemitem1- a CharSequence itemitem2- a CharSequence item- Returns:
- a new CaseInsensitiveOrderedSet that holds the given items
-
with
public static CaseInsensitiveOrderedSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3) Creates a new CaseInsensitiveOrderedSet 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 CaseInsensitiveOrderedSet that holds the given items
-
with
public static CaseInsensitiveOrderedSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4) Creates a new CaseInsensitiveOrderedSet 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 CaseInsensitiveOrderedSet that holds the given items
-
with
public static CaseInsensitiveOrderedSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5) Creates a new CaseInsensitiveOrderedSet 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 CaseInsensitiveOrderedSet that holds the given items
-
with
public static CaseInsensitiveOrderedSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6) Creates a new CaseInsensitiveOrderedSet 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 CaseInsensitiveOrderedSet that holds the given items
-
with
public static CaseInsensitiveOrderedSet with(CharSequence item0, CharSequence item1, CharSequence item2, CharSequence item3, CharSequence item4, CharSequence item5, CharSequence item6, CharSequence item7) Creates a new CaseInsensitiveOrderedSet 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 CaseInsensitiveOrderedSet that holds the given items
-
with
Creates a new CaseInsensitiveOrderedSet 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 CaseInsensitiveOrderedSet that holds the given items
-
parse
CallsObjectOrderedSet.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
-