Files
AzooKeyKanaKanjiConverter/Tests/KanaKanjiConverterModuleWithDefaultDictionaryTests/ExperimentalTests/InputGraph/InputGraphProtocol.swift
Miwa / Ensan 0795b8cf84 [Experimental] ConvertGraphを実装し、その上での完全一致変換を実装 (#47)
* ConvertGraphを実装し、その上での完全一致変換を実装

* 名前空間を汚染していたので修正

* Implementation completed (without test)

* move directory to use default dictionary

* fix implementations to enable conversion

* add test cases

* Backward searchで発見された候補を明示的に削除

* fix tests

* simplify
2024-02-24 23:21:44 +09:00

60 lines
1.6 KiB
Swift

//
// InputGraphProtocol.swift
//
//
// Created by miwa on 2024/02/23.
//
import Foundation
protocol InputGraphNodeProtocol {
var displayedTextRange: InputGraphStructure.Range { get set }
var inputElementsRange: InputGraphStructure.Range { get set }
}
protocol InputGraphProtocol {
associatedtype Node: InputGraphNodeProtocol
var nodes: [Node] { get set }
var structure: InputGraphStructure { get set }
}
extension InputGraphProtocol {
var root: Node {
nodes[0]
}
func nextIndices(for node: Node) -> IndexSet {
self.structure.nextIndices(
displayedTextEndIndex: node.displayedTextRange.endIndex,
inputElementsEndIndex: node.inputElementsRange.endIndex
)
}
func next(for node: Node) -> [Node] {
nextIndices(for: node).map{ self.nodes[$0] }
}
func prevIndices(for node: Node) -> IndexSet {
self.structure.prevIndices(
displayedTextStartIndex: node.displayedTextRange.startIndex,
inputElementsStartIndex: node.inputElementsRange.startIndex
)
}
func prev(for node: Node) -> [Node] {
prevIndices(for: node).map{ self.nodes[$0] }
}
mutating func remove(at index: Int) {
assert(index != 0, "Node at index 0 is root and must not be removed.")
self.structure.remove(at: index)
}
mutating func insert(_ node: Node) {
var nodes = self.nodes
let _ = self.structure.insert(node, nodes: &nodes, displayedTextRange: node.displayedTextRange, inputElementsRange: node.inputElementsRange)
self.nodes = consume nodes
}
}