Oops ... I've missed the request for this feature, which I would like to have available in PureBasic, too.
Not exactly rarely, I store data in both a
map and a corresponding
list.
The map is good for quick direct access, and the list is good for well-arranged enumeration (e.g. after sorting, or after individual arrangement of the elements by me).
Up to now, I've used a solution according to an
approach by Stargate (German forum). This is demonstrated in the first part of the code below.
However, the disadvantage of this approach is, that the strings used as keys (which can be long) are
stored twice: as map keys, and also in the "key$" field of the structure.
The second part of the code below shows a different approach, that uses pointers to map keys: the keys are only
stored once. If
ChangeCurrentMapElement() (or something equivalent) would be implemented in PureBasic, I think it would be a rather efficient approach for dealing with this kind of stuff.
So +10 for this request from me.
Code: Select all
EnableExplicit
Structure myMember
key$
value.i
EndStructure
NewList myList.myMember()
NewMap *myMap.myMember()
Macro MyNewElement (_key_, _value_)
*myMap(_key_) = AddElement(myList())
myList()\key$ = _key_
myList()\value = _value_
EndMacro
MyNewElement("alpha", 1)
MyNewElement("beta", 2)
MyNewElement("gamma", 3)
MyNewElement("delta", 4)
Debug "-- Quick direct access, using the map:"
Debug "beta = " + *myMap("beta")\value
Debug ""
Debug "-- Well-arranged enumeration, using the list:"
ForEach myList()
Debug myList()\key$ + " = " + myList()\value
Next
Debug ""
Debug "--------------------------------------------------"
Debug ""
Structure yourMember
value.i
EndStructure
NewMap yourMap.yourMember()
NewList *yourList.String()
Macro YourNewElement (_key_, _value_)
yourMap(_key_)\value = _value_
AddElement(*yourList())
*yourList() = @yourMap() - SizeOf(Integer)
EndMacro
YourNewElement("alpha", 1)
YourNewElement("beta", 2)
YourNewElement("gamma", 3)
YourNewElement("delta", 4)
Debug "-- Quick direct access, using the map:"
Debug "beta = " + yourMap("beta")\value
Debug ""
Debug "-- Well-arranged enumeration, using the list and the map:"
ForEach *yourList()
; * In a future PB version, this could be done officially and more
; efficiently e.g. with a built-in command ChangeCurrentMapElement(): *
Debug *yourList()\s + " = " + yourMap(*yourList()\s)\value
Next