public WeightedPhraseInfo( Collection< WeightedPhraseInfo > toMerge ) {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();}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 );}
public WeightedPhraseInfo(ICollection<WeightedPhraseInfo> toMerge){IEnumerator<Toffs>[] allToffs = new IEnumerator<Toffs>[toMerge.Count];try{using (IEnumerator<WeightedPhraseInfo> toMergeItr = toMerge.GetEnumerator()){if (!toMergeItr.MoveNext()){throw new ArgumentException("toMerge must contain at least one WeightedPhraseInfo.");}WeightedPhraseInfo first = toMergeItr.Current;termsInfos = new List<TermInfo>();seqnum = first.seqnum;boost = first.boost;allToffs[0] = first.termsOffsets.GetEnumerator();int index = 1;while (toMergeItr.MoveNext()){WeightedPhraseInfo info = toMergeItr.Current;boost += info.boost;termsInfos.AddRange(info.termsInfos);allToffs[index++] = info.termsOffsets.GetEnumerator();}using (MergedIterator<Toffs> itr = new MergedIterator<Toffs>(false, allToffs)){termsOffsets = new List<Toffs>();if (!itr.MoveNext()){return;}Toffs work = itr.Current;while (itr.MoveNext()){Toffs current = itr.Current;if (current.StartOffset <= work.EndOffset){work.EndOffset = Math.Max(work.EndOffset, current.EndOffset);}else{termsOffsets.Add(work);work = current;}}termsOffsets.Add(work);}}}finally{foreach (var allToff in allToffs){allToff.Dispose();}}}