@SuppressWarnings("unchecked") public void testMergeOne() { Term t1 = new Term("foo", "a"); PrefixCodedTerms.Builder b1 = new PrefixCodedTerms.Builder(); b1.add(t1); PrefixCodedTerms pb1 = b1.finish(); Term t2 = new Term("foo", "b"); PrefixCodedTerms.Builder b2 = new PrefixCodedTerms.Builder(); b2.add(t2); PrefixCodedTerms pb2 = b2.finish(); Iterator<Term> merged = new MergedIterator<>(pb1.iterator(), pb2.iterator()); assertTrue(merged.hasNext()); assertEquals(t1, merged.next()); assertTrue(merged.hasNext()); assertEquals(t2, merged.next()); }
@SuppressWarnings("unchecked") public void testMergeOne() { Term t1 = new Term("foo", "a"); PrefixCodedTerms.Builder b1 = new PrefixCodedTerms.Builder(); b1.add(t1); PrefixCodedTerms pb1 = b1.finish(); Term t2 = new Term("foo", "b"); PrefixCodedTerms.Builder b2 = new PrefixCodedTerms.Builder(); b2.add(t2); PrefixCodedTerms pb2 = b2.finish(); Iterator<Term> merged = new MergedIterator<Term>(pb1.iterator(), pb2.iterator()); assertTrue(merged.hasNext()); assertEquals(t1, merged.next()); assertTrue(merged.hasNext()); assertEquals(t2, merged.next()); }
@SuppressWarnings({"unchecked","rawtypes"}) @Override public Iterator<String> iterator() { Iterator<String> subIterators[] = new Iterator[subs.length]; for(int i=0;i<subs.length;i++) { subIterators[i] = subs[i].iterator(); } return new MergedIterator<>(subIterators); }
public Iterable<Term> termsIterable() { return new Iterable<Term>() { @SuppressWarnings({"unchecked","rawtypes"}) @Override public Iterator<Term> iterator() { Iterator<Term> subs[] = new Iterator[iterables.size()]; for (int i = 0; i < iterables.size(); i++) { subs[i] = iterables.get(i).iterator(); } return new MergedIterator<>(subs); } }; }
@SuppressWarnings({"unchecked","rawtypes"}) public void testMergeRandom() { PrefixCodedTerms pb[] = new PrefixCodedTerms[TestUtil.nextInt(random(), 2, 10)]; Set<Term> superSet = new TreeSet<>(); for (int i = 0; i < pb.length; i++) { Set<Term> terms = new TreeSet<>(); int nterms = TestUtil.nextInt(random(), 0, 10000); for (int j = 0; j < nterms; j++) { Term term = new Term(TestUtil.randomUnicodeString(random(), 2), TestUtil.randomUnicodeString(random(), 4)); terms.add(term); } superSet.addAll(terms); PrefixCodedTerms.Builder b = new PrefixCodedTerms.Builder(); for (Term ref: terms) { b.add(ref); } pb[i] = b.finish(); } List<Iterator<Term>> subs = new ArrayList<>(); for (int i = 0; i < pb.length; i++) { subs.add(pb[i].iterator()); } Iterator<Term> expected = superSet.iterator(); Iterator<Term> actual = new MergedIterator<>(subs.toArray(new Iterator[0])); while (actual.hasNext()) { assertTrue(expected.hasNext()); assertEquals(expected.next(), actual.next()); } assertFalse(expected.hasNext()); }
@SuppressWarnings({"unchecked","rawtypes"}) @Override public Iterator<String> iterator() { Iterator<String> subIterators[] = new Iterator[subs.length]; for(int i=0;i<subs.length;i++) { subIterators[i] = subs[i].iterator(); } return new MergedIterator<String>(subIterators); }
public Iterable<Term> termsIterable() { return new Iterable<Term>() { @SuppressWarnings({"unchecked","rawtypes"}) @Override public Iterator<Term> iterator() { Iterator<Term> subs[] = new Iterator[iterables.size()]; for (int i = 0; i < iterables.size(); i++) { subs[i] = iterables.get(i).iterator(); } return new MergedIterator<Term>(subs); } }; }
@SuppressWarnings({"unchecked","rawtypes"}) public void testMergeRandom() { PrefixCodedTerms pb[] = new PrefixCodedTerms[_TestUtil.nextInt(random(), 2, 10)]; Set<Term> superSet = new TreeSet<Term>(); for (int i = 0; i < pb.length; i++) { Set<Term> terms = new TreeSet<Term>(); int nterms = _TestUtil.nextInt(random(), 0, 10000); for (int j = 0; j < nterms; j++) { Term term = new Term(_TestUtil.randomUnicodeString(random(), 2), _TestUtil.randomUnicodeString(random(), 4)); terms.add(term); } superSet.addAll(terms); PrefixCodedTerms.Builder b = new PrefixCodedTerms.Builder(); for (Term ref: terms) { b.add(ref); } pb[i] = b.finish(); } List<Iterator<Term>> subs = new ArrayList<Iterator<Term>>(); for (int i = 0; i < pb.length; i++) { subs.add(pb[i].iterator()); } Iterator<Term> expected = superSet.iterator(); Iterator<Term> actual = new MergedIterator<Term>(subs.toArray(new Iterator[0])); while (actual.hasNext()) { assertTrue(expected.hasNext()); assertEquals(expected.next(), actual.next()); } assertFalse(expected.hasNext()); }
/** * Merging constructor. * * @param toMerge FieldPhraseLists to merge to build this one */ public FieldPhraseList( FieldPhraseList[] toMerge ) { // Merge all overlapping WeightedPhraseInfos // Step 1. Sort by startOffset, endOffset, and boost, in that order. @SuppressWarnings( { "rawtypes", "unchecked" } ) Iterator< WeightedPhraseInfo >[] allInfos = new Iterator[ toMerge.length ]; int index = 0; for ( FieldPhraseList fplToMerge : toMerge ) { allInfos[ index++ ] = fplToMerge.phraseList.iterator(); } MergedIterator< WeightedPhraseInfo > itr = new MergedIterator<>( false, allInfos ); // Step 2. Walk the sorted list merging infos that overlap phraseList = new LinkedList<>(); if ( !itr.hasNext() ) { return; } List< WeightedPhraseInfo > work = new ArrayList<>(); WeightedPhraseInfo first = itr.next(); work.add( first ); int workEndOffset = first.getEndOffset(); while ( itr.hasNext() ) { WeightedPhraseInfo current = itr.next(); if ( current.getStartOffset() <= workEndOffset ) { workEndOffset = Math.max( workEndOffset, current.getEndOffset() ); work.add( current ); } else { if ( work.size() == 1 ) { phraseList.add( work.get( 0 ) ); work.set( 0, current ); } else { phraseList.add( new WeightedPhraseInfo( work ) ); work.clear(); work.add( current ); } workEndOffset = current.getEndOffset(); } } if ( work.size() == 1 ) { phraseList.add( work.get( 0 ) ); } else { phraseList.add( new WeightedPhraseInfo( work ) ); work.clear(); } }
/** * Merging constructor. Note that this just grabs seqnum from the first info. */ public WeightedPhraseInfo( Collection< WeightedPhraseInfo > toMerge ) { // Pretty much the same idea as merging FieldPhraseLists: // Step 1. Sort by startOffset, endOffset // While we are here merge the boosts and termInfos Iterator< WeightedPhraseInfo > toMergeItr = toMerge.iterator(); if ( !toMergeItr.hasNext() ) { throw new IllegalArgumentException( "toMerge must contain at least one WeightedPhraseInfo." ); } WeightedPhraseInfo first = toMergeItr.next(); @SuppressWarnings( { "rawtypes", "unchecked" } ) Iterator< Toffs >[] allToffs = new Iterator[ toMerge.size() ]; termsInfos = new ArrayList<>(); seqnum = first.seqnum; boost = first.boost; allToffs[ 0 ] = first.termsOffsets.iterator(); int index = 1; while ( toMergeItr.hasNext() ) { WeightedPhraseInfo info = toMergeItr.next(); boost += info.boost; termsInfos.addAll( info.termsInfos ); allToffs[ index++ ] = info.termsOffsets.iterator(); } // Step 2. Walk the sorted list merging overlaps MergedIterator< Toffs > itr = new MergedIterator<>( false, allToffs ); termsOffsets = new ArrayList<>(); if ( !itr.hasNext() ) { return; } Toffs work = itr.next(); while ( itr.hasNext() ) { Toffs current = itr.next(); if ( current.startOffset <= work.endOffset ) { work.endOffset = Math.max( work.endOffset, current.endOffset ); } else { termsOffsets.add( work ); work = current; } } termsOffsets.add( work ); }
/** * Merging constructor. * * @param toMerge FieldPhraseLists to merge to build this one */ public FieldPhraseList( FieldPhraseList[] toMerge ) { // Merge all overlapping WeightedPhraseInfos // Step 1. Sort by startOffset, endOffset, and boost, in that order. @SuppressWarnings( { "rawtypes", "unchecked" } ) Iterator< WeightedPhraseInfo >[] allInfos = new Iterator[ toMerge.length ]; int index = 0; for ( FieldPhraseList fplToMerge : toMerge ) { allInfos[ index++ ] = fplToMerge.phraseList.iterator(); } MergedIterator< WeightedPhraseInfo > itr = new MergedIterator< WeightedPhraseInfo >( false, allInfos ); // Step 2. Walk the sorted list merging infos that overlap phraseList = new LinkedList< WeightedPhraseInfo >(); if ( !itr.hasNext() ) { return; } List< WeightedPhraseInfo > work = new ArrayList< WeightedPhraseInfo >(); WeightedPhraseInfo first = itr.next(); work.add( first ); int workEndOffset = first.getEndOffset(); while ( itr.hasNext() ) { WeightedPhraseInfo current = itr.next(); if ( current.getStartOffset() <= workEndOffset ) { workEndOffset = Math.max( workEndOffset, current.getEndOffset() ); work.add( current ); } else { if ( work.size() == 1 ) { phraseList.add( work.get( 0 ) ); work.set( 0, current ); } else { phraseList.add( new WeightedPhraseInfo( work ) ); work.clear(); work.add( current ); } workEndOffset = current.getEndOffset(); } } if ( work.size() == 1 ) { phraseList.add( work.get( 0 ) ); } else { phraseList.add( new WeightedPhraseInfo( work ) ); work.clear(); } }
/** * Merging constructor. Note that this just grabs seqnum from the first info. */ public WeightedPhraseInfo( Collection< WeightedPhraseInfo > toMerge ) { // Pretty much the same idea as merging FieldPhraseLists: // Step 1. Sort by startOffset, endOffset // While we are here merge the boosts and termInfos Iterator< WeightedPhraseInfo > toMergeItr = toMerge.iterator(); if ( !toMergeItr.hasNext() ) { throw new IllegalArgumentException( "toMerge must contain at least one WeightedPhraseInfo." ); } WeightedPhraseInfo first = toMergeItr.next(); @SuppressWarnings( { "rawtypes", "unchecked" } ) Iterator< Toffs >[] allToffs = new Iterator[ toMerge.size() ]; termsInfos = new ArrayList< TermInfo >(); seqnum = first.seqnum; boost = first.boost; allToffs[ 0 ] = first.termsOffsets.iterator(); int index = 1; while ( toMergeItr.hasNext() ) { WeightedPhraseInfo info = toMergeItr.next(); boost += info.boost; termsInfos.addAll( info.termsInfos ); allToffs[ index++ ] = info.termsOffsets.iterator(); } // Step 2. Walk the sorted list merging overlaps MergedIterator< Toffs > itr = new MergedIterator< Toffs >( false, allToffs ); termsOffsets = new ArrayList< Toffs >(); if ( !itr.hasNext() ) { return; } Toffs work = itr.next(); while ( itr.hasNext() ) { Toffs current = itr.next(); if ( current.startOffset <= work.endOffset ) { work.endOffset = Math.max( work.endOffset, current.endOffset ); } else { termsOffsets.add( work ); work = current; } } termsOffsets.add( work ); }