Find in assoc array then iterate
Kevin Bailey
keraba at yahoo.com
Sat Oct 22 15:21:07 UTC 2022
Siarhei,
Thanks for this possibility. I didn't know D had "pointers" like
this.
Unfortunately, it appears that you can't pick up where you left
off with
that pointer? You have to re-scan forward?
bachmeier,
I didn't reply to Steven's question; I replied to his claim that
there
was no value. I found his response insulting and patronizing, and
has
no place on a public forum helping evangelize D. See Siarhei's
response
for a better way.
Siarhei,
I'm attempting to do something like a DFS down a fixed list of
strings.
It's doing something like generating permutations of the strings,
however
the conditions for the search are fairly complicated, and of
course I'm
trying to do as much "early exit" as possible. That is, I can
prune
branches just seeing the first or second word.
IOW, if D could, for example, "iterate through all 3 word
combinations
in this list", it would be useless to this problem (or at least
*very*
expensive.) OTOH, a forward iterator (i.e. copyable but does not
need to
go backwards) solves the problem elegantly and efficiently.
Go-lang supports it too:
package main
import "fmt"
import "reflect"
func main() {
m := make(map[string]int)
m["key1"] = 7
m["key2"] = 8
iter := reflect.ValueOf(m).MapRange()
// Advance iterator
iter.Next()
// Preserve position
iter2 := *iter
// Exhaust first iterator
for iter.Next() {
fmt.Println("iter: ", iter.Key())
}
// What does second iterator see?
for iter2.Next() {
fmt.Println("iter2: ", iter2.Key())
}
}
will correctly print:
iter: key2
iter2: key2
More information about the Digitalmars-d-learn
mailing list