/** * Unwraps an iterator, returning an array. * * <p> * This method iterates over the given type-specific iterator and returns an * array containing the elements returned by the iterator. * * @param i * a type-specific iterator. * @return an array containing the elements returned by the iterator. */ public static int[] unwrap(final IntIterator i) { return unwrap(i, Integer.MAX_VALUE); } /**
/** * Unwraps an iterator into an array. * * <p> * This method iterates over the given type-specific iterator and stores the * elements returned in the given array. The iteration will stop when the * iterator has no more elements or when the end of the array has been reached. * * @param i * a type-specific iterator. * @param array * an array to contain the output of the iterator. * @return the number of elements unwrapped. */ public static int unwrap(final IntIterator i, final int array[]) { return unwrap(i, array, 0, array.length); } /**
/** * Creates a new array list and fills it with a given collection. * * @param c * a collection that will be used to fill the array list. */ public IntArrayList(final Collection<? extends Integer> c) { this(c.size()); size = IntIterators.unwrap(IntIterators.asIntIterator(c.iterator()), a); } /**
public static boolean checkLength(final String resource, final int[] array, final String resouceFullPath) { final int actualLength = IntIterators.unwrap(BinIO.asIntIterator(new DataInputStream(InputBitStream.class.getResourceAsStream(resouceFullPath)))).length; assert array.length == actualLength : resource + " is long " + actualLength + " but we think it should rather be " + array.length; return true; }
/** * Creates a new array list and fills it with a given type-specific collection. * * @param c * a type-specific collection that will be used to fill the array * list. */ public IntArrayList(final IntCollection c) { this(c.size()); size = IntIterators.unwrap(c.iterator(), a); } /**
@Override public int[] toArray(int a[]) { if (a == null || a.length < size()) a = new int[size()]; IntIterators.unwrap(iterator(), a); return a; } @Override
public static void main(final String[] arg) throws IOException, ClassNotFoundException, JSAPException { SimpleJSAP jsap = new SimpleJSAP(PermutedFrontCodedStringList.class.getName(), "Builds a permuted front-coded list of strings using a given front-coded string list and a permutation (either in text or binary format).", new Parameter[] { new Switch("invert", 'i', "invert", "Invert permutation before creating the permuted list."), new Switch("text", 't', "text", "The permutation is a text file."), new UnflaggedOption("list", JSAP.STRING_PARSER, JSAP.REQUIRED, "A front-coded string list."), new UnflaggedOption("permutation", JSAP.STRING_PARSER, JSAP.REQUIRED, "A permutation for the indices of the list (in DataInput format, unless you specify --text)."), new UnflaggedOption("permutedList", JSAP.STRING_PARSER, JSAP.REQUIRED, "A the filename for the resulting permuted list."), }); JSAPResult jsapResult = jsap.parse(arg); if (jsap.messagePrinted()) return; final String permutationFile = jsapResult.getString("permutation"); final int[] permutation = jsapResult.userSpecified("text") ? IntIterators.unwrap(TextIO.asIntIterator(permutationFile)) : BinIO.loadInts(permutationFile); if (jsapResult.getBoolean("invert")) Util.invertPermutationInPlace(permutation); BinIO.storeObject( new PermutedFrontCodedStringList((FrontCodedStringList)BinIO.loadObject(jsapResult.getString("list")), permutation), jsapResult.getString("permutedList") ); } }