AddElement() behaviour

Everything else that doesn't fall into one of the other PB categories.
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

AddElement() behaviour

Post by infratec »

Yesterday I came across the behaviour of AddElement().

I use a Logger.pbi file which is runs a thread to write the entries in a file. The log entries are first stored in a list for speed reasons.
In the thread I use FirstElement() to always write the correct entry to the log file.

In the log file the entries were not in correct order :cry:

I use AddElement() to add the new entries.
I assumed that AddElement() adds the entry at the end of the list.

THIS IS NOT TRUE.

After reading the help, it became clear that AddElement() is more like InsertElement() it adds an entry behind the current element.
I did not respect this.

My 'fix':
Use LastElement() before using AddElement()

Maybe I change this and use PushListPosition() and PopListPosition() in the logger thread which should be faster.
But this is also tricky, because when FisrtElement() is the only element and I delete it after logging, Pop will crash.

So keep in mind:

AddElement() does not adding the element at the end of the list.

In my case the name AddElement() was misleading.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: AddElement() behaviour

Post by STARGÅTE »

You joined 2008 to this forum and realized 16 years later that your assumption of the behavior of AddElement was wrong? What did you do for 16 years? (I'm just kidding. :lol:)

What is wrong with the same "Add" in AddElement? Why "add" should mean adding an element to the end of the list?
To add elements to the end of a list many other languages use the name "append".
So AppendElement() could imitate a LastElement() : AddElement()
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: AddElement() behaviour

Post by tored »

Yes, if you have multiple consumers of the same list it is important to understand that the list still only has one pointer to the current element, thus each consumer needs to set correct position before doing anything to the list.

I have my own thread pool implementation that uses a list internally for incoming tasks, pushing to end by using LastElement() + AddElement() and popping from beginning by using FirstElement() + DeleteElement() but only if ListSize() is positive.
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AddElement() behaviour

Post by infratec »

Yes, in all these years I used AddElement() only to create a fresh list.
And in this case it adds always at the end (as expected).

So, I forgot, that this is not always the case.
Maybe I'm getting old.

:oops:
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: AddElement() behaviour

Post by AZJIO »

I can assume that the addition occurs to the current element. Since we always add sequentially, this caused the feeling that the element was being added to the end of the list.

Code: Select all

NewList f.i()
For i = 1 To 20
AddElement(f())
f() = i
Next
SelectElement(f(), 10)
AddElement(f())
f() = 333

ForEach f()
	Debug f()
Next
tored
User
User
Posts: 86
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: AddElement() behaviour

Post by tored »

infratec wrote: Thu Mar 07, 2024 11:07 am
Maybe I'm getting old.

:oops:
The nice thing of getting old is that you soon forget all about this anyway. :wink:
BarryG
Addict
Addict
Posts: 4122
Joined: Thu Apr 18, 2019 8:17 am

Re: AddElement() behaviour

Post by BarryG »

infratec wrote: Thu Mar 07, 2024 11:07 amMaybe I'm getting old.
Welcome to the club! :)
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: AddElement() behaviour

Post by mk-soft »

I am old ...

I realised that when I was looking for a solution that I found in the forum, but I realised that I had written it myself. :lol:
Is probably normal.
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
ChrisR
Addict
Addict
Posts: 1466
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: AddElement() behaviour

Post by ChrisR »

infratec wrote: Thu Mar 07, 2024 10:26 am My 'fix':
Use LastElement() before using AddElement()
On my side, I do it with
If AddElement(List()) : MoveElement(List(), #PB_List_Last)
It would be nice to have the MoveElement() flags available in AddElement(List()[, Location]) with Location = #PB_List_First, #PB_List_Last, #PB_List_Before, #PB_List_After (Default)
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: AddElement() behaviour

Post by AZJIO »

tored wrote: Thu Mar 07, 2024 11:21 am The nice thing of getting old is that you soon forget all about this anyway. :wink:
The best way is to dynamically change the help file. Those questions that are raised should be added to the help file in the “Notes” section. Then people will ask them less often. We may not know much information, but when we open the help file, we see that the significant things are first on the list.

Yesterday I thought, why not make an example of accessing a list item via Peekl() in the help
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: AddElement() behaviour

Post by skywalk »

Thanks for the post!
I read the forum easier than the manual :shock:

Now, have to check my code how this might burn me?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: AddElement() behaviour

Post by idle »

the list object will have pointers to head, current and tail so maybe use a macro AddElementFirst() AddElementLast()
would clear up any befuddlement.
One good thing about getting old is you can have a beer or wine when ever you want and not feel guilty about it! :lol:

Code: Select all

Macro AddElementFirst(ls,val) 
  FirstElement(ls) 
  InsertElement(ls) 
  ls = val 
EndMacro 

Macro AddElementLast(ls,val) 
  LastElement(ls) 
  AddElement(ls)
  ls = val 
EndMacro 

Global NewList foo()

AddElementFirst(foo(),1) 
AddElementFirst(foo(),2) 
AddElementFirst(foo(),3) 
AddElementFirst(foo(),4) 

ForEach foo() 
  Debug foo() 
Next   
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

AddElement() behaviour

Post by HanPBF »

Just realized the same with AddElement()... :lol:

addElement is not clear, insertElement is clear (expect before/after current element which would give a nice option).
appendElement is clear (maybe with option #PB_List_atBegin, which makes it again not clear... :D)
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: AddElement() behaviour

Post by jacdelad »

Code: Select all

Macro AppendElement(My list)
  LastElement(MyList)
  AddElement(MyList)
EndMacro
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: AddElement() behaviour

Post by AZJIO »

If it moves to the beginning of the list and then to the end, then the loss is 30% of the time. From this we conclude that LastElement() is calculated not from the beginning, but from the current element, and therefore does not move the pointer, since the element is already the last one. Therefore, using LastElement() does not add any noticeable time.

Code: Select all

DisableDebugger
NewList MyList()
StartTime = ElapsedMilliseconds()
For i = 0 To 10000000
	ResetList(MyList())
	LastElement(MyList())
	AddElement(MyList())
Next
StartTime = ElapsedMilliseconds() - StartTime
EnableDebugger
Debug StartTime
Post Reply