Replace generic constraint with sameRowConstraint

This commit is contained in:
Ian Gulliver
2026-02-23 22:35:24 -08:00
parent 49f0cdc290
commit 88407f40c6

View File

@@ -21,7 +21,7 @@ type Timeline struct {
show *Show `json:"-"` show *Show `json:"-"`
trackIdx map[string]*TimelineTrack `json:"-"` trackIdx map[string]*TimelineTrack `json:"-"`
cellIdx map[cellKey]*TimelineCell `json:"-"` cellIdx map[cellKey]*TimelineCell `json:"-"`
constraints []constraint `json:"-"` sameRows []sameRowConstraint `json:"-"`
exclusives []exclusiveGroup `json:"-"` exclusives []exclusiveGroup `json:"-"`
debugW io.Writer `json:"-"` debugW io.Writer `json:"-"`
} }
@@ -61,34 +61,17 @@ func (c *TimelineCell) String() string {
return fmt.Sprintf("%s/%s@%s:r%d", c.BlockID, c.Event, c.track.ID, c.row) return fmt.Sprintf("%s/%s@%s:r%d", c.BlockID, c.Event, c.track.ID, c.row)
} }
type constraintKind string type sameRowConstraint struct {
a *TimelineCell
const ( b *TimelineCell
constraintSameRow constraintKind = "same_row"
)
type constraint struct {
kind constraintKind
a *TimelineCell
b *TimelineCell
} }
func (c constraint) satisfied() bool { func (c sameRowConstraint) satisfied() bool {
switch c.kind { return c.a.row == c.b.row
case constraintSameRow:
return c.a.row == c.b.row
default:
panic("invalid constraint kind: " + string(c.kind))
}
} }
func (c constraint) String() string { func (c sameRowConstraint) String() string {
switch c.kind { return fmt.Sprintf("same_row(%s, %s)", c.a, c.b)
case constraintSameRow:
return fmt.Sprintf("same_row(%s, %s)", c.a, c.b)
default:
panic("invalid constraint kind: " + string(c.kind))
}
} }
type exclusiveGroup struct { type exclusiveGroup struct {
@@ -150,7 +133,7 @@ func (tl *Timeline) debugState() {
} }
fmt.Fprintf(tl.debugW, " %s: [%s]\n", t.ID, strings.Join(parts, " ")) fmt.Fprintf(tl.debugW, " %s: [%s]\n", t.ID, strings.Join(parts, " "))
} }
for _, c := range tl.constraints { for _, c := range tl.sameRows {
sat := "OK" sat := "OK"
if !c.satisfied() { if !c.satisfied() {
sat = "UNSATISFIED" sat = "UNSATISFIED"
@@ -225,8 +208,8 @@ func BuildTimelineDebug(show *Show, debugW io.Writer) (Timeline, error) {
} }
func (tl *Timeline) addConstraint(kind constraintKind, a, b *TimelineCell) { func (tl *Timeline) addSameRow(a, b *TimelineCell) {
tl.constraints = append(tl.constraints, constraint{kind: kind, a: a, b: b}) tl.sameRows = append(tl.sameRows, sameRowConstraint{a: a, b: b})
} }
func (track *TimelineTrack) appendCells(cells ...*TimelineCell) { func (track *TimelineTrack) appendCells(cells ...*TimelineCell) {
@@ -305,7 +288,7 @@ func (tl *Timeline) buildConstraints() {
for _, target := range trigger.Targets { for _, target := range trigger.Targets {
t := tl.findCell(target.Block, target.Hook) t := tl.findCell(target.Block, target.Hook)
if source.track != t.track { if source.track != t.track {
tl.addConstraint(constraintSameRow, source, t) tl.addSameRow(source, t)
source.Type = CellSignal source.Type = CellSignal
} }
group.members = append(group.members, t) group.members = append(group.members, t)
@@ -327,7 +310,7 @@ func (tl *Timeline) assignRows() error {
tl.debugf("converged after %d iterations", i) tl.debugf("converged after %d iterations", i)
return nil return nil
} }
for _, c := range tl.constraints { for _, c := range tl.sameRows {
if !c.satisfied() { if !c.satisfied() {
return fmt.Errorf("assignRows: unsatisfied %s", c) return fmt.Errorf("assignRows: unsatisfied %s", c)
} }
@@ -341,21 +324,16 @@ func (tl *Timeline) assignRows() error {
} }
func (tl *Timeline) enforceConstraints(iter int) bool { func (tl *Timeline) enforceConstraints(iter int) bool {
for _, c := range tl.constraints { for _, c := range tl.sameRows {
if c.satisfied() { if c.satisfied() {
continue continue
} }
switch c.kind { if c.a.row < c.b.row {
case constraintSameRow: tl.debugf("iter %d: constraint %s: insert gap on %s before r%d", iter, c, c.a.track.ID, c.a.row)
if c.a.row < c.b.row { tl.insertGap(c.a.track, c.a.row)
tl.debugf("iter %d: constraint %s: insert gap on %s before r%d", iter, c, c.a.track.ID, c.a.row) } else {
tl.insertGap(c.a.track, c.a.row) tl.debugf("iter %d: constraint %s: insert gap on %s before r%d", iter, c, c.b.track.ID, c.b.row)
} else { tl.insertGap(c.b.track, c.b.row)
tl.debugf("iter %d: constraint %s: insert gap on %s before r%d", iter, c, c.b.track.ID, c.b.row)
tl.insertGap(c.b.track, c.b.row)
}
default:
panic("invalid constraint kind: " + string(c.kind))
} }
return true return true
} }