Java Code Examples for java.util.SortedMap#tailMap()

The following examples show how to use java.util.SortedMap#tailMap() . 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: NGramCorrelationForm.java    From Ngram-Graphs with Apache License 2.0 6 votes vote down vote up
private double determineDistanceDeviation() {
    double dRes = 0.0;
    SortedMap smDelims = identifyCandidateDelimiters(sgOverallGraph.getDataString(), 1);
    int iImportant = determineImportantDelimiters(smDelims);
    Iterator iIter = smDelims.keySet().iterator();
    int iCnt = 0;
    while (iIter.hasNext() && (iCnt++ < smDelims.size() - iImportant))
        iIter.next();
    
    smDelims = smDelims.tailMap(iIter.next());
    if (!smDelims.containsValue(StreamTokenizer.TT_EOF)) {
        smDelims.put((Double)smDelims.lastKey() + 0.1, new StringBuffer().append((char)StreamTokenizer.TT_EOF).toString()); // Add EOF char
    }
    
    String[] saChunks = splitStringByDelimiterPoints(sgOverallGraph.getDataString(), 
            splitPointsByDelimiterList(sgOverallGraph.getDataString(), smDelims));
    Distribution dSizes = new Distribution();
    for (iCnt=0;iCnt < saChunks.length; iCnt++) {
        dSizes.setValue((double)saChunks[iCnt].length(), 
                dSizes.getValue((double)saChunks[iCnt].length()) + 1);
    }
    dRes = dSizes.average(false) + dSizes.standardDeviation(false);
    
    return dRes;
}
 
Example 2
Source File: NGramCorrelationForm.java    From Ngram-Graphs with Apache License 2.0 6 votes vote down vote up
private double determineDistanceDeviation() {
    double dRes = 0.0;
    SortedMap smDelims = identifyCandidateDelimiters(sgOverallGraph.getDataString(), 1);
    int iImportant = determineImportantDelimiters(smDelims);
    Iterator iIter = smDelims.keySet().iterator();
    int iCnt = 0;
    while (iIter.hasNext() && (iCnt++ < smDelims.size() - iImportant))
        iIter.next();
    
    smDelims = smDelims.tailMap(iIter.next());
    if (!smDelims.containsValue(StreamTokenizer.TT_EOF)) {
        smDelims.put((Double)smDelims.lastKey() + 0.1, new StringBuffer().append((char)StreamTokenizer.TT_EOF).toString()); // Add EOF char
    }
    
    String[] saChunks = splitStringByDelimiterPoints(sgOverallGraph.getDataString(), 
            splitPointsByDelimiterList(sgOverallGraph.getDataString(), smDelims));
    Distribution dSizes = new Distribution();
    for (iCnt=0;iCnt < saChunks.length; iCnt++) {
        dSizes.setValue((double)saChunks[iCnt].length(), 
                dSizes.getValue((double)saChunks[iCnt].length()) + 1);
    }
    dRes = dSizes.average(false) + dSizes.standardDeviation(false);
    
    return dRes;
}
 
Example 3
Source File: SourceURLHashLB.java    From Distributed-KV with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) {
	SortedMap<Integer, Invoker<?>> virtualNodes = maps.get();
	// 对服务列表进行遍历,物理节点扩散成虚拟节点加入哈希环
	invokers.forEach(invoker -> {
		URL remoteUrl = invoker.getUrl();
		// 创建virtualNodeSize个虚拟节点,均匀散布在大小为HASH_SIZE的hash环上
		IntStream.range(0, VIRTUAL_NODES_PER_NODE).forEach(i -> {
			String ip = remoteUrl.connectString();
			// 通过键和索引值获取哈希值,性能好的hash函数能保证映射后分布均匀
			int hashCode = (int) hash(ip, String.valueOf(i).intern());
			// 将获取的哈希值和物理节点放入虚拟节点map中
			virtualNodes.put(hashCode, invoker);
		});
	});
	// 本地url映射到哈希环上
	String localIp = url.connectString();
	int location = (int) hash(localIp);
	Invoker<?> node = null;
	if (location > virtualNodes.lastKey()) {
		//location超过范围则取第一个节点
		node = virtualNodes.get(virtualNodes.firstKey());
	} else {
		//获取子map, 顺时针最近节点是子map的第一个节点
		SortedMap<Integer, Invoker<?>> subMap = virtualNodes.tailMap(location);
		node = subMap.get(subMap.firstKey());
	}
	Preconditions.checkNotNull(node);
	return (Invoker<T>) node;
}
 
Example 4
Source File: TreeBasedTable.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
SortedMap<C, V> computeBackingRowMap() {
  SortedMap<C, V> map = wholeRow();
  if (map != null) {
    if (lowerBound != null) {
      map = map.tailMap(lowerBound);
    }
    if (upperBound != null) {
      map = map.headMap(upperBound);
    }
    return map;
  }
  return null;
}
 
Example 5
Source File: QCompilerTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public void testLowestString() {
  SortedMap map = new TreeMap();
  map.put("ab", "value");
  map.put("z", "value");
  map.put("v", "value");
  SortedMap returnMap = map.tailMap(CompiledLike.LOWEST_STRING);
  assertEquals(3, returnMap.size());
}
 
Example 6
Source File: Vertex.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public Vertex getNextVertexOnDest() {
  SortedMap<Long, Vertex> map = graph.getIndexedVertices().get(name);
  SortedMap<Long, Vertex> tailMap = map.tailMap(timestamp + 1);
  if(tailMap.isEmpty()) {
    return null;
  } else {
    return tailMap.get(tailMap.firstKey());
  }
}
 
Example 7
Source File: TreeBasedTable.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
SortedMap<C, V> computeBackingRowMap() {
  SortedMap<C, V> map = wholeRow();
  if (map != null) {
    if (lowerBound != null) {
      map = map.tailMap(lowerBound);
    }
    if (upperBound != null) {
      map = map.headMap(upperBound);
    }
    return map;
  }
  return null;
}
 
Example 8
Source File: QuetzalSummary.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
static Set<String> lookupIds(String p, String prefix, SortedMap<KeyTuple, ValueTuple> sm) {
	KeyTuple k = new KeyTuple(p, prefix);
	SortedMap<KeyTuple, ValueTuple> t = sm.tailMap(k);
	if (!t.isEmpty()) {
		Map.Entry<KeyTuple, ValueTuple> e = t.entrySet().iterator().next();
		if (e.getKey().prefix.startsWith(prefix)) {
			return e.getValue().urls;
		}
	}
	return new HashSet<String>(); // empty map
}
 
Example 9
Source File: CostFedSummary.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
static Set<String> lookupIds(String p, String prefix, SortedMap<KeyTuple, ValueTuple> sm) {
	HashSet<String> result = new HashSet<String>();
	
	KeyTuple k = new KeyTuple(p, prefix);
	SortedMap<KeyTuple, ValueTuple> lessmap = sm.tailMap(k);
	for (Map.Entry<KeyTuple, ValueTuple> e : lessmap.entrySet())
	{
		if (!prefix.startsWith(e.getKey().prefix)) break;
		result.addAll(e.getValue().urls);
	}
	return result;
}
 
Example 10
Source File: CostFedSummary.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
static SortedMap<String, FreqValue> getCut(SortedMap<String, FreqValue> coll, String[] arr, String pattern) {
	int startpos = Arrays.binarySearch(arr, pattern);
	if (startpos == -1 || (startpos < 0 && !pattern.startsWith(arr[-startpos - 2]))) return null;
	if (startpos < 0) {
		startpos = -startpos - 2;
	}
	
	int endpos = Arrays.binarySearch(arr, startpos, arr.length, pattern + Character.MAX_VALUE);
	assert(endpos < 0);
	endpos = -endpos - 1;
	return endpos == arr.length ?
			coll.tailMap(arr[startpos]) :
				coll.subMap(arr[startpos], arr[endpos]);
}
 
Example 11
Source File: ObtainedSubMap.java    From learntosolveit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {
    SortedMap<Time, String> datebook = new TreeMap<Time, String>();
    datebook.put(new Time(12, 30), "Lunch");
    datebook.put(new Time(15, 30), "Afternoon coffee break");
    datebook.put(new Time(9, 0), "Lecture");
    datebook.put(new Time(13, 15), "Board Meeting");
    SortedMap<Time, String> pm = datebook.tailMap(new Time(12, 0));

    for (Map.Entry<Time, String> entry: pm.entrySet())
        System.out.println(entry.getKey() + " " + entry.getValue());
}
 
Example 12
Source File: AnnotationViewDataImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static int findNextUsedLine(int from, SortedMap<Integer, List<Mark>> marks) {
    SortedMap<Integer, List<Mark>> next = marks.tailMap(from + 1);
    
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("AnnotationView.findNextUsedLine from: " + from + "; marks: " + marks + "; next: " + next); //NOI18N
    }
    
    if (next.isEmpty()) {
        return Integer.MAX_VALUE;
    }
    
    return next.firstKey().intValue();
}
 
Example 13
Source File: ConsistentHashingRouter.java    From mantis with Apache License 2.0 5 votes vote down vote up
private AsyncConnection<KeyValuePair<K, V>> lookupConnection(long hash, SortedMap<Long, AsyncConnection<KeyValuePair<K, V>>> ring) {
    if (!ring.containsKey(hash)) {
        SortedMap<Long, AsyncConnection<KeyValuePair<K, V>>> tailMap = ring.tailMap(hash);
        hash = tailMap.isEmpty() ? ring.firstKey() : tailMap.firstKey();
    }
    return ring.get(hash);
}
 
Example 14
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testDescendingTailMapContents() {
    NavigableMap map = dmap5();
    SortedMap sm = map.tailMap(m2);
    assertFalse(sm.containsKey(m1));
    assertTrue(sm.containsKey(m2));
    assertTrue(sm.containsKey(m3));
    assertTrue(sm.containsKey(m4));
    assertTrue(sm.containsKey(m5));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    k = (Integer)(i.next());
    assertEquals(m4, k);
    k = (Integer)(i.next());
    assertEquals(m5, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(m2, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m3, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m4, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m5, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(m4);
    assertEquals(m4, ssm.firstKey());
    assertEquals(m5, ssm.lastKey());
    assertEquals("D", ssm.remove(m4));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}
 
Example 15
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testTailMapContents() {
    NavigableMap map = map5();
    SortedMap sm = map.tailMap(two);
    assertFalse(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertTrue(sm.containsKey(four));
    assertTrue(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    k = (Integer)(i.next());
    assertEquals(four, k);
    k = (Integer)(i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(two, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(three, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(four, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(four);
    assertEquals(four, ssm.firstKey());
    assertEquals(five, ssm.lastKey());
    assertEquals("D", ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}
 
Example 16
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testTailMapContents() {
    NavigableMap map = map5();
    SortedMap sm = map.tailMap(two);
    assertFalse(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertTrue(sm.containsKey(four));
    assertTrue(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    k = (Integer)(i.next());
    assertEquals(four, k);
    k = (Integer)(i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(two, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(three, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(four, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(four);
    assertEquals(four, ssm.firstKey());
    assertEquals(five, ssm.lastKey());
    assertEquals("D", ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}
 
Example 17
Source File: ConcurrentSkipListSubMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testDescendingTailMapContents() {
    ConcurrentNavigableMap map = dmap5();
    SortedMap sm = map.tailMap(m2);
    assertFalse(sm.containsKey(m1));
    assertTrue(sm.containsKey(m2));
    assertTrue(sm.containsKey(m3));
    assertTrue(sm.containsKey(m4));
    assertTrue(sm.containsKey(m5));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    k = (Integer)(i.next());
    assertEquals(m4, k);
    k = (Integer)(i.next());
    assertEquals(m5, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(m2, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m3, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m4, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m5, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(m4);
    assertEquals(m4, ssm.firstKey());
    assertEquals(m5, ssm.lastKey());
    assertEquals("D", ssm.remove(m4));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}
 
Example 18
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testDescendingTailMapContents() {
    NavigableMap map = dmap5();
    SortedMap sm = map.tailMap(m2);
    assertFalse(sm.containsKey(m1));
    assertTrue(sm.containsKey(m2));
    assertTrue(sm.containsKey(m3));
    assertTrue(sm.containsKey(m4));
    assertTrue(sm.containsKey(m5));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    k = (Integer)(i.next());
    assertEquals(m4, k);
    k = (Integer)(i.next());
    assertEquals(m5, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(m2, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m3, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m4, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(m5, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(m4);
    assertEquals(m4, ssm.firstKey());
    assertEquals(m5, ssm.lastKey());
    assertEquals("D", ssm.remove(m4));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}
 
Example 19
Source File: AnnotationHolder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public synchronized List<ErrorDescription> getErrorsGE(int offset) {
    try {
        int index = findPositionGE(Utilities.getRowStart(doc, offset));
        if (index < 0) return Collections.emptyList();

        while (index < knownPositions.size()) {
            Position current = knownPositions.get(index++).get();

            if (current == null) {
                continue;
            }

            List<ErrorDescription> errors = line2Errors.get(current);

            if (errors != null) {
                SortedMap<Integer, List<ErrorDescription>> sortedErrors = new TreeMap<Integer, List<ErrorDescription>>();

                for (ErrorDescription ed : errors) {
                    List<ErrorDescription> errs = sortedErrors.get(ed.getRange().getBegin().getOffset());

                    if (errs == null) {
                        sortedErrors.put(ed.getRange().getBegin().getOffset(), errs = new LinkedList<ErrorDescription>());
                    }

                    errs.add(ed);
                }

                SortedMap<Integer, List<ErrorDescription>> tail = sortedErrors.tailMap(offset);

                if (!tail.isEmpty()) {
                    Integer k = tail.firstKey();

                    return new ArrayList<ErrorDescription>(sortedErrors.get(k));
                }
            }
        }

        return Collections.emptyList();
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
        return Collections.emptyList();
    }
}
 
Example 20
Source File: ConcurrentSkipListSubMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testTailMapContents() {
    ConcurrentNavigableMap map = map5();
    SortedMap sm = map.tailMap(two);
    assertFalse(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertTrue(sm.containsKey(four));
    assertTrue(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    k = (Integer)(i.next());
    assertEquals(four, k);
    k = (Integer)(i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(two, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(three, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(four, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    SortedMap ssm = sm.tailMap(four);
    assertEquals(four, ssm.firstKey());
    assertEquals(five, ssm.lastKey());
    assertEquals("D", ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}