Java Code Examples for java.util.NavigableSet#subSet()

The following examples show how to use java.util.NavigableSet#subSet() . 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: ConcurrentSkipListSubSetTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testDescendingSubSetContents2() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m3);
    assertEquals(1, sm.size());
    assertEquals(m2, sm.first());
    assertEquals(m2, sm.last());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertFalse(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(0, sm.size());
    assertTrue(sm.isEmpty());
    assertFalse(sm.remove(m3));
    assertEquals(4, set.size());
}
 
Example 2
Source File: BTreeSet2Test.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public void testSubSetContents2() {
    NavigableSet set = set5();
    SortedSet sm = set.subSet(two, three);
    assertEquals(1, sm.size());
    assertEquals(two, sm.first());
    assertEquals(two, sm.last());
    assertFalse(sm.contains(one));
    assertTrue(sm.contains(two));
    assertFalse(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(two));
    assertEquals(4, set.size());
    assertEquals(0, sm.size());
    assertTrue(sm.isEmpty());
    assertFalse(sm.remove(three));
    assertEquals(4, set.size());
}
 
Example 3
Source File: EmptyNavigableSet.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSetRanges(String description, NavigableSet navigableSet) {
    Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableSet subSet = navigableSet.subSet(first, true, last, true);

    // same subset
    subSet.subSet(first, true, last, true);

    // slightly smaller
    NavigableSet ns = subSet.subSet(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subSet(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.subSet(first, false, BigInteger.ONE, false);
}
 
Example 4
Source File: EmptyNavigableSet.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSetRanges(String description, NavigableSet navigableSet) {
    Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableSet subSet = navigableSet.subSet(first, true, last, true);

    // same subset
    subSet.subSet(first, true, last, true);

    // slightly smaller
    NavigableSet ns = subSet.subSet(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subSet(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.subSet(first, false, BigInteger.ONE, false);
}
 
Example 5
Source File: StoreTest.java    From TarsosLSH with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testStore(){
	DB db = DBMaker.memoryDB().make();
	NavigableSet<long[]>  treeSet = db.treeSet("tree", Serializer.LONG_ARRAY ).createOrOpen();
	long[] e = {7,78,78};
	long[] f = {7,12,78};
	long[] g = {9,78,78};
	long[] h = {6,78,78};
	long[] i = {7,1,78};
	
	treeSet.add(e);
	treeSet.add(f);
	treeSet.add(g);
	treeSet.add(h);
	treeSet.add(i);
	
	long[] k = {7,0,0};
	long[] l = {7,Integer.MAX_VALUE,Integer.MAX_VALUE};
	
	assertEquals(3,treeSet.subSet(k, l).size());
	
	for(long[] element : treeSet.subSet(k, l)){
		System.out.println(element[1]);
	}
}
 
Example 6
Source File: EmptyNavigableSet.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSetRanges(String description, NavigableSet navigableSet) {
    Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableSet subSet = navigableSet.subSet(first, true, last, true);

    // same subset
    subSet.subSet(first, true, last, true);

    // slightly smaller
    NavigableSet ns = subSet.subSet(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subSet(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.subSet(first, false, BigInteger.ONE, false);
}
 
Example 7
Source File: EmptyNavigableSet.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSetRanges(String description, NavigableSet navigableSet) {
    Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableSet subSet = navigableSet.subSet(first, true, last, true);

    // same subset
    subSet.subSet(first, true, last, true);

    // slightly smaller
    NavigableSet ns = subSet.subSet(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subSet(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.subSet(first, false, BigInteger.ONE, false);
}
 
Example 8
Source File: TreeSubSetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testSubSetContents2() {
    NavigableSet set = set5();
    SortedSet sm = set.subSet(two, three);
    assertEquals(1, sm.size());
    assertEquals(two, sm.first());
    assertEquals(two, sm.last());
    assertFalse(sm.contains(one));
    assertTrue(sm.contains(two));
    assertFalse(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(two));
    assertEquals(4, set.size());
    assertEquals(0, sm.size());
    assertTrue(sm.isEmpty());
    assertFalse(sm.remove(three));
    assertEquals(4, set.size());
}
 
Example 9
Source File: EmptyNavigableSet.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSetRanges(String description, NavigableSet navigableSet) {
    Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableSet subSet = navigableSet.subSet(first, true, last, true);

    // same subset
    subSet.subSet(first, true, last, true);

    // slightly smaller
    NavigableSet ns = subSet.subSet(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subSet(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.subSet(first, false, BigInteger.ONE, false);
}
 
Example 10
Source File: ConcurrentSkipListSubSetJUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public void testDescendingSubSetContents2() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m3);
    assertEquals(1, sm.size());
    assertEquals(m2, sm.first());
    assertEquals(m2, sm.last());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertFalse(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(0, sm.size());
    assertTrue(sm.isEmpty());
    assertFalse(sm.remove(m3));
    assertEquals(4, set.size());
}
 
Example 11
Source File: EmptyNavigableSet.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSetRanges(String description, NavigableSet navigableSet) {
    Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableSet subSet = navigableSet.subSet(first, true, last, true);

    // same subset
    subSet.subSet(first, true, last, true);

    // slightly smaller
    NavigableSet ns = subSet.subSet(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subSet(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.subSet(first, false, BigInteger.ONE, false);
}
 
Example 12
Source File: EmptyNavigableSet.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testSubSetRanges(String description, NavigableSet navigableSet) {
    Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableSet subSet = navigableSet.subSet(first, true, last, true);

    // same subset
    subSet.subSet(first, true, last, true);

    // slightly smaller
    NavigableSet ns = subSet.subSet(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subSet(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subSet.subSet(first, false, BigInteger.ONE, false);
}
 
Example 13
Source File: ConcurrentSkipListSubSetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testDescendingSubSetContents2() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m3);
    assertEquals(1, sm.size());
    assertEquals(m2, sm.first());
    assertEquals(m2, sm.last());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertFalse(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(0, sm.size());
    assertTrue(sm.isEmpty());
    assertFalse(sm.remove(m3));
    assertEquals(4, set.size());
}
 
Example 14
Source File: TreeSubSetTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * subSet returns set with keys in requested range
 */
public void testDescendingSubSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m4);
    assertEquals(m2, sm.first());
    assertEquals(m3, sm.last());
    assertEquals(2, sm.size());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(1, sm.size());
    assertEquals(m3, sm.first());
    assertEquals(m3, sm.last());
    assertTrue(sm.remove(m3));
    assertTrue(sm.isEmpty());
    assertEquals(3, set.size());
}
 
Example 15
Source File: BTreeSet2Test.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * subSet returns set with keys in requested range
 */
public void testSubSetContents() {
    NavigableSet set = set5();
    SortedSet sm = set.subSet(two, four);
    assertEquals(two, sm.first());
    assertEquals(three, sm.last());
    assertEquals(2, sm.size());
    assertFalse(sm.contains(one));
    assertTrue(sm.contains(two));
    assertTrue(sm.contains(three));
    assertFalse(sm.contains(four));
    assertFalse(sm.contains(five));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(two));
    assertEquals(4, set.size());
    assertEquals(1, sm.size());
    assertEquals(three, sm.first());
    assertEquals(three, sm.last());
    assertTrue(sm.remove(three));
    assertTrue(sm.isEmpty());
    assertEquals(3, set.size());
}
 
Example 16
Source File: ConcurrentSkipListSubSetTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * subSet returns set with keys in requested range
 */
public void testDescendingSubSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m4);
    assertEquals(m2, sm.first());
    assertEquals(m3, sm.last());
    assertEquals(2, sm.size());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(1, sm.size());
    assertEquals(m3, sm.first());
    assertEquals(m3, sm.last());
    assertTrue(sm.remove(m3));
    assertTrue(sm.isEmpty());
    assertEquals(3, set.size());
}
 
Example 17
Source File: SortedSetRangeByScoreCommand.java    From claudb with MIT License 5 votes vote down vote up
@Override
public RedisToken execute(Database db, Request request) {
  try {
    DatabaseValue value = db.getOrDefault(safeKey(request.getParam(0)), DatabaseValue.EMPTY_ZSET);
    NavigableSet<Entry<Double, SafeString>> set = value.getSortedSet();

    float from = parseRange(request.getParam(1).toString());
    float to = parseRange(request.getParam(2).toString());

    Options options = parseOptions(request);

    Set<Entry<Double, SafeString>> range = set.subSet(
        score(from, SafeString.EMPTY_STRING), inclusive(request.getParam(1)),
        score(to, SafeString.EMPTY_STRING), inclusive(request.getParam(2)));

    List<Object> result = emptyList();
    if (from <= to) {
      if (options.withScores) {
        result = range.stream().flatMap(
            entry -> Stream.of(entry.getValue(), entry.getKey())).collect(toList());
      } else {
        result = range.stream().map(Entry::getValue).collect(toList());
      }

      if (options.withLimit) {
        result = result.stream().skip(options.offset).limit(options.count).collect(toList());
      }
    }

    return convert(result);
  } catch (NumberFormatException e) {
    return error("ERR value is not an float or out of range");
  }
}
 
Example 18
Source File: ConcurrentSkipListSubSetJUnitTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * subSet returns set with keys in requested range
 */
public void testDescendingSubSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m4);
    assertEquals(m2, sm.first());
    assertEquals(m3, sm.last());
    assertEquals(2, sm.size());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(1, sm.size());
    assertEquals(m3, sm.first());
    assertEquals(m3, sm.last());
    assertTrue(sm.remove(m3));
    assertTrue(sm.isEmpty());
    assertEquals(3, set.size());
}
 
Example 19
Source File: ConcurrentSkipListSubSetTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * subSet returns set with keys in requested range
 */
public void testDescendingSubSetContents() {
    NavigableSet set = dset5();
    SortedSet sm = set.subSet(m2, m4);
    assertEquals(m2, sm.first());
    assertEquals(m3, sm.last());
    assertEquals(2, sm.size());
    assertFalse(sm.contains(m1));
    assertTrue(sm.contains(m2));
    assertTrue(sm.contains(m3));
    assertFalse(sm.contains(m4));
    assertFalse(sm.contains(m5));
    Iterator i = sm.iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    Iterator j = sm.iterator();
    j.next();
    j.remove();
    assertFalse(set.contains(m2));
    assertEquals(4, set.size());
    assertEquals(1, sm.size());
    assertEquals(m3, sm.first());
    assertEquals(m3, sm.last());
    assertTrue(sm.remove(m3));
    assertTrue(sm.isEmpty());
    assertEquals(3, set.size());
}
 
Example 20
Source File: ContentOrderedEvaluator.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean evaluate(List<List<TermWeightPosition>> offsets) {
    if (offsets.isEmpty() || offsets.size() < terms.length) {
        return false;
    }
    
    NavigableSet<EvaluateTermPosition> termPositions = new TreeSet<>();
    int direction = FORWARD;
    
    // This is a little hokey, but because when the travers fails it falls back to looking for the first node
    // A short "first" or "last" list should make it fast.
    if (offsets.get(0).size() > offsets.get(offsets.size() - 1).size()) {
        direction = REVERSE;
    }
    
    int[] maxSkips = new int[terms.length];
    for (int i = 0; i < terms.length; i++) {
        int maxSkip = 0;
        for (TermWeightPosition twp : offsets.get(i)) {
            if (twp.getPrevSkips() > maxSkip) {
                maxSkip = twp.getPrevSkips();
            }
            
            // Skip terms greater then the max score if it score is set
            if (twp.getScore() > maxScore) {
                if (log.isTraceEnabled()) {
                    log.trace("[" + terms[i] + "] Skip score => " + twp);
                }
                continue;
            }
            
            EvaluateTermPosition etp = new EvaluateTermPosition(terms[i], i, twp);
            termPositions.add(etp);
        }
        maxSkips[i] = maxSkip;
    }
    
    // Low/High now needs to consider the distance of the skips
    EvaluateTermPosition[] lowHigh = getLowHigh(offsets, maxSkips);
    if (null == lowHigh) {
        return false;
    }
    termPositions = termPositions.subSet(lowHigh[0], true, lowHigh[1], true);
    
    if (log.isTraceEnabled()) {
        log.trace("Term Positions: " + termPositions);
    }
    
    // Number of matched terms
    termPositions = (direction == FORWARD) ? termPositions : termPositions.descendingSet();
    List<EvaluateTermPosition> found = traverse(termPositions, direction);
    
    if (null != found && found.size() == terms.length) {
        // to return offset => result.add(found.get(0).termWeightPosition.getOffset());
        return true;
    }
    
    // fail
    return false;
}