Repo // Megrez v2.0.3 update.

This commit is contained in:
ShikiSuen 2022-08-27 19:18:18 +08:00
parent d27094976e
commit 4e8039550e
1 changed files with 42 additions and 24 deletions

View File

@ -140,6 +140,43 @@ extension Array where Element == Megrez.Compositor.Node {
/// ///
public var keys: [String] { map(\.key) } public var keys: [String] { map(\.key) }
/// (Result A, Result B)
/// Result A Result B
public var nodeBorderPointDictPair: ([Int: Int], [Int: Int]) {
// Result A Result B
var resultA = [Int: Int]()
var resultB = [Int: Int]()
var i = 0
for (j, neta) in enumerated() {
resultA[j] = i
neta.keyArray.forEach { _ in
resultB[i] = j
i += 1
}
}
resultA[resultA.count] = i
resultB[i] = resultB.count
return (resultA, resultB)
}
///
public var totalKeyCount: Int { map(\.keyArray.count).reduce(0, +) }
///
/// - Parameter cursor:
public func contextRange(ofGivenCursor cursor: Int) -> Range<Int> {
guard !isEmpty else { return 0..<0 }
let lastSpanningLength = reversed()[0].keyArray.count
var nilReturn = (totalKeyCount - lastSpanningLength)..<totalKeyCount
if cursor >= totalKeyCount { return nilReturn } //
let cursor = Swift.max(0, cursor) //
nilReturn = cursor..<cursor
guard let rearNodeID = nodeBorderPointDictPair.1[cursor] else { return nilReturn } // nilReturn
guard let rearIndex = nodeBorderPointDictPair.0[rearNodeID] else { return nilReturn } // nilReturn
guard let frontIndex = nodeBorderPointDictPair.0[rearNodeID + 1] else { return nilReturn } // nilReturn
return rearIndex..<frontIndex
}
/// ///
/// - Parameters: /// - Parameters:
/// - cursor: /// - cursor:
@ -147,30 +184,11 @@ extension Array where Element == Megrez.Compositor.Node {
/// - Returns: /// - Returns:
public func findNode(at cursor: Int, target outCursorPastNode: inout Int) -> Megrez.Compositor.Node? { public func findNode(at cursor: Int, target outCursorPastNode: inout Int) -> Megrez.Compositor.Node? {
guard !isEmpty else { return nil } guard !isEmpty else { return nil }
let cursor = Swift.max(0, Swift.min(cursor, keys.count)) let cursor = Swift.min(Swift.max(0, cursor), totalKeyCount - 1) //
let range = contextRange(ofGivenCursor: cursor)
if cursor == 0, let theFirst = first { outCursorPastNode = range.upperBound
outCursorPastNode = theFirst.spanLength guard let rearNodeID = nodeBorderPointDictPair.1[cursor] else { return nil }
return theFirst return count - 1 >= rearNodeID ? self[rearNodeID] : nil
}
//
if cursor >= keys.count - 1, let theLast = last {
outCursorPastNode = keys.count
return theLast
}
var accumulated = 0
for neta in self {
accumulated += neta.spanLength
if accumulated > cursor {
outCursorPastNode = accumulated
return neta
}
}
//
return nil
} }
/// ///