Commit 4e1a25df authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 7c1cc95d
...@@ -131,7 +131,16 @@ func (S PPTreeSubSet) AddPath(path []zodb.Oid) { ...@@ -131,7 +131,16 @@ func (S PPTreeSubSet) AddPath(path []zodb.Oid) {
} }
} }
// ---- Union/Difference ---- // ---- Union/Difference/Intersetctior ----
// Union returns U = PP(A.leafs | B.leafs)
//
// In other words it returns sum of A and B.
func (A PPTreeSubSet) Union(B PPTreeSubSet) PPTreeSubSet {
U := A.Clone()
U.UnionInplace(B)
return U
}
// UnionInplace sets A = PP(A.leafs | B.leafs) // UnionInplace sets A = PP(A.leafs | B.leafs)
// //
...@@ -151,6 +160,8 @@ func (A PPTreeSubSet) UnionInplace(B PPTreeSubSet) { ...@@ -151,6 +160,8 @@ func (A PPTreeSubSet) UnionInplace(B PPTreeSubSet) {
A.xUnionInplace(B) A.xUnionInplace(B)
} }
// XXX Difference
// DifferenceInplace sets A = PP(A.leafs \ B.leafs) // DifferenceInplace sets A = PP(A.leafs \ B.leafs)
// //
// In other words it removes B nodes from A while still maintaining A as PP-connected. // In other words it removes B nodes from A while still maintaining A as PP-connected.
...@@ -169,6 +180,8 @@ func (A PPTreeSubSet) DifferenceInplace(B PPTreeSubSet) { ...@@ -169,6 +180,8 @@ func (A PPTreeSubSet) DifferenceInplace(B PPTreeSubSet) {
A.xDifferenceInplace(B) A.xDifferenceInplace(B)
} }
// XXX Intersection
func (A PPTreeSubSet) xUnionInplace(B PPTreeSubSet) { func (A PPTreeSubSet) xUnionInplace(B PPTreeSubSet) {
if debugPPSet { if debugPPSet {
fmt.Printf("\n\n xUnionInplace:\n") fmt.Printf("\n\n xUnionInplace:\n")
......
...@@ -33,8 +33,8 @@ func TestPPTreeSubSetOps(t *testing.T) { ...@@ -33,8 +33,8 @@ func TestPPTreeSubSetOps(t *testing.T) {
d d
ø = zodb.InvalidOid ø = zodb.InvalidOid
) )
type S = PPTreeSubSet type S = PPTreeSubSet
type testEntry struct { type testEntry struct {
A, B S A, B S
Union S Union S
Difference S Difference S
......
// Copyright (C) 2021 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
package xbtree
// set of [lo,hi) Key ranges.
// Range represents [lo,hi) Key range.
type Range struct {
lo Key
hi_ Key // NOTE _not_ hi) to avoid overflow at ∞; hi = hi_ + 1
}
// RangeSet is set of non-overlapping Key ranges.
type RangeSet struct {
// TODO use BTree lo->hi_ instead
rangev []Range // lo↑
}
// Add adds Range r to the set of keys.
func (S *RangeSet) Add(r Range) {
panic("TODO") // XXX
}
// Del removes Range r from the set of keys.
func (S *RangeSet) Del(r Range) {
panic("TODO") // XXX
}
// XXX Has?
// XXX AddKey ?
// XXX DelKey ?
// XXX HasKey
// Union returns RangeSet(A.keys | B.keys).
func (A *RangeSet) Union(B *RangeSet) *RangeSet {
U := A.Clone()
U.UnionInplace(B)
return U
}
// Difference returns RangeSet(A.keys \ B.keys).
func (A *RangeSet) Difference(B *RangeSet) *RangeSet {
D := A.Clone()
D.DifferenceInplace(B)
return D
}
// XXX Intersection
// --------
// XXX RangeSet.verify to check ranges are not overlapping and ↑
func (r Range) String() string {
slo := "-∞"; if rn.lo > KeyMin { slo = fmt.Sprintf("%v", rn.lo) }
shi := "∞"; if rn.hi_ < KeyMax { shi = fmt.Sprintf("%v", rn.hi_+1) }
return fmt.Sprintf("[%s,%s)", slo, shi)
}
func (S RangeSet) String() string {
s := "{"
for i, r := range S.rangev {
if i > 0 {
s += " "
}
s += r.String()
}
s += "}"
return s
}
// Copyright (C) 2021 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
package xbtree
import (
"testing"
)
func TestRangeSet(t *testing.T) {
const (
oo = KeyMax
noo = KeyMin
)
type testEntry struct {
A, B *RangeSet
Union *RangeSet
Difference *RangeSet
}
E := func(A, B, U, D *RangeSet) testEntry {
return testEntry{A, B, U, D}
}
// S is shorthand to create RangeSet, e.g. S(1,2, 4,5) will return {[1,2) [4,5)}
S := func(kv ...Key) *RangeSet {
l := len(kv)
if l % 2 != 0 {
panic("odd number of keys")
}
S := &RangeSet{}
for i := 0; i < l/2; i++ {
S.Add(Range{kv[2*i], kv[2*i+1]})
}
return S
}
testv := testEntry{
E(
S(), // A
S(), // B
S(), // U
S()), // D
E(
S(1,2), // A
S(1,2), // B
S(1,2), // U
S()), // D
// XXX
}
}
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