Skip to content

Commit

Permalink
Ensures parser output is deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpatrickmorgan committed Jan 16, 2019
1 parent cddeea0 commit f1b70da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/App/Models/Parser Models/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension Color {
wysteria,
pumpkin,
]
return colors[index % colors.count]
return colors[abs(index) % colors.count]
}

static let defaultColor = turquoise
Expand Down
23 changes: 19 additions & 4 deletions Sources/App/Models/Parser Models/ConstraintGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct ConstraintGroup {
for (i, item) in layoutItems.enumerated() {
// Adding a seed value ensures the color scheme can differ per input while
// remaining deterministic.
let color = Color.flatColor(index: i + abs(seed))
let color = Color.flatColor(index: i + seed)

let requiresUniquing = (counts[item.prettyName] ?? 0) > 1
let uniquingSuffix = requiresUniquing ? uniquingSuffixes[i] : ""
Expand All @@ -41,7 +41,7 @@ struct ConstraintGroup {

self.raw = raw
self.constraints = constraints
let layoutItems = Set(constraints.flatMap { $0.layoutItems })
let layoutItems = constraints.flatMap { $0.layoutItems }.removingDuplicates()
self.annotations = ConstraintGroup.annotations(for: Array(layoutItems), seed: raw.count)
}
}
Expand All @@ -52,7 +52,22 @@ extension ConstraintGroup {
return constraints.contains(where: { $0.origin == .autoresizingMask })
}

var footnotes: Set<Footnote> {
return Set(constraints.compactMap { $0.footnote })
var footnotes: [Footnote] {
return constraints.compactMap { $0.footnote }.removingDuplicates()
}
}

private extension Collection where Element: Hashable {

func removingDuplicates() -> [Element] {
var seen: Set<Element> = []
var uniques: [Element] = []
for element in self {
guard !seen.contains(element) else { continue }
seen.insert(element)
uniques.append(element)
}
return uniques
}
}

0 comments on commit f1b70da

Please sign in to comment.