Skip to content

Commit

Permalink
Update ListSubsample.java
Browse files Browse the repository at this point in the history
v2.1
  • Loading branch information
Sublimis committed Dec 4, 2021
1 parent 0b36c3e commit c9b41e4
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions app/src/main/java/lib/sublimis/subsample/ListSubsample.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* Two input segments will be merged in the output unless they are separated by more than the bucket size.
*
* @author Sublimis
* @version 2.0 (2021-09-27)
* @version 2.1 (2021-12-04)
*/
public class ListSubsample<T>
{
Expand Down Expand Up @@ -112,6 +112,7 @@ public List<List<T>> getSubsample(final double bucketSize)
if (LSUtils.isValidAndNotEmpty(listList))
{
T min = null, max = null, first = null, last = null;
int minIndex = -1, maxIndex = -1;
double xBucket = 0;

List<T> oneSegment = new ArrayList<>();
Expand All @@ -133,7 +134,7 @@ public List<List<T>> getSubsample(final double bucketSize)
{
if (shouldStartNewSegment(current, last, bucketSize))
{
insertPoint(oneSegment, min, max);
insertPoint(oneSegment, min, max, minIndex, maxIndex);

if (LSUtils.isValidAndNotEmpty(oneSegment))
{
Expand All @@ -143,38 +144,46 @@ public List<List<T>> getSubsample(final double bucketSize)
}

min = max = current;
minIndex = maxIndex = index;
xBucket = getBucket(current, first, bucketSize);
}
}

if (last == null || xBucket != getBucket(current, first, bucketSize))
{
if (last != null)
final double bucket = getBucket(current, first, bucketSize);

if (last == null || xBucket != bucket)
{
insertPoint(oneSegment, min, max);
}
if (last != null)
{
insertPoint(oneSegment, min, max, minIndex, maxIndex);
}

min = max = current;
xBucket = getBucket(current, first, bucketSize);
min = max = current;
minIndex = maxIndex = index;
xBucket = bucket;
}
}

last = current;

if (mComparatorY.compare(current, min) < 0)
{
min = current;
minIndex = index;
}

if (mComparatorY.compare(current, max) >= 0)
{
// Prefer returning the rightmost element as "max" if all elements are equal (leftmost will be "min" in this case)
max = current;
maxIndex = index;
}
}
}
}

insertPoint(oneSegment, min, max);
insertPoint(oneSegment, min, max, minIndex, maxIndex);

if (LSUtils.isValidAndNotEmpty(oneSegment))
{
Expand Down Expand Up @@ -229,7 +238,7 @@ protected boolean shouldStartNewSegment(final T current, final T last, final dou
return retVal;
}

protected void insertPoint(final List<T> oneSegment, final T min, final T max)
protected void insertPoint(final List<T> oneSegment, final T min, final T max, final int minIndex, final int maxIndex)
{
if (min == null || max == null)
{
Expand All @@ -243,16 +252,29 @@ else if (min == max)
{
final int compare = mComparatorX.compare(min, max);

if (compare <= 0)
if (compare < 0)
{
oneSegment.add(min);
oneSegment.add(max);
}
else
else if (compare > 0)
{
oneSegment.add(max);
oneSegment.add(min);
}
else
{
if (minIndex <= maxIndex)
{
oneSegment.add(min);
oneSegment.add(max);
}
else
{
oneSegment.add(max);
oneSegment.add(min);
}
}
}
}
}

0 comments on commit c9b41e4

Please sign in to comment.