Karim Belabas on Tue, 23 Apr 2019 23:02:27 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: Iterating over a map content |
* Jérôme Raulin [2019-04-23 22:18]: > Is there a better way of iterating over an entire map content than something > like : > > map = Map(); > \\ Do stuff with map > mapkeys = Vec(map); > for (i = 1, # mapkeys, > key = mapkeys[i]; > value = mapget(map, key); > \\ Do other stuff with (key, value) > ); > > There is at least two issues with this implementation : > * Storage duplication, keys are stored in both map and mapkeys. > * Access performance, all values are looked up from standard map access. Here's a better way: M = Mat(map); for (i = 1, matsize(M)[1], my([key, value] = M[i,]); \\ ... ) You still get storage duplication though, which is unavoidable unless you're willing to destroy the map (e.g., map = Mat(map)) Cheers, K.B. -- Karim Belabas, IMB (UMR 5251) Tel: (+33) (0)5 40 00 26 17 Universite de Bordeaux Fax: (+33) (0)5 40 00 21 23 351, cours de la Liberation http://www.math.u-bordeaux.fr/~kbelabas/ F-33405 Talence (France) http://pari.math.u-bordeaux.fr/ [PARI/GP] `