/** * {@inheritDoc} * * @see java.util.SortedSet#subSet(java.lang.Object, java.lang.Object) */ public SortedSet<E> subSet(final E fromElement, final E toElement) { lockRead(); try { return m_set.subSet(fromElement, toElement); } finally { unlockRead(); } }
/** * Splits this range in two new (smaller) ranges. * * @return a pair of ranges. */ public Pair<Range<K>, Range<K>> split() { // not sure about this... if (!smallEnough()) { Iterator<K> it = set.iterator(); K l = it.next(); K h = it.next(); return new Pair<>(new KeyRange<>(set.subSet(l, h)), new KeyRange<>(set.tailSet(h))); } return new Pair<>(this, this); }
/** @tests java.util.TreeSet#subSet(java.lang.Object, java.lang.Object) */ public void test_subSetLjava_lang_ObjectLjava_lang_Object() { // Test for method java.util.SortedSet // java.util.TreeSet.subSet(java.lang.Object, java.lang.Object) final int startPos = objArray.length / 4; final int endPos = 3 * objArray.length / 4; SortedSet aSubSet = ts.subSet(objArray[startPos], objArray[endPos]); assertTrue("Subset has wrong number of elements", aSubSet.size() == (endPos - startPos)); for (int counter = startPos; counter < endPos; counter++) assertTrue( "Subset does not contain all the elements it should", aSubSet.contains(objArray[counter])); int result; try { ts.subSet(objArray[3], objArray[0]); result = 0; } catch (IllegalArgumentException e) { result = 1; } assertEquals("end less than start should throw", 1, result); }
private SortedSet<E> getSubSet(final SortedSet<E> set) { final E[] elements = AbstractSortedSetTest.this.getFullElements(); switch (m_Type) { case TYPE_SUBSET: return set.subSet(elements[m_LowBound], elements[m_HighBound]); case TYPE_HEADSET: return set.headSet(elements[m_HighBound]); case TYPE_TAILSET: return set.tailSet(elements[m_LowBound]); default: return null; } }
// The marker has the last time stamp (milli since 1970) and hence is the last transaction for any // days public SortedSet<TransactionElement> headTransactionsTo( Date currentStartDate, Date currentEndDate) { if (currentStartDate == null || currentStartDate.equals(DateFactory.dateAtZero())) { return transactions.headSet( new TransactionElement(null, null, null, currentEndDate, null, null, null)); } else { Calendar currentDateCal = Calendar.getInstance(); currentDateCal.setTime(currentStartDate); currentDateCal.add(Calendar.DAY_OF_YEAR, -1); return transactions.subSet( new TransactionElement(null, null, null, currentDateCal.getTime(), null, null, null), new TransactionElement(null, null, null, currentEndDate, null, null, null)); } }
public void testElementSetSortedSetMethods() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("c", 1); ms.add("a", 3); ms.add("b", 2); SortedSet<String> elementSet = ms.elementSet(); assertEquals("a", elementSet.first()); assertEquals("c", elementSet.last()); assertEquals(Ordering.natural(), elementSet.comparator()); ASSERT.that(elementSet.headSet("b")).has().exactly("a").inOrder(); ASSERT.that(elementSet.tailSet("b")).has().exactly("b", "c").inOrder(); ASSERT.that(elementSet.subSet("a", "c")).has().exactly("a", "b").inOrder(); }
private Set<Long> getTimestampsToSweep( Cell cell, Collection<Long> timestamps /* start timestamps */, @Modified Map<Long, Long> startTsToCommitTs, @Output Set<Cell> sentinelsToAdd, long sweepTimestamp, boolean sweepLastCommitted, SweepStrategy sweepStrategy) { Set<Long> uncommittedTimestamps = Sets.newHashSet(); SortedSet<Long> committedTimestampsToSweep = Sets.newTreeSet(); long maxStartTs = TransactionConstants.FAILED_COMMIT_TS; boolean maxStartTsIsCommitted = false; for (long startTs : timestamps) { long commitTs = ensureCommitTimestampExists(startTs, startTsToCommitTs); if (startTs > maxStartTs && commitTs < sweepTimestamp) { maxStartTs = startTs; maxStartTsIsCommitted = commitTs != TransactionConstants.FAILED_COMMIT_TS; } // Note: there could be an open transaction whose start timestamp is equal to // sweepTimestamp; thus we want to sweep all cells such that: // (1) their commit timestamp is less than sweepTimestamp // (2) their start timestamp is NOT the greatest possible start timestamp // passing condition (1) if (commitTs > 0 && commitTs < sweepTimestamp) { committedTimestampsToSweep.add(startTs); } else if (commitTs == TransactionConstants.FAILED_COMMIT_TS) { uncommittedTimestamps.add(startTs); } } if (committedTimestampsToSweep.isEmpty()) { return uncommittedTimestamps; } if (sweepStrategy == SweepStrategy.CONSERVATIVE && committedTimestampsToSweep.size() > 1) { // We need to add a sentinel if we are removing a committed value sentinelsToAdd.add(cell); } if (sweepLastCommitted && maxStartTsIsCommitted) { return Sets.union(uncommittedTimestamps, committedTimestampsToSweep); } return Sets.union( uncommittedTimestamps, committedTimestampsToSweep.subSet(0L, committedTimestampsToSweep.last())); }
public boolean containsNearbyAlmostDuplicate2(int[] nums, int k, int t) { if (k < 1 || t < 0) return false; SortedSet<Long> set = new TreeSet<Long>(); for (int i = 0; i < nums.length; i++) { long leftBound = (long) nums[i] - t; long rightBound = (long) nums[i] + t + 1; SortedSet<Long> subSet = set.subSet(leftBound, rightBound); if (!subSet.isEmpty()) { return true; } set.add((long) nums[i]); if (i >= k) { set.remove((long) nums[i - k]); } } return false; }
public void testElementSetSubsetClear() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("a", 1); ms.add("b", 3); ms.add("c", 2); ms.add("d", 1); ms.add("e", 3); ms.add("f", 2); SortedSet<String> elementSet = ms.elementSet(); ASSERT.that(elementSet).has().exactly("a", "b", "c", "d", "e", "f").inOrder(); SortedSet<String> subset = elementSet.subSet("b", "f"); ASSERT.that(subset).has().exactly("b", "c", "d", "e").inOrder(); subset.clear(); ASSERT.that(elementSet).has().exactly("a", "f").inOrder(); ASSERT.that(subset).isEmpty(); assertEquals(3, ms.size()); }
public void testElementSetSubsetRetainAll() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("a", 1); ms.add("b", 3); ms.add("c", 2); ms.add("d", 1); ms.add("e", 3); ms.add("f", 2); SortedSet<String> elementSet = ms.elementSet(); ASSERT.that(elementSet).has().exactly("a", "b", "c", "d", "e", "f").inOrder(); SortedSet<String> subset = elementSet.subSet("b", "f"); ASSERT.that(subset).has().exactly("b", "c", "d", "e").inOrder(); assertTrue(subset.retainAll(Arrays.asList("a", "c"))); ASSERT.that(elementSet).has().exactly("a", "c", "f").inOrder(); ASSERT.that(subset).has().exactly("c").inOrder(); assertEquals(5, ms.size()); }
public static void main(String[] args) { SortedSet<String> sortedSet = new TreeSet<String>(); Collections.addAll(sortedSet, "one two three four five six seven eight".split(" ")); print(sortedSet); String low = sortedSet.first(); String high = sortedSet.last(); print(low); print(high); Iterator<String> it = sortedSet.iterator(); for (int i = 0; i <= 6; i++) { if (i == 3) low = it.next(); if (i == 6) high = it.next(); else it.next(); } print(low); print(high); print(sortedSet.subSet(low, high)); print(sortedSet.headSet(high)); print(sortedSet.tailSet(low)); }
private <T> SortedSet<T> getElementsByRange(SortedSet<T> total, int beginIndex, int endIndex) { // Redis command: ZRANGE T beginElement = getElementByIndex(total, beginIndex); T endElement = getElementByIndex(total, endIndex); SortedSet<T> newSet = new ConcurrentSkipListSet<T>(); SortedSet<T> subSet = null; if (null == beginElement && null == endElement) { return null; } else if (null == beginElement) { subSet = total.headSet(endElement); newSet.add(endElement); } else if (null == endElement) { subSet = total.tailSet(beginElement); } else { subSet = total.subSet(beginElement, endElement); newSet.add(endElement); } if (null != subSet) newSet.addAll(subSet); return newSet; }
@Override public SortedSet<E> subSet(E fromElement, E toElement) { return constrainedSortedSet(delegate.subSet(fromElement, toElement), constraint); }
/** * Expert: highlights the top-N passages from multiple fields, for the provided int[] docids, to * custom Object as returned by the {@link PassageFormatter}. Use this API to render to something * other than String. * * @param fieldsIn field names to highlight. Must have a stored string value and also be indexed * with offsets. * @param query query to highlight. * @param searcher searcher that was previously used to execute the query. * @param docidsIn containing the document IDs to highlight. * @param maxPassagesIn The maximum number of top-N ranked passages per-field used to form the * highlighted snippets. * @return Map keyed on field name, containing the array of formatted snippets corresponding to * the documents in <code>docidsIn</code>. If no highlights were found for a document, the * first {@code maxPassages} from the field will be returned. * @throws IOException if an I/O error occurred during processing * @throws IllegalArgumentException if <code>field</code> was indexed without {@link * IndexOptions#DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS} */ protected Map<String, Object[]> highlightFieldsAsObjects( String fieldsIn[], Query query, IndexSearcher searcher, int[] docidsIn, int maxPassagesIn[]) throws IOException { if (fieldsIn.length < 1) { throw new IllegalArgumentException("fieldsIn must not be empty"); } if (fieldsIn.length != maxPassagesIn.length) { throw new IllegalArgumentException("invalid number of maxPassagesIn"); } final IndexReader reader = searcher.getIndexReader(); Query rewritten = rewrite(query); SortedSet<Term> queryTerms = new TreeSet<>(); rewritten.extractTerms(queryTerms); IndexReaderContext readerContext = reader.getContext(); List<AtomicReaderContext> leaves = readerContext.leaves(); // Make our own copies because we sort in-place: int[] docids = new int[docidsIn.length]; System.arraycopy(docidsIn, 0, docids, 0, docidsIn.length); final String fields[] = new String[fieldsIn.length]; System.arraycopy(fieldsIn, 0, fields, 0, fieldsIn.length); final int maxPassages[] = new int[maxPassagesIn.length]; System.arraycopy(maxPassagesIn, 0, maxPassages, 0, maxPassagesIn.length); // sort for sequential io Arrays.sort(docids); new InPlaceMergeSorter() { @Override protected void swap(int i, int j) { String tmp = fields[i]; fields[i] = fields[j]; fields[j] = tmp; int tmp2 = maxPassages[i]; maxPassages[i] = maxPassages[j]; maxPassages[j] = tmp2; } @Override protected int compare(int i, int j) { return fields[i].compareTo(fields[j]); } }.sort(0, fields.length); // pull stored data: String[][] contents = loadFieldValues(searcher, fields, docids, maxLength); Map<String, Object[]> highlights = new HashMap<>(); for (int i = 0; i < fields.length; i++) { String field = fields[i]; int numPassages = maxPassages[i]; Term floor = new Term(field, ""); Term ceiling = new Term(field, UnicodeUtil.BIG_TERM); SortedSet<Term> fieldTerms = queryTerms.subSet(floor, ceiling); // TODO: should we have some reasonable defaults for term pruning? (e.g. stopwords) // Strip off the redundant field: BytesRef terms[] = new BytesRef[fieldTerms.size()]; int termUpto = 0; for (Term term : fieldTerms) { terms[termUpto++] = term.bytes(); } Map<Integer, Object> fieldHighlights = highlightField( field, contents[i], getBreakIterator(field), terms, docids, leaves, numPassages, query); Object[] result = new Object[docids.length]; for (int j = 0; j < docidsIn.length; j++) { result[j] = fieldHighlights.get(docidsIn[j]); } highlights.put(field, result); } return highlights; }
private <T> void verify(SortedSet<T> immutableSet, SortedSet<T> mutableSet, T value1, T value2) { try { immutableSet.add(value1); Assert.fail(); } catch (UnsupportedOperationException e) { } try { immutableSet.addAll(java.util.Collections.singleton(value1)); Assert.fail(); } catch (UnsupportedOperationException e) { } try { immutableSet.clear(); Assert.assertTrue(mutableSet.isEmpty()); } catch (UnsupportedOperationException e) { Assert.assertFalse(mutableSet.isEmpty()); } Assert.assertEquals(immutableSet.contains(value1), mutableSet.contains(value1)); Assert.assertEquals(immutableSet.contains(value2), mutableSet.contains(value2)); Assert.assertEquals( immutableSet.containsAll(java.util.Collections.emptySet()), mutableSet.containsAll(java.util.Collections.emptySet())); Assert.assertEquals( immutableSet.containsAll(java.util.Collections.singleton(0)), mutableSet.containsAll(java.util.Collections.singleton(0))); Assert.assertEquals( immutableSet.containsAll(java.util.Collections.singleton(1)), mutableSet.containsAll(java.util.Collections.singleton(1))); Iterator<T> iterator = immutableSet.iterator(); if (!mutableSet.isEmpty()) { Assert.assertTrue(iterator.hasNext()); Assert.assertEquals(iterator.next(), mutableSet.iterator().next()); } Assert.assertFalse(iterator.hasNext()); try { iterator.next(); Assert.fail(); } catch (NoSuchElementException e) { } Assert.assertEquals(immutableSet.isEmpty(), mutableSet.isEmpty()); try { immutableSet.remove(value1); } catch (UnsupportedOperationException e) { } try { immutableSet.removeAll(java.util.Collections.singleton(value1)); } catch (UnsupportedOperationException e) { } try { immutableSet.retainAll(java.util.Collections.singleton(value1)); } catch (UnsupportedOperationException e) { } Assert.assertEquals(immutableSet.size(), mutableSet.size()); Assert.assertArrayEquals(immutableSet.toArray(), mutableSet.toArray()); Assert.assertSame(immutableSet.comparator(), mutableSet.comparator()); if (!mutableSet.isEmpty()) { Assert.assertEquals(immutableSet.first(), mutableSet.first()); Assert.assertEquals(immutableSet.last(), mutableSet.last()); } else { try { immutableSet.first(); Assert.fail(); } catch (NoSuchElementException e) { } try { immutableSet.last(); Assert.fail(); } catch (NoSuchElementException e) { } } Assert.assertEquals(immutableSet.headSet(value1), mutableSet.headSet(value1)); Assert.assertEquals(immutableSet.headSet(value2), mutableSet.headSet(value2)); Assert.assertEquals(immutableSet.subSet(value1, value2), mutableSet.subSet(value1, value2)); Assert.assertEquals(immutableSet.tailSet(value1), mutableSet.tailSet(value1)); Assert.assertEquals(immutableSet.tailSet(value2), mutableSet.tailSet(value2)); }