Reference to Map element?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Reference to Map element?

Post by Coiler » 30 Dec 2020, 17:36

Is there any way to reference a map element? Say, for example, to perform 2 operations on it without having to look it up twice? Here's an example of what I mean:

Code: Select all

tmap := map()
; ...add elements to tmap here

; get current element value
element := tmap[ "my_lookup_string" ]

; update the element value
if( element * element > 500 )
	element := 500 / (element * element)

; update the element value in the map
tmap[ "my_lookup_string" ] := element
Is there any way to perform something like above without having to do the map->string->table lookup twice (once to get the value and once to set the value)?

Related to something else, I timed the operations, and it appears that looking up elements from a large map of 250 elements is actually more efficient than constantly adding and removing from a small map of about 5 elements (it was close, but the large map was always faster). This means item lookup is actually faster than element addition/removal, which almost certainly means its using a hash map to find its elements. This is great/efficient, but is there any way for us to access the lookup data? Or any way for us to store a temporary node or reference to access the same element multiple times in a situation?

Thanks for any advice!

Edit: Sorry, didn't realize maps were new in V2. Thanks for moving, gregster.
Last edited by gregster on 30 Dec 2020, 17:53, edited 1 time in total.
Reason: Topic moved to 'v2 Ask For Help'
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Reference to Map element?

Post by Coiler » 30 Dec 2020, 21:23

Believe it or not, I came up with a simple solution. Just wrap the map elements into a class object:

Code: Select all

class VALUE
{
	__New(v)
	{
		this.Value := v
	}
}

tmap := map()
; ...add elements to tmap here
tmap["some_id_1"] := VALUE.New( 10)
tmap["some_id_2"] := VALUE.New( 40)
tmap["some_id_3"] := VALUE.New( 60)

; get current element value
element := tmap[ "some_id_2" ]

; update the element value
if( element.Value * element.Value > 30 )
	element.Value := 500 / (element.Value * element.Value)
	
; element is already updated
Since class instance variables are references by default, it works without trying. I wonder what other situations classes could be used as "super pointers" in? Pretty much any situation where you need multiple references to a single variable.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Reference to Map element?

Post by Helgef » 04 Jan 2021, 10:22

Is there any way to perform something like above without having to do the map->string->table lookup twice
No.
Believe it or not, I came up with a simple solution
No you didn't, at least not if you want to avoid the looking-up stuff, i.e., in

Code: Select all

if( element.Value * element.Value > 30 )
	element.Value := 500 / (element.Value * element.Value)
you potentially do 5 such look-ups.

Cheers :champagne: :fireworks:
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Reference to Map element?

Post by Coiler » 17 Jan 2021, 17:22

You're saying referencing a map key lookup some how references the lookup itself, rather than the object that exists in the hash table? Why on earth would it work that way?

If that's the case, then define a linear array of data/objects, and store their indices in the map. Once an object is located by index, it can be updated any number of times without messing with the map.

Or were you just referring to the fact that accessing any variable by name is looking stuff up in the background? If so, you're probably right. I'm still not used to coding in uncompiled scripting languages.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Reference to Map element?

Post by swagfag » 23 Apr 2021, 10:56

which almost certainly means its using a hash map to find its elements.
its not. it sorts numbers, objects and string in that order in an array, so it can then binary search for the key
you potentially do 5 such look-ups.
you're saying referencing a map key lookup some how references the lookup itself, rather than the object that exists in the hash table?
element.Value = one lookup of the object's .Value own property using the same binary search algorithm a Map would have used. ure saving nothing, in fact considering ure were originally concerned about two Map lookups, ure making it even worse.
If that's the case, then define a linear array of data/objects, and store their indices in the map. Once an object is located by index, it can be updated any number of times without messing with the map.
but now ull have to take care of 2 entities, keep them in sync, invalidate, etc. Other aspects would be impacted - insertion, deletion and so. its all trade-offs and im not sure this is one worth making for an interpreted scripting language
Post Reply

Return to “Ask for Help (v2)”