public class MultiKey
extends java.lang.Object
which
, that specifies which
keyset the method applies to. For example, contains(int, Object)
checks for the presence of the second
parameter in the keyset specified by the first parameter. Adding items to a MultiKey uses put(Object...)
,
and that does not take a which
parameter because it needs to add an item to every keyset to succeed, and will
do nothing if the array or varargs passed to it has a different length than the keyCount
of the MultiKey.
Created by Tommy Ettinger on 10/23/2016.Modifier and Type | Field and Description |
---|---|
int |
keyCount |
Constructor and Description |
---|
MultiKey()
Constructs an empty MultiKey.
|
MultiKey(squidpony.squidmath.Arrangement[] keysets) |
MultiKey(java.util.Collection<java.lang.Iterable> keysets)
Constructs a MultiKey from a Collection of Iterables that will be processed in tandem, adding a unique item from
each keyset in keysets if and only if it can also add a unique item from all other keysets, otherwise skipping
that iteration in each Iterable.
|
MultiKey(int keyCount,
int expected)
Constructs a MultiKey with the expected number of indices to hold (the number of items in each keyset is always
the same, and this will be more efficient if expected is greater than that number).
|
MultiKey(int keyCount,
int expected,
float f)
Constructs a MultiKey with the expected number of indices to hold (the number of items in each keyset 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).
|
MultiKey(MultiKey other) |
Modifier and Type | Method and Description |
---|---|
MultiKey |
alter(int which,
java.lang.Object past,
java.lang.Object future)
In the keyset specified by
which , changes an existing key, past , to another key, future ,
if past exists in that keyset and future does not yet exist in that keyset. |
MultiKey |
alterAt(int which,
int index,
java.lang.Object future)
In the keyset specified by
which , changes the key at index to another key, future , if
index is valid and future does not yet exist in that keyset. |
boolean |
contains(int which,
java.lang.Object key)
Returns true if this contains key in the keyset specified by which.
|
boolean |
containsIndex(int index)
Returns true if index is between 0 (inclusive) and
size() (exclusive), or false otherwise. |
java.lang.Object[] |
getAllAt(int index)
Given an int index, finds the associated keys at all keysets (using index as a point in the ordering) and returns
them as a newly-allocated Object array.
|
java.lang.Object[] |
getAllAt(int index,
java.lang.Object[] into)
Given an int index and an Object array to reuse, finds the associated keys at all keysets (using index as a point
in the ordering) and fills into with those keys, up to keyCount items.
|
java.lang.Object |
getAt(int which,
int index)
Given an index of a keyset (which) and an int index, finds the associated key in the keyset specified by which
(using index as a point in the ordering).
|
java.lang.Object |
getFrom(int lookingUp,
int getting,
java.lang.Object key)
Given an index of the keyset to look up a key in (lookingUp), an index of the keyset to get from (getting), and
an Object key to look up (key), finds the Object key in the keyset specified by getting that is associated with
key in the keyset specified by lookingUp.
|
squidpony.squidmath.OrderedSet |
getOrderedSet(int which)
To be called sparingly, since this allocates a new OrderedSet instead of reusing one.
|
java.util.SortedSet |
getSet(int which)
Gets and caches the keys in the keyset specified by which as a Collection that implements SortedSet (and so also
implements Set).
|
int |
indexOf(int which,
java.lang.Object key)
Given an index of a keyset (which) and an Object key, finds the position in the ordering that key has in the
keyset at which, or -1 if key is not present.
|
boolean |
isEmpty() |
java.util.Iterator |
iterator(int which)
Creates a new iterator over the keys this holds in the keyset specified by which.
|
int |
keyCount() |
boolean |
put(java.lang.Object... k)
Adds a key to each keyset at the same point in the ordering (the end) of this MultiKey.
|
boolean |
putAll(java.util.Collection<java.lang.Iterable> k)
Goes through all Iterable items in
k and adds their unique items into their corresponding keyset at the
end. |
boolean |
putAll(MultiKey other)
Puts all unique keys in
other into this MultiKey, respecting other's ordering. |
boolean |
putAt(int index,
java.lang.Object... k)
Adds a key to each keyset at the given index in the ordering of this MultiKey.
|
java.lang.Object |
randomKey(int which,
squidpony.squidmath.IRNG random)
Gets a random key from the keyset specified by which using the given IRNG.
|
java.lang.Object |
randomKey(squidpony.squidmath.IRNG random)
Gets a random key from a random keyset in this MultiKey using the given IRNG.
|
MultiKey |
remove(int which,
java.lang.Object removing)
Removes a given Object key from the keyset specified by which, if
removing exists in that keyset, and
also removes any keys associated with its point in the ordering. |
MultiKey |
removeAt(int index)
Removes a given point in the ordering, if
index is at least 0 and less than size() . |
MultiKey |
reorder(int... ordering)
Reorders this MultiKey using
ordering , which have the same length as this MultiKey'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. |
MultiKey |
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.
|
int |
size() |
int |
valueCount() |
public MultiKey()
public MultiKey(int keyCount, int expected)
expected
- how many items this should be ready to hold; can resize if neededpublic MultiKey(int keyCount, int expected, float f)
expected
- the amount of indices (the number of items in each keyset is always the same) this should holdf
- the load factor, probably between 0.1f and 0.8fpublic MultiKey(java.util.Collection<java.lang.Iterable> keysets)
keysets
- a Collection of Iterable data structures, each containing items that should all be uniquepublic MultiKey(MultiKey other)
public MultiKey(squidpony.squidmath.Arrangement[] keysets)
public boolean contains(int which, java.lang.Object key)
which
- which keyset to check inkey
- the key to check the presence ofpublic boolean containsIndex(int index)
size()
(exclusive), or false otherwise.index
- the index to checkpublic int indexOf(int which, java.lang.Object key)
List.indexOf(Object)
, this runs in constant time.which
- which keyset to check inkey
- the key to find the position ofpublic java.lang.Object getAt(int which, int index)
which
- which keyset to get fromindex
- an int index into this MultiKeypublic java.lang.Object[] getAllAt(int index)
index
- an int index into this MultiKeygetAllAt can avoid allocating a new array each time
public java.lang.Object[] getAllAt(int index, java.lang.Object[] into)
index
- an int index into this MultiKeyinto
- an Object array to reuse and fill with items; will be returned as-is if smaller than keyCountpublic java.lang.Object getFrom(int lookingUp, int getting, java.lang.Object key)
lookingUp
- which keyset to look up the key
parameter ingetting
- which keyset to get a value fromkey
- an object to use as a key, which should be the right type for the keyset at lookingUppublic java.lang.Object randomKey(int which, squidpony.squidmath.IRNG random)
which
- which keyset to get a random key fromrandom
- generates a random index to get a key withpublic java.lang.Object randomKey(squidpony.squidmath.IRNG random)
random
- generates a random keyset index and random item index, to get a random keypublic MultiKey alter(int which, java.lang.Object past, java.lang.Object future)
which
, changes an existing key, past
, to another key, future
,
if past exists in that keyset and future does not yet exist in that keyset. 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.which
- which keyset to alter the items inpast
- a key, that must exist in the keyset specified by which, and will be changedfuture
- a key, that cannot currently exist in the keyset specified by which, but will if this succeedspublic MultiKey alterAt(int which, int index, java.lang.Object future)
which
, changes the key at index
to another key, future
, if
index is valid and future does not yet exist in that keyset. 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.which
- which keyset to alter the items inindex
- a position in the ordering to change; must be at least 0 and less than size()
future
- a key, that cannot currently exist in the keyset specified by which, but will if this succeedspublic boolean put(java.lang.Object... k)
alter(int, Object, Object)
or alterAt(int, int, Object)
.k
- an array or varargs of keys to add after the last index of this MultiKey; length must equal keyCountpublic boolean putAll(java.util.Collection<java.lang.Iterable> k)
k
and adds their unique items into their corresponding keyset at the
end. If an item from the nth Iterable is already present in the nth keyset in this when this would add one, this
will not put any keys at that point in the iteration order, and will place the next unique group of items it
finds in the arguments at that position instead.k
- a Collection of Iterable s of keys to add to their respective keysets; should all be unique (like a Set)public boolean putAll(MultiKey other)
other
into this MultiKey, respecting other's ordering. If a key in other is
already present when this would add one, this will not put the keys at that point in the iteration
order, and will place the next unique items it finds in other at that position instead.other
- another MultiKey collection with the same keyCountpublic boolean putAt(int index, java.lang.Object... k)
alter(int, Object, Object)
or alterAt(int, int, Object)
.k
- an array or varargs of keys to add after the last index of this MultiKey; length must equal keyCountpublic MultiKey remove(int which, java.lang.Object removing)
removing
exists in that keyset, and
also removes any keys associated with its point in the ordering.which
- which keyset to remove the item from; if removing
isn't in that keyset, this does nothingremoving
- the key to removepublic MultiKey removeAt(int index)
index
is at least 0 and less than size()
.index
- the position in the ordering to removepublic MultiKey reorder(int... ordering)
ordering
, which have the same length as this MultiKey'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.ordering
- an int array or vararg that should contain each int from 0 to size()
(or less)public MultiKey shuffle(squidpony.squidmath.IRNG rng)
rng
- an IRNG to produce the random ordering this will usepublic java.util.Iterator iterator(int which)
getAt(int, int)
in a for(int i=0...) loop.public java.util.SortedSet getSet(int which)
which
- which keyset to get as a separate valuepublic squidpony.squidmath.OrderedSet getOrderedSet(int which)
which
- which keyset to get as a separate valuepublic int keyCount()
public int valueCount()
public int size()
public boolean isEmpty()
Copyright © Eben Howard 2012–2022. All rights reserved.