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 (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) { if let theNode = span.nodeOf(length: j) {
vertexSpans[i].append(.init(node: theNode)) vertexSpans[i].append(.init(node: theNode))
} }

View File

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