Commit 97edfd89 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent eff86eeb
......@@ -22,7 +22,7 @@ package xbtree
import (
"fmt"
// "sort"
"sort"
)
// Range represents [lo,hi) Key range.
......@@ -58,7 +58,63 @@ func (S *RangeSet) AddRange(r Range) {
S.verify()
defer S.verify()
panic("TODO") // XXX
// find first ilo: r.lo < [ilo].hi
l := len(S.rangev)
ilo := sort.Search(l, func(i int) bool {
return r.lo <= S.rangev[i].hi_
})
if ilo == l { // not found
// XXX append r; l++
// XXX check & merge with prev
}
// find firt jhi: r.hi > =(?) [jhi].lo
jhi := ilo
for ; jhi < l; jhi++ {
if r.hi_ >= S.rangev[jhi].lo {
break
}
}
// entries in [ilo:jhi) ∈ [r.lo,r.hi) and should be merged into one
if ilo < jhi {
lo := S.rangev[ilo].lo
hi_ := S.rangev[jhi-1].hi_
S.rangev = append(
S.rangev[:ilo],
Range{lo, hi_},
S.rangev[jhi:]...)
}
// if [r.lo,r.hi) was outside of any entry - create new entry
if r.hi_ <= S.rangev[ilo].lo {
S.rangev = append(
S.rangev[:ilo],
r,
S.rangev[ilo:])
}
// now we have covered entries merged as needed into [ilo]
// extend this entry if r coverage is wider
if r.lo < S.rangev[ilo].lo {
S.rangev[ilo].lo = r.lo
}
if r.hi_ > S.rangev[ilo].hi_ {
S.rangev[ilo].hi_ = r.hi_
}
// and check if we should merge it with right/left neighbours
if ilo < len(S.rangev) {
if S.rangev[ilo].hi_ == S.rangev[ilo+1].lo {
S.rangev = append(
S.rangev[:ilo],
Range{S.rangev[ilo].lo, S.rangev[ilo+1].hi_},
S.rangev[ilo+2:]...)
}
}
// done
}
// Del removes Range r from the set.
......@@ -66,6 +122,7 @@ func (S *RangeSet) DelRange(r Range) {
S.verify()
defer S.verify()
return // XX temp
panic("TODO") // XXX
}
......
......@@ -93,6 +93,16 @@ func TestRangeSet(t *testing.T) {
S(noo, oo), // B
S(1,3, 5,7, 11,100), // U
S()), // D
// [1,3) [5,7) + insert [3,5) -> [1,7)
E(
S(1,3, 5,7), // A
S(3,5), // B
S(1,7), // U
S(1,3, 5,7)), // D
// XXX Add to left - merge
// XXX Add to right - merge
}
for _, tt := range testv {
......
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