Commit cc519ed7 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 1ebc3574
......@@ -32,7 +32,7 @@ import (
//
// It contains []Entry in key order.
//
// It mimics ?OBTree from btree/py, with ? being any integer.
// It mimics BTree from btree/py.
type BTree struct {
zodb.Persistent
......@@ -67,7 +67,7 @@ type Entry struct {
//
// It contains []BucketEntry in ↑ key order.
//
// It mimics ?OBucket from btree/py, with ? being any integer.
// It mimics Bucket from btree/py.
type Bucket struct {
zodb.Persistent
......@@ -123,8 +123,6 @@ func (e *BucketEntry) Key() KEY { return e.key }
func (e *BucketEntry) Value() interface{} { return e.value }
// Entryv returns entries of a Bucket node.
//
// XXX
func (b *Bucket) Entryv() []BucketEntry {
ev := make([]BucketEntry, len(b.keys))
for i, k := range b.keys {
......@@ -270,17 +268,27 @@ func (b *bucketState) PySetState(pystate interface{}) (err error) {
b.keys = make([]KEY, 0, n)
b.values = make([]interface{}, 0, n)
var kprev int64
for i := 0; i < n; i++ {
xk := t[2*i]
v := t[2*i+1]
k, ok := xk.(int64) // XXX use KEY XXX -> Xint64
k, ok := xk.(int64) // XXX -> Xint64
if !ok {
return fmt.Errorf("data: [%d]: key must be integer; got %T", i, xk)
}
// XXX check keys are sorted
b.keys = append(b.keys, KEY(k)) // XXX cast
kk := KEY(k)
if int64(kk) != k {
return fmt.Errorf("data: [%d]: key overflows %T", i, kk)
}
if i > 0 && !(k > kprev) {
return fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = k
b.keys = append(b.keys, kk)
b.values = append(b.values, v)
}
......@@ -375,8 +383,10 @@ func (bt *btreeState) PySetState(pystate interface{}) (err error) {
n := (len(t) + 1) / 2
bt.data = make([]Entry, 0, n)
var kprev int64
var childrenKind int // 1 - BTree, 2 - Bucket
for i, idx := 0, 0; i < n; i++ {
key := int64(math.MinInt64) // int64(-∞) (qualifies for ≤)
key := int64(math.Min<Key>) // KEY(-∞) (qualifies for ≤)
if i > 0 {
// key[0] is unused and not saved
key, ok = t[idx].(int64) // XXX Xint
......@@ -388,17 +398,36 @@ func (bt *btreeState) PySetState(pystate interface{}) (err error) {
child := t[idx]
idx++
kkey := KEY(key)
if int64(kkey) != key {
return fmt.Errorf("data: [%d]: key overflows %T", i, kkey)
}
if i > 1 && !(key > kprev) {
fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = key
// check all children are of the same type
var kind int // see childrenKind ^^^
switch child.(type) {
default:
return fmt.Errorf("data: [%d]: child must be BTree|Bucket; got %T", i, child)
// XXX check all children are of the same type
case *BTree: // ok
case *Bucket: // ok
case *BTree:
kind = 1
case *Bucket:
kind = 2
}
if i == 0 {
childrenKind = kind
}
if kind != childrenKind {
fmt.Errorf("data: [%d]: children must be of the same type", i)
}
// XXX check key ↑
bt.data = append(bt.data, Entry{key: KEY(key), child: child})
bt.data = append(bt.data, Entry{key: kkey, child: child})
}
return nil
......
......@@ -26,6 +26,7 @@ KEY=$2
out=$3
kind=${KIND,,} # IO -> io
Key=${KEY^}
input=$(dirname $0)/btree.go.in
......@@ -36,6 +37,7 @@ sed \
-e "s/<kind>/$kind/g" \
-e "s/<KIND>/$KIND/g" \
-e "s/KEY/$KEY/g" \
-e "s/<Key>/$Key/g" \
-e "s/\bBTree\b/${KIND}BTree/g" \
-e "s/\bEntry\b/${KIND}Entry/g" \
-e "s/\bBucket\b/${KIND}Bucket/g" \
......
......@@ -34,7 +34,7 @@ import (
//
// It contains []IOEntry in ↑ key order.
//
// It mimics ?OBTree from btree/py, with ? being any integer.
// It mimics IOBTree from btree/py.
type IOBTree struct {
zodb.Persistent
......@@ -69,7 +69,7 @@ type IOEntry struct {
//
// It contains []IOBucketEntry in ↑ key order.
//
// It mimics ?OBucket from btree/py, with ? being any integer.
// It mimics IOBucket from btree/py.
type IOBucket struct {
zodb.Persistent
......@@ -125,8 +125,6 @@ func (e *IOBucketEntry) Key() int32 { return e.key }
func (e *IOBucketEntry) Value() interface{} { return e.value }
// Entryv returns entries of a IOBucket node.
//
// XXX
func (b *IOBucket) Entryv() []IOBucketEntry {
ev := make([]IOBucketEntry, len(b.keys))
for i, k := range b.keys {
......@@ -272,17 +270,27 @@ func (b *iobucketState) PySetState(pystate interface{}) (err error) {
b.keys = make([]int32, 0, n)
b.values = make([]interface{}, 0, n)
var kprev int64
for i := 0; i < n; i++ {
xk := t[2*i]
v := t[2*i+1]
k, ok := xk.(int64) // XXX use int32 XXX -> Xint64
k, ok := xk.(int64) // XXX -> Xint64
if !ok {
return fmt.Errorf("data: [%d]: key must be integer; got %T", i, xk)
}
// XXX check keys are sorted
b.keys = append(b.keys, int32(k)) // XXX cast
kk := int32(k)
if int64(kk) != k {
return fmt.Errorf("data: [%d]: key overflows %T", i, kk)
}
if i > 0 && !(k > kprev) {
return fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = k
b.keys = append(b.keys, kk)
b.values = append(b.values, v)
}
......@@ -377,8 +385,10 @@ func (bt *iobtreeState) PySetState(pystate interface{}) (err error) {
n := (len(t) + 1) / 2
bt.data = make([]IOEntry, 0, n)
var kprev int64
var childrenKind int // 1 - IOBTree, 2 - IOBucket
for i, idx := 0, 0; i < n; i++ {
key := int64(math.MinInt64) // int64(-∞) (qualifies for ≤)
key := int64(math.MinInt32) // int32(-∞) (qualifies for ≤)
if i > 0 {
// key[0] is unused and not saved
key, ok = t[idx].(int64) // XXX Xint
......@@ -390,17 +400,36 @@ func (bt *iobtreeState) PySetState(pystate interface{}) (err error) {
child := t[idx]
idx++
kkey := int32(key)
if int64(kkey) != key {
return fmt.Errorf("data: [%d]: key overflows %T", i, kkey)
}
if i > 1 && !(key > kprev) {
fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = key
// check all children are of the same type
var kind int // see childrenKind ^^^
switch child.(type) {
default:
return fmt.Errorf("data: [%d]: child must be IOBTree|IOBucket; got %T", i, child)
// XXX check all children are of the same type
case *IOBTree: // ok
case *IOBucket: // ok
case *IOBTree:
kind = 1
case *IOBucket:
kind = 2
}
if i == 0 {
childrenKind = kind
}
if kind != childrenKind {
fmt.Errorf("data: [%d]: children must be of the same type", i)
}
// XXX check key ↑
bt.data = append(bt.data, IOEntry{key: int32(key), child: child})
bt.data = append(bt.data, IOEntry{key: kkey, child: child})
}
return nil
......
......@@ -34,7 +34,7 @@ import (
//
// It contains []LOEntry in ↑ key order.
//
// It mimics ?OBTree from btree/py, with ? being any integer.
// It mimics LOBTree from btree/py.
type LOBTree struct {
zodb.Persistent
......@@ -69,7 +69,7 @@ type LOEntry struct {
//
// It contains []LOBucketEntry in ↑ key order.
//
// It mimics ?OBucket from btree/py, with ? being any integer.
// It mimics LOBucket from btree/py.
type LOBucket struct {
zodb.Persistent
......@@ -125,8 +125,6 @@ func (e *LOBucketEntry) Key() int64 { return e.key }
func (e *LOBucketEntry) Value() interface{} { return e.value }
// Entryv returns entries of a LOBucket node.
//
// XXX
func (b *LOBucket) Entryv() []LOBucketEntry {
ev := make([]LOBucketEntry, len(b.keys))
for i, k := range b.keys {
......@@ -272,17 +270,27 @@ func (b *lobucketState) PySetState(pystate interface{}) (err error) {
b.keys = make([]int64, 0, n)
b.values = make([]interface{}, 0, n)
var kprev int64
for i := 0; i < n; i++ {
xk := t[2*i]
v := t[2*i+1]
k, ok := xk.(int64) // XXX use int64 XXX -> Xint64
k, ok := xk.(int64) // XXX -> Xint64
if !ok {
return fmt.Errorf("data: [%d]: key must be integer; got %T", i, xk)
}
// XXX check keys are sorted
b.keys = append(b.keys, int64(k)) // XXX cast
kk := int64(k)
if int64(kk) != k {
return fmt.Errorf("data: [%d]: key overflows %T", i, kk)
}
if i > 0 && !(k > kprev) {
return fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = k
b.keys = append(b.keys, kk)
b.values = append(b.values, v)
}
......@@ -377,6 +385,8 @@ func (bt *lobtreeState) PySetState(pystate interface{}) (err error) {
n := (len(t) + 1) / 2
bt.data = make([]LOEntry, 0, n)
var kprev int64
var childrenKind int // 1 - LOBTree, 2 - LOBucket
for i, idx := 0, 0; i < n; i++ {
key := int64(math.MinInt64) // int64(-∞) (qualifies for ≤)
if i > 0 {
......@@ -390,17 +400,36 @@ func (bt *lobtreeState) PySetState(pystate interface{}) (err error) {
child := t[idx]
idx++
kkey := int64(key)
if int64(kkey) != key {
return fmt.Errorf("data: [%d]: key overflows %T", i, kkey)
}
if i > 1 && !(key > kprev) {
fmt.Errorf("data: [%d]: key not ↑", i)
}
kprev = key
// check all children are of the same type
var kind int // see childrenKind ^^^
switch child.(type) {
default:
return fmt.Errorf("data: [%d]: child must be LOBTree|LOBucket; got %T", i, child)
// XXX check all children are of the same type
case *LOBTree: // ok
case *LOBucket: // ok
case *LOBTree:
kind = 1
case *LOBucket:
kind = 2
}
if i == 0 {
childrenKind = kind
}
if kind != childrenKind {
fmt.Errorf("data: [%d]: children must be of the same type", i)
}
// XXX check key ↑
bt.data = append(bt.data, LOEntry{key: int64(key), child: child})
bt.data = append(bt.data, LOEntry{key: kkey, child: child})
}
return nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment