fix tree, start fieldpath stuff
parent
4b98518812
commit
ffab114a0b
@ -0,0 +1,47 @@
|
|||||||
|
package ent
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// a fieldpath is a list of integers that is used to walk the type hierarchy to
|
||||||
|
// identify a given field on a given type.
|
||||||
|
type fieldPath struct {
|
||||||
|
vals []int
|
||||||
|
last int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFieldPath() *fieldPath {
|
||||||
|
f := &fieldPath{vals: make([]int, 32)}
|
||||||
|
f.vals[f.last] = -1
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fieldPath) add(i int) {
|
||||||
|
f.vals[f.last] += i
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fieldPath) push(i int) {
|
||||||
|
f.last++
|
||||||
|
f.vals[f.last] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fieldPath) pop() int {
|
||||||
|
f.last--
|
||||||
|
return f.vals[f.last+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fieldPath) replaceAll(fn func(v int) int) {
|
||||||
|
for i := 0; i <= f.last; i++ {
|
||||||
|
f.vals[i] = fn(f.vals[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fieldPath) pathString() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for i := 0; i <= f.last; i++ {
|
||||||
|
fmt.Fprintf(&buf, "/%d", f.vals[i])
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
@ -1,14 +1,70 @@
|
|||||||
package ent
|
package ent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDump(t *testing.T) {
|
// thanks to @spheenik and @invokr for the expected huffman codes. these are
|
||||||
t.Log(hlist)
|
// ripped from the huffman trees that are known to be working in clarity and
|
||||||
|
// manta.
|
||||||
|
var expected_codes = map[string]string{
|
||||||
|
"PlusOne": "0",
|
||||||
|
"FieldPathEncodeFinish": "10",
|
||||||
|
"PlusTwo": "1110",
|
||||||
|
"PushOneLeftDeltaNRightNonZeroPack6Bits": "1111",
|
||||||
|
"PushOneLeftDeltaOneRightNonZero": "11000",
|
||||||
|
"PlusN": "11010",
|
||||||
|
"PlusThree": "110010",
|
||||||
|
"PopAllButOnePlusOne": "110011",
|
||||||
|
"PushOneLeftDeltaNRightNonZero": "11011001",
|
||||||
|
"PushOneLeftDeltaOneRightZero": "11011010",
|
||||||
|
"PushOneLeftDeltaNRightZero": "11011100",
|
||||||
|
"PopAllButOnePlusNPack6Bits": "11011110",
|
||||||
|
"PlusFour": "11011111",
|
||||||
|
"PopAllButOnePlusN": "110110000",
|
||||||
|
"PushOneLeftDeltaNRightNonZeroPack8Bits": "110110110",
|
||||||
|
"NonTopoPenultimatePlusOne": "110110111",
|
||||||
|
"PopAllButOnePlusNPack3Bits": "110111010",
|
||||||
|
"PushNAndNonTopological": "110111011",
|
||||||
|
"NonTopoComplexPack4Bits": "1101100010",
|
||||||
|
"NonTopoComplex": "11011000111",
|
||||||
|
"PushOneLeftDeltaZeroRightZero": "110110001101",
|
||||||
|
"PopOnePlusOne": "110110001100001",
|
||||||
|
"PushOneLeftDeltaZeroRightNonZero": "110110001100101",
|
||||||
|
"PopNAndNonTopographical": "1101100011000000",
|
||||||
|
"PopNPlusN": "1101100011000001",
|
||||||
|
"PushN": "1101100011000100",
|
||||||
|
"PushThreePack5LeftDeltaN": "1101100011000101",
|
||||||
|
"PopNPlusOne": "1101100011000110",
|
||||||
|
"PopOnePlusN": "1101100011000111",
|
||||||
|
"PushTwoLeftDeltaZero": "1101100011001000",
|
||||||
|
"PushThreeLeftDeltaZero": "11011000110010010",
|
||||||
|
"PushTwoPack5LeftDeltaZero": "11011000110010011",
|
||||||
|
"PushTwoLeftDeltaN": "11011000110011000",
|
||||||
|
"PushThreePack5LeftDeltaOne": "11011000110011001",
|
||||||
|
"PushThreeLeftDeltaN": "11011000110011010",
|
||||||
|
"PushTwoPack5LeftDeltaN": "11011000110011011",
|
||||||
|
"PushTwoLeftDeltaOne": "11011000110011100",
|
||||||
|
"PushThreePack5LeftDeltaZero": "11011000110011101",
|
||||||
|
"PushThreeLeftDeltaOne": "11011000110011110",
|
||||||
|
"PushTwoPack5LeftDeltaOne": "11011000110011111",
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTree(t *testing.T) {
|
||||||
|
var testWalk func(node, string)
|
||||||
|
testWalk = func(n node, code string) {
|
||||||
|
switch v := n.(type) {
|
||||||
|
case lNode:
|
||||||
|
if expected_codes[v.name] != code {
|
||||||
|
t.Errorf("op %s has code %s, expected %s", v.name, code, expected_codes[v.name])
|
||||||
|
} else {
|
||||||
|
t.Logf("op %s has expected code %s", v.name, code)
|
||||||
|
}
|
||||||
|
case iNode:
|
||||||
|
testWalk(v.left, code+"0")
|
||||||
|
testWalk(v.right, code+"1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
testWalk(htree, "")
|
||||||
dump(htree, "", &buf)
|
|
||||||
t.Logf("%s", buf.String())
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue