/** * Given an int index, finds the associated A key (using index as a point in the ordering). * @param index an int index into this K2 * @return the A object with index for its position in the ordering, or null if index was invalid */ public A getAAt(int index) { return keysA.keyAt(index); } /**
/** * Given an int index, finds the associated B key (using index as a point in the ordering). * @param index an int index into this K2 * @return the B object with index for its position in the ordering, or null if index was invalid */ public B getBAt(int index) { return keysB.keyAt(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). * @param which which keyset to get from * @param index an int index into this MultiKey * @return the key (in the keyset found by which) with index for its position in the ordering, or null if index or which was invalid */ public Object getAt(int which, int index) { if(which >= 0 && which < keyCount) return keys[which].keyAt(index); return null; }
/** * Gets the name that corresponds to the given identifying number. * @param number the number to look up, like 1 * @return the corresponding name as a String, or null if no such number is used */ public String numberToName(int number) { return names.keyAt(number); }
/** * 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. * @param index an int index into this MultiKey * @return the array of keys with index for their position in the ordering, or an array of null if index was invalid * @see #getAllAt(int, Object[]) getAllAt can avoid allocating a new array each time */ public Object[] getAllAt(int index) { Object[] os = new Object[keyCount]; for (int i = 0; i < keyCount; i++) { os[i] = keys[i].keyAt(index); } return os; }
/** * 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. * @param index an int index into this MultiKey * @param into an Object array to reuse and fill with items; will be returned as-is if smaller than keyCount * @return the array of keys with index for their position in the ordering, or an array of null if index was invalid */ public Object[] getAllAt(int index, Object[] into) { if (into != null && into.length >= keyCount) { for (int i = 0; i < keyCount; i++) { into[i] = keys[i].keyAt(index); } } return into; }
/** * Gets a random key from this Arrangement in constant time, using the given IRNG to generate a random number. * @param rng used to generate a random index for a key * @return a random key from this Arrangement, or null if this is empty */ public K randomKey(IRNG rng) { if(rng == null) return null; return keyAt(rng.nextInt(order.size)); }
/** * Given an A object, finds the associated B object (it will be at the same point in the ordering). * @param key an A object to use as a key * @return the B object associated with key, or null if key was not present */ public B getBFromA(Object key) { return keysB.keyAt(keysA.getInt(key)); }
/** * Changes the K at the given index to replacement while keeping replacement at the same point in the ordering. * * @param index an index to replace the K key at * @param replacement another K key that will replace the original at the remembered index * @return the int associated with the possibly-altered key; should be equal to index */ public int alterAt(int index, K replacement) { return alter(keyAt(index), replacement); }
/** * Given a B object, finds the associated A object (it will be at the same point in the ordering). * @param key a B object to use as a key * @return the A object associated with key, or null if key was not present */ public A getAFromB(Object key) { return keysA.keyAt(keysB.getInt(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. key and the returned value will be at the same point in the ordering. * @param lookingUp which keyset to look up the {@code key} parameter in * @param getting which keyset to get a value from * @param key an object to use as a key, which should be the right type for the keyset at lookingUp * @return the object from getting's keyset that is associated with key, or null if key was not present */ public Object getFrom(int lookingUp, int getting, Object key) { if(lookingUp >= 0 && lookingUp < keyCount && getting >= 0 && getting < keyCount) return keys[getting].keyAt(keys[lookingUp].getInt(key)); return null; }
@Override public String toString() { final StringBuilder s = new StringBuilder(); boolean first = true; K k; s.append("Arrangement{"); for (int i = 0; i < size; i++) { if (first) first = false; else s.append(", "); k = keyAt(i); s.append(k == Arrangement.this ? "(this collection)" : String.valueOf(k)) .append("=>") .append(getAt(i)); } s.append("}"); return s.toString(); } @Override
/** * Produces a copy of this Arrangement, but only using up to the given amount of items to take. Does a shallow copy * of individual keys, so the references will be shared. * @param amount the number of items to copy from this Arrangement; will copy all items if greater than size * @return a new Arrangement with up to amount items copied from this into it. */ public Arrangement<K> take(int amount) { amount = Math.min(size, Math.max(0, amount)); Arrangement<K> nx = new Arrangement<>(amount, f); for (int i = 0; i < amount; i++) { nx.add(keyAt(i)); } return nx; } protected int positionOf(final Object k) {
public OrderedSet<K> keysAsOrderedSet() { OrderedSet<K> os = new OrderedSet<K>(size, f, hasher); for (int i = 0; i < size; i++) { os.add(keyAt(i)); } return os; }
/** * In the keyset specified by {@code which}, changes the key at {@code index} to another key, {@code 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. * @param which which keyset to alter the items in * @param index a position in the ordering to change; must be at least 0 and less than {@link #size()} * @param future a key, that cannot currently exist in the keyset specified by which, but will if this succeeds * @return this for chaining */ public MultiKey alterAt(int which, int index, Object future) { if(which >= 0 && which < keyCount && !keys[which].containsKey(future) && index >= 0 && index < keys[which].size) keys[which].alter(keys[which].keyAt(index), future); return this; }
/** * Changes the A key at {@code index} to another A key, {@code 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. * @param index a position in the ordering to change; must be at least 0 and less than {@link #size()} * @param future an A key, that cannot currently exist in this K2's A keys, but will if this succeeds * @return this for chaining */ public K2<A, B> alterAAt(int index, A future) { if(!keysA.containsKey(future) && index >= 0 && index < keysA.size) keysA.alter(keysA.keyAt(index), future); return this; }
@Override public String stringify(Arrangement<K> item) { StringBuilder sb = new StringBuilder(100); K k; for (int i = 0; i < item.size(); ) { k = item.keyAt(i); if (item == k) return ""; ObText.appendQuoted(sb, convert.stringify(k)); if (++i < item.size()) sb.append(' '); } return sb.toString(); }
/** * Changes the B key at {@code index} to another B key, {@code 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. * @param index a position in the ordering to change; must be at least 0 and less than {@link #size()} * @param future a B key, that cannot currently exist in this K2's B keys, but will if this succeeds * @return this for chaining */ public K2<A, B> alterBAt(int index, B future) { if(!keysB.containsKey(future) && index >= 0 && index < keysB.size) keysB.alter(keysB.keyAt(index), future); return this; } /**
public OrderedSet<K> keysAt(int... positions) { if(keys == null || positions == null || positions.length <= 0) return new OrderedSet<K>(); OrderedSet<K> ks = new OrderedSet<>(positions.length); for(int i = 0; i < positions.length; i++) { ks.add(keyAt(positions[i])); } return ks; } public OrderedSet<K> keysAt(IntVLA positions)
public OrderedSet<K> keysAt(IntVLA positions) { if(keys == null || positions == null || positions.size <= 0) return new OrderedSet<K>(); OrderedSet<K> ks = new OrderedSet<>(positions.size); for(int i = 0; i < positions.size; i++) { ks.add(keyAt(positions.get(i))); } return ks; }