toArray ( )源码实例Demo

java.util.PriorityQueue#toArray ( )源码实例Demo

下面列出了java.util.PriorityQueue#toArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: SoloPi   文件: OperationNode.java
/**
 * 根据属性排序子节点
 * @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()]);
}
 
源代码2 项目: THUCTC   文件: LiblinearTextClassifier.java
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...=======================================");
}
 
源代码3 项目: RDFS   文件: MinimumSpanningTree.java
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;	
}
 
源代码4 项目: RecSys2018   文件: FloatElement.java
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;
}
 
源代码5 项目: RecSys2018   文件: FloatElement.java
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;
}
 
源代码6 项目: RecSys2018   文件: FloatElement.java
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;
}
 
源代码7 项目: RecSys2018   文件: MLMatrixElement.java
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;
}
 
源代码8 项目: RecSys2018   文件: MLMatrixElement.java
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;
}
 
源代码9 项目: openjdk-jdk9   文件: PriorityQueueTest.java
/**
 * 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());
}
 
源代码10 项目: openjdk-jdk9   文件: PriorityQueueTest.java
/**
 * 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());
}
 
源代码11 项目: j2objc   文件: PriorityQueueTest.java
/**
 * {@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]));
    }
}
 
源代码12 项目: j2objc   文件: PriorityQueueTest.java
/**
 * 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());
}
 
源代码13 项目: j2objc   文件: PriorityQueueTest.java
/**
 * 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());
}
 
源代码14 项目: RDFS   文件: MinimumSpanningTree.java
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);
}
 
源代码15 项目: RDFS   文件: MinimumSpanningTree.java
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);
}
 
源代码16 项目: hop   文件: ReadAllCache.java
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() ][] )
  };
}
 
源代码17 项目: yolov3-android-tflite   文件: Classifier.java
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;
}
 
源代码18 项目: RecSys2018   文件: FloatElement.java
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;
}
 
源代码19 项目: pentaho-kettle   文件: ReadAllCache.java
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() ][] )
  };
}