1 package jalview.datamodel;
3 import java.util.ArrayList;
4 import java.util.Iterator;
8 * An iterator which iterates over a list of ranges. Works with a copy of the
9 * collection of ranges.
11 public class RangeIterator implements Iterator<int[]>
13 // current index in rangeList
14 private int currentPosition = 0;
16 // current range in rangeList
17 private int[] currentRange;
19 // local copy or reference to rangeList
20 private List<int[]> localRanges;
23 * Unbounded constructor
26 * list of ranges to iterate over
28 RangeIterator(List<int[]> rangeList)
30 if (!rangeList.isEmpty())
32 int last = rangeList.get(rangeList.size() - 1)[1];
33 init(0, last, rangeList);
37 init(0, 0, rangeList);
42 * Construct an iterator over rangeList bounded at [lowerBound,upperBound]
45 * lower bound to iterate from
47 * upper bound to iterate to
49 * list of ranges to iterate over
51 RangeIterator(int lowerBound, int upperBound,
52 List<int[]> rangeList)
54 init(lowerBound, upperBound, rangeList);
58 * Construct an iterator over rangeList bounded at [lowerBound,upperBound]
61 * lower bound to iterate from
63 * upper bound to iterate to
65 private void init(int lowerBound, int upperBound,
66 List<int[]> rangeList)
68 int start = lowerBound;
71 if (rangeList != null)
73 localRanges = new ArrayList<>();
75 // iterate until a range overlaps with [start,end]
77 while ((i < rangeList.size()) && (rangeList.get(i)[1] < start))
82 // iterate from start to end, adding each range. Positions are
83 // absolute, and all ranges which *overlap* [start,end] are added.
84 while (i < rangeList.size() && (rangeList.get(i)[0] <= end))
86 int[] rh = rangeList.get(i);
87 int[] cp = new int[2];
88 System.arraycopy(rh, 0, cp, 0, rh.length);
96 public boolean hasNext()
98 return (localRanges != null) && (currentPosition < localRanges.size());
104 currentRange = localRanges.get(currentPosition);
112 localRanges.remove(--currentPosition);