Skip to content

Commit

Permalink
fix: implement function of appendIntoSortedSlice
Browse files Browse the repository at this point in the history
issue: #338
  • Loading branch information
CodeBear801 committed May 13, 2020
1 parent 57754dd commit 2ed8804
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stationconnquerier

import (
"sort"
"strconv"

"github.com/Telenav/osrm-backend/integration/api/nav"
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 2ed8804

Please sign in to comment.