use of gnu.trove.list.TIntList in project Terasology by MovingBlocks.
the class NetEntityRefTypeHandler method deserialize.
@Override
public EntityRef deserialize(PersistedData data, DeserializationContext context) {
if (data.isArray()) {
PersistedDataArray array = data.getAsArray();
if (array.isNumberArray() && array.size() == 3) {
TIntList items = data.getAsArray().getAsIntegerArray();
Vector3i pos = new Vector3i(items.get(0), items.get(1), items.get(2));
return blockEntityRegistry.getBlockEntityAt(pos);
}
}
if (data.isNumber()) {
return networkSystem.getEntity(data.getAsInteger());
}
return EntityRef.NULL;
}
use of gnu.trove.list.TIntList in project Terasology by MovingBlocks.
the class LocalChunkProviderTest method testCompleteUpdateSendsBlockActivatedEvents.
@Test
public void testCompleteUpdateSendsBlockActivatedEvents() throws Exception {
final Chunk chunk = mockChunkAt(0, 0, 0);
final TShortObjectHashMap<TIntList> blockPositionMappings = new TShortObjectHashMap<>();
final short blockId = 42;
final EntityRef blockEntity = mock(EntityRef.class);
registerBlockWithIdAndEntity(blockId, blockEntity, blockManager);
blockPositionMappings.put(blockId, withPositions(new Vector3i(1, 2, 3)));
final ReadyChunkInfo readyChunkInfo = ReadyChunkInfo.createForRestoredChunk(chunk, blockPositionMappings, mock(ChunkStore.class), Collections.emptyList());
when(chunkFinalizer.completeFinalization()).thenReturn(readyChunkInfo);
chunkProvider.completeUpdate();
final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
verify(blockEntity, atLeastOnce()).send(eventArgumentCaptor.capture());
final Event event = eventArgumentCaptor.getAllValues().get(1);
assertThat(event, instanceOf(OnActivatedBlocks.class));
assertThat(((OnActivatedBlocks) event).getBlockPositions(), hasItem(new Vector3i(1, 2, 3)));
}
use of gnu.trove.list.TIntList in project Terasology by MovingBlocks.
the class LocalChunkProviderTest method testCompleteUpdateSendsBlockAddedEvents.
@Test
public void testCompleteUpdateSendsBlockAddedEvents() throws Exception {
final Chunk chunk = mockChunkAt(0, 0, 0);
final short blockId = 42;
final EntityRef blockEntity = mock(EntityRef.class);
registerBlockWithIdAndEntity(blockId, blockEntity, blockManager);
final TShortObjectHashMap<TIntList> blockPositionMappings = new TShortObjectHashMap<>();
blockPositionMappings.put(blockId, withPositions(new Vector3i(1, 2, 3)));
final ReadyChunkInfo readyChunkInfo = ReadyChunkInfo.createForRestoredChunk(chunk, blockPositionMappings, mock(ChunkStore.class), Collections.emptyList());
when(chunkFinalizer.completeFinalization()).thenReturn(readyChunkInfo);
chunkProvider.completeUpdate();
final ArgumentCaptor<Event> eventArgumentCaptor = ArgumentCaptor.forClass(Event.class);
verify(blockEntity, atLeastOnce()).send(eventArgumentCaptor.capture());
final Event event = eventArgumentCaptor.getAllValues().get(0);
assertThat(event, instanceOf(OnAddedBlocks.class));
assertThat(((OnAddedBlocks) event).getBlockPositions(), hasItem(new Vector3i(1, 2, 3)));
}
use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.
the class SkipStop method apply.
@Override
public Collection<TripPattern> apply(TripPattern original) {
if (!couldMatch(original))
return Arrays.asList(original);
// figure out which stops we skip
TIntList skippedStops = new TIntArrayList();
// retained stops
// we use stop times to carry a little additional information, e.g. pickup/dropoff type.
List<StopTime> stopTimes = Lists.newArrayList();
{
int i = 0;
for (Stop stop : original.getStops()) {
if (stopId.contains(stop.getId().getId()))
skippedStops.add(i);
else {
// make a fake stop time
StopTime stopTime = new StopTime();
stopTime.setStop(stop);
stopTime.setPickupType(original.stopPattern.pickups[i]);
stopTime.setDropOffType(original.stopPattern.dropoffs[i]);
stopTimes.add(stopTime);
}
i++;
}
}
if (skippedStops.isEmpty()) {
LOG.warn("No stops found to skip on matched trip pattern {}", original);
return Arrays.asList(original);
}
if (original.getStops().size() - skippedStops.size() < 2) {
// TODO best way to handle this case?
LOG.warn("Trip with skipped stops would have less that two stops for TripPattern {}, not skipping stops", original);
return Arrays.asList(original);
}
// make the new stop pattern
StopPattern sp = new StopPattern(stopTimes);
TripPattern modified = new TripPattern(original.route, sp);
// Any trips that are not matched keep the original trip pattern, so put them here.
TripPattern originalClone = new TripPattern(original.route, original.stopPattern);
// keep track of what we have to return
boolean anyTripsMatched = false;
boolean allTripsMatched = true;
for (TripTimes tt : original.scheduledTimetable.tripTimes) {
if (!matches(tt.trip)) {
// this trip should not be modified
allTripsMatched = false;
originalClone.scheduledTimetable.addTripTimes(tt);
} else {
// This trip should be modified
anyTripsMatched = true;
modified.scheduledTimetable.addTripTimes(omitStops(tt, skippedStops.toArray()));
}
}
for (FrequencyEntry fe : original.scheduledTimetable.frequencyEntries) {
if (!matches(fe.tripTimes.trip)) {
allTripsMatched = false;
originalClone.scheduledTimetable.addFrequencyEntry(fe);
} else {
anyTripsMatched = true;
TripTimes newtt = omitStops(fe.tripTimes, skippedStops.toArray());
FrequencyEntry newfe = new FrequencyEntry(fe.startTime, fe.endTime, fe.headway, fe.exactTimes, newtt);
modified.scheduledTimetable.addFrequencyEntry(newfe);
}
}
if (!anyTripsMatched)
return Arrays.asList(original);
List<TripPattern> ret = Lists.newArrayList();
ret.add(modified);
if (!allTripsMatched)
ret.add(originalClone);
return ret;
}
use of gnu.trove.list.TIntList in project OpenTripPlanner by opentripplanner.
the class GTFSPatternHopFactory method removeRepeatedStops.
/**
* Filter out any series of stop times that refer to the same stop. This is very inefficient in
* an array-backed list, but we are assuming that this is a rare occurrence. The alternative is
* to copy every list of stop times during filtering.
*
* TODO: OBA GFTS makes the stoptime lists unmodifiable, so this will not work.
* We need to copy any modified list.
*
* @return whether any repeated stops were filtered out.
*/
private TIntList removeRepeatedStops(List<StopTime> stopTimes) {
boolean filtered = false;
StopTime prev = null;
Iterator<StopTime> it = stopTimes.iterator();
TIntList stopSequencesRemoved = new TIntArrayList();
while (it.hasNext()) {
StopTime st = it.next();
if (prev != null) {
if (prev.getStop().equals(st.getStop())) {
// This is particularly important at the last stop in a route (see issue #2220)
if (prev.getArrivalTime() == StopTime.MISSING_VALUE)
prev.setArrivalTime(st.getArrivalTime());
// prefer to replace with the departure time of this stop time, unless this stop time has no departure time
if (st.getDepartureTime() != StopTime.MISSING_VALUE)
prev.setDepartureTime(st.getDepartureTime());
it.remove();
stopSequencesRemoved.add(st.getStopSequence());
}
}
prev = st;
}
return stopSequencesRemoved;
}
Aggregations