Java Code Examples for it.unimi.dsi.fastutil.ints.IntArrays#quickSort()

The following examples show how to use it.unimi.dsi.fastutil.ints.IntArrays#quickSort() . 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: StringDictionaryEncoder.java    From hive-dwrf with Apache License 2.0 6 votes vote down vote up
private void visitDictionary(Visitor<Text> visitor, VisitorContextImpl context
                    ) throws IOException {
    int[] keysArray = null;
    if (sortKeys) {
      keysArray = new int[numElements];
      for (int idx = 0; idx < numElements; idx++) {
        keysArray[idx] = idx + 1;
      }
      IntArrays.quickSort(keysArray, new TextPositionComparator());
    }

    for (int pos = 0; pos < numElements; pos++) {
      context.setOriginalPosition(keysArray == null? pos + 1: keysArray[pos]);
      visitor.visit(context);
    }
    keysArray = null;
}
 
Example 2
Source File: SliceDictionaryColumnWriter.java    From presto with Apache License 2.0 5 votes vote down vote up
private static int[] getSortedDictionaryNullsLast(Block elementBlock)
{
    int[] sortedPositions = new int[elementBlock.getPositionCount()];
    for (int i = 0; i < sortedPositions.length; i++) {
        sortedPositions[i] = i;
    }

    IntArrays.quickSort(sortedPositions, 0, sortedPositions.length, (int left, int right) -> {
        boolean nullLeft = elementBlock.isNull(left);
        boolean nullRight = elementBlock.isNull(right);
        if (nullLeft && nullRight) {
            return 0;
        }
        if (nullLeft) {
            return 1;
        }
        if (nullRight) {
            return -1;
        }
        return elementBlock.compareTo(
                left,
                0,
                elementBlock.getSliceLength(left),
                elementBlock,
                right,
                0,
                elementBlock.getSliceLength(right));
    });

    return sortedPositions;
}
 
Example 3
Source File: FastUtil.java    From minie with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Given an IntArrayList object, sort it, and return an integer array, containing the sorted elements
 * @param intList: the input list to be sorted
 * @return a sorted integer array
 */
public static int [] sort(IntArrayList intList){
    // Sort the indices and return them
    int [] sorted = new int[intList.size()];
    for (int i = 0; i < intList.size(); i++){
        sorted[i] = intList.getInt(i);
    }
    IntArrays.quickSort(sorted);
    
    return sorted;
}
 
Example 4
Source File: Relation.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public Relation removeColumns(int... columnIndexes) {
  IntArrays.quickSort(columnIndexes, IntComparators.OPPOSITE_COMPARATOR);
  for (int i : columnIndexes) {
    removeColumns(column(i));
  }
  return this;
}
 
Example 5
Source File: Relation.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public Relation removeColumns(int... columnIndexes) {
  IntArrays.quickSort(columnIndexes, IntComparators.OPPOSITE_COMPARATOR);
  for (int i : columnIndexes) {
    removeColumns(column(i));
  }
  return this;
}
 
Example 6
Source File: MutableSegmentImpl.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the docIds to use for iteration when the data is sorted by the given column.
 * <p>Called only by realtime record reader.
 *
 * @param column The column to use for sorting
 * @return The docIds to use for iteration
 */
public int[] getSortedDocIdIterationOrderWithSortedColumn(String column) {
  BaseMutableDictionary dictionary = _dictionaryMap.get(column);
  int numValues = dictionary.length();

  int[] dictIds = new int[numValues];
  for (int i = 0; i < numValues; i++) {
    dictIds[i] = i;
  }

  IntArrays.quickSort(dictIds, (dictId1, dictId2) -> dictionary.compare(dictId1, dictId2));
  RealtimeInvertedIndexReader invertedIndex = (RealtimeInvertedIndexReader) _invertedIndexMap.get(column);
  int[] docIds = new int[_numDocsIndexed];
  int docIdIndex = 0;
  for (int dictId : dictIds) {
    IntIterator intIterator = invertedIndex.getDocIds(dictId).getIntIterator();
    while (intIterator.hasNext()) {
      docIds[docIdIndex++] = intIterator.next();
    }
  }

  // Sanity check
  Preconditions.checkState(_numDocsIndexed == docIdIndex,
      "The number of documents indexed: %s is not equal to the number of sorted documents: %s", _numDocsIndexed,
      docIdIndex);

  return docIds;
}
 
Example 7
Source File: IntDictionaryEncoder.java    From hive-dwrf with Apache License 2.0 5 votes vote down vote up
public void visitDictionary(Visitor<Long> visitor, IntDictionaryEncoderVisitorContext context) throws IOException {
    int[] keysArray = null;
    if (sortKeys) {
      keysArray = new int[numElements];
      for (int idx = 0; idx < numElements; idx++) {
        keysArray[idx] = idx;
      }
      IntArrays.quickSort(keysArray, new LongPositionComparator());
    }
    for (int pos = 0; pos < numElements; pos++) {
      context.setOriginalPosition(keysArray == null? pos : keysArray[pos]);
      visitor.visit(context);
    }
}
 
Example 8
Source File: ArraysOverlapFunction.java    From presto with Apache License 2.0 4 votes vote down vote up
@SqlNullable
@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public Boolean arraysOverlap(
        @OperatorDependency(operator = LESS_THAN, argumentTypes = {"E", "E"}) MethodHandle lessThanFunction,
        @TypeParameter("E") Type type,
        @SqlType("array(E)") Block leftArray,
        @SqlType("array(E)") Block rightArray)
{
    int leftPositionCount = leftArray.getPositionCount();
    int rightPositionCount = rightArray.getPositionCount();

    if (leftPositionCount == 0 || rightPositionCount == 0) {
        return false;
    }

    if (leftPositions == null || leftPositions.length < leftPositionCount) {
        leftPositions = new int[leftPositionCount * 2];
    }

    if (rightPositions == null || rightPositions.length < rightPositionCount) {
        rightPositions = new int[rightPositionCount * 2];
    }

    for (int i = 0; i < leftPositionCount; i++) {
        leftPositions[i] = i;
    }
    for (int i = 0; i < rightPositionCount; i++) {
        rightPositions[i] = i;
    }
    IntArrays.quickSort(leftPositions, 0, leftPositionCount, intBlockCompare(type, leftArray));
    IntArrays.quickSort(rightPositions, 0, rightPositionCount, intBlockCompare(type, rightArray));

    int leftCurrentPosition = 0;
    int rightCurrentPosition = 0;
    while (leftCurrentPosition < leftPositionCount && rightCurrentPosition < rightPositionCount) {
        if (leftArray.isNull(leftPositions[leftCurrentPosition]) || rightArray.isNull(rightPositions[rightCurrentPosition])) {
            // Nulls are in the end of the array. Non-null elements do not overlap.
            return null;
        }
        int compareValue = type.compareTo(leftArray, leftPositions[leftCurrentPosition], rightArray, rightPositions[rightCurrentPosition]);
        if (compareValue > 0) {
            rightCurrentPosition++;
        }
        else if (compareValue < 0) {
            leftCurrentPosition++;
        }
        else {
            return true;
        }
    }
    return leftArray.isNull(leftPositions[leftPositionCount - 1]) || rightArray.isNull(rightPositions[rightPositionCount - 1]) ? null : false;
}
 
Example 9
Source File: LayeredLabelPropagation.java    From fasten with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
	final ImmutableGraph symGraph = this.symGraph;
	final int numNodes = LayeredLabelPropagation.this.n;
	final long numArcs = LayeredLabelPropagation.this.symGraph.numArcs();
	final int[] perm = this.perm;
	int[] permutedSuccessors = new int[32];
	int[] successors;
	final long granularity = Math.max(1024, numArcs >>> 9);
	int start, end;

	double gapCost = 0;
	for (;;) {

		// Try to get another piece of work.
		synchronized(LayeredLabelPropagation.this.cumulativeOutdegrees) {
			if (nextNode == numNodes) {
				LayeredLabelPropagation.this.gapCost.add(gapCost);
				break;
			}
			start = nextNode;
			final long target = nextArcs + granularity;
			if (target >= numArcs) nextNode = numNodes;
			else {
				nextArcs = cumulativeOutdegrees.skipTo(target);
				nextNode = cumulativeOutdegrees.currentIndex();
			}
			end = nextNode;
		}

		final NodeIterator nodeIterator = symGraph.nodeIterator(start);
		for (int i = start; i < end; i++) {
			nodeIterator.nextInt();
			final int node = perm[i];
			final int outdegree = nodeIterator.outdegree();
			if (outdegree > 0) {
				successors = nodeIterator.successorArray();
				permutedSuccessors = IntArrays.grow(permutedSuccessors, outdegree);
				for (int j = outdegree; j-- != 0;)
					permutedSuccessors[j] = perm[successors[j]];
				IntArrays.quickSort(permutedSuccessors, 0, outdegree);
				int prev = node;
				for (int j = 0; j < outdegree; j++) {
					gapCost += Fast.ceilLog2(Math.abs(prev - permutedSuccessors[j]));
					prev = permutedSuccessors[j];
				}
			}
		}
	}
}