/** * 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. * @param bundle a coded bundle as a 2D int array * @return an OrderedSet of E element keys corresponding to the data coded into bundle */ public OrderedSet<E> elementsFromCode(int[][] bundle) { if(bundle == null || bundle.length < 1 || bundle[0] == null) return null; return elements.keysAt(bundle[0]); }
/** * 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. * @param bundle a coded bundle as a 2D int array * @return an OrderedSet of E1 element keys corresponding to the data coded into bundle */ public OrderedSet<E1> elementsFromCode(int[][] bundle) { if(bundle == null || bundle.length < 1 || bundle[0] == null) return null; return elements1.keysAt(bundle[0]); }
/** * Given an index to look up, gets the E2 key present at that position in the ordering. * @param index an int index to look up */ public OrderedSet<E2> getElement2At(int index) { return elements2.keysAt(bm.getBAt(index)[0]); }
/** * Given an index to look up, gets a (newly-allocated) OrderedSet of E 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 with * {@link #getBundleVariationAt(int)}. * @param index an int position in the ordering to look up * @return an OrderedSet of the E elements used in the bundle present at index, or null if invalid */ public OrderedSet<E> getBundleElementsAt(int index) { int[][] bundle; if((bundle = bm.getAAt(index)) == null || bundle.length < 1) return null; return elements.keysAt(bundle[0]); } /**
/** * Given an S key to look up, gets a (newly-allocated) OrderedSet of E element keys corresponding to that S key. * If a variation is part of the bundle, it will not be present in this, but a copy can be retrieved with * {@link #getBundleVariation(Object)}. * @param single a S key to look up * @return an OrderedSet of the E elements used in the bundle that corresponds to single, or null if invalid */ public OrderedSet<E> getBundleElements(S single) { int[][] bundle; if(single == null || (bundle = bm.getAFromB(single)) == null || bundle.length < 1 || bundle[0] == null) return null; return elements.keysAt(bundle[0]); }
/** * 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 with * {@link #getBundleVariationAt(int)}. * @param index an int position in the ordering to look up * @return an OrderedSet of the E1 elements1 used in the bundle present at index, or null if invalid */ public OrderedSet<E1> getBundleElementsAt(int index) { int[][] bundle; if((bundle = bm.getAAt(index)) == null || bundle.length < 1) return null; return elements1.keysAt(bundle[0]); } /**
/** * 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. These coded bundles can be given to * {@link #elementsFromCode(int[][])} and {@link #variationFromCode(int[][])} to get usable information from them. * @param element an E element key to look up (probably a component of one or more bundles) * @return an OrderedSet of all coded 2D int array bundles that contain the given element, or null if E is not in any bundles */ public OrderedSet<int[][]> getManyCoded(E element) { if(element == null) return null; int pos; if((pos = elements.getInt(element)) < 0) return null; return bm.keysA.keysAt(mm.get(pos)); }
/** * 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 to * {@link #elementsFromCode(int[][])} and {@link #variationFromCode(int[][])} to get usable information from them. * @param element an E1 element key to look up (probably a component of one or more bundles) * @return an OrderedSet of all coded 2D int array bundles that contain the given element, or null if E1 is not in any bundles */ public OrderedSet<int[][]> getManyCoded(E1 element) { if(element == null) return null; int pos; if((pos = elements1.getInt(element)) < 0) return null; return bm.keysA.keysAt(mm1.get(pos)); }
/** * 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. Thus, if E 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"]. * @param element an E element key to look up (probably a component of one or more bundles) * @return an OrderedSet of all S keys where the given element is part of the bundle corresponding to that S key, or null if E does not match anything */ public OrderedSet<S> getManySingles(E element) { if(element == null) return null; int pos; if((pos = elements.getInt(element)) < 0) return null; return bm.keysB.keysAt(mm.get(pos)); } /**
/** * 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. * @param e1 an array of E1 * @return the E2 keys that correspond to the given bundle, e1 combined with variations */ public OrderedSet<E2> getElement2(E1[] e1) { if(e1 == null) return null; return elements2.keysAt(bm.getBFromA(new int[][]{elements1.getArray(e1)})[0]); }
/** * 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. * @param e1 an array of E1 element keys * @param variations an int array that should match an int array used as a variation parameter to put * @return the E2 keys that correspond to the given bundle, e1 combined with variations */ public OrderedSet<E2> getElement2(E1[] e1, int[] variations) { if(e1 == null || variations == null) return null; return elements2.keysAt(bm.getBFromA(new int[][]{elements1.getArray(e1), variations})[0]); }
/** * 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. * @param e1 a Collection of E1 element keys (each key can be an E1 or an instance of any subclass of E1) * @return the E2 key that corresponds to the given bundle, e */ public OrderedSet<E2> getElement2(Collection<? extends E1> e1) { if(e1 == null) return null; return elements2.keysAt(bm.getBFromA(new int[][]{elements1.getArray(e1)})[0]); }
/** * 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. * @param e1 a Collection of E1 element keys (each key can be an E1 or an instance of any subclass of E1) * @param variations an int array that should match an int array used as a variation parameter to put * @return the E2 key that corresponds to the given bundle, e1 combined with variations */ public OrderedSet<E2> getElement2(Collection<? extends E1> e1, int[] variations) { if(e1 == null || variations == null) return null; return elements2.keysAt(bm.getBFromA(new int[][]{elements1.getArray(e1), variations})[0]); } /**
/** * Puts all unique E1 and E2 keys in {@code other} 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. * @param other another K2 collection with the same E1 and E2 types * @return true if this collection changed as a result of this call */ public boolean putAll(BundleBundleBiMap<? extends E1, ? extends E2> other) { if(other == null) return false; boolean changed = false; int sz = other.size(); for (int i = 0; i < sz; i++) { int[][] bundle1 = other.bm.getAAt(i), bundle2 = other.bm.getBAt(i); if(bundle1 == null || bundle1.length == 0 || bundle2 == null || bundle2.length == 0) continue; if(bundle1.length == 1 && bundle2.length == 1) { changed |= put(elements1.keysAt(bundle1[0]), elements2.keysAt(bundle2[0])); } else { changed |= put(elements1.keysAt(bundle1[0]), bundle1.length > 1 ? bundle1[1] : null, elements2.keysAt(bundle2[0]), bundle2.length > 1 ? bundle2[1] : null); } } return changed; }
/** * 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 with * {@link #getElement1BundleVariation(Object[])}. * @param lookup a E2 key to look up * @return an OrderedSet of the E1 elements1 used in the bundle that corresponds to single, or null if invalid */ public OrderedSet<E1> getBundleElements(E2[] lookup) { int[][] bundle; if(lookup == null || (bundle = bm.getAFromB(new int[][]{elements2.getArray(lookup)})) == null || bundle.length < 1 || bundle[0] == null) return null; return elements1.keysAt(bundle[0]); }
/** * Puts all unique E and S keys in {@code 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. * @param other another K2 collection with the same E and S types * @return true if this collection changed as a result of this call */ public boolean putAll(BundleBiMap<? extends E, ? extends S> other) { if(other == null) return false; boolean changed = false; int sz = other.size(); for (int i = 0; i < sz; i++) { int[][] bundle = other.bm.getAAt(i); if(bundle == null || bundle.length == 0) continue; if(bundle.length == 1) changed |= put(elements.keysAt(bundle[0]), other.bm.getBAt(i)); else changed |= put(elements.keysAt(bundle[0]), bundle[1], other.bm.getBAt(i)); } return changed; }