Java Code Examples for java.util.TreeSet#pollLast()

The following examples show how to use java.util.TreeSet#pollLast() . 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: Word2VEC.java    From JewelCrawler with GNU General Public License v3.0 5 votes vote down vote up
public Set<WordEntry> distance(String queryWord) {

		float[] center = wordMap.get(queryWord);
		if (center == null) {
			return Collections.emptySet();
		}

		int resultSize = wordMap.size() < topNSize ? wordMap.size() : topNSize;
		TreeSet<WordEntry> result = new TreeSet<WordEntry>();

		double min = Float.MIN_VALUE;
		for (Entry<String, float[]> entry : wordMap.entrySet()) {
			float[] vector = entry.getValue();
			float dist = 0;
			for (int i = 0; i < vector.length; i++) {
				dist += center[i] * vector[i];
			}

			if (dist > min) {
				result.add(new WordEntry(entry.getKey(), dist));
				if (resultSize < result.size()) {
					result.pollLast();
				}
				min = result.last().score;
			}
		}
		result.pollFirst();

		return result;
	}
 
Example 2
Source File: Word2VEC.java    From JewelCrawler with GNU General Public License v3.0 5 votes vote down vote up
public Set<WordEntry> distance(List<String> words) {

		float[] center = null;
		for (String word : words) {
			center = sum(center, wordMap.get(word));
		}

		if (center == null) {
			return Collections.emptySet();
		}

		int resultSize = wordMap.size() < topNSize ? wordMap.size() : topNSize;
		TreeSet<WordEntry> result = new TreeSet<WordEntry>();

		double min = Float.MIN_VALUE;
		for (Entry<String, float[]> entry : wordMap.entrySet()) {
			float[] vector = entry.getValue();
			float dist = 0;
			for (int i = 0; i < vector.length; i++) {
				dist += center[i] * vector[i];
			}

			if (dist > min) {
				result.add(new WordEntry(entry.getKey(), dist));
				if (resultSize < result.size()) {
					result.pollLast();
				}
				min = result.last().score;
			}
		}
		result.pollFirst();

		return result;
	}
 
Example 3
Source File: Word2VEC.java    From Word2Vec with Apache License 2.0 5 votes vote down vote up
public Set<WordEntry> distance(String queryWord) {

		float[] center = wordMap.get(queryWord);
		if (center == null) {
			return Collections.emptySet();
		}

		int resultSize = wordMap.size() < topNSize ? wordMap.size() : topNSize;
		TreeSet<WordEntry> result = new TreeSet<WordEntry>();

		double min = Float.MIN_VALUE;
		for (Map.Entry<String, float[]> entry : wordMap.entrySet()) {
			float[] vector = entry.getValue();
			float dist = 0;
			for (int i = 0; i < vector.length; i++) {
				dist += center[i] * vector[i];
			}

			if (dist > min) {
				result.add(new WordEntry(entry.getKey(), dist));
				if (resultSize < result.size()) {
					result.pollLast();
				}
				min = result.last().score;
			}
		}
		result.pollFirst();

		return result;
	}
 
Example 4
Source File: Word2VEC.java    From Word2Vec with Apache License 2.0 5 votes vote down vote up
public Set<WordEntry> distance(List<String> words) {

		float[] center = null;
		for (String word : words) {
			center = sum(center, wordMap.get(word));
		}

		if (center == null) {
			return Collections.emptySet();
		}

		int resultSize = wordMap.size() < topNSize ? wordMap.size() : topNSize;
		TreeSet<WordEntry> result = new TreeSet<WordEntry>();

		double min = Float.MIN_VALUE;
		for (Map.Entry<String, float[]> entry : wordMap.entrySet()) {
			float[] vector = entry.getValue();
			float dist = 0;
			for (int i = 0; i < vector.length; i++) {
				dist += center[i] * vector[i];
			}

			if (dist > min) {
				result.add(new WordEntry(entry.getKey(), dist));
				if (resultSize < result.size()) {
					result.pollLast();
				}
				min = result.last().score;
			}
		}
		result.pollFirst();

		return result;
	}
 
Example 5
Source File: Word2Vec.java    From Word2Vec with Apache License 2.0 5 votes vote down vote up
/**
 * 获取相似词语
 * @param word
 * @param maxReturnNum
 * @return
 */
public Set<WordEntry> getSimilarWords(String word, int maxReturnNum) {
    if (loadModel == false)
        return null;
    float[] center = getWordVector(word);
    if (center == null) {
        return Collections.emptySet();
    }
    int resultSize = vec.getWords() < maxReturnNum ? vec.getWords() : maxReturnNum;
    TreeSet<WordEntry> result = new TreeSet<WordEntry>();
    double min = Double.MIN_VALUE;
    for (Map.Entry<String, float[]> entry : vec.getWordMap().entrySet()) {
        float[] vector = entry.getValue();
        float dist = calDist(center, vector);
        if (result.size() <= resultSize) {
            result.add(new WordEntry(entry.getKey(), dist));
            min = result.last().score;
        } else {
            if (dist > min) {
                result.add(new WordEntry(entry.getKey(), dist));
                result.pollLast();
                min = result.last().score;
            }
        }
    }
    result.pollFirst();
    return result;
}
 
Example 6
Source File: BuildProviderImpl.java    From pnc with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the page of Latest build (running or finished) filtered by given predicate.
 */
private Page<Build> getLatestBuild(
        java.util.function.Predicate<BuildTask> predicate,
        Predicate<BuildRecord> dbPredicate) {
    TreeSet<Build> sorted = new TreeSet<>(Comparator.comparing(Build::getSubmitTime).reversed());
    readLatestRunningBuild(predicate).ifPresent(sorted::add);
    readLatestFinishedBuild(dbPredicate).ifPresent(sorted::add);
    if (sorted.size() > 1) {
        sorted.pollLast();
    }

    return new Page<>(0, 1, sorted.size(), sorted.size(), sorted);
}
 
Example 7
Source File: MemoryIndex.java    From nlp-lang with Apache License 2.0 5 votes vote down vote up
/**
 * 搜索提示
 * 
 * @param value
 *            返回内容
 * @param score
 *            分数
 * @param fields
 *            提示内容
 */
public void addItem(T value, Double score, String... fields) {
	Set<String> result = null;

	if (fields == null || fields.length == 0) {
		fields = new String[] { value.toString() };
	}

	switch (model) {
	case ALL:
		result = getAllSplit(fields);
		break;
	case PREX:
		result = getPrexSplit(fields);
		break;
	}

	TreeSet<Entry> treeSet;
	for (String key : result) {
		if (StringUtil.isBlank(key)) {
			continue;
		}
		treeSet = index.get(key);

		if (treeSet == null) {
			treeSet = new TreeSet<Entry>();
			index.put(key, treeSet);
		}
		treeSet.add(new Entry(value, score(value, score)));

		if (treeSet.size() > this.size) {
			treeSet.pollLast();
		}
	}
}
 
Example 8
Source File: QueueManagerImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
static TreeSet<UUID> add( TreeSet<UUID> a, UUID uuid, boolean reversed, int limit ) {

        if ( a == null ) {
            a = new TreeSet<UUID>( new UUIDComparator() );
        }

        if ( uuid == null ) {
            return a;
        }

        // if we have less than the limit, just add it
        if ( a.size() < limit ) {
            a.add( uuid );
        }
        else if ( reversed ) {
            // if reversed, we want to add more recent messages
            // and eject the oldest
            if ( UUIDComparator.staticCompare( uuid, a.first() ) > 0 ) {
                a.pollFirst();
                a.add( uuid );
            }
        }
        else {
            // add older messages and eject the newset
            if ( UUIDComparator.staticCompare( uuid, a.last() ) < 0 ) {
                a.pollLast();
                a.add( uuid );
            }
        }

        return a;
    }
 
Example 9
Source File: SnapshotState.java    From jstorm with Apache License 2.0 5 votes vote down vote up
private TransactionState getLastestCommittedState(int taskId) {
    TransactionState ret = null;
    String componentId = context.getComponentId(taskId);
    TreeSet<Long> batchIds = new TreeSet<Long>(inprogressSnapshots.keySet());
    Long batchId = null;
    while ((batchId = batchIds.pollLast()) != null) {
        BatchStateTracker tracker = inprogressSnapshots.get(batchId);
        Map<Integer, TransactionState> states = tracker.getComponentStates(componentId);
        if (states != null && (ret = states.get(taskId)) != null)
            break;
    }
    return ret;
}
 
Example 10
Source File: SMOTE.java    From jatecs with GNU General Public License v3.0 4 votes vote down vote up
protected static HashMap<Integer, List<DocSim>> computeNNfaster(IIndex index, DocSet forDocuments, int k) {
	int nD=index.getDocumentDB().getDocumentsCount();
	SparseMatrix _distcache=new SparseMatrix(nD, nD);
	
	HashMap<Integer, List<DocSim>> nearest_neigbours=new HashMap<>();
	
	List<Integer> alldocuments=DocSet.genDocset(index.getDocumentDB()).asList();
	
	IndexVectorizer docvectorizer = new IndexVectorizer(index);
	HashMap<Integer, SparseVector> docVector = new HashMap<>();
	for(int docid:alldocuments){
		docVector.put(docid, docvectorizer.getDocumentWeights(docid));
	}
	
	List<Integer> doclist=forDocuments.asList();
	int progress=0;
	int totalsteps=doclist.size();
	JatecsLogger.status().println("Knn for " + totalsteps + " documents: ");
	for(int docID_i : doclist){
		TreeSet<DocSim> neighbours = new TreeSet<DocSim>();
		
		double distThreshold = Double.MAX_VALUE;
		SparseVector docvec_i=docVector.get(docID_i);
		
		for(int docID_j : alldocuments){
			if(docID_i==docID_j) continue;
			
			double distance = _distcache.get(Math.min(docID_i, docID_j), Math.max(docID_i, docID_j));
			if(distance==0){				
				distance = euclideanSquaredDistanceThresholded(docvec_i, docVector.get(docID_j), distThreshold);
				_distcache.set(Math.min(docID_i, docID_j), Math.max(docID_i, docID_j), distance);
			}
				
			neighbours.add(new DocSim(docID_j, distance));
			if(neighbours.size()>k){
				neighbours.pollLast();
				distThreshold = Math.min(neighbours.last().distance, distThreshold);
			}
		}

		nearest_neigbours.put(docID_i, new ArrayList<DocSim>(neighbours));
		
		if(progress++%(Math.max(totalsteps/10,1))==0)
			JatecsLogger.status().print("..."+(progress*100/totalsteps)+"%");
	}
	JatecsLogger.status().println("");
	
	return nearest_neigbours;
}
 
Example 11
Source File: PriorityQueueManager.java    From spring-boot-email-tools with Apache License 2.0 4 votes vote down vote up
public boolean enqueue(final EmailSchedulingData emailSchedulingData, final boolean isFromPersistenceLayer) {
    log.debug("Called Enqueue [currently queued = {}, isFromPersistenceLayer = {}]", currentlyInQueue(), isFromPersistenceLayer);
    queueLock.lock();
    try {
        while (isCurrentOperationDequeuing() || isCurrentOperationEnqueuing()) {
            notDequeuing.await();
            if (!isCurrentOperationClosing()) {
                setCurrentOperationToEnqueuing();
            }
        }
        if (isCurrentOperationNone() && !isCurrentOperationClosing()) {
            setCurrentOperationToEnqueuing();
        }

        if (isCurrentOperationEnqueuing() && !isCurrentOperationClosing()) {
            final int queueIndex = queueIndex(emailSchedulingData);
            TreeSet<EmailSchedulingData> queue = queues[queueIndex];
            if (!queue.contains(emailSchedulingData)) { //It may happen when fetching from persistence layer
                final boolean isEnqueuable = isFromPersistenceLayer
                        //True also if there is no persistence layer or the queues are empty
                        || beforeLastLoadedFromPersistenceLayer(emailSchedulingData);
                boolean dequeueLastLoaded = isEnqueuable && !canAddOneInMemory() && hasElements();

                if (isEnqueuable) {
                    queues[queueIndex].add(emailSchedulingData);
                    currentlyQueued++;
                } else {
                    log.debug("Email scheduling data {} not queued but should be persisted afterwards", emailSchedulingData);
                }

                if (dequeueLastLoaded) {
                    int queueIndexOfLatestOfAllLast = queueIndexOfLatestOfAllLast();
                    TreeSet<EmailSchedulingData> queueOfLatestOfAllLast = queues[queueIndexOfLatestOfAllLast];
                    queueOfLatestOfAllLast.pollLast();
                    currentlyQueued--;
                }
                return isEnqueuable;
            }
        }
    } catch (InterruptedException e) {
        if (!isCurrentOperationClosing()) {
            log.error("Priority queue manager interrupted during dequeuing operation.", e);
        }
        completeEnqueue();
    }
    return false;
}
 
Example 12
Source File: AmazonS3Util.java    From datacollector with Apache License 2.0 4 votes vote down vote up
/**
 * Lists objects from AmazonS3 in chronological order [lexicographical order if 2 files have same timestamp] which are
 * later than or equal to the timestamp of the previous offset object
 *
 * @param s3Client
 * @param s3ConfigBean
 * @param pathMatcher glob patterns to match file name against
 * @param s3Offset current offset which provides the timestamp of the previous object
 * @param fetchSize number of objects to fetch in one go
 * @return
 * @throws AmazonClientException
 */
static List<S3ObjectSummary> listObjectsChronologically(
    AmazonS3 s3Client,
    S3ConfigBean s3ConfigBean,
    AntPathMatcher pathMatcher,
    S3Offset s3Offset,
    int fetchSize
) {

  //Algorithm:
  // - Full scan all objects that match the file name pattern and which are later than the file in the offset
  // - Select the oldest "fetchSize" number of files and return them.
  TreeSet<S3ObjectSummary> treeSet = new TreeSet<>((o1, o2) -> {
    int result = o1.getLastModified().compareTo(o2.getLastModified());
    if(result != 0) {
      //same modified time. Use name to sort
      return result;
    }
    return o1.getKey().compareTo(o2.getKey());
  });

  S3Objects s3ObjectSummaries = S3Objects
    .withPrefix(s3Client, s3ConfigBean.s3Config.bucket, s3ConfigBean.s3Config.commonPrefix);

  // SDC-9413: since the s3ObjectSummaries is in lexical order, we should get all list of files in one api call
  for (S3ObjectSummary s : s3ObjectSummaries) {
    String fullPrefix = s.getKey();
    String remainingPrefix = fullPrefix.substring(s3ConfigBean.s3Config.commonPrefix.length(), fullPrefix.length());
    if (!remainingPrefix.isEmpty()) {
      // remainingPrefix can be empty.
      // If the user manually creates a prefix "myFolder/mySubFolder" in bucket "myBucket" and uploads "myObject",
      // then the first objects returned here are:
      // myFolder/mySubFolder
      // myFolder/mySubFolder/myObject
      //
      // All is good when pipeline is run but preview returns with no data. So we should ignore the empty file as it
      // has no data
      if (pathMatcher.match(s3ConfigBean.s3FileConfig.prefixPattern, remainingPrefix) && isEligible(s, s3Offset)) {
        treeSet.add(s);
      }
      if (treeSet.size() > fetchSize) {
        treeSet.pollLast();
      }
    }
  }

  return new ArrayList<>(treeSet);
}
 
Example 13
Source File: GeometryHullTool.java    From geowave with Apache License 2.0 4 votes vote down vote up
/**
 * Gift unwrapping (e.g. dig) concept, taking a convex hull and a set of inner points, add inner
 * points to the hull without violating hull invariants--all points must reside on the hull or
 * inside the hull. Based on: Jin-Seo Park and Se-Jong Oh. "A New Concave Algorithm and
 * Concaveness Measure for n-dimensional Datasets" . Department of Nanobiomedical Science. Dankook
 * University". 2010.
 *
 * <p> Per the paper, N = concaveThreshold
 */
public Geometry concaveHullParkOhMethod(
    final Geometry geometry,
    final Collection<Coordinate> providedInnerPoints) {

  final Set<Coordinate> innerPoints = new HashSet<>(providedInnerPoints);
  final TreeSet<Edge> edges = new TreeSet<>();
  final Coordinate[] geoCoordinateList = geometry.getCoordinates();
  final int s = geoCoordinateList.length - 1;
  final Edge firstEdge =
      createEdgeWithSideEffects(geoCoordinateList[0], geoCoordinateList[1], innerPoints, edges);
  Edge lastEdge = firstEdge;
  for (int i = 1; i < s; i++) {
    final Edge newEdge =
        createEdgeWithSideEffects(
            geoCoordinateList[i],
            geoCoordinateList[i + 1],
            innerPoints,
            edges);
    newEdge.connectLast(lastEdge);
    lastEdge = newEdge;
  }
  firstEdge.connectLast(lastEdge);
  while (!edges.isEmpty() && !innerPoints.isEmpty()) {
    final Edge edge = edges.pollLast();
    lastEdge = edge;
    double score = Double.MAX_VALUE;
    Coordinate selectedCandidate = null;
    for (final Coordinate candidate : innerPoints) {
      final double dist = calcDistance(edge.start, edge.end, candidate);
      // on the hull
      if (MathUtils.equals(dist, 0.0, 0.000000001)) {
        score = 0.0;
        selectedCandidate = candidate;
        break;
      }
      if ((dist > 0) && (dist < score)) {
        score = dist;
        selectedCandidate = candidate;
      }
    }
    if (selectedCandidate == null) {
      continue;
    }
    // if one a line segment of the hull, then remove candidate
    if (FloatCompareUtils.checkDoublesEqual(score, 0.0)) {
      innerPoints.remove(selectedCandidate);
      edges.add(edge);
      continue;
    }
    // Park and Oh look only at the neighbor edges
    // but this fails in some cases.
    if (isCandidateCloserToAnotherEdge(score, edge, edges, selectedCandidate)) {
      continue;
    }

    innerPoints.remove(selectedCandidate);
    final double eh = edge.distance;
    final double startToCandidate =
        distanceFnForCoordinate.measure(edge.start, selectedCandidate);
    final double endToCandidate = distanceFnForCoordinate.measure(edge.end, selectedCandidate);
    final double min = Math.min(startToCandidate, endToCandidate);
    // protected against duplicates
    if ((eh / min) > concaveThreshold) {
      final Edge newEdge1 = new Edge(edge.start, selectedCandidate, startToCandidate);
      final Edge newEdge2 = new Edge(selectedCandidate, edge.end, endToCandidate);
      // need to replace this with something more intelligent. This
      // occurs in cases of sharp angles. An angular approach may also
      // work
      // look for an angle to flip in the reverse direction.
      if (!intersectAnotherEdge(newEdge1, edge)
          && !intersectAnotherEdge(newEdge2, edge)
          && !intersectAnotherEdge(newEdge1, edge.last)
          && !intersectAnotherEdge(newEdge2, edge.next)) {
        edges.add(newEdge2);
        edges.add(newEdge1);
        newEdge1.connectLast(edge.last);
        newEdge2.connectLast(newEdge1);
        edge.next.connectLast(newEdge2);
        lastEdge = newEdge1;
      }
    }
  }
  return geometry.getFactory().createPolygon(reassemble(lastEdge));
}
 
Example 14
Source File: WriteOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<JSONObject> readStatusUpdates(final Node poster,
		final Node user, final int numItems, boolean ownUpdates) {
	if (!poster.equals(user)) {
		ownUpdates = true;
	}

	final List<JSONObject> statusUpdates = new LinkedList<JSONObject>();

	// check if ego network stream is being accessed
	if (!ownUpdates) {
		final TreeSet<StatusUpdateUser> users = new TreeSet<StatusUpdateUser>(
				new StatusUpdateUserComparator());

		// loop through users followed
		Node userNode;
		StatusUpdateUser crrUser;
		for (Relationship relationship : poster.getRelationships(
				SocialGraphRelationshipType.FOLLOW, Direction.OUTGOING)) {
			userNode = relationship.getEndNode();

			// add users having status updates
			crrUser = new StatusUpdateUser(userNode);
			if (crrUser.hasStatusUpdate()) {
				users.add(crrUser);
			}
		}

		// handle user queue
		while ((statusUpdates.size() < numItems) && !users.isEmpty()) {
			crrUser = users.pollLast();

			// add last recent status update of current user
			statusUpdates.add(crrUser.getStatusUpdate());

			// re-add current user if more status updates available
			if (crrUser.hasStatusUpdate()) {
				users.add(crrUser);
			}
		}
	} else {
		// access single stream only
		final StatusUpdateUser posterNode = new StatusUpdateUser(poster);
		while ((statusUpdates.size() < numItems)
				&& posterNode.hasStatusUpdate()) {
			statusUpdates.add(posterNode.getStatusUpdate());
		}
	}

	return statusUpdates;
}
 
Example 15
Source File: ReadOptimizedGraphity.java    From metalcon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<JSONObject> readStatusUpdates(final Node poster,
		final Node user, final int numItems, boolean ownUpdates) {
	if (!poster.equals(user)) {
		ownUpdates = true;
	}

	final List<JSONObject> statusUpdates = new LinkedList<JSONObject>();

	// check if ego network stream is being accessed
	if (!ownUpdates) {
		final TreeSet<GraphityUser> users = new TreeSet<GraphityUser>(
				new StatusUpdateUserComparator());

		// load first user by the replica
		Node replicaAdded = NeoUtils.getNextSingleNode(poster,
				SocialGraphRelationshipType.GRAPHITY);
		Node userAdded;
		GraphityUser crrUser, lastUser = null;
		if (replicaAdded != null) {
			userAdded = NeoUtils.getNextSingleNode(replicaAdded,
					SocialGraphRelationshipType.REPLICA);
			crrUser = new GraphityUser(userAdded, replicaAdded);
			if (crrUser.hasStatusUpdate()) {
				lastUser = crrUser;
				users.add(crrUser);
			}
		}

		// handle user queue
		while ((statusUpdates.size() < numItems) && !users.isEmpty()) {
			crrUser = users.pollLast();

			// add last recent status update of current user
			statusUpdates.add(crrUser.getStatusUpdate());

			// re-add current user if more status updates available
			if (crrUser.hasStatusUpdate()) {
				users.add(crrUser);
			}

			// load additional user if necessary
			if (crrUser == lastUser) {
				replicaAdded = NeoUtils.getNextSingleNode(
						lastUser.getUserReplica(),
						SocialGraphRelationshipType.GRAPHITY);
				if (replicaAdded != null) {
					userAdded = NeoUtils.getNextSingleNode(replicaAdded,
							SocialGraphRelationshipType.REPLICA);
					lastUser = new GraphityUser(userAdded, replicaAdded);

					// add new user if updates available only
					if (lastUser.hasStatusUpdate()) {
						users.add(lastUser);
						continue;
					}
				}

				// further users do not need to be loaded
				lastUser = null;
			}
		}

	} else {
		// access single stream only
		final GraphityUser posterUser = new GraphityUser(poster, null);
		while ((statusUpdates.size() < numItems)
				&& posterUser.hasStatusUpdate()) {
			statusUpdates.add(posterUser.getStatusUpdate());
		}
	}

	return statusUpdates;
}