Commit 7e092087 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 51d449a1
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
"sort" "sort"
) )
const traceRangeSet = true
const debugRangeSet = true const debugRangeSet = true
// Range represents [lo,hi) Key range. // Range represents [lo,hi) Key range.
...@@ -57,7 +58,7 @@ func (S *RangeSet) Del(k Key) { ...@@ -57,7 +58,7 @@ func (S *RangeSet) Del(k Key) {
// AddRange adds Range r to the set. // AddRange adds Range r to the set.
func (S *RangeSet) AddRange(r Range) { func (S *RangeSet) AddRange(r Range) {
if debugRangeSet { if traceRangeSet {
fmt.Printf("\n\nAddRange:\n") fmt.Printf("\n\nAddRange:\n")
fmt.Printf(" S: %s\n", S) fmt.Printf(" S: %s\n", S)
fmt.Printf(" r: %s\n", r) fmt.Printf(" r: %s\n", r)
...@@ -72,12 +73,12 @@ func (S *RangeSet) AddRange(r Range) { ...@@ -72,12 +73,12 @@ func (S *RangeSet) AddRange(r Range) {
ilo := sort.Search(l, func(i int) bool { ilo := sort.Search(l, func(i int) bool {
return r.lo <= S.rangev[i].hi_ return r.lo <= S.rangev[i].hi_
}) })
tracefRSet("\tilo: %d\n", ilo) debugfRSet("\tilo: %d\n", ilo)
if ilo == l { // not found if ilo == l { // not found
S.rangev = append(S.rangev, r) S.rangev = append(S.rangev, r)
l++ l++
tracefRSet("\tappend %s\t-> %s\n", r, S) debugfRSet("\tappend %s\t-> %s\n", r, S)
} }
// find last jhi: [jhi].lo < r.hi // find last jhi: [jhi].lo < r.hi
...@@ -91,7 +92,7 @@ func (S *RangeSet) AddRange(r Range) { ...@@ -91,7 +92,7 @@ func (S *RangeSet) AddRange(r Range) {
} }
break break
} }
tracefRSet("\tjhi: %d\n", jhi) debugfRSet("\tjhi: %d\n", jhi)
// entries in [ilo:jhi) ∈ [r.lo,r.hi) and should be merged into one // entries in [ilo:jhi) ∈ [r.lo,r.hi) and should be merged into one
if (jhi - ilo) > 1 { if (jhi - ilo) > 1 {
...@@ -101,7 +102,7 @@ func (S *RangeSet) AddRange(r Range) { ...@@ -101,7 +102,7 @@ func (S *RangeSet) AddRange(r Range) {
S.rangev[:ilo], append([]Range{ S.rangev[:ilo], append([]Range{
Range{lo, hi_}}, Range{lo, hi_}},
S.rangev[jhi:]...)...) S.rangev[jhi:]...)...)
tracefRSet("\tmerge [%d:%d]\t-> %s\n", ilo, jhi, S) debugfRSet("\tmerge [%d:%d]\t-> %s\n", ilo, jhi, S)
} }
jhi = -1 // no longer valid jhi = -1 // no longer valid
...@@ -111,18 +112,18 @@ func (S *RangeSet) AddRange(r Range) { ...@@ -111,18 +112,18 @@ func (S *RangeSet) AddRange(r Range) {
S.rangev[:ilo], append([]Range{ S.rangev[:ilo], append([]Range{
r}, r},
S.rangev[ilo:]...)...) S.rangev[ilo:]...)...)
tracefRSet("\tinsert %s\t-> %s\n", r, S) debugfRSet("\tinsert %s\t-> %s\n", r, S)
} }
// now we have covered entries merged as needed into [ilo] // now we have covered entries merged as needed into [ilo]
// extend this entry if r coverage is wider // extend this entry if r coverage is wider
if r.lo < S.rangev[ilo].lo { if r.lo < S.rangev[ilo].lo {
S.rangev[ilo].lo = r.lo S.rangev[ilo].lo = r.lo
tracefRSet("\textend left\t-> %s\n", S) debugfRSet("\textend left\t-> %s\n", S)
} }
if r.hi_ > S.rangev[ilo].hi_ { if r.hi_ > S.rangev[ilo].hi_ {
S.rangev[ilo].hi_ = r.hi_ S.rangev[ilo].hi_ = r.hi_
tracefRSet("\textend right\t-> %s\n", S) debugfRSet("\textend right\t-> %s\n", S)
} }
// and check if we should merge it with right/left neighbours // and check if we should merge it with right/left neighbours
...@@ -132,7 +133,7 @@ func (S *RangeSet) AddRange(r Range) { ...@@ -132,7 +133,7 @@ func (S *RangeSet) AddRange(r Range) {
S.rangev[:ilo], append([]Range{ S.rangev[:ilo], append([]Range{
Range{S.rangev[ilo].lo, S.rangev[ilo+1].hi_}}, Range{S.rangev[ilo].lo, S.rangev[ilo+1].hi_}},
S.rangev[ilo+2:]...)...) S.rangev[ilo+2:]...)...)
tracefRSet("\tmerge right\t-> %s\n", S) debugfRSet("\tmerge right\t-> %s\n", S)
} }
} }
...@@ -142,7 +143,7 @@ func (S *RangeSet) AddRange(r Range) { ...@@ -142,7 +143,7 @@ func (S *RangeSet) AddRange(r Range) {
S.rangev[:ilo-1], append([]Range{ S.rangev[:ilo-1], append([]Range{
Range{S.rangev[ilo-1].lo, S.rangev[ilo].hi_}}, Range{S.rangev[ilo-1].lo, S.rangev[ilo].hi_}},
S.rangev[ilo+1:]...)...) S.rangev[ilo+1:]...)...)
tracefRSet("\tmerge left\t-> %s\n", S) debugfRSet("\tmerge left\t-> %s\n", S)
} }
} }
...@@ -151,7 +152,7 @@ func (S *RangeSet) AddRange(r Range) { ...@@ -151,7 +152,7 @@ func (S *RangeSet) AddRange(r Range) {
// Del removes Range r from the set. // Del removes Range r from the set.
func (S *RangeSet) DelRange(r Range) { func (S *RangeSet) DelRange(r Range) {
if debugRangeSet { if traceRangeSet {
fmt.Printf("\n\nDelRange:\n") fmt.Printf("\n\nDelRange:\n")
fmt.Printf(" S: %s\n", S) fmt.Printf(" S: %s\n", S)
fmt.Printf(" r: %s\n", r) fmt.Printf(" r: %s\n", r)
...@@ -281,7 +282,7 @@ func (r Range) String() string { ...@@ -281,7 +282,7 @@ func (r Range) String() string {
} }
func tracefRSet(format string, argv ...interface{}) { func debugfRSet(format string, argv ...interface{}) {
if !debugRangeSet { if !debugRangeSet {
return return
} }
......
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