• Welcome to PlanetSquires Forums.
 

Freebasic version of PB PowerCollection

Started by Richard Kelly, January 24, 2017, 02:59:52 AM

Previous topic - Next topic

Richard Kelly

Extract the includes to the FB folder where Jose's cWindow lives. You'll need the latest version (RC26).

This is a wrapper on Jose's excellent CWstrDic class modeled after the Powerbasic Powercollection object.

It allows key items to be any FB basic type. Over the coming month or so, I'm also planning on taking a crack at being able to store fixed arrays as well.

Maybe Paul can use this instead of SQLite for his editor project.

Test script I used is:


#Include Once "fbcoll/cPowerCollection.inc"

Dim oPowerCollection as cPowerCollection
Dim iByte            as Byte = -1
Dim iuByte           as UByte = 255
Dim iShort           as Short = -1234
Dim iuShort          as UShort = 2345
Dim iLong            as Long = 2000000000
Dim iuLong           as uLong = 345678
Dim iInteger         as Integer = 45678
Dim iuInteger        as UInteger = 56789
Dim iLLong           as LongInt  = 300000000003
Dim iLuLong          as uLongInt = 400000000004
Dim iSingle          as Single = 12.12
Dim iDouble          as Double = 1234567890.23
Dim sString          as String = "Jose"
Dim szString         as ZString * 5 = "Paul"
Dim swString         as WString * 10 = "Freebasic"
Dim uItem            as COLLECTION_DATA
Dim iIndex           as Long
Dim sItemContent     as String
Dim arItems()        as COLLECTION_DATA

' Build a collection of different data types   

    oPowerCollection.Add("Byte",iByte)
    oPowerCollection.Add("UByte",iuByte)
    oPowerCollection.Add("Short",iShort)
    oPowerCollection.Add("UShort",iuShort)
    oPowerCollection.Add("Long",iLong)
    oPowerCollection.Add("ULong",iuLong)
    oPowerCollection.Add("Integer",iInteger)
    oPowerCollection.Add("UInteger",iuInteger)
    oPowerCollection.Add("LongInt",iLLong)
    oPowerCollection.Add("ULongInt",iLuLong)
    oPowerCollection.Add("Single",iSingle)
    oPowerCollection.Add("Double",iDouble)
    oPowerCollection.Add("String",sString)
   
' These are treated as String types   
   
    oPowerCollection.Add("ZString",szString)
    oPowerCollection.Add("WString",swString)
   
' Add some items using literals

    oPowerCollection.Add("Number Literal",99.32)
    oPowerCollection.Add("Integer Literal",98765)
    oPowerCollection.Add("String Literal","cPowerCollection")
   
    Print Str(oPowerCollection.Count()) + " items added"
    Print ""
   
' Sort the list ascending

    oPowerCollection.Sort(cCollectionClass.SORT_ASCENDING)
   
' Cycle through the list top to bottom and show the keys and items
   
   For iIndex = 1 To oPowerCollection.Count()
   
       oPowerCollection.Index(iIndex)
       oPowerCollection.Entry(uItem)
       
       Select Case uItem.DataType
       
       Case cCollectionClass.BYTE_TYPE
       
          sItemContent = "Byte=" + Str(uItem.DataValue_Byte)
       
       Case cCollectionClass.UBYTE_TYPE
       
          sItemContent = "UByte=" + Str(uItem.DataValue_UByte)       
       
       Case cCollectionClass.SHORT_TYPE
       
          sItemContent = "Short=" + Str(uItem.DataValue_Short)       
       
       Case cCollectionClass.USHORT_TYPE
       
          sItemContent = "UShort=" + Str(uItem.DataValue_UShort)       
       
       Case cCollectionClass.LONG_TYPE
       
          sItemContent = "Long=" + Str(uItem.DataValue_Long)       
       
       Case cCollectionClass.ULONG_TYPE
       
          sItemContent = "ULong=" + Str(uItem.DataValue_ULong)       
       
       Case cCollectionClass.INTEGER_TYPE
       
          sItemContent = "Integer=" + Str(uItem.DataValue_Integer)       
       
       Case cCollectionClass.UINTEGER_TYPE
       
          sItemContent = "UInteger=" + Str(uItem.DataValue_UInteger)       
       
       Case cCollectionClass.LONGINT_TYPE
       
          sItemContent = "LongInt=" + Str(uItem.DataValue_LongInt)       
       
       Case cCollectionClass.ULONGINT_TYPE
       
          sItemContent = "ULongint=" + Str(uItem.DataValue_ULongint)       
       
       Case cCollectionClass.SINGLE_TYPE
       
          sItemContent = "Single=" + Str(uItem.DataValue_Single)       
       
       Case cCollectionClass.DOUBLE_TYPE
       
          sItemContent = "Double=" + Str(uItem.DataValue_Double)       
       
       Case Else
       
          sItemContent = "String=" + uItem.DataValue_String
         
       End Select   
       
       Print "Key=" + uItem.DataKey + "," + sItemContent

   Next
   
   Print ""
   
' Remove key "WString"

   oPowerCollection.Remove("WString")
   
   If oPowerCollection.Contains("WString") = True Then
   
      Print "WString still in collection"
     
   Else
   
      Print "WString removed from collection"
     
   End If
   
   Print ""
   
' Update "ZString" from "Freebasic" to match "Byte" value of -1

   oPowerCollection.Replace("ZString",iByte)
   
' Resort to descending

   oPowerCollection.Sort(cCollectionClass.SORT_DESCENDING)
   
' Get all items and show them

   oPowerCollection.Items(arItems())
   
   For iIndex = LBound(arItems) To UBound(arItems)
   
       Select Case arItems(iIndex).DataType
       
       Case cCollectionClass.BYTE_TYPE
       
          sItemContent = "Byte=" + Str(arItems(iIndex).DataValue_Byte)
       
       Case cCollectionClass.UBYTE_TYPE
       
          sItemContent = "UByte=" + Str(arItems(iIndex).DataValue_UByte)       
       
       Case cCollectionClass.SHORT_TYPE
       
          sItemContent = "Short=" + Str(arItems(iIndex).DataValue_Short)       
       
       Case cCollectionClass.USHORT_TYPE
       
          sItemContent = "UShort=" + Str(arItems(iIndex).DataValue_UShort)       
       
       Case cCollectionClass.LONG_TYPE
       
          sItemContent = "Long=" + Str(arItems(iIndex).DataValue_Long)       
       
       Case cCollectionClass.ULONG_TYPE
       
          sItemContent = "ULong=" + Str(arItems(iIndex).DataValue_ULong)       
       
       Case cCollectionClass.INTEGER_TYPE
       
          sItemContent = "Integer=" + Str(arItems(iIndex).DataValue_Integer)       
       
       Case cCollectionClass.UINTEGER_TYPE
       
          sItemContent = "UInteger=" + Str(arItems(iIndex).DataValue_UInteger)       
       
       Case cCollectionClass.LONGINT_TYPE
       
          sItemContent = "LongInt=" + Str(arItems(iIndex).DataValue_LongInt)       
       
       Case cCollectionClass.ULONGINT_TYPE
       
          sItemContent = "ULongint=" + Str(arItems(iIndex).DataValue_ULongint)       
       
       Case cCollectionClass.SINGLE_TYPE
       
          sItemContent = "Single=" + Str(arItems(iIndex).DataValue_Single)       
       
       Case cCollectionClass.DOUBLE_TYPE
       
          sItemContent = "Double=" + Str(arItems(iIndex).DataValue_Double)       
       
       Case Else
       
          sItemContent = "String=" + arItems(iIndex).DataValue_String
         
       End Select   
       
       Print "Key=" + arItems(iIndex).DataKey + "," + sItemContent

   Next
   
   Print ""

' Clear the collection

   oPowerCollection.Clear()
   
   Print "All items have been removed and the count is " + Str(oPowerCollection.Count())


Paul Squires

Quote from: Richard Kelly on January 24, 2017, 02:59:52 AM
Maybe Paul can use this instead of SQLite for his editor project.

Thanks for creating and sharing this code. Cool stuff :)

I already have a super fast hash based solution that I have implemented into the editor that I am using instead of my plan to use SQLite. It is simple, flexible, and won't have all that overhead that SQLite would have brought into the mix.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Richard Kelly

Quote from: TechSupport on January 24, 2017, 10:43:20 AM
Quote from: Richard Kelly on January 24, 2017, 02:59:52 AM
Maybe Paul can use this instead of SQLite for his editor project.

Thanks for creating and sharing this code. Cool stuff :)

I already have a super fast hash based solution that I have implemented into the editor that I am using instead of my plan to use SQLite. It is simple, flexible, and won't have all that overhead that SQLite would have brought into the mix.

I'll take a look at it in the future. Now that I have a FB PowerCollection, LinkList and Stack should be quick wrappers base on CWStrArray.