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,19 +147,17 @@ 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 {
let candidates = theNode.candidates() continue
// Reset the candidate-fixed state of every node at the location. }
nodesCrossingOrEndingAt(location: location)[index].node?.resetCandidate() let candidates = theNode.candidates()
// Reset the candidate-fixed state of every node at the location.
for (i, candidate) in candidates.enumerated() { theNode.resetCandidate()
if candidate.value == value { for (i, candidate) in candidates.enumerated() {
nodesCrossingOrEndingAt(location: location)[index].node?.selectFloatingCandidateAt( if candidate.value == value {
index: i, score: overridingScore theNode.selectFloatingCandidateAt(index: i, score: overridingScore)
) break
break
}
} }
} }
} }