X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/522d7fd117285e4e4ce3ea5ec9b5a1722521b681..c781f79f3c8cb9a7577ee1574d196988da2719b3:/view.go diff --git a/view.go b/view.go index 2357198..1b3ed6a 100644 --- a/view.go +++ b/view.go @@ -23,42 +23,6 @@ const ( viewPending = "(not summarized yet: zoom it)" ) -// newView returns an empty view with the limits high and low. -func newView(high, low int) *View { - return &View{ - high: high, - low: low, - nodes: make(map[NodeKey]Node), - } -} - -// restoreView returns the view of state with the limits high and low. Each line -// must continue the one before it, and each line above level 0 must be built. -func restoreView(state ViewState, high, low int, tree ViewNodeReader) (*View, error) { - v := newView(high, low) - v.merging = state.Merging - for _, pair := range state.Lines { - line := NodeKey{pair[0], pair[1]} - if line.level < 0 || line.level >= bits.UintSize-2 || line.index < 0 || line.index< 0 && !built { - return nil, fmt.Errorf("view line (%d, %d) is not built", line.level, line.index) - } - - v.lines = append(v.lines, line) - v.end = nodeEnd(line.level, line.index) - } - - return v, nil -} - // View is a list of tree nodes that covers messages 0 to end-1 without a gap, // oldest first. Each new message appends its leaf as a line. Once the size of // the view goes above high, one batch merges the most due sibling pairs until @@ -83,6 +47,27 @@ type View struct { merging bool } +// ViewState is the saved form of a View. The limits are not part of it. +type ViewState struct { + Lines [][2]int `json:"lines"` + Merging bool `json:"merging"` +} + +// ViewNodeReader gives the built nodes of a tree. +type ViewNodeReader interface { + Built(level, index int) (bool, error) + Node(level, index int) (Node, bool, error) +} + +// newView returns an empty view with the limits high and low. +func newView(high, low int) *View { + return &View{ + high: high, + low: low, + nodes: make(map[NodeKey]Node), + } +} + // State returns the saved form of the view. func (v *View) State() ViewState { state := ViewState{ @@ -343,14 +328,29 @@ func (v *View) lineSize(line NodeKey) int { return node.Size } -// ViewState is the saved form of a View. The limits are not part of it. -type ViewState struct { - Lines [][2]int `json:"lines"` - Merging bool `json:"merging"` -} +// restoreView returns the view of state with the limits high and low. Each line +// must continue the one before it, and each line above level 0 must be built. +func restoreView(state ViewState, high, low int, tree ViewNodeReader) (*View, error) { + v := newView(high, low) + v.merging = state.Merging + for _, pair := range state.Lines { + line := NodeKey{pair[0], pair[1]} + if line.level < 0 || line.level >= bits.UintSize-2 || line.index < 0 || line.index< 0 && !built { + return nil, fmt.Errorf("view line (%d, %d) is not built", line.level, line.index) + } + + v.lines = append(v.lines, line) + v.end = nodeEnd(line.level, line.index) + } + + return v, nil }