List of usage examples for org.apache.commons.lang3 ArrayUtils lastIndexOf
public static int lastIndexOf(final boolean[] array, final boolean valueToFind)
Finds the last index of the given value within the array.
This method returns #INDEX_NOT_FOUND ( -1 ) if null array input.
From source file:fr.inria.atlanmod.neoemf.data.berkeleydb.store.DirectWriteBerkeleyDBStore.java
@Override public int lastIndexOf(InternalEObject object, EStructuralFeature feature, Object value) { int resultValue; PersistentEObject persistentEObject = PersistentEObject.from(object); Object[] array = (Object[]) getFromMap(persistentEObject, feature); if (isNull(array)) { resultValue = ArrayUtils.INDEX_NOT_FOUND; } else if (feature instanceof EAttribute) { resultValue = ArrayUtils.lastIndexOf(array, serializeToProperty((EAttribute) feature, value)); } else {//from w w w .ja va2 s .co m PersistentEObject childEObject = PersistentEObject.from(value); resultValue = ArrayUtils.lastIndexOf(array, childEObject.id()); } return resultValue; }
From source file:fr.inria.atlanmod.neoemf.data.blueprints.store.DirectWriteBlueprintsStore.java
@Override protected int lastIndexOfAttribute(PersistentEObject object, EAttribute eAttribute, Object value) { return ArrayUtils.lastIndexOf(toArray(object, eAttribute), value); }
From source file:fr.inria.atlanmod.neoemf.data.hbase.store.DirectWriteHBaseStore.java
@Override public int lastIndexOf(InternalEObject object, EStructuralFeature feature, Object value) { PersistentEObject eObject = PersistentEObject.from(object); String[] array = (String[]) getFromTable(eObject, feature); if (isNull(array)) { return PersistentStore.NO_INDEX; }//from ww w .j a v a2 s . co m if (feature instanceof EAttribute) { return ArrayUtils.lastIndexOf(array, serializeToProperty((EAttribute) feature, value)); } else { PersistentEObject childEObject = PersistentEObject.from(value); return ArrayUtils.lastIndexOf(array, childEObject.id().toString()); } }
From source file:org.apache.mahout.classifier.df.builder.DecisionTreeBuilderTest.java
/** * make sure that DecisionTreeBuilder.randomAttributes() returns the correct number of attributes, that have not been * selected yet/* w w w .j av a 2 s .c om*/ */ @Test public void testRandomAttributes() throws Exception { Random rng = RandomUtils.getRandom(); int nbAttributes = rng.nextInt(100) + 1; boolean[] selected = new boolean[nbAttributes]; for (int nloop = 0; nloop < 100; nloop++) { Arrays.fill(selected, false); // randomly select some attributes int nbSelected = rng.nextInt(nbAttributes - 1); for (int index = 0; index < nbSelected; index++) { int attr; do { attr = rng.nextInt(nbAttributes); } while (selected[attr]); selected[attr] = true; } int m = rng.nextInt(nbAttributes); Method randomAttributes = DecisionTreeBuilder.class.getDeclaredMethod("randomAttributes", Random.class, boolean[].class, int.class); randomAttributes.setAccessible(true); int[] attrs = (int[]) randomAttributes.invoke(null, rng, selected, m); assertNotNull(attrs); assertEquals(Math.min(m, nbAttributes - nbSelected), attrs.length); for (int attr : attrs) { // the attribute should not be already selected assertFalse("an attribute has already been selected", selected[attr]); // each attribute should be in the range [0, nbAttributes[ assertTrue(attr >= 0); assertTrue(attr < nbAttributes); // each attribute should appear only once assertEquals(ArrayUtils.indexOf(attrs, attr), ArrayUtils.lastIndexOf(attrs, attr)); } } }
From source file:org.apache.mahout.classifier.df.builder.DefaultTreeBuilderTest.java
/** * make sure that DefaultTreeBuilder.randomAttributes() returns the correct number of attributes, that have not been * selected yet/*ww w.j a v a 2 s. c o m*/ */ @Test public void testRandomAttributes() throws Exception { Random rng = RandomUtils.getRandom(); int nbAttributes = rng.nextInt(100) + 1; boolean[] selected = new boolean[nbAttributes]; for (int nloop = 0; nloop < 100; nloop++) { Arrays.fill(selected, false); // randomly select some attributes int nbSelected = rng.nextInt(nbAttributes - 1); for (int index = 0; index < nbSelected; index++) { int attr; do { attr = rng.nextInt(nbAttributes); } while (selected[attr]); selected[attr] = true; } int m = rng.nextInt(nbAttributes); int[] attrs = DefaultTreeBuilder.randomAttributes(rng, selected, m); assertNotNull(attrs); assertEquals(Math.min(m, nbAttributes - nbSelected), attrs.length); for (int attr : attrs) { // the attribute should not be already selected assertFalse("an attribute has already been selected", selected[attr]); // each attribute should be in the range [0, nbAttributes[ assertTrue(attr >= 0); assertTrue(attr < nbAttributes); // each attribute should appear only once assertEquals(ArrayUtils.indexOf(attrs, attr), ArrayUtils.lastIndexOf(attrs, attr)); } } }
From source file:org.apache.sysml.hops.codegen.opt.PlanSelectionFuseCostBasedV2.java
private static long getNumSkipPlans(boolean[] plan) { int pos = ArrayUtils.lastIndexOf(plan, true); return UtilFunctions.pow(2, plan.length - pos - 1); }
From source file:yoyo.framework.standard.shared.commons.lang.ArrayUtilsTest.java
@Test public void test() { assertThat(ArrayUtils.EMPTY_BOOLEAN_ARRAY.length, is(0)); assertThat(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, is(new Boolean[] {})); boolean[] testee = ArrayUtils.add(null, true); assertThat(ArrayUtils.clone(testee), is(testee)); assertThat(ArrayUtils.contains(testee, true), is(true)); assertThat(ArrayUtils.contains(testee, false), is(false)); assertThat(ArrayUtils.getLength(testee), is(1)); assertThat(ArrayUtils.hashCode(testee), is(not(nullValue()))); assertThat(ArrayUtils.indexOf(testee, true), is(not(ArrayUtils.INDEX_NOT_FOUND))); assertThat(ArrayUtils.indexOf(testee, false), is(ArrayUtils.INDEX_NOT_FOUND)); assertThat(ArrayUtils.isEmpty(testee), is(false)); assertThat(ArrayUtils.isEmpty(ArrayUtils.EMPTY_BOOLEAN_ARRAY), is(true)); assertThat(ArrayUtils.isEquals(testee, new boolean[] { true }), is(true)); assertThat(ArrayUtils.isNotEmpty(testee), is(true)); assertThat(ArrayUtils.isSameLength(testee, new boolean[] { true }), is(true)); assertThat(ArrayUtils.isSameType(testee, new boolean[] { true }), is(true)); assertThat(ArrayUtils.lastIndexOf(testee, true), is(not(ArrayUtils.INDEX_NOT_FOUND))); assertThat(ArrayUtils.lastIndexOf(testee, false), is(ArrayUtils.INDEX_NOT_FOUND)); boolean[] booleanNull = null; assertThat(ArrayUtils.nullToEmpty(booleanNull), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY)); assertThat(ArrayUtils.remove(testee, 0), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY)); assertThat(ArrayUtils.removeAll(testee, 0), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY)); assertThat(ArrayUtils.removeElement(testee, true), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY)); assertThat(ArrayUtils.removeElement(testee, false), is(testee)); assertThat(ArrayUtils.removeElements(testee, true, false), is(ArrayUtils.EMPTY_BOOLEAN_ARRAY)); }