Map read-only or no-create-on-search states

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
sc4pb
User
User
Posts: 32
Joined: Tue Mar 07, 2023 5:33 pm

Map read-only or no-create-on-search states

Post by sc4pb »

It would be nice if a Map could be optionally set into a state where a search for a non-existent item would not create that item with a default value. In other words this situation:

Code: Select all

NewMap xTest.s()

If xTest("foo")="bar"                   ; if for whatever reason I want to test if a map element has a certain value...
  Debug "bar found"                     ; and of course here it doesn't, it would be nice sometimes if the element weren't created as "" or 0
Else  
  If FindMapElement(xTest(),"foo")      ; in this example "foo" element is now found,
    Debug "foo found!"                  ; even though the intention was never to create it.
  EndIf
EndIf
I know this can be avoided by always using FindMapElement, but that can get bulky. If there was an instruction that would disable this default behavior, like "CreateOnSearch(xTest(),#False)" ... to set it into a mode where xTest("foo") would return "" above, but would also *not* create element "foo", that would be helpful.

I've also thought a SetReadOnly(xTest(),#True) would be handy, meaning even a call to AddMapElement() would fail, preserving the current values in the Map until read-only set back to #False.

As always, thanks for everything!
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Map read-only or no-create-on-search states

Post by Bisonte »

Its a question of the correct order ....

Code: Select all

NewMap xTest.s()

If FindMapElement(xTest(),"foo")
  If xTest() = "bar"
    Debug "bar found"
  Else
    Debug "foo found!"
  EndIf
EndIf
In your code above, "Debug MapSize(xTest())" will show 1.... a new element was created. But in my case it will show 0...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Map read-only or no-create-on-search states

Post by Quin »

+1 to this.
This actually was the normal behavior for a long time, but it was changed in 6.00. More specifically, beta 2.
Fred wrote: Wed May 19, 2021 10:08 am - Changed the way the map elements are created when using passive syntax, to be more consistent. There is no more a dummy element.
I forgot what the exact specifics of it were, but there was a bug where elements would get created when using this syntax sometimes and not always, and for some reason they decided to make it always create the element rather than never, like I'd expect. :?:
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Map read-only or no-create-on-search states

Post by Andre »

There were previous feature requests about this - see here: https://www.purebasic.fr/english/viewtopic.php?p=576259
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Map read-only or no-create-on-search states

Post by skywalk »

Was it because elements are created in both cases below?

mymap("some new key") = "some new value"

If mymap("some new key")
; new null element added
EndIf
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Map read-only or no-create-on-search states

Post by AZJIO »

There are many ways to eliminate an empty element using minimal effort and minimal CPU overhead. If you embed the check inside the programming language, you will no longer be able to remove it; it will always be used and waste CPU time. Even if you use flags, this forces the compiler author to have different algorithms for different flags and maintain this throughout life. Why complicate the language if these capabilities are available in external algorithms?
I would also like to have ready-made external algorithms based on the internal code of the language, while this would attract beginners to PureBasic. But this requires additional effort. Although I am sure that this is precisely what can attract more beginners, and work on the compiler attracts more professionals.

Code: Select all

; example 1 (you can't add many empty elements because the #PB_Map_ElementCheck flag doesn't allow it)
NewMap mm()

For i = 0 To 10
	AddMapElement(mm() , "", #PB_Map_ElementCheck)
Next

Debug MapSize(mm())
ForEach mm()
	Debug "+" + mm()
Next

; example 2 (you can't create an empty key since Asc() prevents that)
NewMap nn()
Define tmp$
For i = 0 To 10
	If Asc(tmp$)
		AddMapElement(nn() , tmp$, #PB_Map_ElementCheck)
	EndIf
Next

Debug MapSize(nn())
ForEach nn()
	Debug "+" + nn()
Next

; example 3 (you can't create an empty value since Asc() prevents that)
NewMap aa()
Define tmp
For i = 0 To 10
	If Asc(tmp$)
		AddMapElement(aa() , "", #PB_Map_ElementCheck)
		aa() = tmp
	EndIf
Next

Debug MapSize(aa())
ForEach aa()
	Debug "+" + aa()
Next
Post Reply