Commit 696428d9 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 64b03b76
......@@ -40,7 +40,7 @@ type RangedMap struct {
// mapEntry represents one entry in RangedMap.
type mapEntry struct {
keycov KeyRange
KeyRange
value VALUE
}
......@@ -68,16 +68,6 @@ func (M *RangedMap) Get(k Key) VALUE {
// SetRange changes M to map key range r to value v.
func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
panic("TODO") // XXX
}
// DelRange removes range r from the map.
func (M *RangedMap) DelRange(r KeyRange) {
panic("TODO") // XXX
}
// HasRange returns whether all keys from range r belong to the map.
func (M *RangedMap) HasRange(r KeyRange) bool {
panic("TODO") // XXX
......@@ -93,11 +83,11 @@ func (M *RangedMap) Get_(k Key) (VALUE, bool) {
// SetRange changes M to map key range r to value v.
func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
e := mapEntry{r,v}
if traceRangeSet {
fmt.Printf("\n\nAddRange:\n")
fmt.Printf("\n\nSetRange:\n")
fmt.Printf(" M: %s\n", M)
fmt.Printf(" r: %s\n", r)
fmt.Printf(" v: %s\n", v)
fmt.Printf(" e: %s\n", e)
defer fmt.Printf("->u: %s\n", M)
}
......@@ -105,16 +95,16 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
defer M.verify()
// find first ilo: r.Lo < [ilo].hi
l := len(S.rangev)
l := len(M.entryv)
ilo := sort.Search(l, func(i int) bool {
return r.Lo <= S.rangev[i].Hi_
return r.Lo <= M.entryv[i].Hi_
})
debugfRMap("\tilo: %d\n", ilo)
if ilo == l { // not found
S.rangev = append(S.rangev, r)
M.entryv = append(M.entryv, e)
l++
debugfRMap("\tappend %s\t-> %s\n", r, S)
debugfRMap("\tappend %s\t-> %s\n", e, M)
}
// find last jhi: [jhi].Lo < r.hi
......@@ -123,7 +113,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
if jhi == l {
break
}
if S.rangev[jhi].Lo <= r.Hi_ {
if M.entryv[jhi].Lo <= r.Hi_ {
continue
}
break
......@@ -131,6 +121,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
debugfRMap("\tjhi: %d\n", jhi)
// entries in [ilo:jhi) [r.Lo,r.hi) and should be merged into one
// XXX check different values
if (jhi - ilo) > 1 {
lo := S.rangev[ilo].Lo
hi_ := S.rangev[jhi-1].Hi_
......@@ -158,7 +149,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
// and check if we should merge it with right/left neighbours
if ilo+1 < len(S.rangev) { // right
if S.rangev[ilo].Hi_+1 == S.rangev[ilo+1].Lo {
if S.rangev[ilo].Hi_+1 == S.rangev[ilo+1].Lo { // XXX && .value same
vReplaceSlice(&S.rangev, ilo,ilo+2,
KeyRange{S.rangev[ilo].Lo, S.rangev[ilo+1].Hi_})
debugfRMap("\tmerge right\t-> %s\n", S)
......@@ -166,7 +157,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
}
if ilo > 0 { // left
if S.rangev[ilo-1].Hi_+1 == S.rangev[ilo].Lo {
if S.rangev[ilo-1].Hi_+1 == S.rangev[ilo].Lo { // XXX && .value same
vReplaceSlice(&S.rangev, ilo-1,ilo+1,
KeyRange{S.rangev[ilo-1].Lo, S.rangev[ilo].Hi_})
debugfRMap("\tmerge left\t-> %s\n", S)
......@@ -176,22 +167,22 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
// done
}
// DelRange removes range r from the set.
func (S *RangedKeySet) DelRange(r KeyRange) {
// DelRange removes range r from the map.
func (M *RangedKeySet) DelRange(r KeyRange) {
if traceRangeSet {
fmt.Printf("\n\nDelRange:\n")
fmt.Printf(" S: %s\n", S)
fmt.Printf(" M: %s\n", M)
fmt.Printf(" r: %s\n", r)
defer fmt.Printf("->d: %s\n", S)
defer fmt.Printf("->d: %s\n", M)
}
S.verify()
defer S.verify()
M.verify()
defer M.verify()
// find first ilo: r.Lo < [ilo].hi
l := len(S.rangev)
l := len(M.entryv)
ilo := sort.Search(l, func(i int) bool {
return r.Lo <= S.rangev[i].Hi_
return r.Lo <= M.entryv[i].Hi_
})
debugfRMap("\tilo: %d\n", ilo)
......@@ -206,7 +197,7 @@ func (S *RangedKeySet) DelRange(r KeyRange) {
if jhi == l {
break
}
if S.rangev[jhi].Lo <= r.Hi_ {
if M.entryv[jhi].Lo <= r.Hi_ {
continue
}
break
......@@ -221,26 +212,26 @@ func (S *RangedKeySet) DelRange(r KeyRange) {
// [ilo+1:jhi-1] should be deleted
// [ilo] and [jhi-1] overlap with [r.lo,r.hi) - they should be deleted, or shrinked,
// or split+shrinked if ilo==jhi-1 and r is inside [ilo]
if jhi-ilo == 1 && S.rangev[ilo].Lo < r.Lo && r.Hi_ < S.rangev[ilo].Hi_ {
x := S.rangev[ilo]
vInsert(&S.rangev, ilo, x)
if jhi-ilo == 1 && M.entryv[ilo].Lo < r.Lo && r.Hi_ < M.entryv[ilo].Hi_ {
x := M.entryv[ilo]
vInsert(&M.entryv, ilo, x)
jhi++
debugfRMap("\tpresplit copy %s\t-> %s\n", x, S)
debugfRMap("\tpresplit copy %s\t-> %s\n", x, M)
}
if S.rangev[ilo].Lo < r.Lo { // shrink left
S.rangev[ilo] = KeyRange{S.rangev[ilo].Lo, r.Lo-1}
if M.entryv[ilo].Lo < r.Lo { // shrink left
M.entryv[ilo].Hi_ = r.Lo-1
ilo++
debugfRMap("\tshrink [%d] left\t-> %s\n", ilo, S)
debugfRMap("\tshrink [%d] left\t-> %s\n", ilo, M)
}
if r.Hi_ < S.rangev[jhi-1].Hi_ { // shrink right
S.rangev[jhi-1] = KeyRange{r.Hi_+1, S.rangev[jhi-1].Hi_}
if r.Hi_ < M.entryv[jhi-1].Hi_ { // shrink right
M.entryv[jhi-1].Lo = r.Hi_+1
jhi--
debugfRMap("\tshrink [%d] right\t-> %s\n", jhi-1, S)
debugfRMap("\tshrink [%d] right\t-> %s\n", jhi-1, M)
}
if (jhi - ilo) > 0 {
vDeleteSlice(&S.rangev, ilo,jhi)
debugfRMap("\tdelete S[%d:%d]\t-> %s\n", ilo, jhi, S)
vDeleteSlice(&M.entryv, ilo,jhi)
debugfRMap("\tdelete M[%d:%d]\t-> %s\n", ilo, jhi, M)
}
// done
......@@ -315,7 +306,7 @@ func (M *RangedMap) verify() {
}
}
if !(e.Lo <= e.Hi_) {
badf("[%d]: !(e.lo <= e.hi_)", i)
badf("[%d]: !(e.lo e.hi_)", i)
}
hi_Prev = e.Hi_
v_Prev = e.value
......@@ -367,12 +358,21 @@ func (M RangedMap) String() string {
if i > 0 {
s += " "
}
s += fmt.Sprintf("%s:%v", e.keycov, e.value)
s += e.String()
}
s += "}"
return s
}
func (e *mapEntry) String() string {
s := e.keycov.String()
v := fmt.Sprintf("%v", e.value)
if v != "" { // omit ":<v>" in the case of set
s += ":" + v
}
return s
}
func debugfRMap(format string, argv ...interface{}) {
if !debugRangeMap {
......
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