Commit 64b03b76 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent db2d48ae
......@@ -66,10 +66,6 @@ func (M *RangedMap) Get(k Key) VALUE {
return v
}
// Get_ is comma-ok version of Get.
func (M *RangedMap) Get_(k Key) (VALUE, bool) {
panic("TODO") // XXX
}
// SetRange changes M to map key range r to value v.
......@@ -87,49 +83,38 @@ func (M *RangedMap) HasRange(r KeyRange) bool {
panic("TODO") // XXX
}
// --------
// XXX Equal ?
// XXX AllEntries ?
// Get_ is comma-ok version of Get.
func (M *RangedMap) Get_(k Key) (VALUE, bool) {
panic("TODO") // XXX
}
// AddRange adds range r to the set.
func (S *RangedKeySet) AddRange(r KeyRange) {
// SetRange changes M to map key range r to value v.
func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
if traceRangeSet {
fmt.Printf("\n\nAddRange:\n")
fmt.Printf(" S: %s\n", S)
fmt.Printf(" M: %s\n", M)
fmt.Printf(" r: %s\n", r)
defer fmt.Printf("->u: %s\n", S)
fmt.Printf(" v: %s\n", v)
defer fmt.Printf("->u: %s\n", M)
}
S.verify()
defer S.verify()
M.verify()
defer M.verify()
// 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_
})
debugfRSet("\tilo: %d\n", ilo)
debugfRMap("\tilo: %d\n", ilo)
if ilo == l { // not found
S.rangev = append(S.rangev, r)
l++
debugfRSet("\tappend %s\t-> %s\n", r, S)
debugfRMap("\tappend %s\t-> %s\n", r, S)
}
// find last jhi: [jhi].Lo < r.hi
......@@ -143,32 +128,32 @@ func (S *RangedKeySet) AddRange(r KeyRange) {
}
break
}
debugfRSet("\tjhi: %d\n", jhi)
debugfRMap("\tjhi: %d\n", jhi)
// entries in [ilo:jhi) [r.Lo,r.hi) and should be merged into one
if (jhi - ilo) > 1 {
lo := S.rangev[ilo].Lo
hi_ := S.rangev[jhi-1].Hi_
vReplaceSlice(&S.rangev, ilo,jhi, KeyRange{lo,hi_})
debugfRSet("\tmerge S[%d:%d]\t-> %s\n", ilo, jhi, S)
debugfRMap("\tmerge S[%d:%d]\t-> %s\n", ilo, jhi, S)
}
jhi = -1 // no longer valid
// if [r.lo,r.hi) was outside of any entry - create new entry
if r.Hi_ < S.rangev[ilo].Lo {
vInsert(&S.rangev, ilo, r)
debugfRSet("\tinsert %s\t-> %s\n", r, S)
debugfRMap("\tinsert %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
debugfRSet("\textend left\t-> %s\n", S)
debugfRMap("\textend left\t-> %s\n", S)
}
if r.Hi_ > S.rangev[ilo].Hi_ {
S.rangev[ilo].Hi_ = r.Hi_
debugfRSet("\textend right\t-> %s\n", S)
debugfRMap("\textend right\t-> %s\n", S)
}
// and check if we should merge it with right/left neighbours
......@@ -176,7 +161,7 @@ func (S *RangedKeySet) AddRange(r KeyRange) {
if S.rangev[ilo].Hi_+1 == S.rangev[ilo+1].Lo {
vReplaceSlice(&S.rangev, ilo,ilo+2,
KeyRange{S.rangev[ilo].Lo, S.rangev[ilo+1].Hi_})
debugfRSet("\tmerge right\t-> %s\n", S)
debugfRMap("\tmerge right\t-> %s\n", S)
}
}
......@@ -184,7 +169,7 @@ func (S *RangedKeySet) AddRange(r KeyRange) {
if S.rangev[ilo-1].Hi_+1 == S.rangev[ilo].Lo {
vReplaceSlice(&S.rangev, ilo-1,ilo+1,
KeyRange{S.rangev[ilo-1].Lo, S.rangev[ilo].Hi_})
debugfRSet("\tmerge left\t-> %s\n", S)
debugfRMap("\tmerge left\t-> %s\n", S)
}
}
......@@ -208,10 +193,10 @@ func (S *RangedKeySet) DelRange(r KeyRange) {
ilo := sort.Search(l, func(i int) bool {
return r.Lo <= S.rangev[i].Hi_
})
debugfRSet("\tilo: %d\n", ilo)
debugfRMap("\tilo: %d\n", ilo)
if ilo == l { // not found
debugfRSet("\tnon-overlap right\n")
debugfRMap("\tnon-overlap right\n")
return
}
......@@ -226,10 +211,10 @@ func (S *RangedKeySet) DelRange(r KeyRange) {
}
break
}
debugfRSet("\tjhi: %d\n", jhi)
debugfRMap("\tjhi: %d\n", jhi)
if jhi == 0 {
debugfRSet("\tnon-overlap left\n")
debugfRMap("\tnon-overlap left\n")
return
}
......@@ -240,22 +225,22 @@ func (S *RangedKeySet) DelRange(r KeyRange) {
x := S.rangev[ilo]
vInsert(&S.rangev, ilo, x)
jhi++
debugfRSet("\tpresplit copy %s\t-> %s\n", x, S)
debugfRMap("\tpresplit copy %s\t-> %s\n", x, S)
}
if S.rangev[ilo].Lo < r.Lo { // shrink left
S.rangev[ilo] = KeyRange{S.rangev[ilo].Lo, r.Lo-1}
ilo++
debugfRSet("\tshrink [%d] left\t-> %s\n", ilo, S)
debugfRMap("\tshrink [%d] left\t-> %s\n", ilo, S)
}
if r.Hi_ < S.rangev[jhi-1].Hi_ { // shrink right
S.rangev[jhi-1] = KeyRange{r.Hi_+1, S.rangev[jhi-1].Hi_}
jhi--
debugfRSet("\tshrink [%d] right\t-> %s\n", jhi-1, S)
debugfRMap("\tshrink [%d] right\t-> %s\n", jhi-1, S)
}
if (jhi - ilo) > 0 {
vDeleteSlice(&S.rangev, ilo,jhi)
debugfRSet("\tdelete S[%d:%d]\t-> %s\n", ilo, jhi, S)
debugfRMap("\tdelete S[%d:%d]\t-> %s\n", ilo, jhi, S)
}
// done
......@@ -279,7 +264,7 @@ func (S *RangedKeySet) HasRange(r KeyRange) (yes bool) {
ilo := sort.Search(l, func(i int) bool {
return r.Lo <= S.rangev[i].Hi_
})
debugfRSet("\tilo: %d\n", ilo)
debugfRMap("\tilo: %d\n", ilo)
if ilo == l { // not found
return false
......@@ -389,8 +374,8 @@ func (M RangedMap) String() string {
}
func debugfRSet(format string, argv ...interface{}) {
if !debugRangeSet {
func debugfRMap(format string, argv ...interface{}) {
if !debugRangeMap {
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