Jump to content
Steve Maughan

Most efficient way to delete multiple items from a TList<T>...

Recommended Posts

17 hours ago, Fr0sT.Brutal said:

No, you don't understand 😉 I suggested to exchange them not move. Of course it only sounds simple, there will be many gotchas to avoid so if resources allow, copy is always the easiest option.

Hmm I did misunderstand. Somewhat tricky algorithm to write down. You need a cursor starting at each end and when they meet you are done. 

 

And you reorder the list which may very well be undesirable. 

Edited by David Heffernan

Share this post


Link to post

Have you considered using 2 TList<T> ? One for the actual data and the other as a " T object pool"
Before adding a new Item in MainList, check the pool, if it's empty then return a newly created T or get the first (or last ) element. 
When you're done, move the object back to the Pool list.
You will be able to avoid unnecessary allocating and freeing.

Share this post


Link to post

@Clément Did you actually read the very first post where it said:

 

Quote

I don't need to free the objects, just delete them from the list.

So the question is about the best data structure or algorithm to delete those items from the list and I am very sure the algo TList<T>.Pack uses is the most suited one.

It is one iteration with batch moving remaining items to the front. The most impactful thing here will probably be calling the delegate to determine if the object should be removed and the dereferencing of the object reference to read some field that holds the information to give that information.

Share this post


Link to post
On 12/20/2019 at 6:33 PM, Stefan Glienke said:

@Clément Did you actually read the very first post where it said:

 

So the question is about the best data structure or algorithm to delete those items from the list and I am very sure the algo TList<T>.Pack uses is the most suited one.

It is one iteration with batch moving remaining items to the front. The most impactful thing here will probably be calling the delegate to determine if the object should be removed and the dereferencing of the object reference to read some field that holds the information to give that information.

Indeed Pack is the way to go. I guess I'm just biased by my current project.

Share this post


Link to post
On 12/20/2019 at 8:44 PM, David Heffernan said:

Hmm I did misunderstand. Somewhat tricky algorithm to write down. You need a cursor starting at each end and when they meet you are done. 

 

And you reorder the list which may very well be undesirable. 

That's why I mentioned that a list should be unsorted; anyway this algo isn't trivial indeed, it will only make sense if number of deleted items is very low while the list itself is very large (so assigning/copying the items to remain in list will take significant time). Otherwise "packing" of the list seems more efficient (just one loop, two indexes - current item and current item to remain, 0 reallocs, number of assignments = list.Count - N_DelItems).

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×