Commit 49dcaf65 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent fa425e7a
......@@ -72,29 +72,36 @@ func (S *RangeSet) AddRange(r Range) {
ilo := sort.Search(l, func(i int) bool {
return r.lo <= S.rangev[i].hi_
})
tracefRSet("\tilo: %d\n", ilo)
if ilo == l { // not found
S.rangev = append(S.rangev, r)
l++
tracefRSet("\tappend %s\t-> %s\n", r, S)
}
// find firt jhi: r.hi > =(?) [jhi].lo
// find last jhi: [jhi].lo < r.hi
jhi := ilo
for ; jhi < l; jhi++ {
if r.hi_ >= S.rangev[jhi].lo {
for ;; jhi++ {
if jhi == l {
break
}
if S.rangev[jhi].lo <= r.hi_ {
continue
}
break
}
tracefRSet("\tjhi: %d\n", jhi)
// entries in [ilo:jhi) ∈ [r.lo,r.hi) and should be merged into one
if ilo < jhi {
if (jhi - ilo) > 1 {
lo := S.rangev[ilo].lo
hi_ := S.rangev[jhi-1].hi_
S.rangev = append(
S.rangev[:ilo], append([]Range{
Range{lo, hi_}},
S.rangev[jhi:]...)...)
// debugfRangeSet("\tmerge [%d:%d] -> %s\n", S)
tracefRSet("\tmerge [%d:%d]\t-> %s\n", ilo, jhi, S)
}
// if [r.lo,r.hi) was outside of any entry - create new entry
......@@ -103,15 +110,18 @@ func (S *RangeSet) AddRange(r Range) {
S.rangev[:ilo], append([]Range{
r},
S.rangev[ilo:]...)...)
tracefRSet("\tinsert new %s\t-> %s\n", r, S)
}
// 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
tracefRSet("\textend left\t-> %s\n", S)
}
if r.hi_ > S.rangev[ilo].hi_ {
S.rangev[ilo].hi_ = r.hi_
tracefRSet("\textend right\t-> %s\n", S)
}
// and check if we should merge it with right/left neighbours
......@@ -121,6 +131,7 @@ func (S *RangeSet) AddRange(r Range) {
S.rangev[:ilo], append([]Range{
Range{S.rangev[ilo].lo, S.rangev[ilo+1].hi_}},
S.rangev[ilo+2:]...)...)
tracefRSet("\tmerge right\t-> %s\n", S)
}
}
......@@ -130,6 +141,7 @@ func (S *RangeSet) AddRange(r Range) {
S.rangev[:ilo-1], append([]Range{
Range{S.rangev[ilo-1].lo, S.rangev[ilo].hi_}},
S.rangev[ilo+1:]...)...)
tracefRSet("\tmerge left\t-> %s\n", S)
}
}
......@@ -266,3 +278,11 @@ func (r Range) String() string {
shi := "∞"; if r.hi_ < KeyMax { shi = fmt.Sprintf("%v", r.hi_+1) }
return fmt.Sprintf("[%s,%s)", slo, shi)
}
func tracefRSet(format string, argv ...interface{}) {
if !debugRangeSet {
return
}
fmt.Printf(format, argv...)
}
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