Pointers admission for LinkedList functions

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Pointers admission for LinkedList functions

Post by Psychophanta »

Perhaps this topic was issued in the past, but i have not found...

Code: Select all

Structure blair
  fil.l
  *bin.blair
  *hussein.bush
EndStructure

Structure bush
  aznar.l
  *ggg.blair
EndStructure

NewList saddam.blair()
NewList osama.bush()

AddElement(saddam())
AddElement(osama())

osama()\ggg=saddam()
osama()\ggg\fil=1234
;I can't do this:
ClearList(osama()\ggg);<-- doesn't work
;neither this:
AddElement(osama()\ggg);<-- doesn't work
;neither this:
*poi.blair=@saddam()
AddElement(*poi);<-- doesn't work
;and it would be interesting for these commands to admit pointers
;because sometimes programming is forced to implement unnecessary code, or more than one procedure
;where should be enought only one.
;If use pointers for these kind of commands (AddElement(),etc.) code would be sometimes smaller.
Debug saddam()\fil
Besides, i think that the ability of using pointers as parameters of such functions like CountList(), FirstElement(), ClearList(), etc. would make that LinkedList stuff was more coherent.


AL :roll:
GPI
PureBasic Expert
PureBasic Expert
Posts: 1396
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Code: Select all

Structure blair
Structure bush
NewList saddam.blair()
NewList osama.bush()
what to hell are you programming?
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

War Games 2.0 :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

It don't occurred to me any names for variables, and i put those which are very in fashion now. :twisted:


AL
Justin
Addict
Addict
Posts: 962
Joined: Sat Apr 26, 2003 2:49 pm

Post by Justin »

some of them may sound insulting

osama == BIG MOTHERFUCKER
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

No doubt he's a piece of human trash.. I'm sure Psychophanta meant nothing by it though.. No worries..
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

It would also be handy to pass a pointer of a LinkedList to a procedure such as:

Code: Select all

NewList Hello.s()

AddElement(Hello())
Hello() = "GoodDay"

AddElement(Hello())
Hello() = "Bonjour"

procedure PrintListContents(*ptr)
    ResetList(*ptr())
    While(NextElement(*ptr()))
        debug *ptr()
    Wend
endprocedure

PrintListContents(@Hello())
Something like that :D Maybe it could be thought out better.
--Kale

Image
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Exact, Kale; that is just what i request. :D




...and here is another example to show what we are requesting to Fred:

Code: Select all

;This code doesn't work:

NewList Hello1.s()
NewList Hello2.s()

AddElement(Hello1()) 
Hello1() = "GoodDay1" 

AddElement(Hello2()) 
Hello2() = "Bonjour2" 

Procedure PrintListContents(*ptr.s) 
    ResetList(*ptr.s) 
    While(NextElement(ptr.s)) 
        Debug *ptr.s 
    Wend 
EndProcedure 

PrintListContents(Hello1())

PrintListContents(Hello2())
Since ResetList() and NextElement() don't accept normal pointers as argument, we are forced to implement 2 almost identical functions in this case, which could be 3 or more functions almost identical in other cases; look:

Code: Select all

;This code works:
NewList Hello1.s()
NewList Hello2.s()

AddElement(Hello1()) 
Hello1() = "GoodDay1" 

AddElement(Hello2()) 
Hello2() = "Bonjour2" 

Procedure PrintListContents1(*ptr.s) 
    ResetList(Hello1()) 
    While(NextElement(Hello1())) 
        Debug *ptr.s
    Wend 
EndProcedure

Procedure PrintListContents2(*ptr.s) 
    ResetList(Hello2()) 
    While(NextElement(Hello2())) 
        Debug *ptr.s
    Wend 
EndProcedure

PrintListContents1(Hello1())
PrintListContents2(Hello2())

AL
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

That would be nice to have the list functions accept pointers... could get REALLY tedious if you were keeping many lists and needed many functions for doing the same thing with all of them... PolyVector to the rescue! :D
lol...how sad is this that i've only posted twice now and they've both been on linked lists...anywho...if you need a quick-fix here you go:

Code: Select all

;Modified: uses the 'supergeek' technology described in the help :) 
Structure LinkedString
*Next.LinkedString
*Previous.LinkedString
  value.s
EndStructure


NewList Hello1.LinkedString() 
NewList Hello2.LinkedString() 
*ptr.LinkedString
*ptr=AddElement(Hello1()) 
*ptr\value = "GoodDay1" 

*ptr=AddElement(Hello2()) 
*ptr\value = "Bonjour2" 

Procedure PrintListContents(*ptr.LinkedString) 
  Repeat
    If *ptr
      Debug *ptr\value
    EndIf
    *ptr=*ptr\Next
  Until *ptr=0 
EndProcedure 

PrintListContents(FirstElement(Hello1())) 
PrintListContents(FirstElement(Hello2())) 
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Nice code PolyVector but doing it like that means you would need many List structures to handle all data types. I was thinking about having a more generic and neat solution :)
--Kale

Image
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Yes, of course that is a usual method for to solve the problem.

But anyway, we must face the facts that that code is ugly, not so understandable, because we are forced to use things like:

Code: Select all

    *ptr=*ptr\Next 
  Until *ptr=0 
when PureBasic has a beautiful function which is: NextElement(), which do all it and we can not use inside our functions. :cry:

Besides you are giving FirstElement(Hello1()) as parameter of functions which is not so clean too. :(


AL
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Brr, sorry this can't accept inserting Code when i write the message. :oops:


Well i hope you understand it.


I take advantage to add that your method, if in a big program with lots of structures, and lots of functions, we forced to use linked lists in an own way (out of PureBasic commands and functions offer us), and to add to all (or most) of structures 2 more elements inside: *Next.structure and *Previous.structure.

AL
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

Yeah, I do agree that the linked list commands need a little work... Maybe I'm wierd but I always use linked lists the way I described above only because it makes more sense to me... hehe I guess we all code differently... :)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Hi PolyVector.

You said:
"I always use linked lists the way I described above only because it makes more sense to me... "

However, keep in mind that the main matter of Linked Lists as is implemented in PB, is that each element in list points internally (made by PB transparent to the programmer) to its next and to its previous element, so i understand that it has more sense to you to program like that, but the matter is to make it easy and transparent to the programmer.
So your programs are efficient, but it is redundant to have to use externally a pointer to next and another to previous element in each element in list.

Thank you :D


AL
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

The problem is you don't now where osama is...

And if you push bush into osama, then yu get a major crisis, maybe you should use saddam
Post Reply