From 559db02035bc46b62afd9f870cb724d5e79faf77 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 3 Jun 2019 13:26:47 +0200 Subject: [PATCH] ui: simplify op size and reference count code Signed-off-by: Elias Naur --- ui/internal/ops/ops.go | 51 +++++++++++++++++++++++++++--------------- ui/ops.go | 46 +++++-------------------------------- 2 files changed, 38 insertions(+), 59 deletions(-) diff --git a/ui/internal/ops/ops.go b/ui/internal/ops/ops.go index a5c59122..e48de5d5 100644 --- a/ui/internal/ops/ops.go +++ b/ui/internal/ops/ops.go @@ -3,10 +3,10 @@ package ops type OpType byte // Start at a high number for easier debugging. -const FirstOpIndex = 200 +const firstOpIndex = 200 const ( - TypeBlockDef OpType = iota + FirstOpIndex + TypeBlockDef OpType = iota + firstOpIndex TypeBlock TypeTransform TypeLayer @@ -39,20 +39,35 @@ const ( TypePopLen = 1 TypeAuxLen = 1 + 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 + } +} diff --git a/ui/ops.go b/ui/ops.go index ebd21184..383a0d4b 100644 --- a/ui/ops.go +++ b/ui/ops.go @@ -58,42 +58,6 @@ type pc struct { 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 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} t := ops.OpType(r.ops.data[r.pc.data]) - n := typeLengths[t-ops.FirstOpIndex] - nrefs := refLengths[t-ops.FirstOpIndex] + n := t.Size() + nrefs := t.NumRefs() data := r.ops.data[r.pc.data : r.pc.data+n] refs := r.ops.refs[r.pc.refs : r.pc.refs+nrefs] switch t { @@ -293,7 +257,7 @@ func (r *OpsReader) Decode() (EncodedOp, bool) { panic("invalid OpBlock reference to reset Ops") } 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.data += n retPC.refs += nrefs @@ -304,8 +268,8 @@ func (r *OpsReader) Decode() (EncodedOp, bool) { }) r.ops = blockOps r.pc = op.pc - r.pc.data += ops.TypeBlockDefLen - r.pc.refs += ops.TypeBlockDefRefs + r.pc.data += ops.TypeBlockDef.Size() + r.pc.refs += ops.TypeBlockDef.NumRefs() continue case ops.TypeBlockDef: var op opBlockDef