Package squidpony.squidmath
Class BundleBundleBiMap<E1,E2>
java.lang.Object
squidpony.squidmath.BundleBundleBiMap<E1,E2>
public class BundleBundleBiMap<E1,E2> extends Object
An ordered bidirectional map-like data structure, with "bundles" of unique E1 (Element type 1) keys and unique E2
(Element type 2) keys updated together like a map that can be queried by E1 key bundles, E2 key bundles, or int
indices, as well as like a multimap that can be queried by lone E1 or E2 keys. A bundle can be specified as a
Collection of E1 or E2 values, an array of E1 or E2 values, or in two parts as either of the above given to a
BundleBiMap method along with an int array that can represent variations on keys (e.g. some quantity for an E1 or E2
value that permits different amounts to be passed with the unique element values to it yield a different E2 bundle
for, say, 1 Foo and 1 Bar vs. 2 Foo and 3 Bar). This allows an intended purpose for this class as a way to describe
crafting recipes that may need various amounts of an item to be mixed in and for multiple items with their own
quantities to be produced, and since the quantities are optional, it can still be used to group multiple E1 keys with
multiple E2 keys. The multimap property allows you to use an E1 or E2 element key to get all of the E2 or E1 keys
that are associated with a bundle that contains that E1 or E2 key.
Does not implement any interfaces that you would expect for a data structure, because almost every method it has needs to specify whether it applies to E1 or E2 items, or otherwise doesn't fit a normal Collection-like interface's requirements.
Closely related to BundleBiMap, Arrangement, K2, and K2V1 in implementation.
Created by Tommy Ettinger on 4/26/2017.
Does not implement any interfaces that you would expect for a data structure, because almost every method it has needs to specify whether it applies to E1 or E2 items, or otherwise doesn't fit a normal Collection-like interface's requirements.
Closely related to BundleBiMap, Arrangement, K2, and K2V1 in implementation.
Created by Tommy Ettinger on 4/26/2017.
-
Constructor Summary
Constructors Constructor Description BundleBundleBiMap()
Constructs an empty BundleBiMap.BundleBundleBiMap(int expected)
Constructs a BundleBiMap with the expected number of indices to hold (the number of bundles and number of E2 items is always the same, and this will be more efficient if expected is greater than that number).BundleBundleBiMap(int expected, float f)
Constructs a BundleBiMap with the expected number of indices to hold (the number of bundles and number of E2 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).BundleBundleBiMap(BundleBundleBiMap<E1,E2> other)
Constructs a BundleBiMap using another BundleBiMap to copy. -
Method Summary
Modifier and Type Method Description boolean
containsElement1(E1 element)
Returns true if this contains the E1, element, in any of its bundles of E1 keys.boolean
containsElement2(E2 element)
Returns true if this contains the E2, element, in its collection of E2 items.boolean
containsIndex(int index)
Returns true if index is between 0 (inclusive) andsize()
(exclusive), or false otherwise.OrderedSet<E1>
elementsFromCode(int[][] bundle)
Given a coded bundle as produced by some methods in this class, decodes the elements1 part of the bundle and returns it as a newly-allocated OrderedSet of E1 element keys.int
elementSize()
Gets the total number of unique E1 element keys across all bundles in this BundleBiMap.OrderedSet<E1>
getBundleElements(E2[] lookup)
Given an E2 key to look up, gets a (newly-allocated) OrderedSet of E1 element keys corresponding to that E2 key.OrderedSet<E1>
getBundleElementsAt(int index)
Given an index to look up, gets a (newly-allocated) OrderedSet of E1 element keys in the bundle at that index.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[][]
getCodeAt(int index)
Given an index to look up, gets a 2D int array representing the bundle at that index.int[]
getElement1BundleVariation(E2[] lookup)
Given an E2 key to look up, gets a (newly-allocated) int array that is equivalent to the variation part of the bundle corresponding to single.OrderedSet<E1>
getElement1OrderedSet()
To be called sparingly, since this allocates a new OrderedSet instead of reusing one.SortedSet<E1>
getElement1Set()
Gets and caches the individual E1 keys as a Collection that implements SortedSet (and so also implements Set).OrderedSet<E2>
getElement2(E1[] e1)
Given a bundle of E1 keys as an array with no variation, gets the matching E2 key for that bundle, or null if there is none.OrderedSet<E2>
getElement2(E1[] e1, int[] variations)
Given a bundle of E1 keys as an array with an int array variation, gets the matching E2 key for that bundle, or null if there is none.OrderedSet<E2>
getElement2(Collection<? extends E1> e1)
Given a bundle of E1 keys as a Collection with no variation, gets the matching E2 key for that bundle, or null if there is none.OrderedSet<E2>
getElement2(Collection<? extends E1> e1, int[] variations)
Given a bundle of E1 keys as a Collection with an int array variation, gets the matching E2 key for that bundle, or null if there is none.OrderedSet<E2>
getElement2At(int index)
Given an index to look up, gets the E2 key present at that position in the ordering.int[][]
getElement2Coded(int[][] code)
Given an E1 bundle as a coded 2D int array, gets the matching E2 bundle, also coded, or null if there is none.OrderedSet<E2>
getElement2OrderedSet()
To be called sparingly, since this allocates a new OrderedSet instead of reusing one.SortedSet<E2>
getElement2Set()
Gets and caches the E2 single keys as a Collection that implements SortedSet (and so also implements Set).OrderedSet<int[][]>
getManyCoded(E1 element)
Given an E1 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.OrderedSet<OrderedSet<? extends E2>>
getManyElement2(E1 element)
Given an E1 element key that could be used in one or more bundles this uses, finds all E2 single keys corresponding to bundles that contain the given element.int[]
getManyIndices(E1 element)
Given an E1 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.int
indexOfElement2Coded(int[][] code)
Gets (in near-constant time) the index of the given E2 coded key in the ordering.boolean
isEmpty()
Iterator<E1>
iteratorElement1()
Creates a new iterator over the individual E1 element keys this holds, with a larger total count the iterator may yield thansize()
in most cases (it should be equal toelementSize()
), 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).Iterator<E2>
iteratorElement2()
Creates a new iterator over the E2 single keys this holds.boolean
put(E1[] e1, int[] variation1, E2[] e2, int[] variation2)
Adds a bundle of E1 keys, mixed with an int array of variations, and a E2 key at the same point in the ordering (the end) to this BundleBiMap.boolean
put(E1[] e1, E2[] e2)
Adds a bundle of E1 keys and a E2 key at the same point in the ordering (the end) to this BundleBiMap.boolean
put(Collection<? extends E1> e1, int[] variation1, Collection<? extends E2> e2, int[] variation2)
Adds a bundle of E1 keys, mixed with an int array of variations, and a bundle of E2 keys at the same point in the ordering (the end) to this BundleBiMap.boolean
put(Collection<? extends E1> e1, Collection<? extends E2> e2)
Adds a bundle of E1 keys and a E2 key at the same point in the ordering (the end) to this BundleBiMap.boolean
putAll(Iterable<? extends Collection<? extends E1>> aKeys, Iterable<? extends Collection<? extends E2>> bKeys)
Puts all unique E1 and E2 keys inaKeys
andbKeys
into this K2 at the end.boolean
putAll(BundleBundleBiMap<? extends E1,? extends E2> other)
Puts all unique E1 and E2 keys inother
into this K2, respecting other's ordering.E1
randomElement1(IRNG random)
Gets a random E1 from this BundleBiMap's individual elements1 using the given IRNG.E2
randomElement2(IRNG random)
Gets a random E2 from this BundleBiMap using the given IRNG.BundleBundleBiMap<E1,E2>
reorder(int... ordering)
Reorders this BundleBiMap usingordering
, which has the same length as this object'ssize()
and can be generated withArrayTools.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 callingshuffle(IRNG)
), or made in some other way.BundleBundleBiMap<E1,E2>
shuffle(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.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.
-
Constructor Details
-
BundleBundleBiMap
public BundleBundleBiMap()Constructs an empty BundleBiMap. -
BundleBundleBiMap
Constructs a BundleBiMap with the expected number of indices to hold (the number of bundles and number of E2 items is always the same, and this will be more efficient if expected is greater than that number).- Parameters:
expected
- how many bundle-to-single pairings this is expected to hold
-
BundleBundleBiMap
Constructs a BundleBiMap with the expected number of indices to hold (the number of bundles and number of E2 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).- Parameters:
expected
- the amount of indices (the count of bundles is the same as the count of E2 items) this should holdf
- the load factor, probably between 0.1f and 0.8f
-
BundleBundleBiMap
Constructs a BundleBiMap using another BundleBiMap to copy.- Parameters:
other
- the other BundleBiMap to copy
-
-
Method Details
-
containsElement1
Returns true if this contains the E1, element, in any of its bundles of E1 keys.- Parameters:
element
- the E1 to check the presence of in all bundles- Returns:
- true if element is present in this; false otherwise
-
containsElement2
Returns true if this contains the E2, element, in its collection of E2 items.- Parameters:
element
- the E2 to check the presence of- Returns:
- true if element is present in this; false otherwise
-
containsIndex
Returns true if index is between 0 (inclusive) andsize()
(exclusive), or false otherwise.- Parameters:
index
- the index to check- Returns:
- true if index is a valid index in the ordering of this BundleBiMap
-
randomElement1
Gets a random E1 from this BundleBiMap's individual elements1 using the given IRNG.- Parameters:
random
- generates a random index to get an E1 with- Returns:
- a randomly chosen E1, or null if this is empty
-
randomElement2
Gets a random E2 from this BundleBiMap using the given IRNG.- Parameters:
random
- generates a random index to get a E2 with- Returns:
- a randomly chosen E2, or null if this is empty
-
put
Adds a bundle of E1 keys and a E2 key at the same point in the ordering (the end) to this BundleBiMap. Neither parameter can be present in this collection before this is called.- Parameters:
e1
- an array of E1 keys to add; the array cannot already be present, nor can it be nulle2
- an array of E2 keys to add; the array cannot already be present, nor can it be null- Returns:
- true if this collection changed as e1 result of this call
-
put
Adds a bundle of E1 keys, mixed with an int array of variations, and a E2 key at the same point in the ordering (the end) to this BundleBiMap. Neither the E2 key nor the bundle (effectively, the pair of e1 and variation) can be present in this collection before this is called.- Parameters:
e1
- an array of E1 keys to add; the array cannot already have been inserted with an equivalent variation, nor can it be nullvariation1
- an int array that can be used to make a different version of e1, i.e. the same things at different quantities; cannot be nulle2
- an array of E2 keys to add; the array cannot already have been inserted with an equivalent variation, nor can it be nullvariation2
- an int array that can be used to make a different version of e2, i.e. the same things at different quantities; cannot be null- Returns:
- true if this collection changed as e1 result of this call
-
put
Adds a bundle of E1 keys and a E2 key at the same point in the ordering (the end) to this BundleBiMap. Neither parameter can be present in this collection before this is called.- Parameters:
e1
- a Collection of E1 keys to add; the contents cannot already be present, nor can it be nulle2
- a Collection of E2 keys to add; the contents cannot already be present, nor can it be null- Returns:
- true if this collection changed as a result of this call
-
put
public boolean put(Collection<? extends E1> e1, int[] variation1, Collection<? extends E2> e2, int[] variation2)Adds a bundle of E1 keys, mixed with an int array of variations, and a bundle of E2 keys at the same point in the ordering (the end) to this BundleBiMap. Neither the E1 bundle nor E2 bundle (effectively, the pair of element collection and variation) can be present in this collection before this is called.- Parameters:
e1
- a Collection of E1 keys to add; the Collection cannot already have been inserted with an equivalent variation, nor can it be nullvariation1
- an int array that can be used to make a different version of e1, i.e. the same things at different quantities; cannot be nulle2
- a Collection of E2 keys to add; the Collection cannot already have been inserted with an equivalent variation, nor can it be nullvariation2
- an int array that can be used to make a different version of e2, i.e. the same things at different quantities; cannot be null- Returns:
- true if this collection changed as a result of this call
-
putAll
public boolean putAll(Iterable<? extends Collection<? extends E1>> aKeys, Iterable<? extends Collection<? extends E2>> bKeys)Puts all unique E1 and E2 keys inaKeys
andbKeys
into this K2 at the end. If an E1 in aKeys or a E2 in bKeys is already present when this would add one, this will not put the E1 and E2 keys at that point in the iteration order, and will place the next unique E1 and E2 it finds in the arguments at that position instead.- Parameters:
aKeys
- an Iterable or Collection of E1 keys to add; should all be unique (like a Set)bKeys
- an Iterable or Collection of E2 keys to add; should all be unique (like a Set)- Returns:
- true if this collection changed as a result of this call
-
putAll
Puts all unique E1 and E2 keys inother
into this K2, respecting other's ordering. If an E1 or a E2 in other is already present when this would add one, this will not put the E1 and E2 keys at that point in the iteration order, and will place the next unique E1 and E2 it finds in the arguments at that position instead.- Parameters:
other
- another K2 collection with the same E1 and E2 types- Returns:
- true if this collection changed as a result of this call
-
getCodeAt
Given an index to look up, gets a 2D int array representing the bundle at that index. The bundle is likely to use a representation for the first sub-array that will be meaningless without internal information from this BundleBiMap, but the second sub-array, if present, should match the variation supplied with that bundle toput(Object[], int[], Object[], int[])
orput(Collection, int[], Collection, int[])
.
This method copies the 2D int array it returns, so modifying it won't affect the original BundleBiMap.- Parameters:
index
- an int index to look up- Returns:
- a 2D int array that represents a bundle, or null if index is out of bounds
-
getElement2At
Given an index to look up, gets the E2 key present at that position in the ordering.- Parameters:
index
- an int index to look up
-
getElement2
Given a bundle of E1 keys as an array with no variation, gets the matching E2 key for that bundle, or null if there is none.- Parameters:
e1
- an array of E1- Returns:
- the E2 keys that correspond to the given bundle, e1 combined with variations
-
getElement2
Given a bundle of E1 keys as an array with an int array variation, gets the matching E2 key for that bundle, or null if there is none.- Parameters:
e1
- an array of E1 element keysvariations
- an int array that should match an int array used as a variation parameter to put- Returns:
- the E2 keys that correspond to the given bundle, e1 combined with variations
-
getElement2
Given a bundle of E1 keys as a Collection with no variation, gets the matching E2 key for that bundle, or null if there is none.- Parameters:
e1
- a Collection of E1 element keys (each key can be an E1 or an instance of any subclass of E1)- Returns:
- the E2 key that corresponds to the given bundle, e
-
getElement2
Given a bundle of E1 keys as a Collection with an int array variation, gets the matching E2 key for that bundle, or null if there is none.- Parameters:
e1
- a Collection of E1 element keys (each key can be an E1 or an instance of any subclass of E1)variations
- an int array that should match an int array used as a variation parameter to put- Returns:
- the E2 key that corresponds to the given bundle, e1 combined with variations
-
getElement2Coded
Given an E1 bundle as a coded 2D int array, gets the matching E2 bundle, also coded, or null if there is none. The code parameter should be obtained from one of the methods that specifically returns that kind of 2D array, since the code uses internal information to efficiently represent a bundle.- Parameters:
code
- a 2D int array representing a bundle that should have been obtained directly from this object- Returns:
- the E2 key that corresponds to the given coded bundle
-
indexOfElement2Coded
Gets (in near-constant time) the index of the given E2 coded key in the ordering.- Parameters:
code
- a E2 coded key to look up- Returns:
- the position in the ordering of code
-
elementsFromCode
Given a coded bundle as produced by some methods in this class, decodes the elements1 part of the bundle and returns it as a newly-allocated OrderedSet of E1 element keys.- Parameters:
bundle
- a coded bundle as a 2D int array- Returns:
- an OrderedSet of E1 element keys corresponding to the data coded into bundle
-
variationFromCode
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. If there is no variation in the given coded bundle, this returns null.- Parameters:
bundle
- a coded bundle as a 2D int array- Returns:
- the variation part of the data coded into bundle
-
getBundleElements
Given an E2 key to look up, gets a (newly-allocated) OrderedSet of E1 element keys corresponding to that E2 key. If a variation is part of the bundle, it will not be present in this, but a copy can be retrieved withgetElement1BundleVariation(Object[])
.- Parameters:
lookup
- a E2 key to look up- Returns:
- an OrderedSet of the E1 elements1 used in the bundle that corresponds to single, or null if invalid
-
getElement1BundleVariation
Given an E2 key to look up, gets a (newly-allocated) int array that is equivalent to the variation part of the bundle corresponding to single. If there is no variation in the corresponding bundle, then this returns null. To get the E1 elements1 that are the main part of a bundle, you can usegetBundleElements(Object[])
.- Parameters:
lookup
- a bundle of E2 keys to look up- Returns:
- the variation part of the E1 bundle that corresponds to the looked-up bundle, or null if invalid
-
getBundleElementsAt
Given an index to look up, gets a (newly-allocated) OrderedSet of E1 element keys in the bundle at that index. If a variation is part of the bundle, it will not be present in this, but a copy can be retrieved withgetBundleVariationAt(int)
.- Parameters:
index
- an int position in the ordering to look up- Returns:
- an OrderedSet of the E1 elements1 used in the bundle present at index, or null if invalid
-
getBundleVariationAt
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. If there is no variation in the corresponding bundle, then this returns null. To get the E1 elements1 that are the main part of a bundle, you can usegetBundleElementsAt(int)
.- Parameters:
index
- an int position in the ordering to look up- Returns:
- an int array copied from the variation part of the bundle present at index, or null if invalid
-
getManyElement2
Given an E1 element key that could be used in one or more bundles this uses, finds all E2 single keys corresponding to bundles that contain the given element. Thus, if E1 was String and this BundleBiMap contained ["Copper", "Tin"] mapped to "Bronze" and ["Zinc", "Copper"] mapped to "Brass", then calling this method with "Copper" would get an OrderedSet that contains ["Bronze", "Brass"].- Parameters:
element
- an E1 element key to look up (probably a component of one or more bundles)- Returns:
- an OrderedSet of all E2 keys where the given element is part of the bundle corresponding to that E2 key, or null if E1 does not match anything
-
getManyCoded
Given an E1 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. These coded bundles can be given toelementsFromCode(int[][])
andvariationFromCode(int[][])
to get usable information from them.- Parameters:
element
- an E1 element key to look up (probably a component of one or more bundles)- Returns:
- an OrderedSet of all coded 2D int array bundles that contain the given element, or null if E1 is not in any bundles
-
getManyIndices
Given an E1 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. From such an index, you can usegetElement2At(int)
(int)} to get the E2 key at that position,getBundleElementsAt(int)
to get the E1 element keys at that position,getBundleVariationAt(int)
to get the possible variation at that position, orgetCodeAt(int)
to get the coded bundle at that position.- Returns:
- an OrderedSet of all coded 2D int array bundles that contain the given element, or null if E1 is not in any bundles
-
reorder
Reorders this BundleBiMap usingordering
, which has the same length as this object'ssize()
and can be generated withArrayTools.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 callingshuffle(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 useArrayTools.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."- Parameters:
ordering
- an int array or vararg that must contain each int from 0 tosize()
- Returns:
- this for chaining
-
shuffle
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.- Parameters:
rng
- an IRNG to produce the random ordering this will use- Returns:
- this for chaining
-
iteratorElement1
Creates a new iterator over the individual E1 element keys this holds, with a larger total count the iterator may yield thansize()
in most cases (it should be equal toelementSize()
), 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 E1 keys are individual, not bundled, and duplicate keys should never be encountered even if an E1 key appears in many bundles.- Returns:
- a newly-created iterator over this BundleBiMap's individual (non-bundled) E1 keys
-
iteratorElement2
Creates a new iterator over the E2 single keys this holds. The total number of items this yields should be equal tosize()
. 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) usinggetElement2At(int)
in a for(int i=0...) loop.- Returns:
- a newly-created iterator over this BundleBiMap's E2 keys
-
getElement1Set
Gets and caches the individual E1 keys as a Collection that implements SortedSet (and so also implements Set).- Returns:
- the E1 keys as a SortedSet
-
getElement2Set
Gets and caches the E2 single keys as a Collection that implements SortedSet (and so also implements Set).- Returns:
- the E2 keys as a SortedSet
-
getElement1OrderedSet
To be called sparingly, since this allocates a new OrderedSet instead of reusing one.- Returns:
- the E1 keys as an OrderedSet
-
getElement2OrderedSet
To be called sparingly, since this allocates a new OrderedSet instead of reusing one.- Returns:
- the E2 keys as an OrderedSet
-
size
Gets the total number of bundle-to-single pairs in this BundleBiMap.- Returns:
- the total number of bundle keys (equivalently, the number of single keys) in this object
-
elementSize
Gets the total number of unique E1 element keys across all bundles in this BundleBiMap. Usually not the same assize()
, and ordinarily a fair amount larger, though this can also be smaller.- Returns:
- the total number of unique E1 element keys in all bundles this contains
-
isEmpty
-