public class BundleBiMap<E,S>
extends java.lang.Object
Constructor and Description |
---|
BundleBiMap()
Constructs an empty BundleBiMap.
|
BundleBiMap(BundleBiMap<E,S> other)
Constructs a BundleBiMap using another BundleBiMap to copy.
|
BundleBiMap(int expected)
Constructs a BundleBiMap with the expected number of indices to hold (the number of bundles and number of S items
is always the same, and this will be more efficient if expected is greater than that number).
|
BundleBiMap(int expected,
float f)
Constructs a BundleBiMap with the expected number of indices to hold (the number of bundles and number of S items
is always the same, and this will be more efficient if expected is greater than that number) and the load factor
to use, between 0.1f and 0.8f usually (using load factors higher than 0.8f can cause problems).
|
Modifier and Type | Method and Description |
---|---|
BundleBiMap<E,S> |
alterB(S past,
S future)
Changes an existing S key,
past , to another S key, future , if past exists in this BundleBiMap
and future does not yet exist in this BundleBiMap. |
BundleBiMap<E,S> |
alterSingleAt(int index,
S future)
Changes the S key at
index to another S key, future , if index is valid and future does not
yet exist in this K2. |
boolean |
containsElement(E element)
Returns true if this contains the E, element, in any of its bundles of E keys.
|
boolean |
containsIndex(int index)
Returns true if index is between 0 (inclusive) and
size() (exclusive), or false otherwise. |
boolean |
containsSingle(S single)
Returns true if this contains the S, single, in its collection of S items.
|
squidpony.squidmath.OrderedSet<E> |
elementsFromCode(int[][] bundle)
Given a coded bundle as produced by some methods in this class, decodes the elements part of the bundle and
returns it as a newly-allocated OrderedSet of E element keys.
|
int |
elementSize()
Gets the total number of unique E element keys across all bundles in this BundleBiMap.
|
squidpony.squidmath.OrderedSet<E> |
getBundleElements(S single)
Given an S key to look up, gets a (newly-allocated) OrderedSet of E element keys corresponding to that S key.
|
squidpony.squidmath.OrderedSet<E> |
getBundleElementsAt(int index)
Given an index to look up, gets a (newly-allocated) OrderedSet of E element keys in the bundle at that index.
|
int[] |
getBundleVariation(S single)
Given an S key to look up, gets a (newly-allocated) int array that is equivalent to the variation part of the
bundle corresponding to single.
|
int[] |
getBundleVariationAt(int index)
Given an index to look up, gets a (newly-allocated) int array that is equivalent to the variation part of the
bundle present at that index.
|
int[][] |
getCode(S single)
Given an S key to look up, gets a 2D int array representing the key's matching bundle.
|
int[][] |
getCodeAt(int index)
Given an index to look up, gets a 2D int array representing the bundle at that index.
|
squidpony.squidmath.OrderedSet<E> |
getElementOrderedSet()
To be called sparingly, since this allocates a new OrderedSet instead of reusing one.
|
java.util.SortedSet<E> |
getElementSet()
Gets and caches the individual E keys as a Collection that implements SortedSet (and so also implements Set).
|
squidpony.squidmath.OrderedSet<int[][]> |
getManyCoded(E element)
Given an E element key that could be used in one or more bundles this uses, gets all bundles in this object that
contain the given element, as coded 2D int arrays.
|
int[] |
getManyIndices(E element)
Given an E element key that could be used in one or more bundles this uses, gets all indices in the ordering
that contain a bundle with that element.
|
squidpony.squidmath.OrderedSet<S> |
getManySingles(E element)
Given an E element key that could be used in one or more bundles this uses, finds all S single keys corresponding
to bundles that contain the given element.
|
S |
getSingle(java.util.Collection<? extends E> e)
Given a bundle of E keys as a Collection with no variation, gets the matching S key for that bundle, or
null if there is none.
|
S |
getSingle(java.util.Collection<? extends E> e,
int[] variations)
Given a bundle of E keys as a Collection with an int array variation, gets the matching S key for that bundle, or
null if there is none.
|
S |
getSingle(E[] e)
Given a bundle of E keys as an array with no variation, gets the matching S key for that bundle, or null if there
is none.
|
S |
getSingle(E[] e,
int[] variations)
Given a bundle of E keys as an array with an int array variation, gets the matching S key for that bundle, or
null if there is none.
|
S |
getSingleAt(int index)
Given an int index, finds the associated S key (using index as a point in the ordering).
|
S |
getSingleCoded(int[][] code)
Given a bundle as a coded 2D int array, gets the matching S key for that bundle, or null if there is none.
|
squidpony.squidmath.OrderedSet<S> |
getSingleOrderedSet()
To be called sparingly, since this allocates a new OrderedSet instead of reusing one.
|
java.util.SortedSet<S> |
getSingleSet()
Gets and caches the S single keys as a Collection that implements SortedSet (and so also implements Set).
|
int |
indexOfSingle(S single)
Gets (in near-constant time) the index of the given S single key in the ordering.
|
boolean |
isEmpty() |
java.util.Iterator<E> |
iteratorElements()
Creates a new iterator over the individual E element keys this holds, with a larger total count the iterator may
yield than
size() in most cases (it should be equal to elementSize() ), and in no particular
order (though the order should be stable across versions and platforms, no special means are provided to further
control the order). |
java.util.Iterator<S> |
iteratorSingles()
Creates a new iterator over the S single keys this holds.
|
boolean |
put(java.util.Collection<? extends E> e,
int[] variation,
S s)
Adds a bundle of E keys, mixed with an int array of variations, and a S key at the same point in the ordering
(the end) to this BundleBiMap.
|
boolean |
put(java.util.Collection<? extends E> e,
S s)
Adds a bundle of E keys and a S key at the same point in the ordering (the end) to this BundleBiMap.
|
boolean |
put(E[] e,
int[] variation,
S s)
Adds a bundle of E keys, mixed with an int array of variations, and a S key at the same point in the ordering
(the end) to this BundleBiMap.
|
boolean |
put(E[] e,
S s)
Adds a bundle of E keys and a S key at the same point in the ordering (the end) to this BundleBiMap.
|
boolean |
putAll(BundleBiMap<? extends E,? extends S> other)
Puts all unique E and S keys in
other into this K2, respecting other's ordering. |
boolean |
putAll(java.lang.Iterable<? extends java.util.Collection<? extends E>> aKeys,
java.lang.Iterable<? extends S> bKeys)
Puts all unique E and S keys in
aKeys and bKeys into this K2 at the end. |
E |
randomElement(squidpony.squidmath.IRNG random)
Gets a random E from this BundleBiMap's individual elements using the given IRNG.
|
S |
randomSingle(squidpony.squidmath.IRNG random)
Gets a random S from this BundleBiMap using the given IRNG.
|
BundleBiMap<E,S> |
reorder(int... ordering)
Reorders this BundleBiMap using
ordering , which has the same length as this object's size()
and can be generated with ArrayTools.range(int) (which, if applied, would produce no
change to the current ordering), IRNG.randomOrdering(int) (which gives a random ordering, and if
applied immediately would be the same as calling shuffle(IRNG) ), or made in some other way. |
BundleBiMap<E,S> |
shuffle(squidpony.squidmath.IRNG rng)
Generates a random ordering with rng and applies the same ordering to all kinds of keys this has; they will
maintain their current association to other keys but their ordering/indices will change.
|
S |
singleAt(int index)
Given an index to look up, gets the S key present at that position in the ordering.
|
int |
size()
Gets the total number of bundle-to-single pairs in this BundleBiMap.
|
int[] |
variationFromCode(int[][] bundle)
Given a coded bundle as produced by some methods in this class, decodes the variation part of the bundle, if
present, and returns it as a newly-allocated 1D int array.
|
public BundleBiMap()
public BundleBiMap(int expected)
expected
- how many bundle-to-single pairings this is expected to holdpublic BundleBiMap(int expected, float f)
expected
- the amount of indices (the count of bundles is the same as the count of S items) this should holdf
- the load factor, probably between 0.1f and 0.8fpublic BundleBiMap(BundleBiMap<E,S> other)
other
- the other BundleBiMap to copypublic boolean containsElement(E element)
element
- the E to check the presence of in all bundlespublic boolean containsSingle(S single)
single
- the S to check the presence ofpublic boolean containsIndex(int index)
size()
(exclusive), or false otherwise.index
- the index to checkpublic S getSingleAt(int index)
index
- an int index into this BundleBiMappublic E randomElement(squidpony.squidmath.IRNG random)
random
- generates a random index to get an E withpublic S randomSingle(squidpony.squidmath.IRNG random)
random
- generates a random index to get a S withpublic BundleBiMap<E,S> alterB(S past, S future)
past
, to another S key, future
, if past exists in this BundleBiMap
and future does not yet exist in this BundleBiMap. This will retain past's point in the ordering for future, so
the associated other key(s) will still be associated in the same way.past
- a S key, that must exist in this BundleBiMap's S keys, and will be changedfuture
- a S key, that cannot currently exist in this BundleBiMap's S keys, but will if this succeedspublic BundleBiMap<E,S> alterSingleAt(int index, S future)
index
to another S key, future
, if index is valid and future does not
yet exist in this K2. The position in the ordering for future will be the same as index, and the same
as the key this replaced, if this succeeds, so the other key(s) at that position will still be associated in
the same way.index
- a position in the ordering to change; must be at least 0 and less than size()
future
- a S key, that cannot currently exist in this BundleBiMap's S keys, but will if this succeedspublic boolean put(E[] e, S s)
e
- an array of E keys to add; the array cannot already be present, nor can it be nulls
- e S key to add; cannot already be presentpublic boolean put(E[] e, int[] variation, S s)
e
- an array of E keys to add; the array cannot already have been inserted with an equivalent variation, nor can it be nullvariation
- an int array that can be used to make a different version of e, i.e. the same things at different quantities; cannot be nulls
- e S key to add; cannot already be presentpublic boolean put(java.util.Collection<? extends E> e, S s)
e
- an array of E keys to add; the array cannot already be present, nor can it be nulls
- e S key to add; cannot already be presentpublic boolean put(java.util.Collection<? extends E> e, int[] variation, S s)
e
- an array of E keys to add; the array cannot already have been inserted with an equivalent variation, nor can it be nullvariation
- an int array that can be used to make a different version of e, i.e. the same things at different quantities; cannot be nulls
- e S key to add; cannot already be presentpublic boolean putAll(java.lang.Iterable<? extends java.util.Collection<? extends E>> aKeys, java.lang.Iterable<? extends S> bKeys)
aKeys
and bKeys
into this K2 at the end. If an E in aKeys or a S
in bKeys is already present when this would add one, this will not put the E and S keys at that point in the
iteration order, and will place the next unique E and S it finds in the arguments at that position instead.aKeys
- an Iterable or Collection of E keys to add; should all be unique (like a Set)bKeys
- an Iterable or Collection of S keys to add; should all be unique (like a Set)public boolean putAll(BundleBiMap<? extends E,? extends S> other)
other
into this K2, respecting other's ordering. If an E or a S in other
is already present when this would add one, this will not put the E and S keys at that point in the iteration
order, and will place the next unique E and S it finds in the arguments at that position instead.other
- another K2 collection with the same E and S typespublic int[][] getCode(S single)
put(Object[], int[], Object)
or put(Collection, int[], Object)
.
single
- a S key to look uppublic int[][] getCodeAt(int index)
put(Object[], int[], Object)
or put(Collection, int[], Object)
.
index
- an int index to look uppublic S singleAt(int index)
index
- an int index to look uppublic S getSingle(E[] e)
e
- an array of Epublic S getSingle(E[] e, int[] variations)
e
- an array of E element keysvariations
- an int array that should match an int array used as a variation parameter to putpublic S getSingle(java.util.Collection<? extends E> e)
e
- a Collection of E element keys (each key can be an E or an instance of any subclass of E)public S getSingle(java.util.Collection<? extends E> e, int[] variations)
e
- a Collection of E element keys (each key can be an E or an instance of any subclass of E)variations
- an int array that should match an int array used as a variation parameter to putpublic S getSingleCoded(int[][] code)
code
- a 2D int array representing a bundle that should have been obtained directly from this objectpublic int indexOfSingle(S single)
single
- a S single key to look uppublic squidpony.squidmath.OrderedSet<E> elementsFromCode(int[][] bundle)
bundle
- a coded bundle as a 2D int arraypublic int[] variationFromCode(int[][] bundle)
bundle
- a coded bundle as a 2D int arraypublic squidpony.squidmath.OrderedSet<E> getBundleElements(S single)
getBundleVariation(Object)
.single
- a S key to look uppublic int[] getBundleVariation(S single)
getBundleElements(Object)
.single
- a S key to look uppublic squidpony.squidmath.OrderedSet<E> getBundleElementsAt(int index)
getBundleVariationAt(int)
.index
- an int position in the ordering to look uppublic int[] getBundleVariationAt(int index)
getBundleElementsAt(int)
.index
- an int position in the ordering to look uppublic squidpony.squidmath.OrderedSet<S> getManySingles(E element)
element
- an E element key to look up (probably a component of one or more bundles)public squidpony.squidmath.OrderedSet<int[][]> getManyCoded(E element)
elementsFromCode(int[][])
and variationFromCode(int[][])
to get usable information from them.element
- an E element key to look up (probably a component of one or more bundles)public int[] getManyIndices(E element)
getSingleAt(int)
to get the
S key at that position, getBundleElementsAt(int)
to get the E element keys at that position,
getBundleVariationAt(int)
to get the possible variation at that position, or getCodeAt(int)
to
get the coded bundle at that position.public BundleBiMap<E,S> reorder(int... ordering)
ordering
, which has the same length as this object's size()
and can be generated with ArrayTools.range(int)
(which, if applied, would produce no
change to the current ordering), IRNG.randomOrdering(int)
(which gives a random ordering, and if
applied immediately would be the same as calling shuffle(IRNG)
), or made in some other way. If you
already have an ordering and want to make a different ordering that can undo the change, you can use
ArrayTools.invertOrdering(int[])
called on the original ordering. The effects of this method, if called
with an ordering that has repeat occurrences of an int or contains ints that are larger than its size should
permit, are undefined other than the vague definition of "probably bad, unless you like crashes."ordering
- an int array or vararg that must contain each int from 0 to size()
public BundleBiMap<E,S> shuffle(squidpony.squidmath.IRNG rng)
rng
- an IRNG to produce the random ordering this will usepublic java.util.Iterator<E> iteratorElements()
size()
in most cases (it should be equal to elementSize()
), and in no particular
order (though the order should be stable across versions and platforms, no special means are provided to further
control the order). The E keys are individual, not bundled, and duplicate keys should never be encountered even
if an E key appears in many bundles.public java.util.Iterator<S> iteratorSingles()
size()
. This method can be problematic for garbage collection if called very frequently; it may be
better to access items by index (which also lets you access other keys associated with that index) using
getSingleAt(int)
in a for(int i=0...) loop.public java.util.SortedSet<E> getElementSet()
public java.util.SortedSet<S> getSingleSet()
public squidpony.squidmath.OrderedSet<E> getElementOrderedSet()
public squidpony.squidmath.OrderedSet<S> getSingleOrderedSet()
public int size()
public int elementSize()
size()
, and ordinarily a fair amount larger, though this can also be smaller.public boolean isEmpty()
Copyright © Eben Howard 2012–2022. All rights reserved.