Megrez // Refactor certain functions in Grid and Walker.

This commit is contained in:
ShikiSuen 2022-05-17 00:19:25 +08:00
parent 9127a2bb2e
commit dd317455e2
2 changed files with 38 additions and 19 deletions

View File

@ -31,6 +31,25 @@ extension Megrez {
mutGrid = grid mutGrid = grid
} }
public func walk(
at location: Int,
score accumulatedScore: Double = 0.0,
nodesLimit: Int = 0,
balanced: Bool = false
) -> [NodeAnchor] {
var arrReturn: [NodeAnchor] = []
let arrReversedSource = reverseWalk(
at: location, score: accumulatedScore,
nodesLimit: nodesLimit, balanced: balanced
).reversed()
for neta in arrReversedSource {
arrReturn.append(neta)
}
return arrReturn
}
public func reverseWalk( public func reverseWalk(
at location: Int, at location: Int,
score accumulatedScore: Double = 0.0, score accumulatedScore: Double = 0.0,

View File

@ -128,15 +128,17 @@ extension Megrez {
public func fixNodeSelectedCandidate(location: Int, value: String) -> NodeAnchor { public func fixNodeSelectedCandidate(location: Int, value: String) -> NodeAnchor {
var node = NodeAnchor() var node = NodeAnchor()
for (index, nodeAnchor) in nodesCrossingOrEndingAt(location: location).enumerated() { for nodeAnchor in nodesCrossingOrEndingAt(location: location) {
guard let theNode = nodeAnchor.node else {
continue
}
let candidates = theNode.candidates()
// Reset the candidate-fixed state of every node at the location. // Reset the candidate-fixed state of every node at the location.
let candidates = nodeAnchor.node?.candidates() ?? [] theNode.resetCandidate()
nodesCrossingOrEndingAt(location: location)[index].node?.resetCandidate()
for (i, candidate) in candidates.enumerated() { for (i, candidate) in candidates.enumerated() {
if candidate.value == value { if candidate.value == value {
nodesCrossingOrEndingAt(location: location)[index].node?.selectCandidateAt(index: i) theNode.selectCandidateAt(index: i)
node = nodesCrossingOrEndingAt(location: location)[index] node = nodeAnchor
break break
} }
} }
@ -145,17 +147,16 @@ extension Megrez {
} }
public func overrideNodeScoreForSelectedCandidate(location: Int, value: String, overridingScore: Double) { public func overrideNodeScoreForSelectedCandidate(location: Int, value: String, overridingScore: Double) {
for (index, nodeAnchor) in nodesCrossingOrEndingAt(location: location).enumerated() { for nodeAnchor in nodesCrossingOrEndingAt(location: location) {
if let theNode = nodeAnchor.node { guard let theNode = nodeAnchor.node else {
continue
}
let candidates = theNode.candidates() let candidates = theNode.candidates()
// Reset the candidate-fixed state of every node at the location. // Reset the candidate-fixed state of every node at the location.
nodesCrossingOrEndingAt(location: location)[index].node?.resetCandidate() theNode.resetCandidate()
for (i, candidate) in candidates.enumerated() { for (i, candidate) in candidates.enumerated() {
if candidate.value == value { if candidate.value == value {
nodesCrossingOrEndingAt(location: location)[index].node?.selectFloatingCandidateAt( theNode.selectFloatingCandidateAt(index: i, score: overridingScore)
index: i, score: overridingScore
)
break break
} }
} }
@ -163,4 +164,3 @@ extension Megrez {
} }
} }
} }
}