Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
Use property forwarding from Entry to ItemID
Browse files Browse the repository at this point in the history
Progress on #15
  • Loading branch information
dabrahams committed Dec 30, 2022
1 parent 842c667 commit 4136fec
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
14 changes: 7 additions & 7 deletions Sources/Lotsawa/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ extension Chart {

/// Returns `true` iff `lhs` should precede `rhs` in a derivation set.
public static func < (lhs: Self, rhs: Self) -> Bool {
(lhs.item.key, lhs.mainstemIndexStorage) < (rhs.item.key, rhs.mainstemIndexStorage)
(lhs.key, lhs.mainstemIndexStorage) < (rhs.key, rhs.mainstemIndexStorage)
}

subscript<Target>(dynamicMember m: KeyPath<ItemID, Target>) -> Target {
Expand Down Expand Up @@ -327,9 +327,9 @@ extension Chart {
let ithSet = i == currentEarleme ? currentEarleySet : earleySet(i)
let k = ItemID.transitionKey(s)

let j = ithSet.partitionPoint { d in d.item.transitionKey >= k }
let j = ithSet.partitionPoint { d in d.transitionKey >= k }
let items = ithSet[j...]
return items.prefix(while: { x in x.item.symbolID == s.id })
return items.prefix(while: { x in x.symbolID == s.id })
}

/// Returns the items in Earley set `i` whose use is triggered by the recognition of `s`.
Expand All @@ -346,9 +346,9 @@ extension Chart {
let ithSet = earleySet(extent.upperBound)
let k = ItemID.completionKey(lhs, origin: extent.lowerBound)

let j = ithSet.partitionPoint { d in d.item.key >= k }
let j = ithSet.partitionPoint { d in d.key >= k }
let r0 = ithSet[j...]
let r = r0.prefix(while: { x in x.item.lhs == lhs && x.item.origin == extent.lowerBound })
let r = r0.prefix(while: { x in x.lhs == lhs && x.origin == extent.lowerBound })
return r
}

Expand Down Expand Up @@ -386,7 +386,7 @@ extension Chart {
/// Injects a Leo item memoizing `x` with transition symbol `t` before entries[i].item, returning
/// true if it was not already present.
mutating func insertLeo(_ leo: Entry, at i: Int) -> Bool {
assert(leo.item.isLeo)
assert(leo.isLeo)
if entries[i].item == leo.item {
// FIXME: can we prove we never arrive here? Should be possible.
assert(
Expand Down Expand Up @@ -475,6 +475,6 @@ extension Chart {
{

let predecessors = transitionEntries(on: s, inEarleySet: i)
return predecessors.first.map { $0.item.isLeo ? predecessors : nil } ?? nil
return predecessors.first.map { $0.isLeo ? predecessors : nil } ?? nil
}
}
12 changes: 6 additions & 6 deletions Sources/Lotsawa/Forest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ extension Forest {
public mutating func requireCompletion(
_ c: Chart.Entry, inEarleme i: SourcePosition
) -> Bool {
assert(c.item.isCompletion, "requiring non-completion \(c)")
assert(c.isCompletion, "requiring non-completion \(c)")
let setI = chart.earleySet(i)
let p = setI.partitionPoint { $0 >= c }
/// Derivation already in the chart? We're done
Expand All @@ -125,7 +125,7 @@ extension Forest {
let r0 = setI[p...].first?.item == c.item || setI[..<p].last?.item == c.item

return mutate(
&leoCompletions[.init(locus: c.item.origin..<i, lhs: c.item.lhs!), default: []]
&leoCompletions[.init(locus: c.origin..<i, lhs: c.lhs!), default: []]
) { leoSet in
let q = leoSet.partitionPoint { $0 >= c }
// Derivation already in the leoSet? We're done
Expand All @@ -141,8 +141,8 @@ extension Forest {
public mutating func collectLeoCompletions(
causing top: Chart.Entry, endingAt endEarleme: SourcePosition
) {
assert(top.item.isCompletion, "collecting leo completions on non-completion \(top)")
guard let lim0Index = top.mainstemIndex, chart.entries[lim0Index].item.isLeo else { return }
assert(top.isCompletion, "collecting leo completions on non-completion \(top)")
guard let lim0Index = top.mainstemIndex, chart.entries[lim0Index].isLeo else { return }
var workingLIMIndex = lim0Index
while true {
let workingBaseIndex = workingLIMIndex + 1
Expand All @@ -164,7 +164,7 @@ extension Forest {
collectLeoCompletions(causing: top, endingAt: locus.upperBound)
}
let storedCompletionsWithEarleyMainstem
= storedCompletions.lazy.filter { [chart] in chart.entries[$0.mainstemIndex!].item.isEarley }
= storedCompletions.lazy.filter { [chart] in chart.entries[$0.mainstemIndex!].isEarley }

let leos = leoCompletions[.init(locus: locus, lhs: lhs), default: []]

Expand All @@ -180,7 +180,7 @@ extension Forest {
func first(of d: DerivationSet.Storage) -> Derivation {
.init(
path: d, domain: self,
rule: grammar.rule(containing: d.completions.first!.item.dotPosition))
rule: grammar.rule(containing: d.completions.first!.dotPosition))
}
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/Lotsawa/Recognizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension Recognizer {

mutating func insert(_ newEntry: Chart.Entry) -> Bool {
if !chart.insert(newEntry) { return false }
if leoPositions.contains(newEntry.item.dotPosition) {
if leoPositions.contains(newEntry.dotPosition) {
self.leoCandidateFound = true
}
return true
Expand All @@ -75,14 +75,14 @@ extension Recognizer {
let mainstems = chart.transitionEntries(on: s, inEarleySet: origin)

if let head = mainstems.first,
let p = head.item.memoizedPenultIndex
let p = head.memoizedPenultIndex
{
derive(
.init(item: chart.entries[p].item.advanced(in: g), mainstemIndex: mainstems.startIndex))
}
else {
assert(
mainstems.allSatisfy(\.item.isEarley),
mainstems.allSatisfy(\.isEarley),
"Leo item is not first in mainstems.")

let transitionItemIndices
Expand All @@ -101,7 +101,7 @@ extension Recognizer {
let s = g.recognized(at: x.dotPosition + 1)!

let mainstems = chart.transitionEntries(on: s, inEarleySet: x.origin)
if let head = mainstems.first, head.item.isLeo {
if let head = mainstems.first, head.isLeo {
return mainstems.startIndex
}
return nil
Expand All @@ -110,14 +110,14 @@ extension Recognizer {
/// Ensures that `x` is represented in the current derivation set, and draws any consequent
/// conclusions.
private mutating func derive(_ x: Chart.Entry) {
assert(!x.item.isLeo)
assert(!x.isLeo)
if !insert(x) { return }

if let t = x.item.transitionSymbol {
if let t = x.transitionSymbol {
predict(t)
}
else { // it's complete
discover(g.recognized(at: x.item.dotPosition)!, startingAt: x.item.origin)
discover(g.recognized(at: x.dotPosition)!, startingAt: x.origin)
}
}

Expand All @@ -139,14 +139,14 @@ extension Recognizer {
// If the dot is in a Leo position and the transition symbol was unique in this set
if leoPositions.contains(x.dotPosition)
&& (endOfItem == chart.currentEarleySet.endIndex
|| chart.currentEarleySet[endOfItem].item.transitionSymbol != t)
|| chart.currentEarleySet[endOfItem].transitionSymbol != t)
{
let l: Chart.Entry

if let p = leoPredecessorIndex(x) {
l = .init(
item: .init(
memoizingItemIndex: chart.entries[p].item.memoizedPenultIndex!, transitionSymbol: t),
memoizingItemIndex: chart.entries[p].memoizedPenultIndex!, transitionSymbol: t),
mainstemIndex: p)
}
else {
Expand All @@ -161,7 +161,7 @@ extension Recognizer {
else {
// Else skip over everything with this transition symbol
i = chart.currentEarleySet[endOfItem...].prefix {
$0.item.transitionSymbol == t
$0.transitionSymbol == t
}.endIndex
}
}
Expand Down
14 changes: 7 additions & 7 deletions Tests/LotsawaTests/ChartInternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class ChartInternalTests: XCTestCase {

let chart = r.base.chart
let top3 = try chart.completions(of: sum, over: 0..<3).checkedOnlyElement()
XCTAssert(top3.item.isCompletion)
let topRuleID = g.raw.rule(containing: top3.item.dotPosition)
XCTAssert(top3.isCompletion)
let topRuleID = g.raw.rule(containing: top3.dotPosition)
let topRule = g.raw.storedRule(topRuleID)
XCTAssertEqual(topRule.lhs, sum)
XCTAssertEqual(Array(topRule.rhs), [sum, additive, product])
Expand All @@ -104,20 +104,20 @@ class ChartInternalTests: XCTestCase {

let rhsProduct = try chart.completions(of: product, over: 2..<3)
.checkedOnlyElement()
XCTAssert(rhsProduct.item.isCompletion)
XCTAssert(rhsProduct.isCompletion)

let top2 = try chart.earleyMainstem(of: top3).checkedOnlyElement()
XCTAssertEqual(g.raw.rule(containing: top2.item.dotPosition), topRuleID)
XCTAssertEqual(g.raw.rule(containing: top2.dotPosition), topRuleID)

XCTAssertEqual(top2.item.transitionSymbol, product)
XCTAssertEqual(top2.transitionSymbol, product)

let top1 = try chart.earleyMainstem(of: top2).checkedOnlyElement()

XCTAssertEqual(g.raw.rule(containing: top1.item.dotPosition), topRuleID)
XCTAssertEqual(g.raw.rule(containing: top1.dotPosition), topRuleID)

let top0 = try chart.earleyMainstem(of: top1).checkedOnlyElement()

XCTAssertEqual(g.raw.rule(containing: top0.item.dotPosition), topRuleID)
XCTAssertEqual(g.raw.rule(containing: top0.dotPosition), topRuleID)

// Could explore more, but it's time to create a better abstraction.

Expand Down
10 changes: 5 additions & 5 deletions Tests/LotsawaTests/Utils/DebugChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ extension DebugChart: CustomStringConvertible {
while let head = remainingDerivations.first {
let itemDerivations = remainingDerivations.prefix { x in x.item == head.item }
result += "\(itemDerivations.startIndex): "
if !head.item.isLeo {
result.append("<\(head.item.origin)> ")
if !head.isLeo {
result.append("<\(head.origin)> ")
}
if head.mainstemIndex != nil {
result.append(
"{\(itemDerivations.map { String($0.mainstemIndex!) }.joined(separator: ", "))} ")
}

if head.item.isLeo {
result += "L(\(head.item.memoizedPenultIndex!)) •\(head.item.transitionSymbol!)"
if head.isLeo {
result += "L(\(head.memoizedPenultIndex!)) •\(head.transitionSymbol!)"
}
else {
result += language.dottedText(head.item.dotPosition)
result += language.dottedText(head.dotPosition)
}
result += "\n"
remainingDerivations.removeFirst(itemDerivations.count)
Expand Down

0 comments on commit 4136fec

Please sign in to comment.