Forum Discussion

EnTerr's avatar
EnTerr
Roku Guru
9 years ago

REP: ifAssociativeArray.Values()

I propose enhancement to AAs - addition of a method .values(), which returns array of all dictionary values, in the matching order to .keys().

In other words, .values() is the counterpart of .keys() - one returns the keys ready to be enumerated, the other returns the values, in the same order. This will allow to do a much faster (>100x) enumeration over the dictionary. Here is a quick simulation: 
d = {}: for i = 1 to 100000: d[i.toStr()] = i: next   'whip us a dictionary'
keys = d.keys()
values = []: for each key in d.keys(): values.push(d[key]): next 'faking .values() here, but note this takes _long_ time'
ti = createObject("roTimeSpan")
ti.mark(): for i = 0 to d.count()-1: key = keys[i]: value = values[i]: next: ? ti.totalMilliSeconds()
'>>> 144  - using array indexing'  
ti.mark(): values.reset(): for each key in keys: value = values.next(): next: ? ti.totalMilliSeconds()
'>>> 107  - using foreach + ifEnum'
ti.mark(): for each key in keys: value = d[key]: next: ? ti.totalMilliSeconds()
'>>> 54330 - whoa, that _is_ slow'

On a side note, here is a bonus idea of extending foreach, so it can do multiple enumerations in parallel, e.g. the above shrinks to:
inverted = {}
for each key in d.keys(), value in d.values():
   inverted[value] = key  'correct only for a map that is a bijection'
next
No RepliesBe the first to reply