it.unimi.dsi.fastutil.longs.LongIterator Java Examples

The following examples show how to use it.unimi.dsi.fastutil.longs.LongIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DateTimeColumn.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an iterator over elements of type {@code T}.
 *
 * @return an Iterator.
 */
@Override
public Iterator<LocalDateTime> iterator() {

  return new Iterator<LocalDateTime>() {

    final LongIterator longIterator = longIterator();

    @Override
    public boolean hasNext() {
      return longIterator.hasNext();
    }

    @Override
    public LocalDateTime next() {
      return PackedLocalDateTime.asLocalDateTime(longIterator.nextLong());
    }
  };
}
 
Example #2
Source File: InstantColumn.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an iterator over elements of type {@code T}.
 *
 * @return an Iterator.
 */
@Override
public Iterator<Instant> iterator() {

  return new Iterator<Instant>() {

    final LongIterator longIterator = longIterator();

    @Override
    public boolean hasNext() {
      return longIterator.hasNext();
    }

    @Override
    public Instant next() {
      return PackedInstant.asInstant(longIterator.nextLong());
    }
  };
}
 
Example #3
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, InstantColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    LongIterator iterator = column.longIterator();
    while (iterator.hasNext()) {
      dos.writeLong(iterator.nextLong());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example #4
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, DateTimeColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    LongIterator iterator = column.longIterator();
    while (iterator.hasNext()) {
      dos.writeLong(iterator.nextLong());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example #5
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, LongColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    LongIterator iterator = (LongIterator) column.iterator();
    while (iterator.hasNext()) {
      dos.writeLong(iterator.nextLong());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example #6
Source File: MultiThreadedPageRank.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  // Each thread runs on its own partition of the nodes, distributing PageRank mass for the
  // nodes that were assigned to it.
  final LongIterator iter = nodes.iterator();
  while (iter.hasNext()) {
    long v = iter.nextLong();
    int outDegree = graph.getOutDegree(v);
    if (outDegree > 0) {
      double outWeight = dampingFactor * prVector.get((int) v) / outDegree;
      EdgeIterator edges = graph.getOutEdges(v);
      while (edges.hasNext()) {
        int nbr = (int) edges.nextLong();
        // Note that getAndAdd is implemented as an atomic operation with CAS, so
        // it's fine to have concurrent accesses to the PageRank vector.
        nextPR.getAndAdd(nbr, outWeight);
      }
    }

    nextPR.getAndAdd((int) v, mass);
  }

  latch.countDown();
}
 
Example #7
Source File: PageRank.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
/**
 * Runs PageRank, either until the max number of iterations has been reached or the L1 norm of
 * the difference between PageRank vectors drops below the tolerance.
 *
 * @return number of iterations that was actually run
 */
public int run() {
  LongArrayList noOuts = new LongArrayList();
  LongIterator iter = nodes.iterator();
  while (iter.hasNext()) {
    long v = iter.nextLong();
    if (graph.getOutDegree(v) == 0) {
      noOuts.add(v);
    }
  }

  double dampingAmount = (1.0 - dampingFactor) / nodeCount;
  prVector = new double[(int) (maxNodeId + 1)];
  nodes.forEach(v -> prVector[(int) (long) v] = 1.0 / nodeCount);

  int i = 0;
  while (i < this.maxIterations && normL1 > tolerance) {
    iterate(dampingAmount, noOuts);
    i++;
  }

  return i;
}
 
Example #8
Source File: DateTimeColumn.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an iterator over elements of type {@code T}.
 *
 * @return an Iterator.
 */
@Override
public Iterator<LocalDateTime> iterator() {

  return new Iterator<LocalDateTime>() {

    final LongIterator longIterator = longIterator();

    @Override
    public boolean hasNext() {
      return longIterator.hasNext();
    }

    @Override
    public LocalDateTime next() {
      return PackedLocalDateTime.asLocalDateTime(longIterator.nextLong());
    }
  };
}
 
Example #9
Source File: InstantColumn.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an iterator over elements of type {@code T}.
 *
 * @return an Iterator.
 */
@Override
public Iterator<Instant> iterator() {

  return new Iterator<Instant>() {

    final LongIterator longIterator = longIterator();

    @Override
    public boolean hasNext() {
      return longIterator.hasNext();
    }

    @Override
    public Instant next() {
      return PackedInstant.asInstant(longIterator.nextLong());
    }
  };
}
 
Example #10
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, InstantColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    LongIterator iterator = column.longIterator();
    while (iterator.hasNext()) {
      dos.writeLong(iterator.nextLong());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example #11
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, DateTimeColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    LongIterator iterator = column.longIterator();
    while (iterator.hasNext()) {
      dos.writeLong(iterator.nextLong());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example #12
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, LongColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    LongIterator iterator = (LongIterator) column.iterator();
    while (iterator.hasNext()) {
      dos.writeLong(iterator.nextLong());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example #13
Source File: TransactionManager.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
private boolean doTruncateInvalidTxBefore(long time) throws InvalidTruncateTimeException {
  LOG.info("Removing tx ids before {} from invalid list", time);
  long truncateWp = time * TxConstants.MAX_TX_PER_MS;
  // Check if there any in-progress transactions started earlier than truncate time
  if (inProgress.lowerKey(truncateWp) != null) {
    throw new InvalidTruncateTimeException("Transactions started earlier than " + time + " are in-progress");
  }
  
  // Find all invalid transactions earlier than truncateWp
  LongSet toTruncate = new LongArraySet();
  LongIterator it = invalidTxList.toRawList().iterator();
  while (it.hasNext()) {
    long wp = it.nextLong();
    if (wp < truncateWp) {
      toTruncate.add(wp);
    }
  }
  LOG.info("Removing tx ids {} from invalid list", toTruncate);
  return invalidTxList.removeAll(toTruncate);
}
 
Example #14
Source File: FastCollectionsUtils.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
/**
 * 移除map中符合条件的元素,并对删除的元素执行后续的操作。
 *
 * @param collection 必须是可修改的集合
 * @param predicate  过滤条件,为真的删除
 * @param then       元素删除之后执行的逻辑
 * @return 删除的元素数量
 */
public static int removeIfAndThen(LongCollection collection, LongPredicate predicate, LongConsumer then) {
    if (collection.size() == 0) {
        return 0;
    }
    final LongIterator itr = collection.iterator();
    long value;
    int removeNum = 0;
    while (itr.hasNext()) {
        value = itr.nextLong();
        if (predicate.test(value)) {
            itr.remove();
            removeNum++;
            then.accept(value);
        }
    }
    return removeNum;
}
 
Example #15
Source File: ThrottleTracker.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean throttle(InetAddress address) {
	synchronized (tracker) {
		long ctime = Utils.currentTimeMillisFromNanoTime();
		LongIterator iterator = tracker.values().iterator();
		while (iterator.hasNext()) {
			if (iterator.nextLong() < ctime) {
				iterator.remove();
			}
		}
		long ret = tracker.put(address, ctime + time);
		return ret != tracker.defaultReturnValue();
	}
}
 
Example #16
Source File: DateTimeColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public DateTimeColumn removeMissing() {
  DateTimeColumn noMissing = emptyCopy();
  LongIterator iterator = longIterator();
  while (iterator.hasNext()) {
    long i = iterator.nextLong();
    if (!valueIsMissing(i)) {
      noMissing.appendInternal(i);
    }
  }
  return noMissing;
}
 
Example #17
Source File: InstantColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public InstantColumn removeMissing() {
  InstantColumn noMissing = emptyCopy();
  LongIterator iterator = longIterator();
  while (iterator.hasNext()) {
    long i = iterator.nextLong();
    if (!valueIsMissing(i)) {
      noMissing.appendInternal(i);
    }
  }
  return noMissing;
}
 
Example #18
Source File: PageRank.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
private void iterate(double dampingAmount, LongArrayList noOuts) {
  double nextPR[] = new double[(int) (maxNodeId + 1)]; // PageRank vector after the iteration.

  // First compute how much mass is trapped at the dangling nodes.
  double dangleSum = 0.0;
  LongIterator iter = noOuts.iterator();
  while (iter.hasNext()) {
    dangleSum += prVector[(int) iter.nextLong()];
  }
  dangleSum = dampingFactor * dangleSum / nodeCount;

  // Distribute PageRank mass.
  iter = nodes.iterator();
  while (iter.hasNext()) {
    long v = iter.nextLong();
    int outDegree = graph.getOutDegree(v);
    if (outDegree > 0) {
      double outWeight = dampingFactor * prVector[(int) v] / outDegree;
      EdgeIterator edges = graph.getOutEdges(v);
      while (edges.hasNext()) {
        int nbr = (int) edges.nextLong();
        nextPR[nbr] += outWeight;
      }
    }

    nextPR[(int) v] += dampingAmount + dangleSum;
  }

  normL1 = computeL1Norm(prVector, nextPR);
  prVector = nextPR;
}
 
Example #19
Source File: MultiThreadedPageRank.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
private void iterate(double dampingAmount, LongArrayList noOuts, final LongArrayList[] nodePartitions) {
  AtomicDoubleArray nextPR = new AtomicDoubleArray((int) (maxNodeId + 1));

  // First compute how much mass is trapped at the dangling nodes.
  double dangleSum = 0.0;
  LongIterator iter = noOuts.iterator();
  while (iter.hasNext()) {
    dangleSum += prVector.get((int) iter.nextLong());
  }
  dangleSum = dampingFactor * dangleSum / nodeCount;
  final double d = dangleSum;

  // We use a CountDownLatch as a sync barrier to wait for all threads to finish on their
  // respective partitions.
  final CountDownLatch latch = new CountDownLatch(threads);

  // Start all the worker threads over each partition.
  for (int i=0;i<threads; i++ ) {
    new PageRankWorker(i, nextPR, nodePartitions[i], latch, dampingAmount + d).start();
  }
  // Note that an alternative implementation would be to use a CyclicBarrier so we don't need to
  // respawn new threads each time, but for a graph of any size, the cost of respawning new
  // threads is small relative to the cost the actual iterations.

  // Wait for all the threads to finish.
  try {
    latch.await();
  } catch (InterruptedException ex) {
    // Something bad happened, just abort.
    throw new RuntimeException("Error running PageRank!");
  }

  normL1 = computeL1Norm(prVector, nextPR);
  prVector = nextPR;
}
 
Example #20
Source File: DateTimeColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public DateTimeColumn removeMissing() {
  DateTimeColumn noMissing = emptyCopy();
  LongIterator iterator = longIterator();
  while (iterator.hasNext()) {
    long i = iterator.nextLong();
    if (!valueIsMissing(i)) {
      noMissing.appendInternal(i);
    }
  }
  return noMissing;
}
 
Example #21
Source File: DictionaryValuesWriter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
@Override
public DictionaryPage toDictPageAndClose() {
  if (lastUsedDictionarySize > 0) {
    // return a dictionary only if we actually used it
    PlainValuesWriter dictionaryEncoder = new PlainValuesWriter(lastUsedDictionaryByteSize, maxDictionaryByteSize, allocator);
    LongIterator longIterator = longDictionaryContent.keySet().iterator();
    // write only the part of the dict that we used
    for (int i = 0; i < lastUsedDictionarySize; i++) {
      dictionaryEncoder.writeLong(longIterator.nextLong());
    }
    return dictPage(dictionaryEncoder);
  }
  return null;
}
 
Example #22
Source File: InstantColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public InstantColumn removeMissing() {
  InstantColumn noMissing = emptyCopy();
  LongIterator iterator = longIterator();
  while (iterator.hasNext()) {
    long i = iterator.nextLong();
    if (!valueIsMissing(i)) {
      noMissing.appendInternal(i);
    }
  }
  return noMissing;
}
 
Example #23
Source File: SpotEliasFanoOffsets.java    From dexter with Apache License 2.0 4 votes vote down vote up
@Override
public LongIterator iterator() {
	return new OffsetsFileIterator(file);
}
 
Example #24
Source File: DateTimeColumn.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public LongIterator longIterator() {
  return data.iterator();
}
 
Example #25
Source File: InstantColumn.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public LongIterator longIterator() {
  return data.iterator();
}
 
Example #26
Source File: Player.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
protected boolean orderChunks() {
    if (!this.connected) {
        return false;
    }

    Timings.playerChunkOrderTimer.startTiming();

    this.nextChunkOrderRun = 200;

    loadQueue.clear();
    Long2ObjectOpenHashMap<Boolean> lastChunk = new Long2ObjectOpenHashMap<>(this.usedChunks);

    int centerX = (int) this.x >> 4;
    int centerZ = (int) this.z >> 4;

    int radius = spawned ? this.chunkRadius : (int) Math.ceil(Math.sqrt(spawnThreshold));
    int radiusSqr = radius * radius;



    long index;
    for (int x = 0; x <= radius; x++) {
        int xx = x * x;
        for (int z = 0; z <= x; z++) {
            int distanceSqr = xx + z * z;
            if (distanceSqr > radiusSqr) continue;

            /* Top right quadrant */
            if(this.usedChunks.get(index = Level.chunkHash(centerX + x, centerZ + z)) != Boolean.TRUE) {
                this.loadQueue.put(index, Boolean.TRUE);
            }
            lastChunk.remove(index);
            /* Top left quadrant */
            if(this.usedChunks.get(index = Level.chunkHash(centerX - x - 1, centerZ + z)) != Boolean.TRUE) {
                this.loadQueue.put(index, Boolean.TRUE);
            }
            lastChunk.remove(index);
            /* Bottom right quadrant */
            if(this.usedChunks.get(index = Level.chunkHash(centerX + x, centerZ - z - 1)) != Boolean.TRUE) {
                this.loadQueue.put(index, Boolean.TRUE);
            }
            lastChunk.remove(index);
            /* Bottom left quadrant */
            if(this.usedChunks.get(index = Level.chunkHash(centerX - x - 1, centerZ - z - 1)) != Boolean.TRUE) {
                this.loadQueue.put(index, Boolean.TRUE);
            }
            lastChunk.remove(index);
            if(x != z){
                /* Top right quadrant mirror */
                if(this.usedChunks.get(index = Level.chunkHash(centerX + z, centerZ + x)) != Boolean.TRUE) {
                    this.loadQueue.put(index, Boolean.TRUE);
                }
                lastChunk.remove(index);
                /* Top left quadrant mirror */
                if(this.usedChunks.get(index = Level.chunkHash(centerX - z - 1, centerZ + x)) != Boolean.TRUE) {
                    this.loadQueue.put(index, Boolean.TRUE);
                }
                lastChunk.remove(index);
                /* Bottom right quadrant mirror */
                if(this.usedChunks.get(index = Level.chunkHash(centerX + z, centerZ - x - 1)) != Boolean.TRUE) {
                    this.loadQueue.put(index, Boolean.TRUE);
                }
                lastChunk.remove(index);
                /* Bottom left quadrant mirror */
                if(this.usedChunks.get(index = Level.chunkHash(centerX - z - 1, centerZ - x - 1)) != Boolean.TRUE) {
                    this.loadQueue.put(index, Boolean.TRUE);
                }
                lastChunk.remove(index);
            }
        }
    }

    LongIterator keys = lastChunk.keySet().iterator();
    while (keys.hasNext()) {
        index = keys.nextLong();
        this.unloadChunk(Level.getHashX(index), Level.getHashZ(index));
    }

    Timings.playerChunkOrderTimer.stopTiming();
    return true;
}
 
Example #27
Source File: MockEdgeIterator.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
MockEdgeIterator(LongIterator iterator) {
  edgeIterator = iterator;
}
 
Example #28
Source File: DateTimeColumn.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public LongIterator longIterator() {
  return data.iterator();
}
 
Example #29
Source File: InstantColumn.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public LongIterator longIterator() {
  return data.iterator();
}
 
Example #30
Source File: PrecisionOrRecall.java    From StreamingRec with Apache License 2.0 4 votes vote down vote up
public void evaluate(Transaction transaction, LongArrayList recommendations, LongOpenHashSet userTransactions) {
	//if there is no ground truth, there is nothing to evaluate
	if (userTransactions == null || userTransactions.isEmpty()) {
		return;
	}
	// if the algorithm does not return any recommendations, count it as 0
	if (recommendations.isEmpty()) {
		results.add(0);
		return;
	}

	// if the algorithm retrieves less than k recommendations, we calculate
	// the real k value for this case
	int realK = Math.min(k, recommendations.size());

	// check duplicates
	LongOpenHashSet uniqueRecs = new LongOpenHashSet();
	for (int i = 0; i < realK; i++) {
		if (!uniqueRecs.add(recommendations.getLong(i))) {
			throw new RuntimeException("Duplicate recommendation.");
		}
	}

	// calculate the precision
	double result = 0;
	// iterate over relevant items and recommendations to calculate the
	// intersection
	for (LongIterator iterator = userTransactions.iterator(); iterator.hasNext();) {
		long itemID = iterator.nextLong();
		for (int i = 0; i < realK; i++) {
			if (itemID == recommendations.getLong(i)) {
				result++;
			}
		}
	}

	//determine the divider of the fraction (different for precision and recall)
	double divider;
	if(type == Type.Precision){
		divider = realK;
	}else if(type == Type.Recall){
		divider = userTransactions.size();
	}else{
		throw new RuntimeException("Neither precision nor recall defined.");
	}
	// store the precision/Recall
	results.add(result / divider);
}