private void sortMetrics(TObjectDoubleMap<String> metrics, final List<String> activities, final TDoubleList values) { metrics.forEachEntry((s, v) -> { boolean inserted = false; for (int i = 0; i < values.size() && i < limit; i++) { if (v > values.get(i)) { values.insert(i, v); activities.add(i, s); inserted = true; break; } } if (!inserted && values.size() < limit) { activities.add(s); values.add(v); } return true; }); } }
public double[] toArray( int offset, int len ) { synchronized( mutex ) { return list.toArray( offset, len ); } } public double[] toArray( double[] dest, int offset, int len ) {
@Override public Double set( int index, Double value ) { double previous_value = list.set( index, value ); if ( previous_value == list.getNoEntryValue() ) return null; else return Double.valueOf( previous_value ); }
@Override public Double remove( int index ) { double previous_value = list.removeAt( index ); if ( previous_value == list.getNoEntryValue() ) return null; else return Double.valueOf( previous_value ); }
@Override public Double get( int index ) { double value = list.get( index ); if ( value == list.getNoEntryValue() ) return null; else return Double.valueOf( value ); }
public void add( double[] vals ) { synchronized( mutex ) { list.add( vals ); } } public void add( double[] vals, int offset, int length ) {
/** * Get average speed for set of trips that begin within the time window in meters per second. * @param trips * @param from * @param to * @return avg. speed (meters per second) */ public double getAverageSpeedForTrips (Collection<Trip> trips, LocalTime from, LocalTime to) { TDoubleList speeds = new TDoubleArrayList(); for (Trip trip : trips) { StopTime firstStopTime = feed.stop_times.ceilingEntry(Fun.t2(trip.trip_id, null)).getValue(); LocalTime tripBeginTime = LocalTime.ofSecondOfDay(firstStopTime.departure_time % 86399); // convert 24hr+ seconds to 0 - 86399 // skip trip if begin time is before or after specified time period if (tripBeginTime.isAfter(to) || tripBeginTime.isBefore(from)) { continue; } // TODO: swap straight lines for actual geometry? double speed = feed.getTripSpeed(trip.trip_id, true); if (!Double.isNaN(speed)) { speeds.add(speed); } } if (speeds.isEmpty()) return -1; return speeds.sum() / speeds.size(); }
@Override public int size() { return list.size(); }
/** * Calculates Transit center from median of coordinates of all transitStops if graph * has transit. If it doesn't it isn't calculated. (mean walue of min, max latitude and longitudes are used) * * Transit center is saved in center variable * * This speeds up calculation, but problem is that median needs to have all of latitudes/longitudes * in memory, this can become problematic in large installations. It works without a problem on New York State. */ public void calculateTransitCenter() { if (hasTransit) { TDoubleList latitudes = new TDoubleLinkedList(); TDoubleList longitudes = new TDoubleLinkedList(); Median median = new Median(); getVertices().stream() .filter(v -> v instanceof TransitStop) .forEach(v -> { latitudes.add(v.getLat()); longitudes.add(v.getLon()); }); median.setData(latitudes.toArray()); double medianLatitude = median.evaluate(); median = new Median(); median.setData(longitudes.toArray()); double medianLongitude = median.evaluate(); this.center = new Coordinate(medianLongitude, medianLatitude); } }
public double get( int index ) { synchronized( mutex ) { return list.get( index ); } } public double set( int index, double element) {
/** * Get total revenue distance (in meters) for set of trips. * @param trips * @return total trip distance (in meters) */ public double getTotalDistanceForTrips (Collection<Trip> trips) { TDoubleList distances = new TDoubleArrayList(); for (Trip trip : trips) { distances.add(feed.getTripDistance(trip.trip_id, false)); } return distances.sum(); }
public void set( int offset, double[] values ) { synchronized( mutex ) { list.set( offset, values ); } } public void set( int offset, double[] values, int valOffset, int length ) {
/** * Constructs a new {@code MovingAverage} * * @param historicalValues list of entry values * @param windowSize length over which to take the average */ public MovingAverage(TDoubleList historicalValues, double total, int windowSize) { if(windowSize <= 0) { throw new IllegalArgumentException("Window size must be > 0"); } this.windowSize = windowSize; calc = new Calculation(); calc.historicalValues = historicalValues == null || historicalValues.size() < 1 ? new TDoubleArrayList(windowSize) : historicalValues; calc.total = total != -1 ? total : calc.historicalValues.sum(); }
public TDoubleList subList( int fromIndex, int toIndex ) { synchronized( mutex ) { return new TSynchronizedDoubleList( list.subList( fromIndex, toIndex ), mutex ); } }
public double sum() { return list.sum(); }
public void insert( int offset, double[] values ) { synchronized( mutex ) { list.insert( offset, values ); } } public void insert( int offset, double[] values, int valOffset, int len ) {
/** * Replaces the current buffer data with the list of bytes in the give {@link gnu.trove.list.TDoubleList} This method arbitrarily creates data for the ByteBuffer regardless of the data type of the * vertex attribute. * * @param list to set */ public void setData(TDoubleList list) { this.buffer = CausticUtil.createByteBuffer(list.size() * DataType.DOUBLE.getByteSize()); final TDoubleIterator iterator = list.iterator(); while (iterator.hasNext()) { buffer.putDouble(iterator.next()); } }