2024-06-10 13:09:06 +00:00
|
|
|
package nu.marginalia.sequence;
|
|
|
|
|
2024-07-30 10:01:53 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
2024-06-10 13:09:06 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntIterator;
|
2024-07-30 10:01:53 +00:00
|
|
|
import it.unimi.dsi.fastutil.ints.IntList;
|
2024-06-10 13:09:06 +00:00
|
|
|
|
|
|
|
public class SequenceOperations {
|
|
|
|
|
|
|
|
/** Return true if the sequences intersect, false otherwise.
|
|
|
|
* */
|
|
|
|
public static boolean intersectSequences(IntIterator... sequences) {
|
|
|
|
|
|
|
|
if (sequences.length <= 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Initialize values and find the maximum value
|
|
|
|
int[] values = new int[sequences.length];
|
|
|
|
|
|
|
|
for (int i = 0; i < sequences.length; i++) {
|
|
|
|
if (sequences[i].hasNext())
|
|
|
|
values[i] = sequences[i].nextInt();
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect the sequences by advancing all values smaller than the maximum seen so far
|
|
|
|
// until they are equal to the maximum value, or until the end of the sequence is reached
|
|
|
|
int max = Integer.MIN_VALUE;
|
|
|
|
int successes = 0;
|
|
|
|
for (int i = 0; successes < sequences.length; i = (i + 1) % sequences.length)
|
|
|
|
{
|
|
|
|
if (values[i] == max) {
|
|
|
|
successes++;
|
|
|
|
} else {
|
2024-07-30 10:01:53 +00:00
|
|
|
successes = 1;
|
2024-06-10 13:09:06 +00:00
|
|
|
|
|
|
|
// Discard values until we reach the maximum value seen so far,
|
|
|
|
// or until the end of the sequence is reached
|
|
|
|
while (values[i] < max) {
|
|
|
|
if (sequences[i].hasNext())
|
|
|
|
values[i] = sequences[i].nextInt();
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the maximum value, if necessary
|
|
|
|
max = Math.max(max, values[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-08-25 11:16:31 +00:00
|
|
|
public static IntList findIntersections(IntList... positions) {
|
|
|
|
return findIntersections(new int[positions.length], positions);
|
2024-08-25 10:23:09 +00:00
|
|
|
}
|
2024-08-25 11:16:31 +00:00
|
|
|
public static IntList findIntersections(int[] iterOffsets, IntList... positions) {
|
2024-07-30 10:01:53 +00:00
|
|
|
|
2024-08-25 11:16:31 +00:00
|
|
|
if (positions.length < 1)
|
2024-07-30 10:01:53 +00:00
|
|
|
return IntList.of();
|
|
|
|
|
2024-08-25 11:16:31 +00:00
|
|
|
int[] indexes = new int[positions.length];
|
2024-07-30 10:01:53 +00:00
|
|
|
// Initialize values and find the maximum value
|
2024-08-25 11:16:31 +00:00
|
|
|
int[] values = new int[positions.length];
|
2024-07-30 10:01:53 +00:00
|
|
|
|
2024-08-25 11:16:31 +00:00
|
|
|
for (int i = 0; i < positions.length; i++) {
|
|
|
|
if (indexes[i]++ < positions[i].size())
|
|
|
|
values[i] = positions[i].getInt(indexes[i]) + iterOffsets[i];
|
2024-07-30 10:01:53 +00:00
|
|
|
else
|
|
|
|
return IntList.of();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect the sequences by advancing all values smaller than the maximum seen so far
|
|
|
|
// until they are equal to the maximum value, or until the end of the sequence is reached
|
|
|
|
int max = Integer.MIN_VALUE;
|
|
|
|
int successes = 0;
|
|
|
|
|
|
|
|
IntList ret = new IntArrayList();
|
|
|
|
|
|
|
|
outer:
|
2024-08-25 11:16:31 +00:00
|
|
|
for (int i = 0;; i = (i + 1) % positions.length)
|
2024-07-30 10:01:53 +00:00
|
|
|
{
|
2024-08-25 11:16:31 +00:00
|
|
|
if (successes == positions.length) {
|
2024-07-30 10:01:53 +00:00
|
|
|
ret.add(max);
|
|
|
|
successes = 1;
|
|
|
|
|
2024-08-25 11:16:31 +00:00
|
|
|
if (indexes[i]++ < positions[i].size()) {
|
|
|
|
values[i] = positions[i].getInt(indexes[i]) + iterOffsets[i];
|
2024-07-30 10:01:53 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (values[i] == max) {
|
|
|
|
successes++;
|
|
|
|
} else {
|
|
|
|
successes = 1;
|
|
|
|
|
|
|
|
// Discard values until we reach the maximum value seen so far,
|
|
|
|
// or until the end of the sequence is reached
|
|
|
|
while (values[i] < max) {
|
2024-08-25 11:16:31 +00:00
|
|
|
if (indexes[i]++ < positions[i].size()) {
|
|
|
|
values[i] = positions[i].getInt(indexes[i]) + iterOffsets[i];
|
2024-07-30 10:01:53 +00:00
|
|
|
} else {
|
|
|
|
break outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the maximum value, if necessary
|
|
|
|
max = Math.max(max, values[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-06-10 13:09:06 +00:00
|
|
|
/** Return the minimum word distance between two sequences, or a negative value if either sequence is empty.
|
|
|
|
* */
|
|
|
|
public static int minDistance(IntIterator seqA, IntIterator seqB)
|
|
|
|
{
|
|
|
|
int minDistance = Integer.MAX_VALUE;
|
|
|
|
|
|
|
|
if (!seqA.hasNext() || !seqB.hasNext())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int a = seqA.nextInt();
|
|
|
|
int b = seqB.nextInt();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
int distance = Math.abs(a - b);
|
|
|
|
if (distance < minDistance)
|
|
|
|
minDistance = distance;
|
|
|
|
|
|
|
|
if (a <= b) {
|
|
|
|
if (seqA.hasNext()) {
|
|
|
|
a = seqA.nextInt();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (seqB.hasNext()) {
|
|
|
|
b = seqB.nextInt();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return minDistance;
|
|
|
|
}
|
2024-08-03 10:04:23 +00:00
|
|
|
|
2024-08-25 09:01:35 +00:00
|
|
|
public static int minDistance(IntIterator[] iterators) {
|
2024-08-25 10:23:09 +00:00
|
|
|
return minDistance(iterators, new int[iterators.length]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int minDistance(IntIterator[] iterators, int[] iterOffsets) {
|
2024-08-25 09:01:35 +00:00
|
|
|
if (iterators.length <= 1)
|
2024-08-03 10:04:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2024-08-25 09:01:35 +00:00
|
|
|
int[] values = new int[iterators.length];
|
2024-08-03 10:04:23 +00:00
|
|
|
|
2024-08-25 09:01:35 +00:00
|
|
|
for (int i = 0; i < iterators.length; i++) {
|
|
|
|
if (iterators[i].hasNext())
|
2024-08-25 10:23:09 +00:00
|
|
|
values[i] = iterators[i].nextInt() + iterOffsets[i];
|
2024-08-03 10:04:23 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int minDist = Integer.MAX_VALUE;
|
|
|
|
|
|
|
|
int minVal = Integer.MAX_VALUE;
|
|
|
|
int maxVal = Integer.MIN_VALUE;
|
|
|
|
|
|
|
|
for (int val : values) {
|
|
|
|
minVal = Math.min(minVal, val);
|
|
|
|
maxVal = Math.max(maxVal, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
minDist = Math.min(minDist, maxVal - minVal);
|
|
|
|
|
2024-08-25 09:01:35 +00:00
|
|
|
for (int i = 0;; i = (i + 1) % iterators.length)
|
2024-08-03 10:04:23 +00:00
|
|
|
{
|
|
|
|
if (values[i] == minVal) {
|
2024-08-25 09:01:35 +00:00
|
|
|
if (!iterators[i].hasNext()) {
|
2024-08-03 10:04:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-08-25 10:23:09 +00:00
|
|
|
values[i] = iterators[i].nextInt() + iterOffsets[i];
|
2024-08-03 10:04:23 +00:00
|
|
|
|
|
|
|
if (values[i] > maxVal) {
|
|
|
|
maxVal = values[i];
|
|
|
|
}
|
|
|
|
if (values[i] > minVal) {
|
|
|
|
minVal = Integer.MAX_VALUE;
|
|
|
|
for (int val : values) {
|
|
|
|
minVal = Math.min(minVal, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
minDist = Math.min(minDist, maxVal - minVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return minDist;
|
|
|
|
}
|
2024-06-10 13:09:06 +00:00
|
|
|
}
|