News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Generic BaseList

Started by empty, April 25, 2008, 07:01:05 PM

Previous topic - Next topic

empty

This is a generic baselist loosely based on Kevin's generic list example

It's got a few more functions like inserting items, getting and setting the position and so on.
The main difference to it (and also to the built-in linked list) is that items are stored the other way around (aka the right way ;)), that means the first item in the list is the first that you've added.
Similar to the built in list, it's based on a "current item" approach. Basically you have a pointer that points at the current item and remove/insert operations are based on this. However, there's an example of a string list that shows how to do an index-based list. It's certainly buggy (and does crash here with larger amounts of items).

Anyway, this will become the base of a generic list, which in turn will become the base of a generic tree node system, which in turn will become the base of a native XML library.

PlayBASIC Code: [Select]
; PROJECT : BaseList
; CREATED : 25.04.2008
; EDITED : 26.04.2008
; ---------------------------------------------------------------------

Explicit

Type TBaseListManager
FirstItem As Integer
ItemCount As Integer
CurrentItem As Integer
LastItem As Integer
EndType

Type TBaseList
NextItem As Integer ; As TBaseList Pointer
PrevItem As Integer ; As TBaseList Pointer
ItemType As Integer
EndType

Dim _BaseListManager(1) As TBaseListManager


Function CreateBaseList()
Local result = GetFreeCell(_BaseListManager())
_BaseListManager(result).FirstItem = 0
_BaseListManager(result).LastItem = 0
_BaseListManager(result).ItemCount = 0
EndFunction result

Function GetBaseListFirstItem(Handle)
Dim result As Integer
If Handle > 0 And Handle <= GetArrayElements(_BaseListManager(),1)
result = _BaseListManager(Handle).FirstItem
EndIf
EndFunction result


Function GetBaseListNextItem(Handle)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction 0
EndIf
Dim CurrentItem As TBaseList Pointer
Dim Result As TBaseList Pointer

CurrentItem = _BaseListManager(Handle).CurrentItem
Result = CurrentItem.NextItem
EndFunction Result As TBaseList Pointer


Function GetBaseListPrevItem(Handle)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction
EndIf
Dim CurrentItem As TBaseList Pointer
Dim Result As TBaseList Pointer

CurrentItem = _BaseListManager(Handle).CurrentItem
Result = CurrentItem.PrevItem
EndFunction Result As TBaseList Pointer


Function SetBaseListCurrentToLast(Handle)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction NULL
EndIf
Dim result As TBaseList Pointer
_BaseListManager(Handle).CurrentItem = _BaseListManager(Handle).LastItem
result = _BaseListManager(Handle).LastItem
EndFunction result As TBaseList Pointer


Function SetBaseListCurrentToFirst(Handle)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction NULL
EndIf
Dim result As TBaseList Pointer
_BaseListManager(Handle).CurrentItem = _BaseListManager(Handle).FirstItem
result = _BaseListManager(Handle).FirstItem
EndFunction result As TBaseList Pointer

Function SetBaseListCurrentPos(Handle, Pos)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction NULL
EndIf
If Pos < 1 Or Pos > _BaseListManager(Handle).ItemCount
Exitfunction NULL
EndIf
Local thispos = 1
Dim thisitem As TBaseList Pointer
_BaseListManager(Handle).CurrentItem = _BaseListManager(Handle).FirstItem
While thisPos < Pos
thisitem = _BaseListManager(Handle).CurrentItem
thisitem = thisitem.NextItem
_BaseListManager(Handle).CurrentItem = Int(thisitem)
Inc thispos
EndWhile

EndFunction _BaseListManager(Handle).CurrentItem

Function GetBaseListCurrentPos(Handle)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction 0
EndIf
Local result = 1
Dim thisitem As TBaseList Pointer
Dim tmpItem As TBaseList Pointer
thisitem = _BaseListManager(Handle).FirstItem
While Int(thisitem) <> _BaseListManager(Handle).CurrentItem
Inc result
If Int(thisitem) = NULL Then Exitfunction 0
thisitem = thisitem.NextItem
EndWhile
EndFunction result

Function GetBaseListCurrentItem(Handle)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction 0
EndIf
Dim Result As TBaseList Pointer

Result = _BaseListManager(Handle).CurrentItem
EndFunction Result As TBaseList Pointer



Function AddToBaseList(Handle, Me As TBaseList Pointer)
If Handle < 1 Or Handle > GetArrayElements(_BaseListManager(),1)
Exitfunction
EndIf
Local Result = 0
Dim NextItem As TBaseList Pointer
Me.NextItem = 0

If _BaseListManager(Handle).LastItem <> 0
NextItem = _BaseListManager(Handle).LastItem
NextItem.NextItem = Int(Me)
Else
_BaseListManager(Handle).FirstItem = Int(Me)
EndIf

Me.PrevItem = _BaseListManager(Handle).LastItem

_BaseListManager(Handle).LastItem =Int(Me)

_BaseListManager(Handle).CurrentItem = Int(Me)
_BaseListManager(Handle).ItemCount = _BaseListManager(Handle).ItemCount + 1
Result = _BaseListManager(Handle).ItemCount
EndFunction result
Login required to view complete source code