@@ -17,10 +17,7 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
17
17
18
18
def heapOrdering : HeapOrdering [Key ] = coreSupport.heapOrdering
19
19
20
- def heapKeyForLabel = {
21
- case Some (nextStep) => coreSupport.heapKeyForLabel(nextStep.weight)
22
- case None => coreSupport.heapOrdering.AlwaysBottom
23
- }
20
+ def heapKeyForLabel : Label => Key = _.fold(coreSupport.heapOrdering.AlwaysBottom )(x => coreSupport.heapKeyForLabel(x.weight))
24
21
25
22
// todo could be a Seq instead if just used in Dijkstra's algorithm
26
23
case class FirstSteps (weight: CoreLabel ,choices: Set [Node ]) extends FirstStepsTrait [Node , CoreLabel ] {
@@ -51,7 +48,7 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
51
48
52
49
def convertEdgeToLabel [EdgeLabel ](coreLabelForEdge: (Node ,Node ,EdgeLabel )=> CoreLabel )
53
50
(start : Node , end : Node , edgeLabel : EdgeLabel ): Label = {
54
- Some (FirstSteps (coreLabelForEdge(start,end,edgeLabel),Set (end)))
51
+ Option (FirstSteps (coreLabelForEdge(start,end,edgeLabel),Set (end)))
55
52
}
56
53
57
54
def convertEdgeToLabelFunc [EdgeLabel ](coreLabelForEdge: (Node ,Node ,EdgeLabel )=> CoreLabel ): ((Node ,Node ,EdgeLabel ) => Label ) = convertEdgeToLabel(coreLabelForEdge)
@@ -62,14 +59,11 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
62
59
// val coreSemiring = coreSupport.semiring
63
60
64
61
def inDomain (label : Label ): Boolean = {
65
- label match {
66
- case Some (steps: FirstSteps ) => coreSupport.semiring.inDomain(steps.weight)
67
- case None => true
68
- }
62
+ label.forall(steps => coreSupport.semiring.inDomain(steps.weight))
69
63
}
70
64
71
65
// identity and annihilator
72
- val I = Some (FirstSteps (coreSupport.semiring.I ,Set [Node ]()))
66
+ val I = Option (FirstSteps (coreSupport.semiring.I ,Set [Node ]()))
73
67
val O = None
74
68
75
69
def summary (fromThroughToLabel: Label ,currentLabel: Label ): Label = {
@@ -80,7 +74,7 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
80
74
val fromThroughToSteps : FirstStepsTrait [Node ,CoreLabel ] = fromThroughToLabel.get
81
75
val summ = coreSupport.semiring.summary(fromThroughToSteps.weight,currentSteps.weight)
82
76
if ((summ== fromThroughToSteps.weight)&& (summ== currentSteps.weight)) {
83
- Some ( new FirstSteps (currentSteps.weight,
77
+ Option ( FirstSteps (currentSteps.weight,
84
78
currentSteps.choices ++ fromThroughToSteps.choices))
85
79
}
86
80
else if (summ== fromThroughToSteps.weight) fromThroughToLabel
@@ -101,8 +95,8 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
101
95
val choices : Set [Node ] = if (fromThroughLabel == I ) throughToSteps.choices
102
96
else fromThroughSteps.choices
103
97
104
- Some ( new FirstSteps (coreSupport.semiring.extend(fromThroughSteps.weight,throughToSteps.weight),
105
- choices))
98
+ Option ( FirstSteps (coreSupport.semiring.extend(fromThroughSteps.weight,throughToSteps.weight),
99
+ choices))
106
100
}
107
101
else O
108
102
}
@@ -114,26 +108,21 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
114
108
115
109
def leastPathsOfInnerNodes (fromInner: Option [leastPathDigraph.InnerNodeType ],
116
110
toInner: Option [leastPathDigraph.InnerNodeType ]): Seq [Path ] = {
117
- (fromInner,toInner) match {
118
- case (Some (f),Some (t)) => {
119
- val label : Label = leastPathDigraph.label(f,t)
120
- label match {
121
- case Some (firstStep) => {
122
- if (firstStep.choices == Set .empty) Seq (Seq (t)) // No further steps. from should be to and the label should be I
123
- else {
124
- for (choice <- firstStep.choices.to[Seq ]) yield {
125
- val tails : Seq [Path ] = leastPathsOfInnerNodes(leastPathDigraph.innerNode(choice), toInner)
126
- for (tail <- tails) yield {
127
- Seq (f +: tail)
128
- }
129
- }.flatten
130
- }.flatten
131
- }
132
- case None => Seq () // No path from one to the other
133
- }
134
- }
135
- case _ => Seq () // One node or the other isn't in the graph
136
- }
111
+ val fromToOption : Option [(leastPathDigraph.InnerNodeType , leastPathDigraph.InnerNodeType )] = for (f <- fromInner; t <- toInner) yield (f, t)
112
+ fromToOption.fold(Seq .empty[Path ])(fromTo => {
113
+ val label : Label = leastPathDigraph.label(fromTo._1, fromTo._2)
114
+ label.fold(Seq .empty[Path ])(firstSteps => {
115
+ if (firstSteps.choices == Set .empty) Seq (Seq (fromTo._2)) // No further steps. from should be to and the label should be I
116
+ else {
117
+ for (choice <- firstSteps.choices.to[Seq ]) yield {
118
+ val tails : Seq [Path ] = leastPathsOfInnerNodes(leastPathDigraph.innerNode(choice), toInner)
119
+ for (tail <- tails) yield {
120
+ Seq (fromTo._1 +: tail)
121
+ }
122
+ }.flatten
123
+ }.flatten
124
+ })
125
+ })
137
126
}
138
127
139
128
val fromInner = leastPathDigraph.innerNode(from)
@@ -152,18 +141,15 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
152
141
// todo capture visited nodes and don't revisit them, by taking innerFrom as a Set, pulling out bits, passing in Sets of novel nodes to visit, and passing around another set of nodes already visited.
153
142
def recurse (innerFrom: labelGraph.InnerNodeType ,innerTo: labelGraph.InnerNodeType ): Set [(labelGraph.InnerNodeType ,labelGraph.InnerNodeType ,Label )] = {
154
143
val label : Label = labelGraph.label(innerFrom,innerTo)
155
- label match {
156
- case Some (firstSteps) => {
157
144
158
- val innerChoices = firstSteps.choices.map(choice => labelGraph.innerNode(choice).get)
159
- val closeEdges : Set [(labelGraph.InnerNodeType ,labelGraph.InnerNodeType ,Label )] = innerChoices.map((innerFrom,_,label))
145
+ label.fold(Set .empty[(labelGraph.InnerNodeType ,labelGraph.InnerNodeType ,Label )])(firstSteps => {
146
+ val innerChoices = firstSteps.choices.map(choice => labelGraph.innerNode(choice).get)
147
+ val closeEdges : Set [(labelGraph.InnerNodeType ,labelGraph.InnerNodeType ,Label )] = innerChoices.map((innerFrom,_,label))
160
148
161
- val farEdges : Set [(labelGraph.InnerNodeType ,labelGraph.InnerNodeType ,Label )] = innerChoices.map(recurse(_,innerTo)).flatten
149
+ val farEdges : Set [(labelGraph.InnerNodeType ,labelGraph.InnerNodeType ,Label )] = innerChoices.map(recurse(_,innerTo)).flatten
162
150
163
- closeEdges ++ farEdges
164
- }
165
- case None => Set .empty
166
- }
151
+ closeEdges ++ farEdges
152
+ })
167
153
}
168
154
val innerFrom = labelGraph.innerNode(from).getOrElse(throw new IllegalArgumentException (s " $from not in labelGraph " ))
169
155
0 commit comments