PureBasic Forum
http://forums.purebasic.com/english/

Map keys behave differently between PB and SB
http://forums.purebasic.com/english/viewtopic.php?f=12&t=75388
Page 1 of 1

Author:  ljgww [ Mon May 25, 2020 3:24 pm ]
Post subject:  Map keys behave differently between PB and SB

It is not safe to assume that keys in the map are behaving in the same way when porting code forth and back between SpiderBasic and PureBasic.

Apparently SpiderBasic preserves map keys in order of populating while PureBasic does something else (likely some hashing for speed optimization)

the code:

Code:
Procedure.s makeword()
  Protected.s word
  Protected.i i
  word = ""
  For i = 1 To 5:
    word + Chr(Random(90,65))
  Next
  ProcedureReturn word
EndProcedure

Global NewList myList.s()
Global NewMap myMap.s()
Global i
Global.s word

Debug "Populate"
For i = 1 To 10:
  word = makeword()
  Debug word
  AddElement(myList())
  myList() = word
  AddMapElement(myMap(), word)
Next

Debug "List:"
ForEach myList()
  Debug myList()
Next

Debug "Map:"
ForEach myMap()
  Debug MapKey(myMap())
Next


SpiderBasic output:

Code:
Populate
LTASV
GJMTS
JJIKJ
ATHOY
ZQSXC
JNFST
UFECP
KLJQQ
RYFRZ
ZHLYK

List:
LTASV
GJMTS
JJIKJ
ATHOY
ZQSXC
JNFST
UFECP
KLJQQ
RYFRZ
ZHLYK

Map:
LTASV
GJMTS
JJIKJ
ATHOY
ZQSXC
JNFST
UFECP
KLJQQ
RYFRZ
ZHLYK


PureBasic output:
Code:
Populate
GLJPG
OBWPJ
QCDLB
TIMXS
YCWMJ
NPSKG
ZRNNL
AXGMG
YZWMQ
XMNYB

List:
GLJPG
OBWPJ
QCDLB
TIMXS
YCWMJ
NPSKG
ZRNNL
AXGMG
YZWMQ
XMNYB

Map:
ZRNNL
QCDLB
TIMXS
YZWMQ
AXGMG
GLJPG
XMNYB
OBWPJ
YCWMJ
NPSKG

Author:  Fred [ Mon May 25, 2020 3:27 pm ]
Post subject:  Re: Map keys behave differently between PB and SB

True. It can also change if we implement a better algorythm in PB, you shouldn't rely on that.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/