ui: simplify op size and reference count code

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-03 13:26:47 +02:00
parent 86f6765e64
commit 559db02035
2 changed files with 38 additions and 59 deletions
+33 -18
View File
@@ -3,10 +3,10 @@ package ops
type OpType byte type OpType byte
// Start at a high number for easier debugging. // Start at a high number for easier debugging.
const FirstOpIndex = 200 const firstOpIndex = 200
const ( const (
TypeBlockDef OpType = iota + FirstOpIndex TypeBlockDef OpType = iota + firstOpIndex
TypeBlock TypeBlock
TypeTransform TypeTransform
TypeLayer TypeLayer
@@ -39,20 +39,35 @@ const (
TypePopLen = 1 TypePopLen = 1
TypeAuxLen = 1 + 4 TypeAuxLen = 1 + 4
TypeClipLen = 1 + 4*4 TypeClipLen = 1 + 4*4
TypeBlockDefRefs = 0
TypeBlockRefs = 1
TypeTransformRefs = 0
TypeLayerRefs = 0
TypeRedrawRefs = 0
TypeImageRefs = 1
TypeDrawRefs = 0
TypeColorRefs = 0
TypePointerHandlerRefs = 2
TypeKeyHandlerRefs = 1
TypeHideInputRefs = 0
TypePushRefs = 0
TypePopRefs = 0
TypeAuxRefs = 0
TypeClipRefs = 0
) )
func (t OpType) Size() int {
return [...]int{
TypeBlockDefLen,
TypeBlockLen,
TypeTransformLen,
TypeLayerLen,
TypeRedrawLen,
TypeImageLen,
TypeDrawLen,
TypeColorLen,
TypePointerHandlerLen,
TypeKeyHandlerLen,
TypeHideInputLen,
TypePushLen,
TypePopLen,
TypeAuxLen,
TypeClipLen,
}[t-firstOpIndex]
}
func (t OpType) NumRefs() int {
switch t {
case TypeBlock, TypeImage, TypeKeyHandler:
return 1
case TypePointerHandler:
return 2
default:
return 0
}
}
+5 -41
View File
@@ -58,42 +58,6 @@ type pc struct {
refs int refs int
} }
var typeLengths = [...]int{
ops.TypeBlockDefLen,
ops.TypeBlockLen,
ops.TypeTransformLen,
ops.TypeLayerLen,
ops.TypeRedrawLen,
ops.TypeImageLen,
ops.TypeDrawLen,
ops.TypeColorLen,
ops.TypePointerHandlerLen,
ops.TypeKeyHandlerLen,
ops.TypeHideInputLen,
ops.TypePushLen,
ops.TypePopLen,
ops.TypeAuxLen,
ops.TypeClipLen,
}
var refLengths = [...]int{
ops.TypeBlockDefRefs,
ops.TypeBlockRefs,
ops.TypeTransformRefs,
ops.TypeLayerRefs,
ops.TypeRedrawRefs,
ops.TypeImageRefs,
ops.TypeDrawRefs,
ops.TypeColorRefs,
ops.TypePointerHandlerRefs,
ops.TypeKeyHandlerRefs,
ops.TypeHideInputRefs,
ops.TypePushRefs,
ops.TypePopRefs,
ops.TypeAuxRefs,
ops.TypeClipRefs,
}
type OpPush struct{} type OpPush struct{}
type OpPop struct{} type OpPop struct{}
@@ -272,8 +236,8 @@ func (r *OpsReader) Decode() (EncodedOp, bool) {
} }
key := OpKey{ops: r.ops, pc: r.pc.data, version: r.ops.version} key := OpKey{ops: r.ops, pc: r.pc.data, version: r.ops.version}
t := ops.OpType(r.ops.data[r.pc.data]) t := ops.OpType(r.ops.data[r.pc.data])
n := typeLengths[t-ops.FirstOpIndex] n := t.Size()
nrefs := refLengths[t-ops.FirstOpIndex] nrefs := t.NumRefs()
data := r.ops.data[r.pc.data : r.pc.data+n] data := r.ops.data[r.pc.data : r.pc.data+n]
refs := r.ops.refs[r.pc.refs : r.pc.refs+nrefs] refs := r.ops.refs[r.pc.refs : r.pc.refs+nrefs]
switch t { switch t {
@@ -293,7 +257,7 @@ func (r *OpsReader) Decode() (EncodedOp, bool) {
panic("invalid OpBlock reference to reset Ops") panic("invalid OpBlock reference to reset Ops")
} }
var opDef opBlockDef var opDef opBlockDef
opDef.decode(blockOps.data[op.pc.data : op.pc.data+ops.TypeBlockDefLen]) opDef.decode(blockOps.data[op.pc.data : op.pc.data+ops.TypeBlockDef.Size()])
retPC := r.pc retPC := r.pc
retPC.data += n retPC.data += n
retPC.refs += nrefs retPC.refs += nrefs
@@ -304,8 +268,8 @@ func (r *OpsReader) Decode() (EncodedOp, bool) {
}) })
r.ops = blockOps r.ops = blockOps
r.pc = op.pc r.pc = op.pc
r.pc.data += ops.TypeBlockDefLen r.pc.data += ops.TypeBlockDef.Size()
r.pc.refs += ops.TypeBlockDefRefs r.pc.refs += ops.TypeBlockDef.NumRefs()
continue continue
case ops.TypeBlockDef: case ops.TypeBlockDef:
var op opBlockDef var op opBlockDef