Skip to content

Commit 7016483

Browse files
committed
refactor: Merge ParetoSet and ParetoSetWithMarker
1 parent 8dc0804 commit 7016483

14 files changed

Lines changed: 133 additions & 149 deletions

File tree

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/multicriteria/MultiCriteriaRoutingStrategy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public void prepareForTransitWith(RaptorRoute<T> route) {
6969
public void prepareForNextStop(int stopIndex, int stopPos) {
7070
// If no pass-through service exist, this block will be removed by the JIT compiler
7171
if (passThroughPointsService.isPassThroughPoint(stopIndex)) {
72-
for (R ride : patternRides) {
72+
for (int i = 0; i < patternRides.size(); ++i) {
73+
R ride = patternRides.get(i);
7374
// Replace existing ride with same ride with the C2 value updated. This only happens if
7475
// the stop is a pass-through point and the path has visited the pass-through points in the
7576
// correct order.
@@ -83,7 +84,8 @@ public void prepareForNextStop(int stopIndex, int stopPos) {
8384

8485
@Override
8586
public void alightOnlyRegularTransferExist(int stopIndex, int stopPos, int alightSlack) {
86-
for (R ride : patternRides) {
87+
for (int i = 0; i < patternRides.size(); ++i) {
88+
R ride = patternRides.get(i);
8789
state.transitToStop(ride, stopIndex, ride.trip().arrival(stopPos), alightSlack);
8890
}
8991
}

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/multicriteria/arrivals/DebugStopArrivalsStatistics.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import org.opentripplanner.raptor.api.debug.DebugLogger;
66
import org.opentripplanner.raptor.api.debug.DebugTopic;
7-
import org.opentripplanner.raptor.util.paretoset.ParetoSetWithMarker;
7+
import org.opentripplanner.raptor.util.paretoset.ParetoSet;
88

99
/**
1010
* Utility class to print some statistics about stop arrivals.
@@ -17,7 +17,7 @@ class DebugStopArrivalsStatistics {
1717
this.debugLogger = debugLogger;
1818
}
1919

20-
void debugStatInfo(ParetoSetWithMarker<?>[] stops) {
20+
void debugStatInfo(ParetoSet<?>[] stops) {
2121
if (!debugLogger.isEnabled()) {
2222
return;
2323
}
@@ -27,7 +27,7 @@ void debugStatInfo(ParetoSetWithMarker<?>[] stops) {
2727
long numOfStops = 0;
2828
int max = 0;
2929

30-
for (ParetoSetWithMarker<?> stop : stops) {
30+
for (ParetoSet<?> stop : stops) {
3131
if (stop != null) {
3232
++numOfStops;
3333
total += stop.size();

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/multicriteria/arrivals/McStopArrivals.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import org.opentripplanner.raptor.spi.IntIterator;
1414
import org.opentripplanner.raptor.util.BitSetIterator;
1515
import org.opentripplanner.raptor.util.paretoset.ParetoComparator;
16+
import org.opentripplanner.raptor.util.paretoset.ParetoSet;
1617
import org.opentripplanner.raptor.util.paretoset.ParetoSetEventListener;
17-
import org.opentripplanner.raptor.util.paretoset.ParetoSetWithMarker;
1818

1919
/**
2020
* This class serve as a wrapper for all stop arrival pareto set, one set for each stop. It also
@@ -25,7 +25,7 @@
2525
*/
2626
public final class McStopArrivals<T extends RaptorTripSchedule> {
2727

28-
private final ParetoSetWithMarker<McStopArrival<T>>[] arrivals;
28+
private final ParetoSet<McStopArrival<T>>[] arrivals;
2929
private final BitSet touchedStops;
3030

3131
private final DebugHandlerFactory<T> debugHandlerFactory;
@@ -46,14 +46,14 @@ public McStopArrivals(
4646
DebugHandlerFactory<T> debugHandlerFactory
4747
) {
4848
//noinspection unchecked
49-
this.arrivals = (ParetoSetWithMarker<McStopArrival<T>>[]) new ParetoSetWithMarker[nStops];
49+
this.arrivals = (ParetoSet<McStopArrival<T>>[]) new ParetoSet[nStops];
5050
this.touchedStops = new BitSet(nStops);
5151
this.comparator = comparatorFactory.compareArrivalTimeRoundCostAndOnBoardArrival();
5252
this.debugHandlerFactory = debugHandlerFactory;
5353
this.debugStats = new DebugStopArrivalsStatistics(debugHandlerFactory.debugLogger());
5454

5555
for (int stop : arrivalListeners.keys()) {
56-
this.arrivals[stop] = new ParetoSetWithMarker<>(comparator, arrivalListeners.get(stop));
56+
this.arrivals[stop] = ParetoSet.of(comparator, arrivalListeners.get(stop));
5757
}
5858
}
5959

@@ -126,9 +126,9 @@ public void clearTouchedStopsAndSetStopMarkers() {
126126

127127
/* private methods */
128128

129-
private ParetoSetWithMarker<McStopArrival<T>> findOrCreateSet(final int stop) {
129+
private ParetoSet<McStopArrival<T>> findOrCreateSet(final int stop) {
130130
if (arrivals[stop] == null) {
131-
arrivals[stop] = new ParetoSetWithMarker<>(
131+
arrivals[stop] = ParetoSet.of(
132132
comparator,
133133
debugHandlerFactory.paretoSetStopArrivalListener(stop)
134134
);

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/multicriteria/configure/McRangeRaptorConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private HeuristicsProvider<T> createHeuristicsProvider(Heuristics heuristics) {
222222
private <R extends PatternRide<T>> ParetoSet<R> createPatternRideParetoSet(
223223
ParetoComparator<R> comparator
224224
) {
225-
return new ParetoSet<>(comparator, context().debugFactory().paretoSetPatternRideListener());
225+
return ParetoSet.of(comparator, context().debugFactory().paretoSetPatternRideListener());
226226
}
227227

228228
private DestinationArrivalPaths<T> createDestinationArrivalPaths() {

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/path/DestinationArrivalPaths.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public DestinationArrivalPaths(
7171
RaptorStopNameResolver stopNameResolver,
7272
WorkerLifeCycle lifeCycle
7373
) {
74-
this.paths = new ParetoSet<>(
75-
paretoComparator,
76-
debugHandlerFactory.paretoSetDebugPathListener()
77-
);
74+
this.paths = ParetoSet.of(paretoComparator, debugHandlerFactory.paretoSetDebugPathListener());
7875
this.transitCalculator = transitCalculator;
7976
this.costCalculator = costCalculator;
8077
this.slackProvider = slackProvider;
@@ -138,7 +135,7 @@ public boolean qualify(int departureTime, int arrivalTime, int numberOfTransfers
138135
}
139136

140137
public Collection<RaptorPath<T>> listPaths() {
141-
return paths;
138+
return paths.stream().toList();
142139
}
143140

144141
public void debugReject(ArrivalView<T> stopArrival, RaptorAccessEgress egress, String reason) {

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/standard/besttimes/UnknownPathFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private void setIterationDepartureTime(int value) {
5858
}
5959

6060
public Collection<RaptorPath<T>> extractPaths() {
61-
ParetoSet<RaptorPath<T>> paths = new ParetoSet<>(comparator);
61+
ParetoSet<RaptorPath<T>> paths = ParetoSet.of(comparator);
6262
for (RaptorAccessEgress egress : egressPaths) {
6363
createNewPath(egress).ifPresent(paths::add);
6464
}

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/support/RouterResultPathAggregator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public RouterResultPathAggregator(
1818
Collection<RaptorRouterResult<T>> results,
1919
ParetoComparator<RaptorPath<T>> comparator
2020
) {
21-
this.paths = new ParetoSet<>(comparator);
21+
this.paths = ParetoSet.of(comparator);
2222
RaptorRouterResult<T> first = null;
2323
for (var it : results) {
2424
if (first == null) {
@@ -31,7 +31,7 @@ public RouterResultPathAggregator(
3131

3232
@Override
3333
public Collection<RaptorPath<T>> extractPaths() {
34-
return paths;
34+
return paths.stream().toList();
3535
}
3636

3737
@Override

raptor/src/main/java/org/opentripplanner/raptor/rangeraptor/transit/AccessEgressFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ static Collection<RaptorAccessEgress> removeNonOptimalPaths(
137137
ParetoComparator<RaptorAccessEgress> comparator
138138
) {
139139
var mapByStop = groupByStop(paths);
140-
var set = new ParetoSet<>(comparator);
140+
var set = ParetoSet.of(comparator);
141141
var result = new ArrayList<RaptorAccessEgress>();
142142

143143
for (int stop : mapByStop.keys()) {
144144
var list = mapByStop.get(stop);
145145
set.clear();
146146
set.addAll(list);
147-
result.addAll(set);
147+
result.addAll(set.stream().toList());
148148
}
149149
return result;
150150
}

raptor/src/main/java/org/opentripplanner/raptor/util/paretoset/ParetoSet.java

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @param <T> the element type
2626
*/
27-
public sealed class ParetoSet<T> extends AbstractCollection<T> permits ParetoSetWithMarker {
27+
public final class ParetoSet<T> extends AbstractCollection<T> {
2828

2929
private final ParetoComparator<T> comparator;
3030

@@ -38,25 +38,38 @@ public sealed class ParetoSet<T> extends AbstractCollection<T> permits ParetoSet
3838

3939
private T goodElement = null;
4040

41-
/**
42-
* Create a new ParetoSet with a comparator and a drop event listener.
43-
*
44-
* @param comparator The comparator to use with this set
45-
* @param eventListener At most one listener can be registered to listen for drop events.
46-
*/
47-
public ParetoSet(
41+
private int marker = 0;
42+
43+
private ParetoSet(
4844
ParetoComparator<T> comparator,
4945
@Nullable ParetoSetEventListener<? super T> eventListener
5046
) {
5147
this.comparator = comparator;
5248
this.eventListener = eventListener;
5349
}
5450

51+
private ParetoSet(ParetoComparator<T> comparator) {
52+
this(comparator, null);
53+
}
54+
5555
/**
5656
* Create a new ParetoSet with a comparator.
5757
*/
58-
public ParetoSet(ParetoComparator<T> comparator) {
59-
this(comparator, null);
58+
public static <T> ParetoSet<T> of(ParetoComparator<T> comparator) {
59+
return of(comparator, null);
60+
}
61+
62+
/**
63+
* Create a new ParetoSet with a comparator and a drop event listener.
64+
*
65+
* @param comparator The comparator to use with this set
66+
* @param eventListener At most one listener can be registered to listen for drop events.
67+
*/
68+
public static <T> ParetoSet<T> of(
69+
ParetoComparator<T> comparator,
70+
@Nullable ParetoSetEventListener<? super T> eventListener
71+
) {
72+
return new ParetoSet<>(comparator, eventListener);
6073
}
6174

6275
public T get(int index) {
@@ -69,17 +82,35 @@ public T get(int index) {
6982
* This is NOT thread-safe and the behavior is undefined if the collection is modified during the
7083
* iteration.
7184
*/
72-
@Override
7385
public final Iterator<T> iterator() {
7486
return tailIterator(0);
7587
}
7688

77-
@Override
89+
/*
90+
public final Iterable iterable() {
91+
return new Iterable() {
92+
@Override
93+
public Iterator iterator() {
94+
return iterator();
95+
}
96+
};
97+
}
98+
*/
99+
78100
public int size() {
79101
return size;
80102
}
81103

82-
@Override
104+
public boolean isEmpty() {
105+
return size == 0;
106+
}
107+
108+
/*
109+
public Stream<T> stream() {
110+
return Arrays.stream(elements, 0, size);
111+
}
112+
*/
113+
83114
public boolean add(T newValue) {
84115
if (size == 0) {
85116
acceptAndAppendValue(newValue);
@@ -126,15 +157,17 @@ public boolean add(T newValue) {
126157
return false;
127158
}
128159

129-
@Override
130-
public boolean remove(Object o) {
131-
throw new UnsupportedOperationException();
132-
}
160+
/*
161+
public void addAll(Collection<T> elements) {
162+
for (T element : elements) {
163+
add(element);
164+
}
165+
}*/
133166

134-
@Override
135167
public void clear() {
136168
size = 0;
137169
goodElement = null;
170+
marker = 0;
138171
}
139172

140173
@Override
@@ -210,8 +243,10 @@ public String toString(Function<? super T, String> toStringMapper) {
210243
* Notify subclasses about reindexing. This method is empty, and only exist for subclasses to
211244
* override it.
212245
*/
213-
protected void notifyElementMoved(int fromIndex, int toIndex) {
214-
// Noop
246+
private void notifyElementMoved(int fromIndex, int toIndex) {
247+
if (fromIndex == marker) {
248+
marker = toIndex;
249+
}
215250
}
216251

217252
/**
@@ -326,4 +361,22 @@ private void notifyElementRejected(T element, T rejectByElement) {
326361
eventListener.notifyElementRejected(element, rejectByElement);
327362
}
328363
}
364+
365+
public boolean hasElementsAfterMarker() {
366+
return marker != size();
367+
}
368+
369+
/**
370+
* List all elements added after the marker.
371+
*/
372+
public Iterable<T> elementsAfterMarker() {
373+
return tail(marker);
374+
}
375+
376+
/**
377+
* Move the marker after the last element in the set.
378+
*/
379+
public void markAtEndOfSet() {
380+
marker = size();
381+
}
329382
}

raptor/src/main/java/org/opentripplanner/raptor/util/paretoset/ParetoSetWithMarker.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)