PointerLists / PointerMaps

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

PointerLists / PointerMaps

Post by Josh »

For some commands of Lists and Maps a pointer to the element data is returned. For PointerLists and PointerMaps this makes no sense, because the return value is a pointer to a pointer.

Here a pointer is returned that makes no sense:

Code: Select all

*Name = FindMapElement (*Names(), "QueenMum")
Two lines are required:

Code: Select all

FindMapElement (*Names(), "QueenMum")
*Name = *Names()
For PointerLists and PointerMaps not a pointer to the pointer should be returned, but the pointer itself. This should not be a problem for existing code, because the current return value does not make sense.
sorry for my bad english
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: PointerLists / PointerMaps

Post by Demivec »

Not as a comment on the original request but just as a programming note:

use

Code: Select all

Define *Name.String ;dereference pointers with an appropriate structure
*Name = FindMapElement (*Names(), "QueenMum") ;one line
;If *Name <> 0
  Debug *Name\s
;EndIf
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: PointerLists / PointerMaps

Post by Josh »

Most of the time there will be a structure behind the returned pointer. Then .String can't be used anymore :(

Code: Select all

Structure PERSON
  NameFirst$
  NameLast$
EndStructure

NewList Persons.PERSON()
NewMap *Persons.PERSON()
Define *Person .PERSON

AddElement (Persons())
Persons()\NameFirst$ = "Queen"
Persons()\NameLast$  = "Mum"

AddMapElement (*Persons(), "QueenMum")
*Persons() = @Persons()

*Person = FindMapElement (*Persons(), "QueenMum") ; 1. Line
Debug *Person\NameFirst$
Debug *Person\NameLast$
Debug "-----"

*Person = *Persons()                              ; 2. Line needed
Debug *Person\NameFirst$
Debug *Person\NameLast$
Debug "-----"
sorry for my bad english
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PointerLists / PointerMaps

Post by mk-soft »

Use pointer to pointer (ppPerson)

Code: Select all

Structure PERSON
  NameFirst$
  NameLast$
EndStructure

Structure pPerson
  *Person.PERSON
EndStructure

NewList Persons.PERSON()
NewMap *Persons.PERSON()
Define *Person .PERSON

Define *pPerson.pPerson

AddElement (Persons())
Persons()\NameFirst$ = "Queen"
Persons()\NameLast$  = "Mum"

AddMapElement (*Persons(), "QueenMum")
*Persons() = @Persons()

*Person = PeekI(FindMapElement (*Persons(), "QueenMum")) ; Peek Pointer to Person
Debug *Person\NameFirst$
Debug *Person\NameLast$
Debug "-----"

*pPerson = FindMapElement (*Persons(), "QueenMum") ; *pPointer to Pointer of Person
Debug *pPerson\Person\NameFirst$
Debug *pPerson\Person\NameLast$
Debug "-----"
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: PointerLists / PointerMaps

Post by Josh »

mk-soft wrote:Use pointer to pointer (ppPerson)
This is a possibility, but I doubt that the code will be better with the additional structure and additional dereferencing in each line. I like my version better.

But that's not what I want, I know how to deal with the current situation. It's just that it would be much better if functions like FindMapElement(), NextElement(), PreviousElement(), ... for PointerLists/Maps not a pointer to the pointer is returned, but the value of the pointer itself.
sorry for my bad english
Post Reply