/** * Creates a new Arrangement copying a given one. * * @param m a {@link Map} to be copied into the new Arrangement. * @param f the load factor. */ public Arrangement(final Map<? extends K, ? extends Integer> m, final float f) { this(m.size(), f, (m instanceof Arrangement) ? ((Arrangement) m).hasher : CrossHash.defaultHasher); putAll(m); }
/** * Creates a new Arrangement copying a given one. * * @param m a {@link Map} to be copied into the new Arrangement. * @param f the load factor. * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations */ public Arrangement(final Map<? extends K, ? extends Integer> m, final float f, CrossHash.IHasher hasher) { this(m.size(), f, hasher); putAll(m); }
/** * Copies this ProbabilityTable so nothing in the copy is shared with the original, except for the T items (which * might not be possible to copy). The RNG is also copied. * @return a copy of this ProbabilityTable; no references should be shared except for T items */ public ProbabilityTable<T> copy() { ProbabilityTable<T> n = new ProbabilityTable<>(rng.getState()); n.weights.addAll(weights); n.table.putAll(table); for (int i = 0; i < extraTable.size(); i++) { n.extraTable.add(extraTable.get(i).copy()); } n.total = total; n.normalTotal = normalTotal; return n; }
/** * Adds a bundle of E keys and a S 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. * @param e an array of E keys to add; the array cannot already be present, nor can it be null * @param s e S key to add; cannot already be present * @return true if this collection changed as e result of this call */ public boolean put(E[] e, S s) { if(e == null) return false; int len = elements.size; elements.putAll(e); for (int i = len; i < elements.size; i++) { mm.add(new IntVLA(e.length)); } elements.putAll(e); int[][] bundle = new int[][]{elements.getArray(e)}; if(!bm.put(bundle, s)) return false; for (int i = 0; i < e.length; i++) { mm.get(bundle[0][i]).add(bm.size()-1); } return true; } /**
/** * Adds a bundle of E keys and a S 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. * @param e an array of E keys to add; the array cannot already be present, nor can it be null * @param s e S key to add; cannot already be present * @return true if this collection changed as e result of this call */ public boolean put(Collection<? extends E> e, S s) { if(e == null) return false; int len = elements.size; elements.putAll(e); for (int i = len; i < elements.size; i++) { mm.add(new IntVLA(e.size())); } int[][] bundle = new int[][]{elements.getArray(e)}; if(!bm.put(bundle, s)) return false; len = bundle[0].length; for (int i = 0; i < len; i++) { mm.get(bundle[0][i]).add(bm.size()-1); } return true; } /**
/** * Adds a bundle of E keys, mixed with an int array of variations, and a S key at the same point in the ordering * (the end) to this BundleBiMap. Neither the S key nor the bundle (effectively, the pair of e and variation) can be * present in this collection before this is called. * @param e an array of E keys to add; the array cannot already have been inserted with an equivalent variation, nor can it be null * @param variation an int array that can be used to make a different version of e, i.e. the same things at different quantities; cannot be null * @param s e S key to add; cannot already be present * @return true if this collection changed as e result of this call */ public boolean put(Collection<? extends E> e, int[] variation, S s) { if(e == null || variation == null) return false; int len = elements.size; elements.putAll(e); for (int i = len; i < elements.size; i++) { mm.add(new IntVLA(e.size())); } int[][] bundle = new int[][]{elements.getArray(e), variation}; if(!bm.put(bundle, s)) return false; len = bundle[0].length; for (int i = 0; i < len; i++) { mm.get(bundle[0][i]).add(bm.size()-1); } return true; }
/** * Adds a bundle of E keys, mixed with an int array of variations, and a S key at the same point in the ordering * (the end) to this BundleBiMap. Neither the S key nor the bundle (effectively, the pair of e and variation) can be * present in this collection before this is called. * @param e an array of E keys to add; the array cannot already have been inserted with an equivalent variation, nor can it be null * @param variation an int array that can be used to make a different version of e, i.e. the same things at different quantities; cannot be null * @param s e S key to add; cannot already be present * @return true if this collection changed as e result of this call */ public boolean put(E[] e, int[] variation, S s) { if(e == null || variation == null) return false; int len = elements.size; elements.putAll(e); for (int i = len; i < elements.size; i++) { mm.add(new IntVLA(e.length)); } int[][] bundle = new int[][]{elements.getArray(e), variation}; if(!bm.put(bundle, s)) return false; for (int i = 0; i < e.length; i++) { mm.get(bundle[0][i]).add(bm.size()-1); } return true; } /**
/** * Constructor that allows you to specify the adjective and noun collections used by * {@link #toWordMnemonic(int, boolean)} as well as a seed. This should be useful when you want to enforce a stable * relationship between word mnemonics produced by {@link #toWordMnemonic(int, boolean)} and the int values they * decode to with {@link #fromWordMnemonic(String)}, because the default can change if the adjective and noun * collections in {@link Thesaurus} change. There should be a fairly large amount of unique adjectives and nouns; * {@code (long)adjectives.size() * nouns.size() * adjectives.size() * nouns.size()} should be at least 0x80000000L * (2147483648L), with case disregarded. If the total is less than that, not all possible ints can be encoded with * {@link #toWordMnemonic(int, boolean)}. Having 216 adjectives and 216 nouns is enough for a rough target. Each * word (adjectives and nouns alike) can have any characters in it except for space, since space is used during * decoding to separate words. * @param seed a long seed that will be used to randomize the syllables and words used. * @param adjectives a Collection of unique Strings (case-insensitive) that will be used as adjectives * @param nouns a Collection of unique Strings (case-insensitive) that will be used as nouns */ public Mnemonic(long seed, Collection<String> adjectives, Collection<String> nouns) { RNG rng = new RNG(new LightRNG(seed)); int[] order = rng.randomOrdering(431); int o; for (int i = 0; i < 256; i++) { o = order[i]; items.add(baseTriplets.substring(o * 3, o * 3 + 3)); } allAdjectives.putAll(adjectives); allAdjectives.shuffle(rng); allNouns.putAll(nouns); allNouns.shuffle(rng); } /**
/** * Constructor for a Mnemonic generator that allows a different seed to be chosen, which will alter the syllables * produced by {@link #toMnemonic(long)} and the words produced by {@link #toWordMnemonic(int, boolean)} if you give * the same numeric argument to differently-seeded Mnemonic generators. Unless you know you need this, you should * probably use {@link #Mnemonic()} to ensure that your text can be decoded. * @param seed a long seed that will be used to randomize the syllables and words used. */ public Mnemonic(long seed) { RNG rng = new RNG(new LightRNG(seed)); int[] order = rng.randomOrdering(431); int o; for (int i = 0; i < 256; i++) { o = order[i]; items.add(baseTriplets.substring(o * 3, o * 3 + 3)); } for (int i = 0; i < adjective.size(); i++) { allAdjectives.putAll(adjective.getAt(i)); } allAdjectives.shuffle(rng); for (int i = 0; i < noun.size(); i++) { allNouns.putAll(noun.getAt(i)); } allNouns.shuffle(rng); }
@Override public ProbabilityTable<K> restore(String text) { ObText.ContentMatcher m = makeMatcher(text); if(!m.find() || !m.hasMatch()) return null; ProbabilityTable<K> pt = new ProbabilityTable<>(StringKit.longFromHex(m.getMatch())); if(!m.find() || !m.hasMatch()) return pt; pt.weights.addAll(convertIntVLA.restore(m.getMatch())); if(!m.find() || !m.hasMatch()) { pt.weights.clear(); return pt; } pt.table.putAll(convertArrange.restore(m.getMatch())); while (m.find()) { if (m.hasMatch()) { pt.extraTable.add(restore(m.getMatch())); } } return pt; } };