Page 1 of 1

PointerLists / PointerMaps

Posted: Fri Jan 03, 2020 5:57 pm
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.

Re: PointerLists / PointerMaps

Posted: Fri Jan 03, 2020 9:28 pm
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

Re: PointerLists / PointerMaps

Posted: Fri Jan 03, 2020 10:23 pm
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 "-----"

Re: PointerLists / PointerMaps

Posted: Sat Jan 04, 2020 1:10 am
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 "-----"

Re: PointerLists / PointerMaps

Posted: Mon Jan 06, 2020 6:24 pm
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.