diff --git a/README.md b/README.md index de62a04..6d80e2e 100644 --- a/README.md +++ b/README.md @@ -1328,9 +1328,9 @@ record.go records: Record, the JSON/CSV/SQL serializers, and their entry p struct.go structs: FakeStruct, fake tags, and a field's Go type as its column's datatype inline.go inline templates: Template, NewTemplate, FakeTemplate, IsTemplate, and their compile and link template.go the {token} grammar: scanning, tokens, operands, validation, compiling a format -hold.go the hold: one draw per expansion for paths and operands, and its fences +hold.go the hold: one draw per expansion for paths and operands, the rows a render pins, and its fences draw.go one reference draw per render and group: draw sets, the group option, and its fence -family.go a family of linked tables: the rows a render pins, and the fence over paths into one family +family.go a family of linked tables: what a reference path reads of one, and the fence over paths into it reference.go reference sigils, and binding references across the tree graph.go the render graph: edges, cycles, the repeat bound, tree walks builtins.go the {name()} function registry and its implementations diff --git a/family.go b/family.go index d984a7d..73443e4 100644 --- a/family.go +++ b/family.go @@ -2,139 +2,9 @@ package fejkdata import ( "fmt" - "sort" "strings" ) -// tablePin is one table's pinned row. -type tablePin struct { - t *table - row int -} - -// pinned is the row the render pinned for t, if any. -func (d *draws) pinned(t *table) (int, bool) { - for _, p := range d.pins[:d.npins] { - if p.t == t { - return p.row, true - } - } - r, ok := d.more[t] - return r, ok -} - -// mustRow is the row pinned for t, which the walk reaching a column pinned. -func (d *draws) mustRow(t *table) int { - r, ok := d.pinned(t) - if !ok { - panic(fmt.Sprintf("fejkdata: a column of %s is rendered with no row pinned", t.category)) - } - return r -} - -// pin pins row r of t, and the rows of t's ancestors it links to. -func (d *draws) pin(t *table, r int) { - for { - if _, done := d.pinned(t); done { - return - } - if d.npins < len(d.pins) { - d.pins[d.npins] = tablePin{t, r} - d.npins++ - } else { - if d.more == nil { - d.more = map[*table]int{} - } - d.more[t] = r - } - if t.parentT == nil { - return - } - t, r = t.parentT, t.parentRow(r) - } -} - -// each calls fn for every pinned row, in pin order, the spilled ones by table name. -func (d *draws) each(fn func(t *table, r int)) { - for _, p := range d.pins[:d.npins] { - fn(p.t, p.row) - } - spilled := make([]*table, 0, len(d.more)) - for t := range d.more { - spilled = append(spilled, t) - } - sort.Slice(spilled, func(i, j int) bool { return spilled[i].category < spilled[j].category }) - for _, t := range spilled { - fn(t, d.more[t]) - } -} - -// rowOf is the render's row of t: the one pinned, else one drawn inside the -// nearest pinned ancestor — its parent drawn inside that first where the ancestor -// is further up — or over the whole table, and pinned with its ancestors. -func (d *draws) rowOf(t *table) int { - if r, ok := d.pinned(t); ok { - return r - } - r := -1 - for a := t.parentT; a != nil && r < 0; a = a.parentT { - if _, ok := d.pinned(a); !ok { - continue - } - if t.parentT != a { - d.rowOf(t.parentT) - } - pr, _ := d.pinned(t.parentT) - r = t.drawUnder(d.s, pr) - } - if r < 0 { - r = t.draw(d.s) - } - d.pin(t, r) - return r -} - -// pinRow pins row r of t where it agrees with the rows pinned before it. -func (d *draws) pinRow(t *table, r int) error { - if pr, ok := d.pinned(t); ok && pr != r { - return fmt.Errorf("%s and %s are two rows of %s", t.selectorSpelling(pr), t.selectorSpelling(r), t.path) - } - for a := t.parentT; a != nil; a = a.parentT { - if pa, ok := d.pinned(a); ok && !t.under(r, a, pa) { - return fmt.Errorf("%s is not inside %s", t.selectorSpelling(r), a.selectorSpelling(pa)) - } - } - d.pin(t, r) - return nil -} - -// selectRow pins the row a selector names. -func (d *draws) selectRow(t *table, sel string) error { - r, err := t.find(sel, d) - if err != nil { - return err - } - return d.pinRow(t, r) -} - -// inside keeps the rows of t that sit inside every pinned ancestor. -func (d *draws) inside(t *table, rows []int) []int { - for a := t.parentT; a != nil; a = a.parentT { - pa, ok := d.pinned(a) - if !ok { - continue - } - var kept []int - for _, r := range rows { - if t.under(r, a, pa) { - kept = append(kept, r) - } - } - rows = kept - } - return rows -} - // tableRead is what a reference path reads of a table family: the table its head // names, the rows its selectors pin, the tables it draws — those it walks with no // row pinned, and their unpinned ancestors — each selector's spelling, and whether diff --git a/hold.go b/hold.go index 3a82baf..4b89c82 100644 --- a/hold.go +++ b/hold.go @@ -255,6 +255,12 @@ func checkNoRepeatedRead(format string, c formatOps, refs map[string]refBinding) }) } +// tablePin is one table's pinned row. +type tablePin struct { + t *table + row int +} + // draws is what has already been drawn for held names: the variant each was drawn as, // so every path under it reads one row, and the draw each read made, by its one // spelling, so the same read written twice reads one draw. An expansion keeps one for @@ -274,6 +280,129 @@ type draw struct { null bool } +// pinned is the row the render pinned for t, if any. +func (d *draws) pinned(t *table) (int, bool) { + for _, p := range d.pins[:d.npins] { + if p.t == t { + return p.row, true + } + } + r, ok := d.more[t] + return r, ok +} + +// mustRow is the row pinned for t, which the walk reaching a column pinned. +func (d *draws) mustRow(t *table) int { + r, ok := d.pinned(t) + if !ok { + panic(fmt.Sprintf("fejkdata: a column of %s is rendered with no row pinned", t.category)) + } + return r +} + +// pin pins row r of t, and the rows of t's ancestors it links to. +func (d *draws) pin(t *table, r int) { + for { + if _, done := d.pinned(t); done { + return + } + if d.npins < len(d.pins) { + d.pins[d.npins] = tablePin{t, r} + d.npins++ + } else { + if d.more == nil { + d.more = map[*table]int{} + } + d.more[t] = r + } + if t.parentT == nil { + return + } + t, r = t.parentT, t.parentRow(r) + } +} + +// each calls fn for every pinned row, in pin order, the spilled ones by table name. +func (d *draws) each(fn func(t *table, r int)) { + for _, p := range d.pins[:d.npins] { + fn(p.t, p.row) + } + spilled := make([]*table, 0, len(d.more)) + for t := range d.more { + spilled = append(spilled, t) + } + sort.Slice(spilled, func(i, j int) bool { return spilled[i].category < spilled[j].category }) + for _, t := range spilled { + fn(t, d.more[t]) + } +} + +// rowOf is the render's row of t: the one pinned, else one drawn inside the +// nearest pinned ancestor — its parent drawn inside that first where the ancestor +// is further up — or over the whole table, and pinned with its ancestors. +func (d *draws) rowOf(t *table) int { + if r, ok := d.pinned(t); ok { + return r + } + r := -1 + for a := t.parentT; a != nil && r < 0; a = a.parentT { + if _, ok := d.pinned(a); !ok { + continue + } + if t.parentT != a { + d.rowOf(t.parentT) + } + pr, _ := d.pinned(t.parentT) + r = t.drawUnder(d.s, pr) + } + if r < 0 { + r = t.draw(d.s) + } + d.pin(t, r) + return r +} + +// pinRow pins row r of t where it agrees with the rows pinned before it. +func (d *draws) pinRow(t *table, r int) error { + if pr, ok := d.pinned(t); ok && pr != r { + return fmt.Errorf("%s and %s are two rows of %s", t.selectorSpelling(pr), t.selectorSpelling(r), t.path) + } + for a := t.parentT; a != nil; a = a.parentT { + if pa, ok := d.pinned(a); ok && !t.under(r, a, pa) { + return fmt.Errorf("%s is not inside %s", t.selectorSpelling(r), a.selectorSpelling(pa)) + } + } + d.pin(t, r) + return nil +} + +// selectRow pins the row a selector names. +func (d *draws) selectRow(t *table, sel string) error { + r, err := t.find(sel, d) + if err != nil { + return err + } + return d.pinRow(t, r) +} + +// inside keeps the rows of t that sit inside every pinned ancestor. +func (d *draws) inside(t *table, rows []int) []int { + for a := t.parentT; a != nil; a = a.parentT { + pa, ok := d.pinned(a) + if !ok { + continue + } + var kept []int + for _, r := range rows { + if t.under(r, a, pa) { + kept = append(kept, r) + } + } + rows = kept + } + return rows +} + // readField renders one arm of a token. An arm's key is a sibling field or a // reference linkRefs bound into fields. A name the expansion holds — a level some // token addresses by dotted path, or a field an operand reads — is drawn once and diff --git a/todo.md b/todo.md index edebcc5..fd887d3 100644 --- a/todo.md +++ b/todo.md @@ -21,16 +21,13 @@ name says whether a unit runs at `New` or at `Fake`. This round, in order: -- Put `draws` and its methods in one file: it is declared in `hold.go:262` and all - eight of its methods sit in `family.go`, so a jump to the type lands where none of - the behaviour is. - Split `hold.go`, `draw.go` and `family.go` so each file holds one phase, the load-time fences apart from the render-time machinery, changing no logic. - Move `nodeScope`, `checkScope` and `checkRenders` out of `graph.go` and refresh the README Layout lines for `graph.go` and `data.go`: the order every load fence runs in is the load path's spine, and no file name carries it. - Name the `draw` stem apart: the free `pinned` in `hold.go` is not `(*draws).pinned` - in `family.go`, `compileFormat` is a method on both `*template` and `*table`, and + beside it, `compileFormat` is a method on both `*template` and `*table`, and `draws`, `drawSet`, `drawScope`, `drawWalk`, `drawAt`, `drawKey`, `drawVisit` and `drawRoute` no longer tell each other apart. - Spell one fresh draw set one way: `newDrawSet` exists, and `render.go` and `draw.go`