Re: [go-nuts] Question about iterator implementation of golang map

2019-07-15 Thread Jan Mercl
On Mon, Jul 15, 2019 at 4:39 PM Aaron Lau  wrote:

> We won't update the map during the traversal process. So why use mapaccessK 
> to find the k & v again ?

The comment explains that very clearly:

> // The hash table has grown since the iterator was started.
> // The golden data for this key is now somewhere else.
> // Check the current hash table for the data.
> // This code handles the case where the key
> // has been deleted, updated, or deleted and reinserted.
> // NOTE: we need to regrab the key as it has potentially been
> // updated to an equal() but not identical key (e.g. +0.0 vs -0.0).
> rk, rv := mapaccessK(t, h, k)

Iteration does nut mutate the map, correct. But the code that iterates
the map may mutate it.

for k, v := range m {
   if cond(k, v) {
   m[2*k] = 3*v
}
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-Uu1uxvxj%3D6%2BTOUA4h-JbPoqtxsM5CHZnmp%2B1H8SvfSWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Question about iterator implementation of golang map

2019-07-15 Thread Aaron Lau
Question:
Where
In function mapiternext
Why
We won't update the map during the traversal process. So why use mapaccessK 
to find the k & v again ?

if (b.tophash[offi] != evacuatedX && b.tophash[offi] != evacuatedY) ||
!(t.reflexivekey() || alg.equal(k, k)) {
// This is the golden data, we can return it.
// OR
// key!=key, so the entry can't be deleted or updated, so we can just 
return it.
// That's lucky for us because when key!=key we can't look it up 
successfully.
it.key = k
if t.indirectvalue() {
v = *((*unsafe.Pointer)(v))
}
it.value = v
} else {
// The hash table has grown since the iterator was started.
// The golden data for this key is now somewhere else.
// Check the current hash table for the data.
// This code handles the case where the key
// has been deleted, updated, or deleted and reinserted.
// NOTE: we need to regrab the key as it has potentially been
// updated to an equal() but not identical key (e.g. +0.0 vs -0.0).
rk, rv := mapaccessK(t, h, k)
if rk == nil {
continue // key has been deleted
}
it.key = rk
it.value = rv
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5f491cbe-cd6b-404e-a598-dd6e5f670c8d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.