Java Code Examples for java.util.PriorityQueue#toArray()

The following examples show how to use java.util.PriorityQueue#toArray() . 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: OperationNode.java    From SoloPi with Apache License 2.0 6 votes vote down vote up
/**
 * 根据属性排序子节点
 * @param assistantNodes
 * @return
 */
public static AssistantNode[] sortAssistantNodes(List<AssistantNode> assistantNodes) {
    if (assistantNodes == null || assistantNodes.size() == 0) {
        return new AssistantNode[0];
    }

    PriorityQueue<AssistantNode> sorted = new PriorityQueue<>(assistantNodes.size(), new Comparator<AssistantNode>() {
        @Override
        public int compare(AssistantNode lhs, AssistantNode rhs) {
            int lhsP = lhs.calculatePriority();
            int rhsP = rhs.calculatePriority();
            return rhsP- lhsP;
        }
    });
    sorted.addAll(assistantNodes);
    return sorted.toArray(new AssistantNode[assistantNodes.size()]);
}
 
Example 2
Source File: LiblinearTextClassifier.java    From THUCTC with MIT License 6 votes vote down vote up
public void outputSecletedFeatures(PriorityQueue<Term> features){
	System.out.println("store features...=======================================");
	try{
		TextFileWriter tw = new TextFileWriter("selectedFeatures","UTF-8");
		Term[] f;
		f = features.toArray(new Term[features.size()]);
		System.out.println(f.length);
		for(int i=f.length-1; i>=0; i--){
			tw.writeLine(lexicon.getWord(f[i].id).getName() + " " + f[i].weight);
		}
		tw.flush();
		tw.close();
		
	}catch(IOException e){
		e.printStackTrace();
	}
	System.out.println("end store features...=======================================");
}
 
Example 3
Source File: MinimumSpanningTree.java    From RDFS with Apache License 2.0 6 votes vote down vote up
static public MinimumSpanningTree build(TreeNode[] nodes, int[][] distances, int root) {
	MinimumSpanningTree mst = new MinimumSpanningTree(nodes[root]);
	PriorityQueue<Vertex> costList = new PriorityQueue<Vertex>();
	
	for(int i = 0; i < distances.length; i++) {
		if(i != root)
			costList.add(new Vertex(i, root, distances[i][root]));
	}
	
	while(!costList.isEmpty()) {
		Vertex next = costList.poll();
		nodes[next.to].addChild(nodes[next.id]);
		Vertex[] remains = costList.toArray(new Vertex[0]);
		for(int i = 0; i<remains.length; i++) {
			if(distances[remains[i].id][next.id] <= remains[i].value) {
				costList.remove(remains[i]);
				remains[i].to = next.id;
				remains[i].value = distances[remains[i].id][next.id];
				costList.add(remains[i]);
			}
		}
	}
	return mst;	
}
 
Example 4
Source File: MinimumSpanningTree.java    From RDFS with Apache License 2.0 5 votes vote down vote up
static public Result chooseAndBuildLine(TreeNode[] nodes, int[][] distances, 
		int root, int nodeNumber) {
	PriorityQueue<Vertex> costList = new PriorityQueue<Vertex>();
	int[] locationsChoosed = new int[nodeNumber];	
	Arrays.fill(locationsChoosed, -1);
	int totalWeight = 0;

	for (int i = 0; i < distances.length; i++) {
		if (i != root)
			costList.add(new Vertex(i, root, distances[i][root]));
	}
	int number = 0;	
	while ((number < nodeNumber) && (!costList.isEmpty())) {
		Vertex next = costList.poll();
		nodes[next.to].addChild(nodes[next.id]);
		totalWeight += next.value;
		//LOG.info("NTar: add " + nodes[next.id] + " as child of " + nodes[next.to]);
		locationsChoosed[number] = next.id;
		number = number + 1;
		Vertex[] remains = costList.toArray(new Vertex[0]);
		for (int i = 0; i<remains.length; i++) {
			costList.remove(remains[i]);
			remains[i].to = next.id;
			remains[i].value = distances[remains[i].id][next.id];
			costList.add(remains[i]);
		}
	}
	return new Result(totalWeight, locationsChoosed);
}
 
Example 5
Source File: FloatElement.java    From RecSys2018 with Apache License 2.0 5 votes vote down vote up
public static FloatElement[] topNSort(final float[] vec, final int topN,
		final Set<Integer> exclusions) {

	final Comparator<FloatElement> valAscending = new FloatElement.ValueComparator(
			false);
	final Comparator<FloatElement> valDescending = new FloatElement.ValueComparator(
			true);

	PriorityQueue<FloatElement> heap = new PriorityQueue<FloatElement>(topN,
			valAscending);

	for (int i = 0; i < vec.length; i++) {
		if (exclusions != null && exclusions.contains(i) == true) {
			continue;
		}
		float val = vec[i];
		if (heap.size() < topN) {
			heap.add(new FloatElement(i, val));

		} else {
			if (heap.peek().value < val) {
				heap.poll();
				heap.add(new FloatElement(i, val));
			}
		}
	}

	FloatElement[] heapArray = new FloatElement[heap.size()];
	heap.toArray(heapArray);
	Arrays.sort(heapArray, valDescending);

	return heapArray;
}
 
Example 6
Source File: FloatElement.java    From RecSys2018 with Apache License 2.0 5 votes vote down vote up
public static FloatElement[] topNSort(final FloatElement[] vec,
		final int topN, final Set<Integer> exclusions) {

	final Comparator<FloatElement> valAscending = new FloatElement.ValueComparator(
			false);
	final Comparator<FloatElement> valDescending = new FloatElement.ValueComparator(
			true);

	PriorityQueue<FloatElement> heap = new PriorityQueue<FloatElement>(topN,
			valAscending);

	for (int i = 0; i < vec.length; i++) {
		if (exclusions != null && exclusions.contains(i) == true) {
			continue;
		}
		FloatElement element = vec[i];
		if (heap.size() < topN) {
			heap.add(element);

		} else {
			if (heap.peek().value < element.getValue()) {
				heap.poll();
				heap.add(element);
			}
		}
	}

	FloatElement[] heapArray = new FloatElement[heap.size()];
	heap.toArray(heapArray);
	Arrays.sort(heapArray, valDescending);

	return heapArray;
}
 
Example 7
Source File: FloatElement.java    From RecSys2018 with Apache License 2.0 5 votes vote down vote up
public static FloatElement[] topNSortOffset(final float[] vec, int topN,
		final int offset, final int length, Set<Integer> exclusions) {

	final Comparator<FloatElement> valAscending = new FloatElement.ValueComparator(
			false);
	final Comparator<FloatElement> valDescending = new FloatElement.ValueComparator(
			true);
	PriorityQueue<FloatElement> heap = new PriorityQueue<>(topN,
			valAscending);

	for (int i = 0; i < length; i++) {
		if (exclusions != null && exclusions.contains(i) == true) {
			continue;
		}
		float val = vec[i + offset];
		if (heap.size() < topN) {
			heap.add(new FloatElement(i, val));

		} else {
			if (heap.peek().getValue() < val) {
				heap.poll();
				heap.add(new FloatElement(i, val));
			}
		}
	}

	FloatElement[] heapArray = new FloatElement[heap.size()];
	heap.toArray(heapArray);
	Arrays.sort(heapArray, valDescending);

	return heapArray;
}
 
Example 8
Source File: MinimumSpanningTree.java    From RDFS with Apache License 2.0 5 votes vote down vote up
static public Result chooseAndBuildTree(TreeNode[] nodes, int[][] distances, 
		int root, int nodeNumber) {
	PriorityQueue<Vertex> costList = new PriorityQueue<Vertex>();
	int[] locationsChoosed = new int[nodeNumber];	
	int totalWeight = 0;
	for(int i = 0; i < distances.length; i++) {
		if(i != root)
			costList.add(new Vertex(i, root, distances[i][root]));
	}
	int number = 0;	
	while((number < nodeNumber) && (!costList.isEmpty())) {
		Vertex next = costList.poll();
		nodes[next.to].addChild(nodes[next.id]);
		totalWeight += next.value;
		//LOG.info("NTar: add " + nodes[next.id] + " as child of " + nodes[next.to]);
		locationsChoosed[number] = next.id;
		number = number + 1;
		Vertex[] remains = costList.toArray(new Vertex[0]);
		for(int i = 0; i<remains.length; i++) {
			if(distances[remains[i].id][next.id] < remains[i].value) {
				costList.remove(remains[i]);
				remains[i].to = next.id;
				remains[i].value = distances[remains[i].id][next.id];
				costList.add(remains[i]);
			}
		}
	}
	
	return new Result(totalWeight, locationsChoosed);
}
 
Example 9
Source File: MLMatrixElement.java    From RecSys2018 with Apache License 2.0 5 votes vote down vote up
public static MLMatrixElement[] topNSort(final int rowIndex,
		final float[] vec, final int topN, final Set<Integer> exclusions) {

	final Comparator<MLMatrixElement> valAscending = new MLMatrixElement.ValueComparator(
			false);
	final Comparator<MLMatrixElement> valDescending = new MLMatrixElement.ValueComparator(
			true);

	PriorityQueue<MLMatrixElement> heap = new PriorityQueue<MLMatrixElement>(
			topN, valAscending);

	for (int i = 0; i < vec.length; i++) {
		if (exclusions != null && exclusions.contains(i) == true) {
			continue;
		}
		float val = vec[i];
		if (heap.size() < topN) {
			heap.add(new MLMatrixElement(rowIndex, i, val, 0));

		} else {
			if (heap.peek().value < val) {
				heap.poll();
				heap.add(new MLMatrixElement(rowIndex, i, val, 0));
			}
		}
	}

	MLMatrixElement[] heapArray = new MLMatrixElement[heap.size()];
	heap.toArray(heapArray);
	Arrays.sort(heapArray, valDescending);

	return heapArray;
}
 
Example 10
Source File: MLMatrixElement.java    From RecSys2018 with Apache License 2.0 5 votes vote down vote up
public static MLMatrixElement[] topNSort(final MLMatrixElement[] elements,
		final int topN, final Set<Integer> exclusions) {

	final Comparator<MLMatrixElement> valAscending = new MLMatrixElement.ValueComparator(
			false);
	final Comparator<MLMatrixElement> valDescending = new MLMatrixElement.ValueComparator(
			true);

	PriorityQueue<MLMatrixElement> heap = new PriorityQueue<MLMatrixElement>(
			topN, valAscending);

	for (int i = 0; i < elements.length; i++) {
		if (exclusions != null && exclusions.contains(i) == true) {
			continue;
		}
		MLMatrixElement element = elements[i];
		if (heap.size() < topN) {
			heap.add(element);

		} else {
			if (heap.peek().value < element.value) {
				heap.poll();
				heap.add(element);
			}
		}
	}

	MLMatrixElement[] heapArray = new MLMatrixElement[heap.size()];
	heap.toArray(heapArray);
	Arrays.sort(heapArray, valDescending);

	return heapArray;
}
 
Example 11
Source File: PriorityQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * toArray contains all elements
 */
public void testToArray() {
    PriorityQueue q = populatedQueue(SIZE);
    Object[] o = q.toArray();
    Arrays.sort(o);
    for (int i = 0; i < o.length; i++)
        assertSame(o[i], q.poll());
}
 
Example 12
Source File: PriorityQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * toArray(a) contains all elements
 */
public void testToArray2() {
    PriorityQueue<Integer> q = populatedQueue(SIZE);
    Integer[] ints = new Integer[SIZE];
    Integer[] array = q.toArray(ints);
    assertSame(ints, array);
    Arrays.sort(ints);
    for (int i = 0; i < ints.length; i++)
        assertSame(ints[i], q.poll());
}
 
Example 13
Source File: PriorityQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * {@link PriorityQueue#toArray()}
 */
public void test_toArray() throws Exception {
    PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
    Integer[] array = { 2, 45, 7, -12, 9 };
    for (int i = 0; i < array.length; i++) {
        integerQueue.add(array[i]);
    }
    Object[] returnArray = integerQueue.toArray();
    assertEquals(returnArray.length, integerQueue.size());
    for (int i = 0; i < returnArray.length; i++) {
        assertTrue(integerQueue.contains(returnArray[i]));
    }
}
 
Example 14
Source File: PriorityQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toArray contains all elements
 */
public void testToArray() {
    PriorityQueue q = populatedQueue(SIZE);
    Object[] o = q.toArray();
    Arrays.sort(o);
    for (int i = 0; i < o.length; i++)
        assertSame(o[i], q.poll());
}
 
Example 15
Source File: PriorityQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * toArray(a) contains all elements
 */
public void testToArray2() {
    PriorityQueue<Integer> q = populatedQueue(SIZE);
    Integer[] ints = new Integer[SIZE];
    Integer[] array = q.toArray(ints);
    assertSame(ints, array);
    Arrays.sort(ints);
    for (int i = 0; i < ints.length; i++)
        assertSame(ints[i], q.poll());
}
 
Example 16
Source File: ReadAllCache.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private static Object[] createIndexes( DatabaseLookupData stepData, RowMetaInterface keysMeta, Object[][] keys ) {
  final int rowsAmount = keys.length;
  final int[] conditions = stepData.conditions;

  // it makes sense to apply restrictions in the specific order, namely, to use those, that can filter more elements
  // Index.restrictionComparator() uses heuristic "restriction power" of each index
  PriorityQueue<Index> indexes = new PriorityQueue<>( conditions.length, Index.restrictionComparator() );
  List<int[]> otherConditions = new ArrayList<>();
  for ( int i = 0, len = conditions.length; i < len; i++ ) {
    int condition = conditions[ i ];
    Index index = null;
    switch ( condition ) {
      case DatabaseLookupMeta.CONDITION_EQ:
        index = new EqIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_NE:
        index = EqIndex.nonEqualityIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_LT:
        index = new LtIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_LE:
        index = GtIndex.lessOrEqualCache( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_GT:
        index = new GtIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_GE:
        index = LtIndex.greaterOrEqualCache( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_IS_NULL:
        index = new IsNullIndex( i, keysMeta.getValueMeta( i ), rowsAmount, true );
        break;
      case DatabaseLookupMeta.CONDITION_IS_NOT_NULL:
        index = new IsNullIndex( i, keysMeta.getValueMeta( i ), rowsAmount, false );
        break;
    }
    if ( index == null ) {
      otherConditions.add( new int[] { i, condition } );
    } else {
      index.performIndexingOf( keys );
      indexes.add( index );
    }
  }

  return new Object[] {
    indexes.toArray( new Index[ indexes.size() ] ),
    otherConditions.toArray( new int[ otherConditions.size() ][] )
  };
}
 
Example 17
Source File: FloatElement.java    From RecSys2018 with Apache License 2.0 4 votes vote down vote up
public static FloatElement[] topNSortOffset(final float[] vec, int topN,
		int[] exclusionSorted, final int offset, final int length) {

	final Comparator<FloatElement> valAscending = new FloatElement.ValueComparator(
			false);
	final Comparator<FloatElement> valDescending = new FloatElement.ValueComparator(
			true);
	PriorityQueue<FloatElement> heap = new PriorityQueue<>(topN,
			valAscending);

	int skipping = exclusionSorted == null ? -1 : exclusionSorted[0];
	int skippingCur = 0;
	final int exclusionEnd = exclusionSorted == null ? 0
			: exclusionSorted.length;
	for (int i = 0; i < length; i++) {
		if (i == skipping) {
			skippingCur++;
			if (skippingCur < exclusionEnd) {
				skipping = exclusionSorted[skippingCur];
			} else {
				skipping = -1;
			}
			continue;
		}
		float val = vec[i + offset];
		if (heap.size() < topN) {
			heap.add(new FloatElement(i, val));

		} else {
			if (heap.peek().getValue() < val) {
				heap.poll();
				heap.add(new FloatElement(i, val));
			}
		}
	}

	FloatElement[] heapArray = new FloatElement[heap.size()];
	heap.toArray(heapArray);
	Arrays.sort(heapArray, valDescending);

	return heapArray;
}
 
Example 18
Source File: Classifier.java    From yolov3-android-tflite with MIT License 4 votes vote down vote up
protected ArrayList<Recognition> nms(ArrayList<Recognition> list) {
    ArrayList<Recognition> nmsList = new ArrayList<Recognition>();

    for (int k = 0; k < mLabelList.size(); k++) {
        //1.find max confidence per class
        PriorityQueue<Recognition> pq =
                new PriorityQueue<Recognition>(
                        10,
                        new Comparator<Recognition>() {
                            @Override
                            public int compare(final Recognition lhs, final Recognition rhs) {
                                // Intentionally reversed to put high confidence at the head of the queue.
                                return Float.compare(rhs.getConfidence(), lhs.getConfidence());
                            }
                        });

        for (int i = 0; i < list.size(); ++i) {
            if (list.get(i).detectedClass == k) {
                pq.add(list.get(i));
            }
        }
        Log.d("wangmin", "class[" + k + "] pq size: " + pq.size());

        //2.do non maximum suppression
        while(pq.size() > 0) {
            //insert detection with max confidence
            Recognition[] a = new Recognition[pq.size()];
            Recognition[] detections = pq.toArray(a);
            Recognition max = detections[0];
            nmsList.add(max);

            Log.d("wangmin", "before nms pq size: " + pq.size());

            //clear pq to do next nms
            pq.clear();

            for (int j = 1; j < detections.length; j++) {
                Recognition detection = detections[j];
                RectF b = detection.getLocation();
                if (box_iou(max.getLocation(), b) < mNmsThresh){
                    pq.add(detection);
                }
            }
            Log.d("wangmin", "after nms pq size: " + pq.size());
        }
    }
    return nmsList;
}
 
Example 19
Source File: ReadAllCache.java    From hop with Apache License 2.0 4 votes vote down vote up
private static Object[] createIndexes( DatabaseLookupData transformData, IRowMeta keysMeta, Object[][] keys ) {
  final int rowsAmount = keys.length;
  final int[] conditions = transformData.conditions;

  // it makes sense to apply restrictions in the specific order, namely, to use those, that can filter more elements
  // Index.restrictionComparator() uses heuristic "restriction power" of each index
  PriorityQueue<Index> indexes = new PriorityQueue<>( conditions.length, Index.restrictionComparator() );
  List<int[]> otherConditions = new ArrayList<>();
  for ( int i = 0, len = conditions.length; i < len; i++ ) {
    int condition = conditions[ i ];
    Index index = null;
    switch ( condition ) {
      case DatabaseLookupMeta.CONDITION_EQ:
        index = new EqIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_NE:
        index = EqIndex.nonEqualityIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_LT:
        index = new LtIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_LE:
        index = GtIndex.lessOrEqualCache( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_GT:
        index = new GtIndex( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_GE:
        index = LtIndex.greaterOrEqualCache( i, keysMeta.getValueMeta( i ), rowsAmount );
        break;
      case DatabaseLookupMeta.CONDITION_IS_NULL:
        index = new IsNullIndex( i, keysMeta.getValueMeta( i ), rowsAmount, true );
        break;
      case DatabaseLookupMeta.CONDITION_IS_NOT_NULL:
        index = new IsNullIndex( i, keysMeta.getValueMeta( i ), rowsAmount, false );
        break;
    }
    if ( index == null ) {
      otherConditions.add( new int[] { i, condition } );
    } else {
      index.performIndexingOf( keys );
      indexes.add( index );
    }
  }

  return new Object[] {
    indexes.toArray( new Index[ indexes.size() ] ),
    otherConditions.toArray( new int[ otherConditions.size() ][] )
  };
}