public class K2V1<A,B,Q>
extends java.lang.Object
implements java.io.Serializable
Modifier and Type | Field and Description |
---|---|
K2<A,B> |
keys |
java.util.ArrayList<Q> |
values |
Constructor and Description |
---|
K2V1()
Constructs an empty K2V1 with the default parameters: 32 expected indices and a load factor of 0.5f.
|
K2V1(int expected)
Constructs an empty K2V1 that can hold
expected indices before resizing and has a load factor of 0.5f. |
K2V1(int expected,
float f)
Constructs an empty K2V1 that can hold
expected indices before resizing and has load factor f . |
K2V1(java.lang.Iterable<A> aKeys,
java.lang.Iterable<B> bKeys,
java.lang.Iterable<Q> qValues)
Constructs a K2V1 from an A iterable, a B iterable, and a Q iterable, where the A and B items should be unique
(if they aren't, each item that would be associated with a duplicate A or B will be skipped).
|
K2V1(K2V1<A,B,Q> other)
Constructs a K2V1 from another K2V1 with ths same A, B, and Q types.
|
Modifier and Type | Method and Description |
---|---|
K2V1<A,B,Q> |
alterA(A past,
A future)
Changes an existing A key,
past , to another A key, future , if past exists in this K2V1
and future does not yet exist in this K2V1. |
K2V1<A,B,Q> |
alterAAt(int index,
A future)
Changes the A key at
index to another A key, future , if index is valid and future does not
yet exist in this K2V1. |
K2V1<A,B,Q> |
alterB(B past,
B future)
Changes an existing B key,
past , to another B key, future , if past exists in this K2V1
and future does not yet exist in this K2V1. |
K2V1<A,B,Q> |
alterBAt(int index,
B future)
Changes the B key at
index to another B key, future , if index is valid and future does not
yet exist in this K2V1. |
K2V1<A,B,Q> |
alterQAt(int index,
Q future)
Changes the Q value at
index to another Q value, future , if index is valid. |
boolean |
containsA(A key)
Returns true if this contains the A, key, in its collection of A items, or false otherwise.
|
boolean |
containsB(B key)
Returns true if this contains the B, key, in its collection of B items, or false otherwise.
|
boolean |
containsIndex(int index)
Returns true if index is between 0 (inclusive) and
size() (exclusive), or false otherwise. |
boolean |
containsQ(Q value)
Returns true if this contains at least one Q value, or false otherwise
|
A |
getAAt(int index)
Given an int index, finds the associated A key (using index as a point in the ordering).
|
A |
getAFromB(java.lang.Object key)
Given a B object, finds the associated A object (it will be at the same point in the ordering).
|
java.util.ArrayList<Q> |
getArrayListQ()
|
B |
getBAt(int index)
Given an int index, finds the associated B key (using index as a point in the ordering).
|
B |
getBFromA(java.lang.Object key)
Given an A object, finds the associated B object (it will be at the same point in the ordering).
|
java.util.ArrayList<Q> |
getListQ()
|
OrderedSet<A> |
getOrderedSetA()
Returns a separate (shallow) copy of the set of A keys as an
OrderedSet . |
OrderedSet<B> |
getOrderedSetB()
Returns a separate (shallow) copy of the set of B keys as an
OrderedSet . |
Q |
getQAt(int index)
Given an int index, finds the associated Q value (using index as a point in the ordering).
|
Q |
getQFromA(java.lang.Object key)
Given an A object, finds the associated Q value (it will be at the same point in the ordering).
|
Q |
getQFromB(java.lang.Object key)
Given a B object, finds the associated Q value (it will be at the same point in the ordering).
|
java.util.SortedSet<A> |
getSetA()
Gets and caches the A keys as a Collection that implements SortedSet (and so also implements Set).
|
java.util.SortedSet<B> |
getSetB()
Gets and caches the B keys as a Collection that implements SortedSet (and so also implements Set).
|
int |
indexOfA(java.lang.Object key)
Given an A object key, finds the position in the ordering which that A has, or -1 if key is not present.
|
int |
indexOfB(java.lang.Object key)
Given a B object key, finds the position in the ordering which that B has, or -1 if key is not present.
|
int |
indexOfQ(java.lang.Object value)
Given a Q value, finds the first position in the ordering which contains that value, or -1 if not present.
|
boolean |
isEmpty()
I think you can guess what this does.
|
java.util.Iterator<A> |
iteratorA()
Creates a new iterator over the A keys this holds.
|
java.util.Iterator<B> |
iteratorB()
Creates a new iterator over the B keys this holds.
|
java.util.Iterator<Q> |
iteratorQ()
Creates a new iterator over the Q values this holds.
|
int |
keyCount() |
boolean |
put(A aKey,
B bKey,
Q qValue)
Adds an A key, a B key, and a Q value at the same point in the ordering (the end) to this K2V1.
|
boolean |
putAll(java.lang.Iterable<A> aKeys,
java.lang.Iterable<B> bKeys,
java.lang.Iterable<Q> qValues) |
boolean |
putAll(K2V1<A,B,Q> other)
Puts all unique A and B keys in
other into this K2V1, respecting other's ordering. |
boolean |
putAt(int index,
A a,
B b,
Q q)
Adds an A key, a B key, and a Q value at the given index in the ordering to this K2V1.
|
A |
randomA(IRNG random)
Gets a random A from this K2V1 using the given IRNG.
|
B |
randomB(IRNG random)
Gets a random B from this K2V1 using the given IRNG.
|
Q |
randomQ(IRNG random)
Gets a random Q from this K2V1 using the given IRNG.
|
K2V1<A,B,Q> |
removeA(A removing)
Removes a given A key, if
removing exists in this K2V1's A keys, and also removes any B key or Q value
associated with its point in the ordering. |
K2V1<A,B,Q> |
removeAt(int index)
Removes a given point in the ordering, if
index is at least 0 and less than size() . |
K2V1<A,B,Q> |
removeB(B removing)
Removes a given B key, if
removing exists in this K2V1's B keys, and also removes any A key or Q value
associated with its point in the ordering. |
K2V1<A,B,Q> |
reorder(int... ordering)
Reorders this K2V1 using
ordering , which have the same length as this K2V1'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. |
K2V1<A,B,Q> |
shuffle(IRNG rng)
Generates a random ordering with rng and applies the same ordering to all keys and values this has; they will
maintain their current association to other keys and values but their ordering/indices will change.
|
int |
size()
Gets the size of this collection.
|
int |
valueCount() |
public K2V1()
public K2V1(int expected)
expected
indices before resizing and has a load factor of 0.5f.expected
- the expected number of items of any single type that this will hold; each put counts as one itempublic K2V1(int expected, float f)
expected
indices before resizing and has load factor f
.expected
- the expected number of items of any single type that this will hold; each put counts as one itemf
- the load factor; must be greater than 0 and less than 1, but should ideally be between 0.2 and 0.8public K2V1(java.lang.Iterable<A> aKeys, java.lang.Iterable<B> bKeys, java.lang.Iterable<Q> qValues)
aKeys
- an Iterable of A keys; if null will be considered empty and this K2V1 will be emptybKeys
- an Iterable of B keys; if null will be considered empty and this K2V1 will be emptyqValues
- an Iterable of Q values; if null will be considered empty and this K2V1 will be emptypublic K2V1(K2V1<A,B,Q> other)
other
- a K2V1 to copy into this; must have the same A, B, and Q typespublic boolean containsA(A key)
key
- the A to check the presence ofpublic boolean containsB(B key)
key
- the B to check the presence ofpublic boolean containsQ(Q value)
value
- the value to checkpublic boolean containsIndex(int index)
size()
(exclusive), or false otherwise.index
- the index to checkpublic int indexOfA(java.lang.Object key)
List.indexOf(Object)
, this runs in constant time.key
- the A to find the position ofpublic int indexOfB(java.lang.Object key)
List.indexOf(Object)
, this runs in constant time.key
- the B to find the position ofpublic int indexOfQ(java.lang.Object value)
ArrayList.indexOf(Object)
to search the values.value
- the value to find the position of, which should be a Qpublic A getAAt(int index)
index
- an int index into this K2V1public B getBAt(int index)
index
- an int index into this K2V1public Q getQAt(int index)
index
- an int index into this K2V1public B getBFromA(java.lang.Object key)
key
- an A object to use as a keypublic A getAFromB(java.lang.Object key)
key
- a B object to use as a keypublic Q getQFromA(java.lang.Object key)
key
- an A object to use as a keypublic Q getQFromB(java.lang.Object key)
key
- a B object to use as a keypublic A randomA(IRNG random)
random
- generates a random index to get an A withpublic B randomB(IRNG random)
random
- generates a random index to get a B withpublic Q randomQ(IRNG random)
random
- generates a random index to get a Q withpublic K2V1<A,B,Q> alterA(A past, A future)
past
, to another A key, future
, if past exists in this K2V1
and future does not yet exist in this K2V1. 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
- an A key, that must exist in this K2V1's A keys, and will be changedfuture
- an A key, that cannot currently exist in this K2V1's A keys, but will if this succeedspublic K2V1<A,B,Q> alterB(B past, B future)
past
, to another B key, future
, if past exists in this K2V1
and future does not yet exist in this K2V1. 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 B key, that must exist in this K2V1's B keys, and will be changedfuture
- a B key, that cannot currently exist in this K2V1's B keys, but will if this succeedspublic K2V1<A,B,Q> alterAAt(int index, A future)
index
to another A key, future
, if index is valid and future does not
yet exist in this K2V1. 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
- an A key, that cannot currently exist in this K2V1's A keys, but will if this succeedspublic K2V1<A,B,Q> alterBAt(int index, B future)
index
to another B key, future
, if index is valid and future does not
yet exist in this K2V1. 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 B key, that cannot currently exist in this K2V1's B keys, but will if this succeedspublic K2V1<A,B,Q> alterQAt(int index, Q future)
index
to another Q value, future
, if index is valid. 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
keys 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 Q value that will be set at the given index if this succeedspublic boolean put(A aKey, B bKey, Q qValue)
alterA(Object, Object)
or alterB(Object, Object)
. The value, qValue, has no restrictions.aKey
- an A key to add; cannot already be presentbKey
- a B key to add; cannot already be presentqValue
- a Q value to add; can be present any number of timespublic boolean putAt(int index, A a, B b, Q q)
alterA(Object, Object)
or alterB(Object, Object)
. The value, q, has no restrictions. The index
this is given should be at least 0 and no greater than size()
.index
- the point in the ordering to place a and b into; later entries will be shifted forwarda
- an A key to add; cannot already be presentb
- a B key to add; cannot already be presentq
- a Q value to add; can be present any number of timespublic boolean putAll(java.lang.Iterable<A> aKeys, java.lang.Iterable<B> bKeys, java.lang.Iterable<Q> qValues)
public boolean putAll(K2V1<A,B,Q> other)
other
into this K2V1, respecting other's ordering. If an A or a B in other
is already present when this would add one, this will not put the A and B keys at that point in the iteration
order, and will place the next unique A and B it finds in the arguments at that position instead.other
- another K2V1 collection with the same A and B typespublic K2V1<A,B,Q> removeA(A removing)
removing
exists in this K2V1's A keys, and also removes any B key or Q value
associated with its point in the ordering.removing
- the A key to removepublic K2V1<A,B,Q> removeB(B removing)
removing
exists in this K2V1's B keys, and also removes any A key or Q value
associated with its point in the ordering.removing
- the B key to removepublic K2V1<A,B,Q> removeAt(int index)
index
is at least 0 and less than size()
.index
- the position in the ordering to removepublic K2V1<A,B,Q> reorder(int... ordering)
ordering
, which have the same length as this K2V1'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 K2V1<A,B,Q> shuffle(IRNG rng)
rng
- an IRNG to produce the random ordering this will usepublic java.util.Iterator<A> iteratorA()
getAAt(int)
in a for(int i=0...) loop.public java.util.Iterator<B> iteratorB()
getBAt(int)
in a for(int i=0...) loop.public java.util.Iterator<Q> iteratorQ()
getQAt(int)
in a for(int i=0...) loop.public java.util.SortedSet<A> getSetA()
public java.util.SortedSet<B> getSetB()
public OrderedSet<A> getOrderedSetA()
OrderedSet
.
To be called sparingly, since this allocates a new OrderedSet instead of reusing one. This can be useful if you
were going to copy the set produced by getSetA()
anyway.public OrderedSet<B> getOrderedSetB()
OrderedSet
.
To be called sparingly, since this allocates a new OrderedSet instead of reusing one. This can be useful if you
were going to copy the set produced by getSetB()
anyway.public java.util.ArrayList<Q> getListQ()
getSetA()
and
getSetB()
, changes made to the returned list will also change this data structure.
It retains the current ordering.public java.util.ArrayList<Q> getArrayListQ()
getSetA()
or getSetB()
, this does
not cache the value list. It retains the current ordering.public int size()
public boolean isEmpty()
public int keyCount()
public int valueCount()
Copyright © Eben Howard 2012–2022. All rights reserved.