Skip to content

Commit

Permalink
refactor: abstract edgeid2weight and related operation together
Browse files Browse the repository at this point in the history
issue: #351
  • Loading branch information
CodeBear801 committed May 28, 2020
1 parent d0f2f94 commit bd1b43a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
21 changes: 21 additions & 0 deletions integration/service/oasis/stationgraph/edge_2_weight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stationgraph

import "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"

type place2placeID struct {
from common.PlaceID
to common.PlaceID
}
type edgeID2EdgeData map[place2placeID]*common.Weight

func newEdgeID2EdgeData() edgeID2EdgeData {
return make(edgeID2EdgeData, 5000000)
}

func (edge2Weight edgeID2EdgeData) get(id place2placeID) *common.Weight {
return edge2Weight[id]
}

func (edge2Weight edgeID2EdgeData) add(id place2placeID, weight *common.Weight) {
edge2Weight[id] = weight
}
61 changes: 61 additions & 0 deletions integration/service/oasis/stationgraph/edge_2_weight_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package stationgraph

import (
"reflect"
"testing"

"github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"
"github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype"
)

func TestEdgeID2EdgeDataGetResultShouldEqualsToSetValue(t *testing.T) {
cases := []struct {
key place2placeID
value *common.Weight
}{
// case 1
{
place2placeID{
111,
222,
},
&common.Weight{
Duration: 111.0,
Distance: 222.0,
},
},
// case 2
{
place2placeID{
stationfindertype.OrigLocationID,
stationfindertype.DestLocationID,
},
&common.Weight{
Duration: 333.0,
Distance: 444.0,
},
},
}

edgeID2Data := newEdgeID2EdgeData()
for _, c := range cases {
edgeID2Data.add(c.key, c.value)
}

for _, c := range cases {
actualValue := edgeID2Data.get(c.key)
if !reflect.DeepEqual(c.value, actualValue) {
t.Errorf("Expect to get value %#v for key %#v but got %#v\n", c.value, c.key, actualValue)
}
}
}

func TestEdgeID2EdgeDataWhenGetUnsettedKeyShouldGetNil(t *testing.T) {
edgeID2Data := newEdgeID2EdgeData()
actualValue := edgeID2Data.get(place2placeID{
stationfindertype.OrigLocationID,
stationfindertype.DestLocationID})
if actualValue != nil {
t.Errorf("Expect to get nil for unsetted key but got %#v\n", actualValue)
}
}
19 changes: 7 additions & 12 deletions integration/service/oasis/stationgraph/node_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
)

type nodeID2AdjacentNodes map[nodeID][]nodeID
type place2placeID struct {
from common.PlaceID
to common.PlaceID
}
type edgeID2EdgeData map[place2placeID]*common.Weight

type nodeGraph struct {
nodeContainer *nodeContainer
Expand All @@ -31,7 +26,7 @@ func NewNodeGraph(strategy chargingstrategy.Strategy, query connectivitymap.Quer
return &nodeGraph{
nodeContainer: newNodeContainer(),
adjacentList: make(nodeID2AdjacentNodes),
edgeMetric: make(edgeID2EdgeData, 50000000),
edgeMetric: newEdgeID2EdgeData(),
startNodeID: invalidNodeID,
endNodeID: invalidNodeID,
strategy: strategy,
Expand Down Expand Up @@ -64,9 +59,9 @@ func (g *nodeGraph) AdjacentNodes(id nodeID) []nodeID {

// Edge returns edge information between given two nodes
func (g *nodeGraph) Edge(from, to nodeID) *common.Weight {
return g.edgeMetric[place2placeID{
return g.edgeMetric.get(place2placeID{
from: g.nodeContainer.nodeID2PlaceID(from),
to: g.nodeContainer.nodeID2PlaceID(to)}]
to: g.nodeContainer.nodeID2PlaceID(to)})
}

// SetStart generates start node for the nodeGraph
Expand Down Expand Up @@ -119,9 +114,9 @@ func (g *nodeGraph) createLogicalNodes(from nodeID, toPlaceID common.PlaceID, to
endNodeID := g.EndNodeID()
if toPlaceID == g.PlaceID(endNodeID) {
results = append(results, g.Node(endNodeID))
g.edgeMetric[place2placeID{
g.edgeMetric.add(place2placeID{
from: g.nodeContainer.nodeID2PlaceID(from),
to: g.nodeContainer.nodeID2PlaceID(endNodeID)}] = weight
to: g.nodeContainer.nodeID2PlaceID(endNodeID)}, weight)
return results
}

Expand All @@ -130,9 +125,9 @@ func (g *nodeGraph) createLogicalNodes(from nodeID, toPlaceID common.PlaceID, to
n := g.nodeContainer.addNode(toPlaceID, state)
results = append(results, n)

g.edgeMetric[place2placeID{
g.edgeMetric.add(place2placeID{
from: g.nodeContainer.nodeID2PlaceID(from),
to: g.nodeContainer.nodeID2PlaceID(n.id)}] = weight
to: g.nodeContainer.nodeID2PlaceID(n.id)}, weight)
}
return results
}
Expand Down

0 comments on commit bd1b43a

Please sign in to comment.