From 2ed8804b7c4dc7caa05cc93e9b12bee1712b8bc1 Mon Sep 17 00:00:00 2001 From: codebear801 Date: Tue, 12 May 2020 18:43:17 -0700 Subject: [PATCH] fix: implement function of appendIntoSortedSlice issue: https://github.com/Telenav/osrm-backend/issues/338 --- .../station_conn_querier.go | 15 +- .../station_conn_querier_test.go | 151 ++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) diff --git a/integration/service/oasis/stationconnquerier/station_conn_querier.go b/integration/service/oasis/stationconnquerier/station_conn_querier.go index cd967d68a76..bc934c10697 100644 --- a/integration/service/oasis/stationconnquerier/station_conn_querier.go +++ b/integration/service/oasis/stationconnquerier/station_conn_querier.go @@ -1,6 +1,7 @@ package stationconnquerier import ( + "sort" "strconv" "github.com/Telenav/osrm-backend/integration/api/nav" @@ -36,6 +37,7 @@ func New(stationFinder spatialindexer.Finder, stationRanker spatialindexer.Ranke querier.connectStartIntoStationGraph(stationFinder, stationRanker, start, currEnergyLevel) querier.connectEndIntoStationGraph(stationFinder, stationRanker, end, maxEnergyLevel) + glog.Info("Generate StationConnectivityQuerier.\n") return querier } @@ -57,6 +59,7 @@ func (querier *StationConnectivityQuerier) connectStartIntoStationGraph(stationF } querier.reachableStationsByStart = reachableStationsByStart + glog.Infof("Add %d stations connects Start.\n", len(querier.reachableStationsByStart)) } func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFinder spatialindexer.Finder, stationRanker spatialindexer.Ranker, @@ -76,6 +79,7 @@ func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFin } querier.reachableStationToEnd = reachableStationToEnd + glog.Infof("Add %d stations connects End node.\n", len(querier.reachableStationToEnd)) } // NearByStationQuery finds near by stations by given stationID and return them in recorded sequence @@ -147,8 +151,17 @@ func (querier *StationConnectivityQuerier) isStationConnectsToEnd(stationID stri func (querier *StationConnectivityQuerier) connectEndIntoGraph(stationID string, results []*connectivitymap.QueryResult) []*connectivitymap.QueryResult { if queryResult4End, ok := querier.reachableStationToEnd[stationID]; ok { - results = append(results, queryResult4End) + return appendIntoSortedSlice(queryResult4End, results) } + return results +} +func appendIntoSortedSlice(item *connectivitymap.QueryResult, results []*connectivitymap.QueryResult) []*connectivitymap.QueryResult { + insertIndex := sort.Search(len(results), func(i int) bool { + return results[i].Distance > item.Distance + }) + results = append(results, nil) + copy(results[insertIndex+1:], results[insertIndex:]) + results[insertIndex] = item return results } diff --git a/integration/service/oasis/stationconnquerier/station_conn_querier_test.go b/integration/service/oasis/stationconnquerier/station_conn_querier_test.go index 0d30594cd6f..3b19209a638 100644 --- a/integration/service/oasis/stationconnquerier/station_conn_querier_test.go +++ b/integration/service/oasis/stationconnquerier/station_conn_querier_test.go @@ -11,6 +11,152 @@ import ( "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" ) +func TestAppendIntoSortedSlice(t *testing.T) { + cases := []struct { + sortedArray []*connectivitymap.QueryResult + itemToBeInserted *connectivitymap.QueryResult + expectedArray []*connectivitymap.QueryResult + }{ + // case: insert into empty array + { + nil, + &connectivitymap.QueryResult{ + StationID: "3", + StationLocation: mockStation3Location, + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, + []*connectivitymap.QueryResult{ + { + StationID: "3", + StationLocation: mockStation3Location, + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, + }, + }, + + // case: insert to the head of sorted array + { + []*connectivitymap.QueryResult{ + { + StationID: "3", + StationLocation: mockStation3Location, + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, + { + StationID: "2", + StationLocation: mockStation2Location, + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, + { + StationID: "1", + StationLocation: mockStation1Location, + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, + }, + &connectivitymap.QueryResult{ + StationID: "4", + StationLocation: mockStation4Location, + Distance: 222.0, + Duration: 1.0, + }, + []*connectivitymap.QueryResult{ + { + StationID: "4", + StationLocation: mockStation4Location, + Distance: 222.0, + Duration: 1.0, + }, + { + StationID: "3", + StationLocation: mockStation3Location, + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, + { + StationID: "2", + StationLocation: mockStation2Location, + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, + { + StationID: "1", + StationLocation: mockStation1Location, + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, + }, + }, + // case: insert into sorted array + { + []*connectivitymap.QueryResult{ + { + StationID: "3", + StationLocation: mockStation3Location, + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, + { + StationID: "2", + StationLocation: mockStation2Location, + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, + { + StationID: "1", + StationLocation: mockStation1Location, + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, + }, + &connectivitymap.QueryResult{ + StationID: "4", + StationLocation: mockStation4Location, + Distance: 4623.0, + Duration: 1.0, + }, + []*connectivitymap.QueryResult{ + { + StationID: "3", + StationLocation: mockStation3Location, + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, + { + StationID: "4", + StationLocation: mockStation4Location, + Distance: 4623.0, + Duration: 1.0, + }, + { + StationID: "2", + StationLocation: mockStation2Location, + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, + { + StationID: "1", + StationLocation: mockStation1Location, + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, + }, + }, + } + + for _, c := range cases { + actualResult := appendIntoSortedSlice(c.itemToBeInserted, c.sortedArray) + if !reflect.DeepEqual(actualResult, c.expectedArray) { + t.Errorf("Incorrect result expect %+v but got %+v\n", c.expectedArray, actualResult) + } + } + +} + /* Construct graph as follows @@ -226,6 +372,11 @@ var mockStation3Location = &nav.Location{ Lon: -121.977384, } +var mockStation4Location = &nav.Location{ + Lat: 11.11, + Lon: -22.22, +} + func (querier *mockPlaceLocationQuerier) GetLocation(placeID string) *nav.Location { switch placeID { case "1":