Commit 3ecacd99 authored by Kirill Smelkov's avatar Kirill Smelkov

X need to keep Value first so that sizeof(set-entry) = sizeof(KeyRange)

see github.com/golang/go/issues/48651
parent 72974229
...@@ -40,8 +40,8 @@ type RangedMap struct { ...@@ -40,8 +40,8 @@ type RangedMap struct {
// RangedMapEntry represents one entry in RangedMap. // RangedMapEntry represents one entry in RangedMap.
type RangedMapEntry struct { type RangedMapEntry struct {
KeyRange
Value VALUE Value VALUE
KeyRange
} }
...@@ -104,7 +104,7 @@ func (M *RangedMap) Get_(k Key) (v VALUE, ok bool) { ...@@ -104,7 +104,7 @@ func (M *RangedMap) Get_(k Key) (v VALUE, ok bool) {
// SetRange changes M to map key range r to value v. // SetRange changes M to map key range r to value v.
func (M *RangedMap) SetRange(r KeyRange, v VALUE) { func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
e := RangedMapEntry{r,v} e := RangedMapEntry{v,r}
if traceRangeMap { if traceRangeMap {
fmt.Printf("\n\nSetRange:\n") fmt.Printf("\n\nSetRange:\n")
fmt.Printf(" M: %s\n", M) fmt.Printf(" M: %s\n", M)
...@@ -146,8 +146,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) { ...@@ -146,8 +146,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
if (jhi - ilo) > 1 { if (jhi - ilo) > 1 {
lo := M.entryv[ilo].Lo lo := M.entryv[ilo].Lo
hi_ := M.entryv[jhi-1].Hi_ hi_ := M.entryv[jhi-1].Hi_
//vReplaceSlice(&M.entryv, ilo,jhi, KeyRange{lo,hi_}) vReplaceSlice(&M.entryv, ilo,jhi, RangedMapEntry{v, KeyRange{lo,hi_}})
vReplaceSlice(&M.entryv, ilo,jhi, RangedMapEntry{KeyRange{lo,hi_}, v})
debugfRMap("\tmerge M[%d:%d]\t-> %s\n", ilo, jhi, M) debugfRMap("\tmerge M[%d:%d]\t-> %s\n", ilo, jhi, M)
} }
jhi = -1 // no longer valid jhi = -1 // no longer valid
...@@ -175,7 +174,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) { ...@@ -175,7 +174,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
right := M.entryv[ilo+1] right := M.entryv[ilo+1]
if (x.Hi_+1 == right.Lo) && (v == right.Value) { if (x.Hi_+1 == right.Lo) && (v == right.Value) {
vReplaceSlice(&M.entryv, ilo,ilo+2, vReplaceSlice(&M.entryv, ilo,ilo+2,
RangedMapEntry{KeyRange{x.Lo, right.Hi_}, v}) RangedMapEntry{v, KeyRange{x.Lo, right.Hi_}})
debugfRMap("\tmerge right\t-> %s\n", M) debugfRMap("\tmerge right\t-> %s\n", M)
} }
} }
...@@ -185,7 +184,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) { ...@@ -185,7 +184,7 @@ func (M *RangedMap) SetRange(r KeyRange, v VALUE) {
x := M.entryv[ilo] x := M.entryv[ilo]
if (left.Hi_+1 == x.Lo) && (left.Value == v) { if (left.Hi_+1 == x.Lo) && (left.Value == v) {
vReplaceSlice(&M.entryv, ilo-1,ilo+1, vReplaceSlice(&M.entryv, ilo-1,ilo+1,
RangedMapEntry{KeyRange{left.Lo, x.Hi_}, v}) RangedMapEntry{v, KeyRange{left.Lo, x.Hi_}})
debugfRMap("\tmerge left\t-> %s\n", M) debugfRMap("\tmerge left\t-> %s\n", M)
} }
} }
......
...@@ -31,6 +31,9 @@ func TestRangedKeySetTypes(t *testing.T) { ...@@ -31,6 +31,9 @@ func TestRangedKeySetTypes(t *testing.T) {
t.Errorf("sizeof(void) = %d ; want 0", sizeVoid) t.Errorf("sizeof(void) = %d ; want 0", sizeVoid)
} }
// verify sizeof(set-entry) = sizeof(KeyRange)
// NOTE for this to be true Value needs to come first in RangedMapEntry
// (see github.com/golang/go/issues/48651)
sizeKeyRange := unsafe.Sizeof(KeyRange{}) sizeKeyRange := unsafe.Sizeof(KeyRange{})
sizeRangedMap_voidEntry := unsafe.Sizeof(_RangedMap_voidEntry{}) sizeRangedMap_voidEntry := unsafe.Sizeof(_RangedMap_voidEntry{})
if sizeRangedMap_voidEntry != sizeKeyRange { if sizeRangedMap_voidEntry != sizeKeyRange {
...@@ -70,8 +73,8 @@ func TestRangedKeySet(t *testing.T) { ...@@ -70,8 +73,8 @@ func TestRangedKeySet(t *testing.T) {
hi_-- hi_--
} }
S.m.entryv = append(S.m.entryv, _RangedMap_voidEntry{ S.m.entryv = append(S.m.entryv, _RangedMap_voidEntry{
KeyRange{lo, hi_},
void{}, void{},
KeyRange{lo, hi_},
}) })
} }
S.verify() S.verify()
......
...@@ -42,8 +42,8 @@ type _RangedMap_str struct { ...@@ -42,8 +42,8 @@ type _RangedMap_str struct {
// _RangedMap_strEntry represents one entry in _RangedMap_str. // _RangedMap_strEntry represents one entry in _RangedMap_str.
type _RangedMap_strEntry struct { type _RangedMap_strEntry struct {
KeyRange
Value string Value string
KeyRange
} }
...@@ -106,7 +106,7 @@ func (M *_RangedMap_str) Get_(k Key) (v string, ok bool) { ...@@ -106,7 +106,7 @@ func (M *_RangedMap_str) Get_(k Key) (v string, ok bool) {
// SetRange changes M to map key range r to value v. // SetRange changes M to map key range r to value v.
func (M *_RangedMap_str) SetRange(r KeyRange, v string) { func (M *_RangedMap_str) SetRange(r KeyRange, v string) {
e := _RangedMap_strEntry{r,v} e := _RangedMap_strEntry{v,r}
if trace_RangedMap_str { if trace_RangedMap_str {
fmt.Printf("\n\nSetRange:\n") fmt.Printf("\n\nSetRange:\n")
fmt.Printf(" M: %s\n", M) fmt.Printf(" M: %s\n", M)
...@@ -148,8 +148,7 @@ func (M *_RangedMap_str) SetRange(r KeyRange, v string) { ...@@ -148,8 +148,7 @@ func (M *_RangedMap_str) SetRange(r KeyRange, v string) {
if (jhi - ilo) > 1 { if (jhi - ilo) > 1 {
lo := M.entryv[ilo].Lo lo := M.entryv[ilo].Lo
hi_ := M.entryv[jhi-1].Hi_ hi_ := M.entryv[jhi-1].Hi_
//vReplaceSlice__RangedMap_str(&M.entryv, ilo,jhi, KeyRange{lo,hi_}) vReplaceSlice__RangedMap_str(&M.entryv, ilo,jhi, _RangedMap_strEntry{v, KeyRange{lo,hi_}})
vReplaceSlice__RangedMap_str(&M.entryv, ilo,jhi, _RangedMap_strEntry{KeyRange{lo,hi_}, v})
debugf_RangedMap_str("\tmerge M[%d:%d]\t-> %s\n", ilo, jhi, M) debugf_RangedMap_str("\tmerge M[%d:%d]\t-> %s\n", ilo, jhi, M)
} }
jhi = -1 // no longer valid jhi = -1 // no longer valid
...@@ -177,7 +176,7 @@ func (M *_RangedMap_str) SetRange(r KeyRange, v string) { ...@@ -177,7 +176,7 @@ func (M *_RangedMap_str) SetRange(r KeyRange, v string) {
right := M.entryv[ilo+1] right := M.entryv[ilo+1]
if (x.Hi_+1 == right.Lo) && (v == right.Value) { if (x.Hi_+1 == right.Lo) && (v == right.Value) {
vReplaceSlice__RangedMap_str(&M.entryv, ilo,ilo+2, vReplaceSlice__RangedMap_str(&M.entryv, ilo,ilo+2,
_RangedMap_strEntry{KeyRange{x.Lo, right.Hi_}, v}) _RangedMap_strEntry{v, KeyRange{x.Lo, right.Hi_}})
debugf_RangedMap_str("\tmerge right\t-> %s\n", M) debugf_RangedMap_str("\tmerge right\t-> %s\n", M)
} }
} }
...@@ -187,7 +186,7 @@ func (M *_RangedMap_str) SetRange(r KeyRange, v string) { ...@@ -187,7 +186,7 @@ func (M *_RangedMap_str) SetRange(r KeyRange, v string) {
x := M.entryv[ilo] x := M.entryv[ilo]
if (left.Hi_+1 == x.Lo) && (left.Value == v) { if (left.Hi_+1 == x.Lo) && (left.Value == v) {
vReplaceSlice__RangedMap_str(&M.entryv, ilo-1,ilo+1, vReplaceSlice__RangedMap_str(&M.entryv, ilo-1,ilo+1,
_RangedMap_strEntry{KeyRange{left.Lo, x.Hi_}, v}) _RangedMap_strEntry{v, KeyRange{left.Lo, x.Hi_}})
debugf_RangedMap_str("\tmerge left\t-> %s\n", M) debugf_RangedMap_str("\tmerge left\t-> %s\n", M)
} }
} }
......
...@@ -42,8 +42,8 @@ type _RangedMap_void struct { ...@@ -42,8 +42,8 @@ type _RangedMap_void struct {
// _RangedMap_voidEntry represents one entry in _RangedMap_void. // _RangedMap_voidEntry represents one entry in _RangedMap_void.
type _RangedMap_voidEntry struct { type _RangedMap_voidEntry struct {
KeyRange
Value void Value void
KeyRange
} }
...@@ -106,7 +106,7 @@ func (M *_RangedMap_void) Get_(k Key) (v void, ok bool) { ...@@ -106,7 +106,7 @@ func (M *_RangedMap_void) Get_(k Key) (v void, ok bool) {
// SetRange changes M to map key range r to value v. // SetRange changes M to map key range r to value v.
func (M *_RangedMap_void) SetRange(r KeyRange, v void) { func (M *_RangedMap_void) SetRange(r KeyRange, v void) {
e := _RangedMap_voidEntry{r,v} e := _RangedMap_voidEntry{v,r}
if trace_RangedMap_void { if trace_RangedMap_void {
fmt.Printf("\n\nSetRange:\n") fmt.Printf("\n\nSetRange:\n")
fmt.Printf(" M: %s\n", M) fmt.Printf(" M: %s\n", M)
...@@ -148,8 +148,7 @@ func (M *_RangedMap_void) SetRange(r KeyRange, v void) { ...@@ -148,8 +148,7 @@ func (M *_RangedMap_void) SetRange(r KeyRange, v void) {
if (jhi - ilo) > 1 { if (jhi - ilo) > 1 {
lo := M.entryv[ilo].Lo lo := M.entryv[ilo].Lo
hi_ := M.entryv[jhi-1].Hi_ hi_ := M.entryv[jhi-1].Hi_
//vReplaceSlice__RangedMap_void(&M.entryv, ilo,jhi, KeyRange{lo,hi_}) vReplaceSlice__RangedMap_void(&M.entryv, ilo,jhi, _RangedMap_voidEntry{v, KeyRange{lo,hi_}})
vReplaceSlice__RangedMap_void(&M.entryv, ilo,jhi, _RangedMap_voidEntry{KeyRange{lo,hi_}, v})
debugf_RangedMap_void("\tmerge M[%d:%d]\t-> %s\n", ilo, jhi, M) debugf_RangedMap_void("\tmerge M[%d:%d]\t-> %s\n", ilo, jhi, M)
} }
jhi = -1 // no longer valid jhi = -1 // no longer valid
...@@ -177,7 +176,7 @@ func (M *_RangedMap_void) SetRange(r KeyRange, v void) { ...@@ -177,7 +176,7 @@ func (M *_RangedMap_void) SetRange(r KeyRange, v void) {
right := M.entryv[ilo+1] right := M.entryv[ilo+1]
if (x.Hi_+1 == right.Lo) && (v == right.Value) { if (x.Hi_+1 == right.Lo) && (v == right.Value) {
vReplaceSlice__RangedMap_void(&M.entryv, ilo,ilo+2, vReplaceSlice__RangedMap_void(&M.entryv, ilo,ilo+2,
_RangedMap_voidEntry{KeyRange{x.Lo, right.Hi_}, v}) _RangedMap_voidEntry{v, KeyRange{x.Lo, right.Hi_}})
debugf_RangedMap_void("\tmerge right\t-> %s\n", M) debugf_RangedMap_void("\tmerge right\t-> %s\n", M)
} }
} }
...@@ -187,7 +186,7 @@ func (M *_RangedMap_void) SetRange(r KeyRange, v void) { ...@@ -187,7 +186,7 @@ func (M *_RangedMap_void) SetRange(r KeyRange, v void) {
x := M.entryv[ilo] x := M.entryv[ilo]
if (left.Hi_+1 == x.Lo) && (left.Value == v) { if (left.Hi_+1 == x.Lo) && (left.Value == v) {
vReplaceSlice__RangedMap_void(&M.entryv, ilo-1,ilo+1, vReplaceSlice__RangedMap_void(&M.entryv, ilo-1,ilo+1,
_RangedMap_voidEntry{KeyRange{left.Lo, x.Hi_}, v}) _RangedMap_voidEntry{v, KeyRange{left.Lo, x.Hi_}})
debugf_RangedMap_void("\tmerge left\t-> %s\n", M) debugf_RangedMap_void("\tmerge left\t-> %s\n", M)
} }
} }
......
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