/** * Counts the occurrences of a value in an array. * * @param numbers Array of numbers * @param value the value for which we have to count occurrences * @return count of total number of occurrences of the value */ public static long countOccurrences(int[] numbers, int value) { return Arrays.stream(numbers) .filter(number -> number == value) .count(); }
@Override // BEGIN functional public long countPrimes(int upTo) { return IntStream.range(1, upTo) .filter(this::isPrime) .count(); }
@Override public int poolSize() { return (int) connectionManagers.values().stream() .mapToInt(man -> man.counter.get()) .filter(c -> c == 0L) .count(); }
public static long phi(int m) { return IntStream.rangeClosed(1, m).filter(r -> P38.coprime(r, m)).count(); } }
@Override // BEGIN parallel_functional public long countPrimes(int upTo) { return IntStream.range(1, upTo) .parallel() .filter(this::isPrime) .count(); }
public static int countLowercaseLetters(String string) { return (int) string.chars() .filter(Character::isLowerCase) .count(); }
private boolean isProbablyType(String name) { Symbol typeSymbol = FindIdentifiers.findIdent(name, state, KindSelector.TYP); return typeSymbol instanceof TypeSymbol || name.chars().filter(c -> c == '.').count() >= 3 || name.contains("#"); }
@Benchmark public long java8() { // Using Java8 return testString.chars().filter(ch -> ch =='.').count(); }
@Benchmark public long java8_1() { // Using Java8 (case 2) return testString.codePoints().filter(ch -> ch == '.').count(); }
@Benchmark public long java8_1() { // Using Java8 (case 2) return testString.codePoints().filter(ch -> ch == '.').count(); }
@Benchmark public long java8() { // Using Java8 return testString.chars().filter(ch -> ch =='.').count(); }
@Override public <T> long apply( final SqlStreamOptimizerInfo<ENTITY> info, final SqlStreamTerminator<ENTITY> sqlStreamTerminator, final IntPipeline pipeline ) { requireNonNull(info); requireNonNull(sqlStreamTerminator); requireNonNull(pipeline); return sqlStreamTerminator.optimize(pipeline).getAsIntStream().count(); }
default long count(IntPipeline pipeline) { requireNonNull(pipeline); return optimize(pipeline).getAsIntStream().count(); }
public static long countUniqueCharacters(String input) { return input.chars() .distinct() .count(); }
/** * creates a new tensor based on a parent tensor * @param parent - the parent tensor * @param dimensionsLimit - * @param reduceChildDimensions */ public Tensor(Tensor parent, int[][] dimensionsLimit, boolean reduceChildDimensions) { this.startOffset = parent.startOffset; this.globalDimensions = parent.globalDimensions; this.elements = parent.elements; this.dimMultiplicators = parent.dimMultiplicators; this.globalDimensionsLimit = dimensionsLimit; this.dimTmp = new int[globalDimensions.length]; this.dimensions = new int[reduceChildDimensions ? (int) IntStream.range(0, globalDimensions.length).filter(i -> dimensionsLimit[0][i] != dimensionsLimit[1][i]).count() : parent.dimensions.length]; for (int i = 0, j = 0; i < globalDimensions.length; i++) { if (dimensionsLimit[0][i] != dimensionsLimit[1][i] || !reduceChildDimensions) { dimensions[j++] = dimensionsLimit[1][i] - dimensionsLimit[0][i] + 1; } } size = IntStream.range(0, dimensions.length).map(i -> dimensions[i]).reduce(1, (a, b) -> a * b); }
/** * @param parent * @param dimensionsLimit - limit over the parent dimensions * @param reduceChildDimensions - reduce the child vector dimensions if they are limited to 1 * @return */ @SuppressWarnings("unchecked") public static <T extends Tensor> T tensor(Tensor parent, int[][] dimensionsLimit, boolean reduceChildDimensions) { T result = null; long dimensions = 0; if (reduceChildDimensions) { dimensions = IntStream.range(0, dimensionsLimit[0][0]).filter(i -> dimensionsLimit[0][i] != dimensionsLimit[1][i]).count(); } else { dimensions = parent.getDimensions().length; } if (dimensions <= 2) { result = (T) new Matrix(parent, dimensionsLimit); } else { result = (T) new Tensor(parent, dimensionsLimit, reduceChildDimensions); } return result; }
.mapToInt(Block::getPositionCount) .distinct() .count() == 1);
@Test public void testAppend() throws InterruptedException, ExecutionException { AsyncTable<?> table = getTable.get(); int count = 10; CountDownLatch latch = new CountDownLatch(count); char suffix = ':'; AtomicLong suffixCount = new AtomicLong(0L); IntStream.range(0, count).forEachOrdered( i -> table.append(new Append(row).addColumn(FAMILY, QUALIFIER, Bytes.toBytes("" + i + suffix))) .thenAccept(r -> { suffixCount.addAndGet(Bytes.toString(r.getValue(FAMILY, QUALIFIER)).chars() .filter(x -> x == suffix).count()); latch.countDown(); })); latch.await(); assertEquals((1 + count) * count / 2, suffixCount.get()); String value = Bytes.toString( table.get(new Get(row).addColumn(FAMILY, QUALIFIER)).get().getValue(FAMILY, QUALIFIER)); int[] actual = Arrays.asList(value.split("" + suffix)).stream().mapToInt(Integer::parseInt) .sorted().toArray(); assertArrayEquals(IntStream.range(0, count).toArray(), actual); }
@Test public void shouldHandleDocsOfLongerLength() { // 0 0 // 0-- - 5-- - -1--- - 5--- - 2- final String INPUT = "ab\r\ncde\r\nefgh\r\nijk\r\nlm"; SourceSplitter splitter = new SourceSplitter(); splitter.reset(INPUT); assertEquals("split count", 5, splitter.count()); assertEquals("split position", 0, splitter.getPosition(0)); assertEquals("split position", 4, splitter.getPosition(1)); assertEquals("split position", 9, splitter.getPosition(2)); assertEquals("split position", 15, splitter.getPosition(3)); assertEquals("split position", 20, splitter.getPosition(4)); assertEquals("split position", 22, splitter.getPosition(5)); /* * Test findLineOffset() for every character with an alternate * computation that counts every LFs. */ for (int i = 0; i < splitter.originalLength(); ++i) { char c = INPUT.charAt(i); int off = splitter.findLineOffset(i); long numLF = INPUT.substring(0, i + 1).chars().filter(ch -> ch == '\n').count(); long exp = numLF - (c == '\n' ? 1 : 0); assertEquals("split find-offset of " + i, exp, off); } }
int off = splitter.findLineOffset(i); long numLF = INPUT.substring(0, i + 1).chars().filter(ch -> ch == '\n').count(); long exp = numLF - (c == '\n' ? 1 : 0); assertEquals("split find-offset of " + i, exp, off);