@Override public double getDouble(int row) { return data.getDouble(row); }
/** * Helper method that performs all operations required to swap two values. * <ul> * <li> Swaps the values in the array that backs the heap. </li> * <li> Updates the indexToKey and keyToIndex maps due to the swap. </li> * </ul> * @param index1 Index to swap * @param index2 Index to swap */ private void swapValues(int index1, int index2) { if (index1 == index2) { return; } double tmp = _values.getDouble(index1); _values.set(index1, _values.getDouble(index2)); _values.set(index2, tmp); swapKeys(index1, index2); }
/** * Helper method that moves the element at the specified index up * until the heap ordering is established. * * @param index Index of element to sift up. */ private void siftUp(int index) { // Return if already at root node. if (index == 0) { return; } while (index != 0) { int parentIndex = getParentIndex(index); double value = _values.getDouble(index); double parentValue = _values.getDouble(parentIndex); if (compare(parentValue, value) == 1) { swapValues(index, parentIndex); index = parentIndex; } else { // No more sifting up required, break break; } } }
minIndex = leftChildIndex; } else { // Node has both left and right children, find the minimum of the two. double leftChildValue = _values.getDouble(leftChildIndex); double rightChildValue = _values.getDouble(rightChildIndex); if (compare(_values.getDouble(index), _values.getDouble(minIndex)) == 1) { swapValues(index, minIndex); index = minIndex;
/** * Returns the key+value pair with the max priority (min for minHeap mode) * <ul> * <li> key+value pair is not removed from the priority queue. </li> * <li> Throws runtime exception if the priority queue is empty. </li> * <li> Runtime complexity of O(1). </li> * </ul> * * @return Key+Value pair */ public IntDoublePair peek() { if (_values.isEmpty()) { throw new RuntimeException("Empty collection"); } _reusablePair.setIntValue(_indexToKeyMap.get(0)); _reusablePair.setDoubleValue(_values.getDouble(0)); return _reusablePair; }
/** * Returns the value for the specified key. * <ul> * <li> Returns null if the specified key does not exist. </li> * <li> Runtime complexity of O(1). </li> * </ul> * * @param key Key for which to return the value * @return Value for the key */ public IntDoublePair get(int key) { if (!_keyToIndexMap.containsKey(key)) { return null; } int index = _keyToIndexMap.get(key); double value = _values.getDouble(index); _reusablePair.setIntValue(index); _reusablePair.setDoubleValue(value); return _reusablePair; }
/** * Construct the VectorBasedSequentialSparseVector by a int indices and double values. * * @param cardinality * @param indices * @param doubleValues */ public VectorBasedSequentialSparseVector(int cardinality, IntArrayList indices, DoubleArrayList doubleValues) { this(cardinality, indices.size()); int entryCount = indices.size(); OrderedVectorElement[] sortableEntries = new OrderedVectorElement[entryCount]; int elementIndex = 0; for (int index = 0; index < entryCount; index++) { sortableEntries[elementIndex++] = new OrderedVectorElement(indices.getInt(index), doubleValues.getDouble(index)); } Arrays.sort(sortableEntries); int[] tmpIndices = new int[sortableEntries.length]; double[] tmpDoubleValues = new double[sortableEntries.length]; for (int index = 0; index < sortableEntries.length; index++) { tmpIndices[index] = sortableEntries[index].getIndex(); tmpDoubleValues[index] = sortableEntries[index].getValue(); } values = new OrderedIntDoubleMapping(tmpIndices, tmpDoubleValues); }
@Override public Long2DoubleMap finishMap() { if (scores == null) { return Long2DoubleMaps.EMPTY_MAP; } assert size == heap.size(); int[] indices = new int[size]; // Copy backwards so the scored list is sorted. for (int i = size - 1; i >= 0; i--) { indices[i] = heap.dequeueInt(); } assert heap.isEmpty(); long[] keys = new long[indices.length]; double[] values = new double[indices.length]; for (int i = 0; i < indices.length; i++) { keys[i] = items.getLong(indices[i]); values[i] = scores.getDouble(indices[i]); } clear(); return Long2DoubleSortedArrayMap.wrapUnsorted(keys, values); }
@Override public Double getWeight(int i) { return this.weightList.getDouble(i); }
/** * Get a particular entry of the vector. * * @param i the entry * @return the value of the entry. */ public double get(int i) { // The default value is 0.0 if (i >= entries.size()) { return 0.0; } return entries.getDouble(i); }
} else { if (!heap.isEmpty() && score <= scores.getDouble(heap.firstInt())) { return; // the item won't beat anything else
public double[] toDoubleArray() { double[] output = new double[data.size()]; for (int i = 0; i < data.size(); i++) { output[i] = data.getDouble(i); } return output; }
@Override public double get(int i) { if(!reverseMap.containsValue(i)) { return 0; } return values.getDouble(reverseMap.get(i)); }
@Override public int countUnique() { DoubleSet floats = new DoubleOpenHashSet(); for (int i = 0; i < size(); i++) { floats.add(data.getDouble(i)); } return floats.size(); }
/** * Make the edge point to the given index in the backing arrays. * * @param index Index in the arrays */ public void setIndex(int index) { // Update the id and value objects from the superclass. getTargetVertexId().set(neighbors.getLong(index)); getValue().set(edgeValues.getDouble(index)); // Update the index. this.index = index; }
@Override public DoubleColumn unique() { DoubleSet floats = new DoubleOpenHashSet(); for (int i = 0; i < size(); i++) { floats.add(data.getDouble(i)); } DoubleColumn column = new DoubleColumn(name() + " Unique values", floats.size()); floats.forEach(column::add); return column; }