From ebd46f068fbf11ea8793e9f7982b3a5291e21398 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 26 Mar 2018 15:52:43 +0200 Subject: TomTom::Matrix#points_from_way_costs: Use array instead of set Using a set ended up not working out. I needed to be able to index into the list in `#extract_costs_to_way_costs!`, and sets aren't indexable. This is because they're supposed to be unordered, though modern Ruby implements `Set` with `Hash` under the hood, which is ordered in Ruby. I like the idea of having a data structure that automatically eliminates duplicates, but it wasn't meant to be, because for the extraction to `WayCost`s, I need an ordered list. Rather than create a new `OrderedSet` type, I just went the simple route and used an Array, eliminating the duplicates manually because I know when duplicates are supposed to occur due to the nature of the data set. Remove the `#eql?` and `#hash` methods from `TomTom::Matrix::Point`. Because we're not longer using `Set`, these methods don't need to be implemented. Refs #6222 --- lib/tom_tom/matrix.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'lib/tom_tom/matrix.rb') diff --git a/lib/tom_tom/matrix.rb b/lib/tom_tom/matrix.rb index 55568ab66..8e0331cca 100644 --- a/lib/tom_tom/matrix.rb +++ b/lib/tom_tom/matrix.rb @@ -29,23 +29,30 @@ module TomTom end def points_from_way_costs(way_costs) - points = Set.new + points = [] way_costs.each do |way_cost| departure_id, arrival_id = way_cost.id.split('-') - points.add( - TomTom::Matrix::Point.new( - way_cost.departure, - departure_id - ) + departure = TomTom::Matrix::Point.new( + way_cost.departure, + departure_id ) - points.add( - TomTom::Matrix::Point.new( - way_cost.arrival, - arrival_id - ) + arrival = TomTom::Matrix::Point.new( + way_cost.arrival, + arrival_id ) + + # Don't add duplicate coordinates. This assumes that + # `way_costs` consists of an ordered route of points where + # each departure coordinate is the same as the preceding + # arrival coordinate. + if points.empty? || + points.last.coordinates != departure.coordinates + points << departure + end + + points << arrival end points -- cgit v1.2.3