Yet another linked list in PB (OOP)

Share your advanced PureBasic knowledge/code with the community.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Yet another linked list in PB (OOP)

Post by luis »

Yes, I can hear you yawning already !

Anyway, here it is. PB4.41.

; Yet another linked list implementation for PureBasic (OOP)

; You can store the returned object pointer in a structure, in a array, and so on.
; The LinkedList_Create() procedure instantiate the object and requires the size of the data to be allocated
; plus an optional pointer to procedure. Inside this procedure you write the code to safely free the
; structured data type used for your data, and that code will be executed by the object every time it need
;to deallocate an item.
Sound complicated but it's really easy, and great to avoid leakage problems.
; See the example code.
; The SortItems() method too uses a pointer to procedure to perform the logic needed for the sort.
; This way you can easily specify a complex sort, for example ascending for a field, and inside that ordering
; descending for another field. You can create multiple procedures for multiple sorting schemes.
; Again see the example code.

Code: Select all

; -------------------------------------------------------------------
; // DEFINE OBJECT INTERFACE
; -------------------------------------------------------------------

Interface LinkedList

 ; clear the list and free the object
 Destroy() 

 ; select *item as the current list item and return its address back
 SelectItem.i(*item)

 ; delete the current item, select the previous one (if any) or the next one (if any) and return its address
 DeleteItem()

 ; insert a new item before the current one (or add it to an empty list) and return its address
 InsertItem.i()

 ; add a new item after the current one (if any) and return its address
 AddItem.i()

 ; return the pointer to the current item
 CurrentItem.i()

 ; go to the first list item (if any) and return its address
 FirstItem.i()

 ; go to the last list item (if any) and return its address
 LastItem.i()

 ; return the number of the items in the list
 CountItems.i()

 ; go to the next item (if any) and return its address 
 NextItem.i()

 ; go to the previous item (if any) and return its address
 PreviousItem.i()

 ; clear the list contents
 Clear()

 ; sort the list items according to the external callback rules
 SortItems(*fpCompare) 

EndInterface
Download from here.

If you find any bug please let me know.

Bye!
Last edited by luis on Thu Apr 01, 2010 12:13 pm, edited 2 times in total.
"Have you tried turning it off and on again ?"
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Yet another linked list in PB (OOP)

Post by ts-soft »

:D thx
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Yet another linked list in PB (OOP)

Post by idle »

no yawning here, thanks
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Yet another linked list in PB (OOP)

Post by luis »

Actually using this in a real project, I found some bugs... and one...

; 1.01 - Corrected a bug in the return value for FirstItem() and LastItem() for an empty list
"Have you tried turning it off and on again ?"
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Yet another linked list in PB (OOP)

Post by luis »

... and two

; 1.02 - Corrected a bug again in case of an empty list, forgot to zeroing two internal pointers.
"Have you tried turning it off and on again ?"
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Yet another linked list in PB (OOP)

Post by luis »

; 1.03 - Added some examples to show how to properly deallocate complex data structures.

I added some more examples, showing how to properly deallocate memory using the callbacks and how to avoid memory leaks.

There are 4 examples, from the simplest to the more advanced.

You will see how to free the memory for simple structures, complex structures with strings, more complex structures with strings and pointers to dinamically allocated memory, and Über-complex structures (well, not really) with all of the above and more linkedlist objects in the structure itself.

This should clear up the usage of the callback mechanism.

As a side note... those examples could be useful even if you are not interested in this code but you are starting exploring PureBasic and you are an intermediate to advanced programmer, because it's easy to misunderstand some of the concepts shown here if you are coming from another language.

Anyway, to verify the deallocations are really working you can use the debugger and put a breakpoint on the last lst\Destroy() of each example. Than you can see in the task manager the memory allocated just before the Destroy() and right after that.

Try to experiment commenting out some of the statements present in the callbacks and see what happen.

BTW: in the examples I never check if memory allocations are successful to keep the code simple, you should do that obviously!

Hope this will help :wink:


EDIT: PB 4.50 now added the ability to add lists inside structures (hooray!)
Anyway I like so much the free-through-callback implemented here I'll probably end up using both, depending on the occasions. :P
"Have you tried turning it off and on again ?"
Post Reply