Java Code Examples for it.unimi.dsi.fastutil.ints.IntIterator#hasNext()

The following examples show how to use it.unimi.dsi.fastutil.ints.IntIterator#hasNext() . 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: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, TimeColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    IntIterator iterator = column.intIterator();
    while (iterator.hasNext()) {
      dos.writeInt(iterator.nextInt());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example 2
Source File: SparseRepresentation.java    From zetasketch with Apache License 2.0 6 votes vote down vote up
/** Sets the representation's values from {@code iter}. */
private void set(@Nullable IntIterator iter) {
  GrowingByteSlice slice = getRecycledData();
  DifferenceEncoder encoder = new DifferenceEncoder(slice);

  int size = 0;
  while (iter != null && iter.hasNext()) {
    encoder.putInt(iter.nextInt());
    size++;
  }

  buffer.clear();
  recycledData = new WeakReference<>(state.sparseData);
  state.sparseData = slice.flip();
  state.sparseSize = size;
}
 
Example 3
Source File: NormalRepresentation.java    From zetasketch with Apache License 2.0 6 votes vote down vote up
@Override
public NormalRepresentation addSparseValues(
    Encoding.Sparse encoding, @Nullable IntIterator sparseValues) {
  NormalRepresentation repr = maybeDowngrade(encoding.normal(), encoding.sparsePrecision);

  if (sparseValues == null) {
    return repr;
  }

  // Add each sparse value to our backing array, downgrading it if necessary.
  byte[] data = getWriteableData(state);
  while (sparseValues.hasNext()) {
    addSparseValueMaybeDowngrading(data, repr.encoding, sparseValues.nextInt(), encoding);
  }

  return repr;
}
 
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, DateColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    IntIterator iterator = column.intIterator();
    while (iterator.hasNext()) {
      dos.writeInt(iterator.nextInt());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example 5
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(IntCollection collection, IntPredicate predicate, IntConsumer then) {
    if (collection.size() == 0) {
        return 0;
    }
    final IntIterator itr = collection.iterator();
    int value;
    int removeNum = 0;
    while (itr.hasNext()) {
        value = itr.nextInt();
        if (predicate.test(value)) {
            itr.remove();
            removeNum++;
            then.accept(value);
        }
    }
    return removeNum;
}
 
Example 6
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, TimeColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    IntIterator iterator = column.intIterator();
    while (iterator.hasNext()) {
      dos.writeInt(iterator.nextInt());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example 7
Source File: SawWriter.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static void writeColumn(String fileName, DateColumn column) throws IOException {
  try (FileOutputStream fos = new FileOutputStream(fileName);
      SnappyFramedOutputStream sos = new SnappyFramedOutputStream(fos);
      DataOutputStream dos = new DataOutputStream(sos)) {
    int i = 0;
    IntIterator iterator = column.intIterator();
    while (iterator.hasNext()) {
      dos.writeInt(iterator.nextInt());
      i++;
      if (i == FLUSH_AFTER_ITERATIONS) {
        dos.flush();
        i = 0;
      }
    }
    dos.flush();
  }
}
 
Example 8
Source File: CustomHashMap.java    From kourami with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean intersection(CustomHashMap other){
boolean modified = false;
IntIterator itr = this.keySet().iterator();
while(itr.hasNext()){
    int curInt = itr.nextInt();
    if(!other.containsKey(curInt)){
	itr.remove();
	modified = false;
    }
}
return modified;
   }
 
Example 9
Source File: DateFilters.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default Selection eval(IntBiPredicate predicate, int value) {
  Selection selection = new BitmapBackedSelection();
  IntIterator iterator = intIterator();
  int idx = 0;
  while (iterator.hasNext()) {
    int next = iterator.nextInt();
    if (predicate.test(next, value)) {
      selection.add(idx);
    }
    idx++;
  }
  return selection;
}
 
Example 10
Source File: DateFilters.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * This version operates on predicates that treat the given IntPredicate as operating on a packed
 * local time This is much more efficient that using a LocalTimePredicate, but requires that the
 * developer understand the semantics of packedLocalTimes
 */
default Selection eval(IntPredicate predicate) {
  Selection selection = new BitmapBackedSelection();
  IntIterator iterator = intIterator();
  int idx = 0;
  while (iterator.hasNext()) {
    int next = iterator.nextInt();
    if (predicate.test(next)) {
      selection.add(idx);
    }
    idx++;
  }
  return selection;
}
 
Example 11
Source File: SparseDataSet.java    From hlta with GNU General Public License v3.0 5 votes vote down vote up
private DataSet SparseToDense(int batchSize, int start){
	
	if(isRandomised == false){
		randomiseDatacaseIndex();
	}
	/*
	 *  convert from sparse to dense
	 */
	
	// create a dataset over variables corresponding to the external IDs
	DataSet Dense = new DataSet(_VariablesSet);
	
	// adding datacases
	for(int i = start ; i < batchSize + start ; i++){
		
		int[] states = new int[_VariablesSet.length];
		IntCollection row = SDataSet.userMatrix().get(_randomToOriginalDataCaseIndexMap.get(i));
		
		// Filling in the positive entries
		IntIterator iter = row.iterator();
		while(iter.hasNext()){
		    int internal_ID = iter.nextInt(); // the id of the item
		    String name = _item_mapping.toOriginalID(internal_ID);
		    states[_mapNameToIndex.get(name)] = 1;
		}
		Dense.addDataCase(states, 1);
		
	}
	
	return Dense;
}
 
Example 12
Source File: DateColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public DateColumn removeMissing() {
  DateColumn noMissing = emptyCopy();
  IntIterator iterator = intIterator();
  while (iterator.hasNext()) {
    int i = iterator.nextInt();
    if (!valueIsMissing(i)) {
      noMissing.appendInternal(i);
    }
  }
  return noMissing;
}
 
Example 13
Source File: CustomHashMap.java    From kourami with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean removeAll(CustomHashMap other){
boolean modified = false;
IntIterator itr = other.keySet().iterator();
int curKey;
while(itr.hasNext()){
    curKey = itr.next();
    if(this.containsKey(curKey)){
	this.remove(curKey);
	modified = true;
    }
}
return modified;
   }
 
Example 14
Source File: DateFilters.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default Selection eval(IntBiPredicate predicate, int value) {
  Selection selection = new BitmapBackedSelection();
  IntIterator iterator = intIterator();
  int idx = 0;
  while (iterator.hasNext()) {
    int next = iterator.nextInt();
    if (predicate.test(next, value)) {
      selection.add(idx);
    }
    idx++;
  }
  return selection;
}
 
Example 15
Source File: CustomHashMap.java    From kourami with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void printReads(){
IntIterator itr = this.keySet().iterator();
HLA.log.append("{");
while(itr.hasNext())
    HLA.log.append(" (" +itr.nextInt() + ") ");
HLA.log.appendln("}");

   }
 
Example 16
Source File: TimeColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Override
public TimeColumn removeMissing() {
  TimeColumn noMissing = emptyCopy();
  IntIterator iterator = intIterator();
  while (iterator.hasNext()) {
    int i = iterator.nextInt();
    if (!valueIsMissing(i)) {
      noMissing.appendInternal(i);
    }
  }
  return noMissing;
}
 
Example 17
Source File: UnaryInverseDocumentFrequency.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
public UnaryInverseDocumentFrequency(Int2FloatMap keyValues, Collection<TermFrequency> documents) {
	super(keyValues);
	for (TermFrequency document : documents) {
		IntIterator iterator = document.getKeys().iterator();
		while (iterator.hasNext()) {
			int term = iterator.nextInt();
			keyValues.put(term, 1F);
		}
	}
}
 
Example 18
Source File: SparseMatrix.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public int nonZeroElements() {
    int res = 0;
    IntIterator rowIter = indexesMap().keySet().iterator();

    while (rowIter.hasNext()) {
        int row = rowIter.nextInt();
        res += indexesMap().get(row).size();
    }

    return res;
}
 
Example 19
Source File: SetSimilarity.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
private int[] getFasterIntersectionArray(int uidx) {
    int[] intersectionMap = new int[data.numUsers()];

    IntIterator iidxs = data.getUidxIidxs(uidx);
    while (iidxs.hasNext()) {
        IntIterator vidxs = data.getIidxUidxs(iidxs.nextInt());
        while (vidxs.hasNext()) {
            intersectionMap[vidxs.nextInt()]++;
        }
    }

    intersectionMap[uidx] = 0;

    return intersectionMap;
}
 
Example 20
Source File: GroupedTopNBuilder.java    From presto with Apache License 2.0 4 votes vote down vote up
private void processPage(Page newPage, GroupByIdBlock groupIds)
{
    checkArgument(newPage != null);
    checkArgument(groupIds != null);

    // save the new page
    PageReference newPageReference = new PageReference(newPage);
    int newPageId;
    if (emptyPageReferenceSlots.isEmpty()) {
        // all the previous slots are full; create a new one
        pageReferences.ensureCapacity(currentPageCount + 1);
        newPageId = currentPageCount;
        currentPageCount++;
    }
    else {
        // reuse a previously removed page's slot
        newPageId = emptyPageReferenceSlots.dequeueInt();
    }
    verify(pageReferences.get(newPageId) == null, "should not overwrite a non-empty slot");
    pageReferences.set(newPageId, newPageReference);

    // update the affected heaps and record candidate pages that need compaction
    IntSet pagesToCompact = new IntOpenHashSet();
    for (int position = 0; position < newPage.getPositionCount(); position++) {
        long groupId = groupIds.getGroupId(position);
        groupedRows.ensureCapacity(groupId + 1);

        RowHeap rows = groupedRows.get(groupId);
        if (rows == null) {
            // a new group
            rows = new RowHeap(Ordering.from(comparator).reversed());
            groupedRows.set(groupId, rows);
        }
        else {
            // update an existing group;
            // remove the memory usage for this group for now; add it back after update
            memorySizeInBytes -= rows.getEstimatedSizeInBytes();
        }

        if (rows.size() < topN) {
            // still have space for the current group
            Row row = new Row(newPageId, position);
            rows.enqueue(row);
            newPageReference.reference(row);
        }
        else {
            // may compare with the topN-th element with in the heap to decide if update is necessary
            Row previousRow = rows.first();
            Row newRow = new Row(newPageId, position);
            if (comparator.compare(newRow, previousRow) < 0) {
                // update reference and the heap
                rows.dequeue();
                PageReference previousPageReference = pageReferences.get(previousRow.getPageId());
                previousPageReference.dereference(previousRow.getPosition());
                newPageReference.reference(newRow);
                rows.enqueue(newRow);

                // compact a page if it is not the current input page and the reference count is below the threshold
                if (previousPageReference.getPage() != newPage &&
                        previousPageReference.getUsedPositionCount() * COMPACT_THRESHOLD < previousPageReference.getPage().getPositionCount()) {
                    pagesToCompact.add(previousRow.getPageId());
                }
            }
        }

        memorySizeInBytes += rows.getEstimatedSizeInBytes();
    }

    // unreference new page if it was not used
    if (newPageReference.getUsedPositionCount() == 0) {
        pageReferences.set(newPageId, null);
    }
    else {
        // assure new page is loaded
        newPageReference.loadPage();
        memorySizeInBytes += newPageReference.getEstimatedSizeInBytes();

        // may compact the new page as well
        if (newPageReference.getUsedPositionCount() * COMPACT_THRESHOLD < newPage.getPositionCount()) {
            verify(!pagesToCompact.contains(newPageId));
            pagesToCompact.add(newPageId);
        }
    }

    // compact pages
    IntIterator iterator = pagesToCompact.iterator();
    while (iterator.hasNext()) {
        int pageId = iterator.nextInt();
        PageReference pageReference = pageReferences.get(pageId);
        if (pageReference.getUsedPositionCount() == 0) {
            pageReferences.set(pageId, null);
            emptyPageReferenceSlots.enqueue(pageId);
            memorySizeInBytes -= pageReference.getEstimatedSizeInBytes();
        }
        else {
            memorySizeInBytes -= pageReference.getEstimatedSizeInBytes();
            pageReference.compact();
            memorySizeInBytes += pageReference.getEstimatedSizeInBytes();
        }
    }
}