Megrez // Turn SpanUnit.maxLength into a dynamic variable.

This commit is contained in:
ShikiSuen 2023-03-07 14:53:05 +08:00
parent f8abcd59e4
commit 9da9c12809
2 changed files with 12 additions and 31 deletions

View File

@ -24,7 +24,7 @@ public extension Megrez.Compositor {
}
for (i, span) in spans.enumerated() {
for j in 1 ... span.maxLength {
for j in 1 ... max(span.maxLength, 1) {
if let theNode = span.nodeOf(length: j) {
vertexSpans[i].append(.init(node: theNode))
}

View File

@ -7,10 +7,10 @@ extension Megrez.Compositor {
///
public class SpanUnit {
/// nil
public var nodes: [Node?] = []
public var nodes: [Int: Node] = [:]
///
///
public private(set) var maxLength = 0
public var maxLength: Int { nodes.keys.max() ?? 0 }
/// Megrez.Compositor.maxSpanLength
private var maxSpanLength: Int { Megrez.Compositor.maxSpanLength }
@ -24,19 +24,15 @@ extension Megrez.Compositor {
/// 0
public func clear() {
nodes = .init(repeating: nil, count: maxSpanLength)
maxLength = 0
nodes.removeAll()
}
///
/// - Parameter node:
/// - Returns:
@discardableResult public func append(node: Node) -> Bool {
guard allowedLengths.contains(node.spanLength) else {
return false
}
nodes[node.spanLength - 1] = node
maxLength = max(maxLength, node.spanLength)
guard allowedLengths.contains(node.spanLength) else { return false }
nodes[node.spanLength] = node
return true
}
@ -46,32 +42,17 @@ extension Megrez.Compositor {
///
/// - Parameter node:
public func nullify(node givenNode: Node) {
nodes.enumerated().forEach { index, theNode in
guard theNode == givenNode else { return }
nodes[index] = nil
}
let spanLength = givenNode.spanLength
nodes[spanLength] = nil
}
///
/// - Parameter length:
/// - Returns:
@discardableResult public func dropNodesOfOrBeyond(length: Int) -> Bool {
guard allowedLengths.contains(length) else {
return false
}
for i in length ... maxSpanLength {
guard (0 ..< nodes.count).contains(i - 1) else { continue } //
nodes[i - 1] = nil
}
maxLength = 0
guard length > 1 else { return false }
let maxR = length - 2
for i in 0 ... maxR {
guard (0 ..< nodes.count).contains(maxR - i) else { continue } //
if nodes[maxR - i] == nil { continue }
maxLength = maxR - i + 1
break
}
guard allowedLengths.contains(length) else { return false }
let length = min(length, maxSpanLength)
(length ... maxSpanLength).forEach { nodes[$0] = nil }
return true
}
@ -80,7 +61,7 @@ extension Megrez.Compositor {
/// - Returns:
public func nodeOf(length: Int) -> Node? {
guard allowedLengths.contains(length) else { return nil }
return nodes[length - 1]
return nodes[length]
}
}