private Timings getTimings(long[] times) {
   Timings timings = new Timings();
   timings.avg = LongStream.of(times).average().getAsDouble();
   timings.min = LongStream.of(times).min().getAsLong();
   timings.max = LongStream.of(times).max().getAsLong();
   return timings;
 }
 @Override
 public PrimitiveIterator.OfLong iterator() {
   if (intermediateType.shouldUseIntermediate(sorted, distinct)) {
     LongStream stream = performIntermediateRemoteOperation(Function.identity());
     return stream.iterator();
   } else {
     return remoteIterator();
   }
 }
  public static void main(String... args) {

    /*
    -------------------------------------------------------------------------
    From obj to ...
    -------------------------------------------------------------------------
    */
    Stream<String> streamOfStrings = Stream.of("un", "deux", "trois");
    Function<String, StringBuilder> function = StringBuilder::new;
    streamOfStrings.map(function) /*.forEach(System.out::println)*/;
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToIntFunction<String> toIntFunction = String::length;
    IntStream streamOfInts = streamOfStrings.mapToInt(toIntFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToDoubleFunction<String> toDoubleFunction = String::length;
    DoubleStream streamOfDoubles = streamOfStrings.mapToDouble(toDoubleFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToLongFunction<String> toLongFunction = String::length;
    LongStream streamOfLongs = streamOfStrings.mapToLong(toLongFunction);

    /*
    -------------------------------------------------------------------------
    From int to ...
    -------------------------------------------------------------------------
    */
    IntUnaryOperator plusplus = i -> i++;
    IntStream.of(1, 2, 3).map(plusplus)
    /*.forEach(x->System.out.println(x))*/ ;
    IntFunction<String> intFunction = i -> "" + i;
    IntStream.of(1, 2, 3).mapToObj(intFunction)
    /*.forEach(System.out::println)*/ ;
    IntToDoubleFunction itdf = i -> i;
    IntStream.of(1, 2, 3).mapToDouble(itdf)
    /*.forEach(System.out::println)*/ ;
    IntToLongFunction itlf = i -> i;
    IntStream.of(1, 2, 3).mapToLong(itlf)
    /*.forEach(System.out::println)*/ ;

    /*
    -------------------------------------------------------------------------
    From long to ...
    -------------------------------------------------------------------------
    */
    LongUnaryOperator times = l -> l * l;
    LongStream.of(1L, 2L, 3L).map(times)
    /*.forEach(System.out::println)*/ ;
    LongFunction<String> lf = l -> "toto";
    LongStream.of(1L, 2L, 3L).mapToObj(lf);

    /*
    -------------------------------------------------------------------------
    From double to ...
    -------------------------------------------------------------------------
    */
    DoubleToIntFunction dtif = d -> (int) d;
    DoubleStream.of(1.3, 1.5, 1.6).mapToInt(dtif).forEach(System.out::println);
  }
Exemple #4
0
  public static void main(String[] args) {

    // 500000500000

    long[] ref = {0L};
    LongStream.rangeClosed(0, MAX).forEach(l -> ref[0] += l);
    out.println("forEach: " + ref[0]);

    long l = 0L;
    LongStream.rangeClosed(0, MAX).forEach(l2 -> l = l + l2);
    out.println(l);
  }
  @Test
  public void int64() {
    Wire wire = createWire();
    wire.write().int64(1);
    wire.write(BWKey.field1).int64(2);
    wire.write(() -> "Test").int64(3);
    checkWire(
        wire,
        "[pos: 0, rlim: 16, wlim: 8EiB, cap: 8EiB ] À⒈Æfield1⒉ÄTest⒊",
        "[pos: 0, rlim: 40, wlim: 8EiB, cap: 8EiB ] À§⒈٠٠٠٠٠٠٠Æfield1§⒉٠٠٠٠٠٠٠ÄTest§⒊٠٠٠٠٠٠٠",
        "[pos: 0, rlim: 11, wlim: 8EiB, cap: 8EiB ] À⒈º⒈⒉º²ñ\\u009E⒈⒊",
        "[pos: 0, rlim: 35, wlim: 8EiB, cap: 8EiB ] À§⒈٠٠٠٠٠٠٠º⒈§⒉٠٠٠٠٠٠٠º²ñ\\u009E⒈§⒊٠٠٠٠٠٠٠",
        "[pos: 0, rlim: 3, wlim: 8EiB, cap: 8EiB ] ⒈⒉⒊",
        "[pos: 0, rlim: 27, wlim: 8EiB, cap: 8EiB ] §⒈٠٠٠٠٠٠٠§⒉٠٠٠٠٠٠٠§⒊٠٠٠٠٠٠٠");
    checkAsText123(wire);

    // ok as blank matches anything
    AtomicLong i = new AtomicLong();
    LongStream.rangeClosed(1, 3)
        .forEach(
            e -> {
              wire.read().int64(i::set);
              assertEquals(e, i.get());
            });

    assertEquals(0, bytes.readRemaining());
    // check it's safe to read too much.
    wire.read();
  }
  public static long factorial(int n) {

    if (n > LIMIT) {
      throw new IllegalArgumentException();
    }
    return LongStream.rangeClosed(2, n).reduce(1, (a, b) -> a * b);
  }
Exemple #7
0
 public static long[] eqSumPowDig(long hMax, int exp) {
   int numberOfDigits = (int) Math.round(Math.ceil(Math.log10(hMax)));
   long[] result =
       LongStream.range(2, hMax + 1)
           .filter(i -> getSumOfPowers(numberOfDigits, i, exp) == i)
           .toArray();
   return result;
 }
Exemple #8
0
  @Override
  public long solve() {
    long summOfSquares = LongStream.rangeClosed(1, N).map(num -> num * num).sum();
    long summ = (1 + N) * N / 2;
    long squareOfSum = summ * summ;

    return squareOfSum - summOfSquares;
  }
 @Test
 public void testAndLongFlatMap() {
   checkShortCircuitCollector(
       "andLongFlat",
       OptionalLong.of(0),
       2,
       () -> LongStreamEx.of(0).flatMap(x -> LongStream.range(1, 100000000)).boxed(),
       MoreCollectors.andingLong(Long::longValue),
       true);
 }
    @Before
    public void setUp() {

      mock = new TodoRepositoryMock();

      LongStream.rangeClosed(1, 100)
          .mapToObj(
              v -> {
                return new Todo(
                    new TodoId(v), UserId.notNumberingValue(), Summary.empty(), Memo.empty(), true);
              })
          .forEach(mock::addTestData);
      sut = new SearchTodo(mock);
    }
    @Before
    public void setUp() {

      mock = new TodoRepositoryMock();

      LongStream.rangeClosed(1, 100)
          .mapToObj(
              v -> {
                final UserId userId = new UserId(v % 10);

                return new Todo(new TodoId(v), userId, Summary.empty(), Memo.empty(), true);
              })
          .forEach(mock::addTestData);
      sut = new SearchTodo(mock);
    }
  private <E extends RuntimeException> void assertLongToDoubleFunction(
      LongToDoubleFunction test, Class<E> type) {
    assertNotNull(test);
    try {
      test.applyAsDouble(0L);
      fail();
    } catch (RuntimeException e) {
      assertException(type, e, "0");
    }

    try {
      LongStream.of(1L, 2L, 3L).mapToDouble(test);
    } catch (RuntimeException e) {
      assertException(type, e, "1");
    }
  }
 /**
  * Returns a new {@link GapAwareTrackingToken} instance based on this token but which has advanced
  * to given {@code index}. Gaps that have fallen behind the index by more than the {@code
  * maxGapOffset} will not be included in the new token.
  *
  * <p>Note that the given {@code index} should be one of the current token's gaps or be higher
  * than the current token's index.
  *
  * @param index the global sequence number of the next event
  * @param maxGapOffset the maximum distance between a gap and the token's index
  * @return the new token that has advanced from the current token
  */
 public GapAwareTrackingToken advanceTo(long index, int maxGapOffset) {
   long newIndex;
   SortedSet<Long> gaps = new TreeSet<>(this.gaps);
   if (gaps.remove(index)) {
     newIndex = this.index;
   } else if (index > this.index) {
     newIndex = index;
     LongStream.range(this.index + 1L, index).forEach(gaps::add);
   } else {
     throw new IllegalArgumentException(
         String.format(
             "The given index [%d] should be larger than the token index [%d] or be one of the token's gaps [%s]",
             index, this.index, gaps));
   }
   gaps = gaps.tailSet(newIndex - maxGapOffset);
   return new GapAwareTrackingToken(newIndex, gaps);
 }
 @Override
 protected Long compute() {
   return LongStream.of(values).sum();
 }
 public static long sideEffectParallelSum(long n) {
   Accumulator accumulator = new Accumulator();
   LongStream.rangeClosed(1, n).parallel().forEach(accumulator::add);
   return accumulator.total;
 }
Exemple #16
0
 /** this ought to be an override in Xseq - here should stream array */
 public LongStream streamLong() {
   return LongStream.rangeClosed((long) getMin(), (long) getMax());
 }
Exemple #17
0
 public ScalarSequence(LongStream in) {
   x = in.mapToDouble(n -> (double) n).toArray();
   index = capacity = x.length;
   calcStats();
 }
  public void run(final int numJobs, final int payloadSizeBytes, final int numConsumers) {

    luaQ.clear();

    final ExecutorService consumerExecutor = Executors.newFixedThreadPool(numConsumers);
    final List<Future<?>> consumerFutures = new ArrayList<>(numConsumers);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final AtomicBoolean donePublishing = new AtomicBoolean(false);
    final Map<Integer, long[]> publishClaimedStamps = new HashMap<>(MapUtils.capacity(numJobs));
    for (int i = 0; i < numJobs; i++) {
      publishClaimedStamps.put(i, new long[2]);
    }

    for (int i = 0; i < numConsumers; i++) {

      if (jedisPublisherAndPrototype == null) {
        consumerFutures.add(
            consumerExecutor.submit(
                new Consumer(luaQ, startLatch, donePublishing, publishClaimedStamps, 1)));
        continue;
      }

      final LuaQ directConsumerQ =
          new LuaQ(
              new DirectJedisExecutor(
                  new Jedis(
                      jedisPublisherAndPrototype.getClient().getHost(),
                      jedisPublisherAndPrototype.getClient().getPort())),
              ThroughputBenchmark.class.getSimpleName());

      consumerFutures.add(
          consumerExecutor.submit(
              new Consumer(directConsumerQ, startLatch, donePublishing, publishClaimedStamps, 1)));
    }

    final byte[] payload = new byte[payloadSizeBytes];
    Arrays.fill(payload, (byte) 1);

    startLatch.countDown();

    for (int i = 0; i < numJobs; i++) {
      final byte[] id = String.valueOf(i).getBytes(StandardCharsets.UTF_8);
      final long publishClaimedStamp = System.nanoTime();
      luaQ.publish(id, payload);
      publishClaimedStamps.get(i)[0] = publishClaimedStamp;
    }

    donePublishing.set(true);
    for (final Future<?> consumerFuture : consumerFutures) {
      Futures.getUnchecked(consumerFuture);
    }

    consumerExecutor.shutdown();

    final long[] latencyNanos =
        publishClaimedStamps
            .values()
            .stream()
            .mapToLong(publishClaimedStamp -> publishClaimedStamp[1] - publishClaimedStamp[0])
            .sorted()
            .toArray();

    System.out.printf("Min latency %.2f (ms)%n", latencyNanos[0] / 1000000.0);
    System.out.printf("Max latency %.2f (ms)%n", latencyNanos[latencyNanos.length - 1] / 1000000.0);

    final long medianNanos = latencyNanos[latencyNanos.length / 2];
    System.out.printf("Median latency %.2f (ms)%n", medianNanos / 1000000.0);

    final double avgNanos = LongStream.of(latencyNanos).average().orElse(-1);
    System.out.printf("Average latency %.2f (ms)%n", avgNanos / 1000000);
  }
 public static long parallelRangedSum(long n) {
   return LongStream.rangeClosed(1, n).parallel().reduce(Long::sum).getAsLong();
 }
 private long executeSummarize(long[] values) {
   return LongStream.of(values).sum();
 }
  @Override
  public NavigableMap<Long, Long> findEqualIds(Dictionary<Long> otherDict) {
    NavigableMap<Long, Long> res = new TreeMap<>();

    if (otherDict instanceof ArrayCompressedLongDictionary) {
      CompressedLongArray<?> otherSortedValues =
          ((ArrayCompressedLongDictionary) otherDict).sortedValues;
      if (sortedValues.size() == 0 || otherSortedValues.size() == 0) return res;

      // good case: we can traverse the two arrays simultaneously and only need to store O(1) in
      // memory.
      int posThis = 0;
      int posOther = 0;
      long decompressedThis = sortedValues.get(posThis);
      long decompressedOther = otherSortedValues.get(posOther);
      while (posThis < sortedValues.size() && posOther < otherSortedValues.size()) {
        // move 'posThis' right until decompressedThis is >= decompressedOther
        while (posThis < sortedValues.size() - 1 && decompressedThis < decompressedOther)
          decompressedThis = sortedValues.get(++posThis);

        // move 'posOther' right until decompressedOther is >= decompressedThis
        while (posOther < otherSortedValues.size() - 1 && decompressedOther < decompressedThis)
          decompressedOther = otherSortedValues.get(++posOther);

        // validate if we have a match
        if (decompressedThis == decompressedOther) {
          res.put((long) posThis++, (long) posOther++);
          if (posThis < sortedValues.size() && posOther < otherSortedValues.size()) {
            decompressedThis = sortedValues.get(posThis);
            decompressedOther = otherSortedValues.get(posOther);
          }
        } else if ((posThis == sortedValues.size() - 1 && decompressedThis < decompressedOther)
            || (posThis == sortedValues.size() - 1 && posOther == otherSortedValues.size() - 1))
          break;
      }
    } else if (otherDict instanceof ConstantLongDictionary) {
      long otherId = ((ConstantLongDictionary) otherDict).getId();
      long otherValue = ((ConstantLongDictionary) otherDict).getDecompressedValue();

      try {
        long ourId = findIdOfValue(otherValue);
        res.put(ourId, otherId);
      } catch (IllegalArgumentException e) {
        // swallow, return empty dict.
      }
    } else if (otherDict instanceof EmptyLongDictionary) {
      // noop.
    } else {
      // Bad case: decompress whole array (should not happen, though)
      long[] decompressedValues = sortedValues.decompressedArray();
      Long[] otherIds =
          otherDict.findIdsOfValues(
              LongStream.of(decompressedValues).mapToObj(Long::valueOf).toArray(l -> new Long[l]));
      for (int i = 0; i < decompressedValues.length; i++) {
        Long otherId = otherIds[i];
        if (otherId != -1L) res.put((long) i, otherId);
      }
    }

    return res;
  }
  public static void main(String... args) {
    long[] numbers = LongStream.rangeClosed(1, 100000).toArray();

    ForkJoinTask<Long> task = new ForkJoinSumCalculator(numbers);
    System.out.println(new ForkJoinPool().invoke(task));
  }