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

.

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