Package com.github.tommyettinger.ds
Class CharBag
java.lang.Object
com.github.tommyettinger.ds.CharList
com.github.tommyettinger.ds.CharBag
- All Implemented Interfaces:
Arrangeable,Ordered.OfChar,PrimitiveCollection<Character>,PrimitiveCollection.OfChar,Appendable,CharSequence,Comparable<CharList>
An unordered List of char items. This allows efficient iteration via a reused iterator or via index.
This class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
Items are permitted to change position in the ordering when any item is removed or added.
Although this won't keep an order during modifications, you can
CharList.sort() the bag to ensure,
if no modifications are made later, that the iteration will happen in sorted order.-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.tommyettinger.ds.CharList
CharList.CharListIteratorNested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.PrimitiveCollection
PrimitiveCollection.OfBoolean, PrimitiveCollection.OfByte, PrimitiveCollection.OfChar, PrimitiveCollection.OfDouble, PrimitiveCollection.OfFloat, PrimitiveCollection.OfInt, PrimitiveCollection.OfLong, PrimitiveCollection.OfShort -
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionCharBag()Creates an ordered bag with a capacity of 10.CharBag(char[] array) Creates a new bag containing the elements in the specified array.CharBag(char[] array, int startIndex, int count) Creates a new bag containing the elements in the specified array.CharBag(int capacity) Creates an ordered bag with the specified capacity.Creates a new bag containing the elements in the specific list or bag.CharBag(Ordered.OfChar other) Copies the given Ordered.OfChar into a new bag.CharBag(Ordered.OfChar other, int offset, int count) Creates a new bag by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.Creates a new bag containing the items in the specified PrimitiveCollection.OfChar.CharBag(CharIterator coll) Creates a new instance containing the items in the specified iterator.CharBag(CharSequence other) CharBag(CharSequence other, int start, int end) Creates a new CharBag from part of another CharSequence; start is inclusive and end is exclusive. -
Method Summary
Modifier and TypeMethodDescriptionappend(boolean value) Appends either the four chars't', 'r', 'u', 'e'ifvalueis true, or the five chars'f', 'a', 'l', 's', 'e'if it is false.append(char c) append(double number) Appends the base-10 decimal or engineering textual form of the given number, without allocating.append(float number) Appends the base-10 decimal or engineering textual form of the given number, without allocating.append(int number) Appends the base-10 signed textual form of the given number, without allocating.append(long number) Appends the base-10 signed textual form of the given number, without allocating.append(CharSequence csq) append(CharSequence csq, int start, int end) Appends a literal newline (Unicode character u000A).inthashCode()voidinsert(int index, char element) This always addselementto the end of this bag's ordering.booleanReturns true if this implementation retains order, which it does not.line()Appends a literal newline (Unicode character u000A).padLeft(int count, char padWith) Addscountrepetitions ofpadWithto the start (left) of this list.padRight(int count, char padWith) Addscountrepetitions ofpadWithto the end (right) of this list.static CharBagCallsparse(String, String, boolean)with brackets set to false.static CharBagCreates a new collection and fills it by callingPrimitiveCollection.OfChar.addLegible(String, String, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static CharBagCreates a new collection and fills it by callingPrimitiveCollection.OfChar.addLegible(String, String, int, int)with the given four parameters as-is.charremoveAt(int index) Removes and returns the item at the specified index.voidremoveRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive.subSequence(int start, int end) Gets a new CharBag from the given subrange, clamping start and end so that this will not throw any Exception.static CharBagwith()Constructs an empty bag.static CharBagwith(char item) Creates a new CharBag that holds only the given item, but can be resized.static CharBagwith(char... varargs) Creates a new CharBag that holds only the given items, but can be resized.static CharBagwith(char item0, char item1) Creates a new CharBag that holds only the given items, but can be resized.static CharBagwith(char item0, char item1, char item2) Creates a new CharBag that holds only the given items, but can be resized.static CharBagwith(char item0, char item1, char item2, char item3) Creates a new CharBag that holds only the given items, but can be resized.static CharBagwith(char item0, char item1, char item2, char item3, char item4) Creates a new CharBag that holds only the given items, but can be resized.static CharBagwith(char item0, char item1, char item2, char item3, char item4, char item5) Creates a new CharBag that holds only the given items, but can be resized.static CharBagwith(char item0, char item1, char item2, char item3, char item4, char item5, char item6) Creates a new CharBag that holds only the given items, but can be resized.static CharBagwith(char item0, char item1, char item2, char item3, char item4, char item5, char item6, char item7) Creates a new CharBag that holds only the given items, but can be resized.Methods inherited from class com.github.tommyettinger.ds.CharList
add, add, add, add, addAll, addAll, addAll, addAll, addAll, addAll, charAt, clear, compareTo, compareWith, compareWithIgnoreCase, contains, contains, containsAll, containsIgnoreCase, duplicateRange, ensureCapacity, equals, equalsIgnoreCase, first, get, indexOf, indexOf, indexOf, indexOf, indexOfIgnoreCase, indexOfIgnoreCase, indexOfIgnoreCase, indexOfIgnoreCase, isEmpty, iterator, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, lastIndexOfIgnoreCase, leftString, length, notEmpty, order, peek, pop, random, remove, removeAll, removeEach, replaceAll, resize, retainAll, reverse, rightString, set, setSize, shrink, shuffle, size, sort, sort, sort, sort, substring, swap, toArray, toArray, toDenseString, toString, trimToSize, truncateMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffleMethods inherited from interface java.lang.CharSequence
chars, codePoints, toStringMethods inherited from interface com.github.tommyettinger.ds.Ordered.OfChar
getOrderType, random, selectRanked, selectRankedIndexMethods inherited from interface com.github.tommyettinger.ds.PrimitiveCollection.OfChar
addAll, addAll, addDense, addDense, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAll, containsAll, containsAll, containsAny, containsAny, containsAny, containsAny, denseAppendTo, equalContents, forEach, removeAll, removeAll, removeAll, removeEach, removeEach, removeEach, removeIf, retainAll, retainAll, toDenseString, toString, toString, toString
-
Constructor Details
-
CharBag
public CharBag()Creates an ordered bag with a capacity of 10. -
CharBag
public CharBag(int capacity) Creates an ordered bag with the specified capacity.- Parameters:
capacity-
-
CharBag
Creates a new bag containing the elements in the specific list or bag. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
list- another CharList or CharBag
-
CharBag
public CharBag(char[] array) Creates a new bag containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
array- a non-null char array to add to this bag
-
CharBag
public CharBag(char[] array, int startIndex, int count) Creates a new bag containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
array- a non-null char array to add to this bagstartIndex- the first index inarrayto usecount- how many items to use fromarray
-
CharBag
Creates a new bag containing the items in the specified PrimitiveCollection.OfChar.- Parameters:
coll- a primitive collection that will have its contents added to this
-
CharBag
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
CharBag
Copies the given Ordered.OfChar into a new bag.- Parameters:
other- another Ordered.OfChar
-
CharBag
Creates a new bag by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.- Parameters:
other- another Ordered.OfCharoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from other
-
CharBag
-
CharBag
Creates a new CharBag from part of another CharSequence; start is inclusive and end is exclusive.- Parameters:
other- a CharSequence to use part of, such as a String or a CharListstart- first index inotherto use, inclusiveend- last index inotherto use, exclusive
-
-
Method Details
-
keepsOrder
public boolean keepsOrder()Returns true if this implementation retains order, which it does not.- Overrides:
keepsOrderin classCharList- Returns:
- false
-
insert
public void insert(int index, char element) This always addselementto the end of this bag's ordering. -
removeAt
public char removeAt(int index) Removes and returns the item at the specified index. Note that this is equivalent toList.remove(int), but can't have that name because we also haveCharList.remove(char)that removes a value, rather than an index. -
removeRange
public void removeRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections.- Specified by:
removeRangein interfaceOrdered.OfChar- Overrides:
removeRangein classCharList- Parameters:
start- the first index to remove, inclusiveend- the last index (after what should be removed), exclusive
-
subSequence
Gets a new CharBag from the given subrange, clamping start and end so that this will not throw any Exception.- Specified by:
subSequencein interfaceCharSequence- Specified by:
subSequencein interfaceOrdered.OfChar- Overrides:
subSequencein classCharList- Parameters:
start- the start index, inclusive; clampedend- the end index, exclusive; clamped- Returns:
- a new CharBag copied from the given subrange bounds
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classCharList
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classCharList
-
append
- Specified by:
appendin interfaceAppendable- Overrides:
appendin classCharList
-
appendLine
Appends a literal newline (Unicode character u000A).- Overrides:
appendLinein classCharList- Returns:
- this, for chaining.
-
line
Appends a literal newline (Unicode character u000A). This is identical toappendLine()but is faster to type or recommend. -
append
Appends the base-10 signed textual form of the given number, without allocating. This usesBase.appendSigned(CharSequence, int). -
append
Appends the base-10 signed textual form of the given number, without allocating. This usesBase.appendSigned(CharSequence, long). This does not append a trailing'L'. -
append
Appends the base-10 decimal or engineering textual form of the given number, without allocating. This usesBase.appendGeneral(CharSequence, float). This does not append a trailing'f'. -
append
Appends the base-10 decimal or engineering textual form of the given number, without allocating. This usesBase.appendGeneral(CharSequence, double). -
append
Appends either the four chars't', 'r', 'u', 'e'ifvalueis true, or the five chars'f', 'a', 'l', 's', 'e'if it is false. -
hashCode
public int hashCode()- Specified by:
hashCodein interfacePrimitiveCollection<Character>- Overrides:
hashCodein classCharList
-
padLeft
Addscountrepetitions ofpadWithto the start (left) of this list.
Note that because the order of a bag is unreliable, this could conceivably pad to any point in the order, but this does actually move all elements to the right and prepend padWith count times. -
padRight
Addscountrepetitions ofpadWithto the end (right) of this list. -
with
Constructs an empty bag. 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 bag containing nothing
-
with
Creates a new CharBag that holds only the given item, but can be resized.- Parameters:
item- a char item- Returns:
- a new CharBag that holds the given item
-
with
Creates a new CharBag that holds only the given items, but can be resized.- Parameters:
item0- a char itemitem1- a char item- Returns:
- a new CharBag that holds the given items
-
with
Creates a new CharBag that holds only the given items, but can be resized.- Parameters:
item0- a char itemitem1- a char itemitem2- a char item- Returns:
- a new CharBag that holds the given items
-
with
Creates a new CharBag that holds only the given items, but can be resized.- Parameters:
item0- a char itemitem1- a char itemitem2- a char itemitem3- a char item- Returns:
- a new CharBag that holds the given items
-
with
Creates a new CharBag that holds only the given items, but can be resized.- Parameters:
item0- a char itemitem1- a char itemitem2- a char itemitem3- a char itemitem4- a char item- Returns:
- a new CharBag that holds the given items
-
with
Creates a new CharBag that holds only the given items, but can be resized.- Parameters:
item0- a char itemitem1- a char itemitem2- a char itemitem3- a char itemitem4- a char itemitem5- a char item- Returns:
- a new CharBag that holds the given items
-
with
public static CharBag with(char item0, char item1, char item2, char item3, char item4, char item5, char item6) Creates a new CharBag that holds only the given items, but can be resized.- Parameters:
item0- a char itemitem1- a char itemitem2- a char itemitem3- a char itemitem4- a char itemitem5- a char itemitem6- a char item- Returns:
- a new CharBag that holds the given items
-
with
public static CharBag with(char item0, char item1, char item2, char item3, char item4, char item5, char item6, char item7) Creates a new CharBag that holds only the given items, but can be resized.- Parameters:
item0- a char itemitem1- a char itemitem2- a char itemitem3- a char itemitem4- a char itemitem5- a char itemitem6- a char item- Returns:
- a new CharBag that holds the given items
-
with
Creates a new CharBag that holds only the given items, but can be resized. This overload will only be used when an array is supplied and the type of the items requested is the component type of the array, or if varargs are used and there are 9 or more arguments.- Parameters:
varargs- a char varargs or char array; remember that varargs allocate- Returns:
- a new CharBag that holds the given items
-
parse
Callsparse(String, String, 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 callingPrimitiveCollection.OfChar.addLegible(String, String, 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 callingPrimitiveCollection.OfChar.addLegible(String, String, int, int)with the given four 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
-