org.apache.cassandra.utils.OverlapIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-unit-shaded Show documentation
Show all versions of cassandra-unit-shaded Show documentation
Shaded version of cassandra-unit
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.cassandra.utils;
import java.util.*;
/**
* A class for iterating sequentially through an ordered collection and efficiently
* finding the overlapping set of matching intervals.
*
* The algorithm is quite simple: the intervals are sorted ascending by both min and max
* in two separate lists. These lists are walked forwards each time we visit a new point,
* with the set of intervals in the min-ordered list being added to our set of overlaps,
* and those in the max-ordered list being removed.
*/
public class OverlapIterator, V>
{
// indexing into sortedByMin, tracks the next interval to include
int nextToInclude;
final List> sortedByMin;
// indexing into sortedByMax, tracks the next interval to exclude
int nextToExclude;
final List> sortedByMax;
final Set overlaps = new HashSet<>();
final Set accessible = Collections.unmodifiableSet(overlaps);
public OverlapIterator(Collection> intervals)
{
sortedByMax = new ArrayList<>(intervals);
Collections.sort(sortedByMax, Interval.maxOrdering());
// we clone after first sorting by max; this is quite likely to make sort cheaper, since a.max < b.max
// generally increases the likelihood that a.min < b.min, so the list may be partially sorted already.
// this also means if (in future) we sort either collection (or a subset thereof) by the other's comparator
// all items, including equal, will occur in the same order, including
sortedByMin = new ArrayList<>(sortedByMax);
Collections.sort(sortedByMin, Interval.minOrdering());
}
// move the iterator forwards to the overlaps matching point
public void update(I point)
{
// we don't use binary search here since we expect points to be a superset of the min/max values
// add those we are now after the start of
while (nextToInclude < sortedByMin.size() && sortedByMin.get(nextToInclude).min.compareTo(point) <= 0)
overlaps.add(sortedByMin.get(nextToInclude++).data);
// remove those we are now after the end of
while (nextToExclude < sortedByMax.size() && sortedByMax.get(nextToExclude).max.compareTo(point) < 0)
overlaps.remove(sortedByMax.get(nextToExclude++).data);
}
public Set overlaps()
{
return accessible;
}
}