IonicWind Software

IWBasic => General Questions => Topic started by: aurelCB on June 12, 2011, 02:47:30 AM

Title: LList and FOR loop
Post by: aurelCB on June 12, 2011, 02:47:30 AM
I ask LarryMc about linked list before and currently i use While loop
for iterating trough linked list and all work fine.
But i have problem when i try to use For loop.

fill list with strings-> work fine
mylist = ListCreate()
FOR x = 0 to 10000
     temp = ListAdd(mylist, NEW(string,1))
     #<string>temp ="Code is like this"+str$(x)
NEXT x


so now i want read this strings from list using classic FOR loop
but becose of something not work or i recive error messages.
FOR pos = 0 TO 10000
   #tempData = ListGetData(pos)
    move w1,50,50:PRINT w1,#tempData
NEXT pos


So my question is - how with classic FOR loop (not FOR EACH )
properly read all strings from list?
thnks advance...

Aurel
Title: Re: LList and FOR loop
Post by: LarryMc on June 12, 2011, 07:43:15 AM
Aurel
In your example you are trying to treat a linked list like it can be indexed like a simple array.
Can't be treated that way.

A linked-list member is really a UDT.
The standard one for IWBasic is
TYPE LINKEDLIST
DEF pNext as POINTER
DEF pPrev as POINTER
DEF pData as POINTER
ENDTYPE


To do what you are suggesting you'd have to use FindFirst to get the location(pointer) of the first element.
then you'd have to write code that basically would be duplicating what the ListGetNext function does(looks at the pNext pointer in the current UDT element).

The FOREACH was created in order to do the UDT pointer dereferencing that is required and that a simple for/next loop doesn't do.

If it was me I would use either the FOREACH or the WHILE and forget about trying to reinvent the wheel.

LarryMc
Title: Re: LList and FOR loop
Post by: aurelCB on June 12, 2011, 02:14:51 PM
I dont ask this without reason..
if you remember i ask you in pm what you think about reading source code from
array or linked list - which way is faster ....

Ok i cannot use FOREACH becose inside FOREACH loop i cannot use BREAKFOR.
(i think),hmmmm WHILE loop is not in my option becose again breakfor.
Yes i can set condition which will exit while loop.
Reading strings from linked list in while loop is very fast and that is ok.
Currently i have ordinary FOR loop in ABasic lexer/parser so ithink use
FOR loop as main option.
Ok ,thanks Larry i will do some tests and i will see what is better option...
Title: Re: LList and FOR loop
Post by: LarryMc on June 12, 2011, 03:25:41 PM
Okay, here's an example of reading a linked-list in a FOR/NEXT loop so you can use the BREAKFOR.

With the WHILE loop and the FOREACH loop you never had to be concerned with how many entries there were.

With the FOR/NEXT loop you will always have to have your for loop set to some extremely high number to make sure it will ALWAYS be larger than the number of items in your list.

But this will allow you to test your strings and break out cleanly with the BREAKFOR.
Quotemylist = ListCreate()

'add some items to the list
FOR x = 0 to 10
   temp = ListAdd(mylist,NEW(string,1))
   #<string>temp = "This is line"+str$(x)
NEXT x

'iterate through the list
pos = ListGetFirst(mylist)
if pos
   for x= 0 to 100000
      pData = ListGetData(pos)
      PRINT #<string>pData
      pos = ListGetNext(pos)
      if pos=0 then breakfor
   next x
endif
PRINT
'clear out the entire list
ListRemoveAll(mylist,TRUE)

PRINT "Press any key to close"
DO:until inkey$ <> ""

Hope this helps.

LarryMc
Title: Re: LList and FOR loop
Post by: aurelCB on June 13, 2011, 03:45:05 AM
Bingo!
Thank you Larry  ;)
This is what i need ,and it looks that reading is fast enough.
My testing code is this:
mylist = ListCreate()

'add some items to the list
'//in my case add lines from source code//
Move w1,20,20:Print w1,"Loading..."
FOR x = 0 to 10000
   temp = ListAdd(mylist,NEW(string,1))
   #<string>temp = "This is line"+str$(x)
NEXT x
Move w1,20,50:Print w1,"Finished!"

'iterate through the list
pos = ListGetFirst(mylist)
IF pos

   For x = 0 TO 10000
      pData = ListGetData(pos)
      Move w1,20,100:PRINT #<string>pData
      pos = ListGetNext(pos)
      IF pos=0 then BREAKFOR
   Next x

ENDIF
Title: Re: LList and FOR loop
Post by: aurelCB on July 28, 2011, 01:14:49 PM
Hi ...
I was wondering is there a way to access to specified node (position) in list without iteration ?
For example if i know in advance which node i want to access and read data from that node(pos).
I try some options bit it looks that direct access dont work?
I understand that :
pData = ListGetData(fpos)
compiler dont know which list is used...
Title: Re: LList and FOR loop
Post by: LarryMc on July 28, 2011, 01:33:51 PM
Quote from: aurelCB on July 28, 2011, 01:14:49 PM
Hi ...
I was wondering is there a way to access to specified node (position) in list without iteration ?
For example if i know in advance which node i want to access and read data from that node(pos).
I try some options bit it looks that direct access dont work?
I understand that :
pData = ListGetData(fpos)
compiler dont know which list is used...

This question came up before with my linked-list class I wrote.
http://www.ionicwind.com/forums/index.php?topic=2678.msg22641#msg22641

LarryMc
Title: Re: LList and FOR loop
Post by: aurelCB on July 28, 2011, 01:54:48 PM
Hmm Im not sure that i understand your point Larry.
You suggest me to use your class - Linked List , right?
I ask because i need fast as possible acces to variable without iteration.

Larry i will try look into your class again.

Larry sorry if i bothering you but i have another question...
What you think about using dictionary as storage for variable names,and his values.
Do i can acces to variable name in hash trough key as more direct way then with linked list and
how fast this will be ?
I hope that i don't ask to much ::)

By the way where i can find help about dictionary/hash  ?
In EB help there is nothing about that...

thanks advance...
Aurel
Title: Re: LList and FOR loop
Post by: LarryMc on July 28, 2011, 03:10:04 PM
No, I'm not saying you need to use my linked list class.
I'm saying that someone asked the same question you did and I modified my link list so they could extract data straight from a node without iteration.  However, they still had to have the node location stored somewhere.

I've never used a dictionary and hash tables.  I'm not on my other computer so I can look but it seems there are some functions in Fletchie's CTL library.  There's also some info in the old Pyxia Forum database.

If I had to do something like that I'd probably take a stab at using a hidden list view.  One colum would hold the variable name and the 2nd column would be the value. 
Then you could use LVFINDITEM to find the pos of the data.

If it was going to be very many entries you could have the automatic sorting enabled.
Then when you  want to find a variable you do this:
get the count of items
read the variable name at the middle of the list
compare that to what you are searching for
then either split the differenc between the top half of the list or the bottom half of the list
keep splitting the list until you find what you are looking for.
that way you never have to go through the whole list to find what you are looking for.

if you decide to proceed with a associative array/dictionary then post your code when you get it working.

LarryMc
Title: Re: LList and FOR loop
Post by: aurelCB on July 28, 2011, 03:19:26 PM
Quoteif you decide to proceed with a associative array/dictionary then post your code when you get it working.

Ok Larry ,I would like to try that i see how things work because i never use before this type of arrays.

By the way as i say i cannot find in user guide nothing about commands for dictionaries ???
Title: Re: LList and FOR loop
Post by: DominiqueB on July 28, 2011, 03:26:17 PM
hello,

commands for dictionnary are like this one :

DICTCREATE(), DictAdd() , . . .

There are also 2 samples : dictionary_text and dictionary_test2




Dominique
Title: Re: LList and FOR loop
Post by: aurelCB on July 28, 2011, 03:49:40 PM
Hi Dominique ...
yes i found this two examples but i dont find anything in EB user guide,
fortunatly i found in IWB user guide ....

Command DictLookup looks prommising for what i will try to do...
I hope..

STRING = DictLookup(dict as POINTER,key as STRING)
Title: Re: LList and FOR loop
Post by: DominiqueB on July 28, 2011, 03:59:53 PM
effectively, if you search in the index the command doesn't shows but
when i type in the "Search" tab i can see that the dictadd(), DictLookup() , . . .
are there.

I think the help file is not complete ;-)

Even the DictCreate() doesn't shows but it is mentionned in the parameters section for the
dictlookup() function, so . . . it must be there also.

Dominique
Title: Re: LList and FOR loop
Post by: LarryMc on July 28, 2011, 04:10:32 PM
they are only listed in the alphabetical index of commands.

LarryMc