ReneMiner
19-02-2016, 15:35
since tB 1.9.16.x introduced data-structures as Hash & AVL-tree we have great opportunities to store our data and very fast access to it.
But for some cases as named and indexed objects, that need to be stored & accessed not by just one key and to have information about type of data it would be great to have some kind of database that allows to access stored objects by their name [& index] as common [array-] variables and be able to tell theirs Typename.
Stored objects might be controls for some GUI-project, entities or shapes to draw & render and lots more.
I currently use heap-memory and do all the management with a couple of comparison- & scan-methods
but i think this can be improved in execution-speed if we had something as this built-in.
Attached some example database-type which can store any data that mandatory Extends the same Dataheader,
all stored information will have 3 properties telling type, name and index.
Instead of controls, entities or shapes the example-script uses some "food"-type,
split into different subtypes as "meat" and "fruits" which have different properties.
I hope something as this could become a module or part of core-engine.
' #Filename "test_tDatabase.tbasic"
Uses "console"
#INCLUDE Once "tDatabase.tBasicU"
%brightYellow = %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY
%brightPurple = %CONSOLE_FOREGROUND_BLUE | %CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY
' we create a database for some food:
' #####################################################################
Type tFood Extends tDataheader
' #####################################################################
' each item will know it's type
' each item will have a name & index
pTaste As DWord
Weight As Double
' ---------------------------------------------------------------------
Function SetTaste(ByVal sTaste As String)
Me.pTaste = HEAP_ReAllocByStr(Me.pTaste, sTaste)
End Function
' ---------------------------------------------------------------------
Function Info()
' every extension of tFood MUST HAVE Info() or
' has to extend some extension of tFood that has Info()
PrintL Me.Type$(), Me.Name$, Me.Index In %brightYellow
' create layover of the real type at Me:
Local realMe Like Me.Type$() At VarPtr(Me)
' call Info() on the real type:
If realMe.Info() Then Nop
' use "If <...> Then Nop" to request a result even if none needed
' since it's dimensioned dynamic, using keyword "Like"
End Function
' ---------------------------------------------------------------------
Function Free() As DWord
' use a Free()-function on any data stored with tDatabase
' to free allocated resources that are noded to Me
PrintL Function_Name & ": " & Me.Name$, Me.Index In %brightYellow
' kill all information
Me.pType = 0
Me.pName = 0
Me.Index = 0
HEAP_Free(Me.pTaste)
Me.pTaste = 0
Me.Weight = 0
End Function
End Type
' #####################################################################
Type tFruit Extends tFood
' #####################################################################
Stones As Long
' ---------------------------------------------------------------------
Function Info()
PrintL " Taste " & HEAP_Get(Me.pTaste)
PrintL " Weight " & Me.Weight
PrintL " Stones " & Me.Stones
End Function
End Type
' #####################################################################
Type tMeat Extends tFood
' #####################################################################
Bones As Byte
' ---------------------------------------------------------------------
Function Info()
PrintL " Taste " & HEAP_Get(Me.pTaste)
PrintL " Weight " & Me.Weight
PrintL " Bones " & Me.Bones
End Function
End Type
' ---------------------------------------------------------------------
'[!] create Global Database:
Global Database As tDatabase
' ---------------------------------------------------------------------
Function TBMain()
' ---------------------------------------------------------------------
Local pAll() As DWord
Local i As Long
' .....................................................................
PrintL "adding some data to database: " In %brightYellow
PrintL
' dim meat as tMeat, named Porkchop at allocated memory
Local meat Like Database.Alloc("tMeat", "Porkchop") At Database.DataPtr
meat.Weight = 0.5
meat.SetTaste("yummy")
meat.Bones = 1
' allocate another tMeat, named Roulade
Database.Alloc("tMeat", "Roulade")
' place virtual meat upon last by Database used DataPtr
SetAt(meat, Database.DataPtr)
meat.Weight = 0.7
meat.SetTaste("sappy")
' dim fruit as tFruit, named Banana at allocated memory
Local fruit Like Database.Alloc("tFruit", "Banana") At Database.DataPtr
fruit.Weight = 0.2
fruit.setTaste("sweet")
' allocate another tFruit, named Apple
Database.Alloc("tFruit", "Apple")
SetAt(fruit, Database.DataPtr)
fruit.Weight = 0.3
fruit.SetTaste("bittersweet")
fruit.Stones = 14
' get a list of all data-ptrs:
Database.ListAll(pAll)
' prepare tFood -layover
Local item As tFood At 0
' let all items give information:
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "now free some data passing its name:"
PrintL
Database.Free("Roulade")
Database.Free("Banana")
PrintL
PrintL "now we have " & Str$(Database.ListAll(pAll)) & " items stored in database"
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "adding 3 more items of food:" In %brightYellow
Database.Alloc("tMeat", "Liver")
SetAt(meat, Database.DataPtr)
meat.Weight = 1.5
meat.SetTaste("disgusting")
Database.Alloc("tFruit", "Peach")
SetAt(fruit, Database.DataPtr)
fruit.Weight = 0.35
fruit.SetTaste("very good")
fruit.Stones = 1
Database.Alloc("tFruit", "Apple")
SetAt(fruit, Database.DataPtr)
fruit.Weight = 0.4
fruit.SetTaste("sour")
fruit.Stones = 19
Database.ListAll(pAll)
PrintL "Now we have altogether:"
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
Print "does Apple 2 exist? "
PrintL IIf$(Database.GetPtr("Apple", 2), "Yes", "No" ) In %brightYellow
Print "does Apple 3 exist? "
PrintL IIf$(Database.GetPtr("Apple", 3), "Yes", "No" ) In %brightYellow
Print "does a dataptr 123 exist in Database? "
PrintL IIf$(Database.IsPtr(123), "Yes", "No" ) In %brightYellow
Print "is current item valid for Database? "
PrintL IIf$(Database.IsPtr(GetAt(item)), "Yes", "No" ) In %brightYellow
Print "item is: " & Database.Name$(GetAt(item))
Print " " & Database.Index(GetAt(item))
PrintL " of type " & Database.TypeAt(GetAt(item))
PrintL
PrintL "porkchop is of type " & DataBase.Type$("porkchop")
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
If DataBase.ListName(pAll, "Apple") Then
PrintL "we have" & Str$(CountOf(pAll)) & " items named APPLE:"
PrintL
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
EndIf
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
' .....................................................................
PrintL
If DataBase.ListType(pAll, "tFruit") Then
PrintL "we have" & Str$(CountOf(pAll)) & " items of type tFruit:"
PrintL
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
EndIf
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "now free all data named Apple:" In %brightYellow
Database.Free("Apple")
PrintL
PrintL "then we have:"
If Database.ListAll(pAll) Then
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
EndIf
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "now free all passing a pointer" & $CRLF
For i = 1 To CountOf(pAll)
Database.FreeAt(pAll(i))
Next
PrintL
PrintL "we have " & Str$(Database.ListAll(pAll)) & " items stored in database"
PrintL
PrintL Repeat$(30, "-") & " key to end" In %brightPurple
WaitKey
End Function
Be assured- as soon as Hash-table has the ability to list all keys- i will come up with some better & faster methods to do the above :)
But for some cases as named and indexed objects, that need to be stored & accessed not by just one key and to have information about type of data it would be great to have some kind of database that allows to access stored objects by their name [& index] as common [array-] variables and be able to tell theirs Typename.
Stored objects might be controls for some GUI-project, entities or shapes to draw & render and lots more.
I currently use heap-memory and do all the management with a couple of comparison- & scan-methods
but i think this can be improved in execution-speed if we had something as this built-in.
Attached some example database-type which can store any data that mandatory Extends the same Dataheader,
all stored information will have 3 properties telling type, name and index.
Instead of controls, entities or shapes the example-script uses some "food"-type,
split into different subtypes as "meat" and "fruits" which have different properties.
I hope something as this could become a module or part of core-engine.
' #Filename "test_tDatabase.tbasic"
Uses "console"
#INCLUDE Once "tDatabase.tBasicU"
%brightYellow = %CONSOLE_FOREGROUND_GREEN | %CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY
%brightPurple = %CONSOLE_FOREGROUND_BLUE | %CONSOLE_FOREGROUND_RED | %CONSOLE_FOREGROUND_INTENSITY
' we create a database for some food:
' #####################################################################
Type tFood Extends tDataheader
' #####################################################################
' each item will know it's type
' each item will have a name & index
pTaste As DWord
Weight As Double
' ---------------------------------------------------------------------
Function SetTaste(ByVal sTaste As String)
Me.pTaste = HEAP_ReAllocByStr(Me.pTaste, sTaste)
End Function
' ---------------------------------------------------------------------
Function Info()
' every extension of tFood MUST HAVE Info() or
' has to extend some extension of tFood that has Info()
PrintL Me.Type$(), Me.Name$, Me.Index In %brightYellow
' create layover of the real type at Me:
Local realMe Like Me.Type$() At VarPtr(Me)
' call Info() on the real type:
If realMe.Info() Then Nop
' use "If <...> Then Nop" to request a result even if none needed
' since it's dimensioned dynamic, using keyword "Like"
End Function
' ---------------------------------------------------------------------
Function Free() As DWord
' use a Free()-function on any data stored with tDatabase
' to free allocated resources that are noded to Me
PrintL Function_Name & ": " & Me.Name$, Me.Index In %brightYellow
' kill all information
Me.pType = 0
Me.pName = 0
Me.Index = 0
HEAP_Free(Me.pTaste)
Me.pTaste = 0
Me.Weight = 0
End Function
End Type
' #####################################################################
Type tFruit Extends tFood
' #####################################################################
Stones As Long
' ---------------------------------------------------------------------
Function Info()
PrintL " Taste " & HEAP_Get(Me.pTaste)
PrintL " Weight " & Me.Weight
PrintL " Stones " & Me.Stones
End Function
End Type
' #####################################################################
Type tMeat Extends tFood
' #####################################################################
Bones As Byte
' ---------------------------------------------------------------------
Function Info()
PrintL " Taste " & HEAP_Get(Me.pTaste)
PrintL " Weight " & Me.Weight
PrintL " Bones " & Me.Bones
End Function
End Type
' ---------------------------------------------------------------------
'[!] create Global Database:
Global Database As tDatabase
' ---------------------------------------------------------------------
Function TBMain()
' ---------------------------------------------------------------------
Local pAll() As DWord
Local i As Long
' .....................................................................
PrintL "adding some data to database: " In %brightYellow
PrintL
' dim meat as tMeat, named Porkchop at allocated memory
Local meat Like Database.Alloc("tMeat", "Porkchop") At Database.DataPtr
meat.Weight = 0.5
meat.SetTaste("yummy")
meat.Bones = 1
' allocate another tMeat, named Roulade
Database.Alloc("tMeat", "Roulade")
' place virtual meat upon last by Database used DataPtr
SetAt(meat, Database.DataPtr)
meat.Weight = 0.7
meat.SetTaste("sappy")
' dim fruit as tFruit, named Banana at allocated memory
Local fruit Like Database.Alloc("tFruit", "Banana") At Database.DataPtr
fruit.Weight = 0.2
fruit.setTaste("sweet")
' allocate another tFruit, named Apple
Database.Alloc("tFruit", "Apple")
SetAt(fruit, Database.DataPtr)
fruit.Weight = 0.3
fruit.SetTaste("bittersweet")
fruit.Stones = 14
' get a list of all data-ptrs:
Database.ListAll(pAll)
' prepare tFood -layover
Local item As tFood At 0
' let all items give information:
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "now free some data passing its name:"
PrintL
Database.Free("Roulade")
Database.Free("Banana")
PrintL
PrintL "now we have " & Str$(Database.ListAll(pAll)) & " items stored in database"
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "adding 3 more items of food:" In %brightYellow
Database.Alloc("tMeat", "Liver")
SetAt(meat, Database.DataPtr)
meat.Weight = 1.5
meat.SetTaste("disgusting")
Database.Alloc("tFruit", "Peach")
SetAt(fruit, Database.DataPtr)
fruit.Weight = 0.35
fruit.SetTaste("very good")
fruit.Stones = 1
Database.Alloc("tFruit", "Apple")
SetAt(fruit, Database.DataPtr)
fruit.Weight = 0.4
fruit.SetTaste("sour")
fruit.Stones = 19
Database.ListAll(pAll)
PrintL "Now we have altogether:"
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
Print "does Apple 2 exist? "
PrintL IIf$(Database.GetPtr("Apple", 2), "Yes", "No" ) In %brightYellow
Print "does Apple 3 exist? "
PrintL IIf$(Database.GetPtr("Apple", 3), "Yes", "No" ) In %brightYellow
Print "does a dataptr 123 exist in Database? "
PrintL IIf$(Database.IsPtr(123), "Yes", "No" ) In %brightYellow
Print "is current item valid for Database? "
PrintL IIf$(Database.IsPtr(GetAt(item)), "Yes", "No" ) In %brightYellow
Print "item is: " & Database.Name$(GetAt(item))
Print " " & Database.Index(GetAt(item))
PrintL " of type " & Database.TypeAt(GetAt(item))
PrintL
PrintL "porkchop is of type " & DataBase.Type$("porkchop")
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
If DataBase.ListName(pAll, "Apple") Then
PrintL "we have" & Str$(CountOf(pAll)) & " items named APPLE:"
PrintL
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
EndIf
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
' .....................................................................
PrintL
If DataBase.ListType(pAll, "tFruit") Then
PrintL "we have" & Str$(CountOf(pAll)) & " items of type tFruit:"
PrintL
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
EndIf
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "now free all data named Apple:" In %brightYellow
Database.Free("Apple")
PrintL
PrintL "then we have:"
If Database.ListAll(pAll) Then
For i = 1 To CountOf(pAll)
SetAt( item, pAll(i) )
If item.Info() Then Nop
Next
EndIf
PrintL
PrintL Repeat$(30, "-") & " key to continue" In %brightPurple
WaitKey
PrintL
' .....................................................................
PrintL "now free all passing a pointer" & $CRLF
For i = 1 To CountOf(pAll)
Database.FreeAt(pAll(i))
Next
PrintL
PrintL "we have " & Str$(Database.ListAll(pAll)) & " items stored in database"
PrintL
PrintL Repeat$(30, "-") & " key to end" In %brightPurple
WaitKey
End Function
Be assured- as soon as Hash-table has the ability to list all keys- i will come up with some better & faster methods to do the above :)