Page 5 of 6 FirstFirst ... 3456 LastLast
Results 41 to 50 of 52

Thread: ARRAY-ideas

  1. #41
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    what am i doing wrong here on Array Scan ?

    Uses "console"
    
    ' try both, the normal way...
    String test1 = MKDWD$(123)
    String test2 = MKDWD$(321)
    
    DWord hPtr = HEAP_AllocByStr( test1 & test2 & test1 & test2 & test1 & test2 & test1 & test2)
    
    Dim vPtr(8) As DWord At hPtr
    DWord toFind = 123
    Long lPos, Index
    
    PrintL "normal scan..."
    Do
      lPos = Array Scan vPtr(Index+1), Byte(1,4), = toFind
      
      If lPos Then
        Index += lPos
        PrintL "found", index
        
      EndIf  
    Loop While lPos
    
    ' output i await is found 1, found 3, found 5, found 7...
    PrintL "key to continue"
    
    WaitKey                                                        
    HEAP_Free(hPtr)
    
    
    ' some data to scan for:
    'test1 = MKDWD$(123,321)
    'test2 = MKDWD$(321,123)
    
    ' arrange it in an array
    hPtr = HEAP_AllocByStr( MKDWD$( HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2), _
                                    HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2), _
                                    HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2), _
                                    HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2)  _
                                 )       )
    
    index = 0
    
    ReDim vPtr(8) At hPtr
    
    PrintL $CRLF & "ptr scan"
    
    Do
      lPos = Array Scan vPtr(Index+1) Ptr, Byte(1,4), = MkDwd$(toFind)
      
      If lPos Then
        Index += lPos
        PrintL "found", index
        
      EndIf          
      
    Loop While lPos
    ' output i await is found 1, found 3, found 5, found 7...
    
    PrintL "key to end"
    WaitKey
    
    Last edited by ReneMiner; 13-10-2014 at 10:06.
    I think there are missing some Forum-sections as beta-testing and support

  2. #42
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    always new ideas but see above first...


    my current idea is about Array Sort this time, it needs a few steps to realize:

    Array Sort for standard UDTs alike

    Type t_Type
      X as Long
      S as String
    End Type
    
    Dim myArray(123) as t_Type
    
    Array Sort myArray([StartIndex]) [For nElements][,{Ascend | Descend}][, AsFiles] [,Udt_ElementByte( myArray.X)[,Long] ]
    
    Array Sort myArray([StartIndex]) [For nElements][,{Ascend | Descend}][, AsFiles] [,Udt_ElementByte( myArray.S)[,String] ]
    
    If we can sort udts by a certain property than we could think about sorting theirs Pointers and add some Ptr/Heap_Ptr-option...


    And another small one:

    Array Swap member1, member2
    
    Last edited by ReneMiner; 15-10-2014 at 10:55.
    I think there are missing some Forum-sections as beta-testing and support

  3. #43
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,780
    Rep Power
    10
    Quote Originally Posted by ReneMiner View Post
    what am i doing wrong here on Array Scan ?

    Uses "console"
    
    ' try both, the normal way...
    String test1 = MKDWD$(123)
    String test2 = MKDWD$(321)
    
    DWord hPtr = HEAP_AllocByStr( test1 & test2 & test1 & test2 & test1 & test2 & test1 & test2)
    
    Dim vPtr(8) As DWord At hPtr
    DWord toFind = 123
    Long lPos, Index
    
    PrintL "normal scan..."
    Do
      lPos = Array Scan vPtr(Index+1), Byte(1,4), = toFind
      
      If lPos Then
        Index += lPos
        PrintL "found", index
        
      EndIf  
    Loop While lPos
    
    ' output i await is found 1, found 3, found 5, found 7...
    PrintL "key to continue"
    
    WaitKey                                                        
    HEAP_Free(hPtr)
    
    
    ' some data to scan for:
    'test1 = MKDWD$(123,321)
    'test2 = MKDWD$(321,123)
    
    ' arrange it in an array
    hPtr = HEAP_AllocByStr( MKDWD$( HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2), _
                                    HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2), _
                                    HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2), _
                                    HEAP_AllocByStr(test1), _
                                    HEAP_AllocByStr(test2)  _
                                 )       )
    
    index = 0
    
    ReDim vPtr(8) At hPtr
    
    PrintL $CRLF & "ptr scan"
    
    Do
      lPos = Array Scan vPtr(Index+1) Ptr, Byte(1,4), = MkDwd$(toFind)
      
      If lPos Then
        Index += lPos
        PrintL "found", index
        
      EndIf          
      
    Loop While lPos
    ' output i await is found 1, found 3, found 5, found 7...
    
    PrintL "key to end"
    WaitKey
    
    You are doing nothing wrong.
    I did something wrong: I forgot that ARRAY SCAN ...returns position relative to starting scan position.
    Instead I was returning absolute position when using ARRAYS SCAN ... PTR ...

    Please find here attached an updated thinCore.dll version.
    As usual, just unzip into \thinBasic\ directory replacing your current one.
    Let me know if it works

    Ciao
    Eros
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #44
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    perfect
    I think there are missing some Forum-sections as beta-testing and support

  5. #45
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171

    idea for 2 functions: Array Load & Save

    working on any type of fixed size array

    #MinVersion 1.9.11.0
    
    Function Array_Load(ByVal sFilename As String, _
                        ByRef a() As Any           _
                        ) As Long
    
    
      ReDim a(1)
      
      Local lSize As Long = SizeOf(a(1))
      Local sData As String = Load_File(sFilename)
      
      If StrPtrLen(StrPtr(sData)) = 0 Then Return 0
      
      ReDim a(StrPtrLen(StrPtr(sData))/lSize)   
      
      Memory_Set(VarPtr(a(1)), sData )
      Function = UBound(a)
      
    End Function                    
    
    
    Function Array_Save(ByVal sFilename As String,       _
                        ByRef a() As Any,                _
               Optional ByVal FirstElementIndex As Long, _
                        ByVal numElements       As Long  _
                        ) As Boolean
    
      
      If UBound(a) < 1 Then Return FALSE
      
      Local lSize As Long = SizeOf(a(1))
      
      firstElementIndex = MinMax(firstElementIndex, 1, UBound(a) )
      
      If numElements < 1 Then numElements = UBound(a)
      
      numElements       = MinMax(numElements, 1, UBound(a) + 1 - FirstElementIndex)
         
      Function = ( Save_File(sFilename, Memory_Get(VarPtr(a(FirstElementIndex)), numElements * lSize)) <> 0 )
      
    End Function
    
    
    ' -------------------------------------------------------------
    ' test
    
    Uses "console"
    
    Type t_myType
      A As Long
      B As Byte
      C As Double 
      D As String * 8
    End Type
    
    $Filename = APP_ScriptPath & "test.dat"
    
    Dim foo(3) As t_myType
    Dim i As Long
    ' fill in some data
    For i = 1 To UBound(foo)
      foo(i).a = i * 123
      foo(i).b = i
      foo(i).c = i * 1.23
      foo(i).d = "foo(" & TStr$(i) & ")"
    Next
    
    If Not Array_Save($Filename, foo ) Then
      PrintL "oops- did not work..."
    Else
      PrintL "data saved"
      ' kill data:
      ReDim foo(1)       
      PrintL "memory erased now"
      
      If Not Array_Load($Filename, foo ) Then
        PrintL "damn, error on loading!"
      Else  
        PrintL "seems it loaded, let's check data"
        
        For i = 1 To UBound(foo)
          PrintL foo(i).d & " :", foo(i).A, foo(i).B, foo(i).c
        Next 
      EndIf
    EndIf
    
    WaitKey
    
    side product of some thoughts only...
    Petr would name those Array ToFile/FromFile probably
    Last edited by ReneMiner; 24-01-2015 at 13:30.
    I think there are missing some Forum-sections as beta-testing and support

  6. #46
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732
    Hi Rene,

    I was thinking about it and Save/Load is okay for me


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #47
    In Oxygen, using string overlays:

    string s
    GetFile "Vertex.b",s
    float fl at (strptr s)

    'fl[..] is now a dynamic float array


    PutFile "Vertex.b",s

  8. #48
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,780
    Rep Power
    10
    Exactly the same in ThinBASIC:
    '---Load a string from a file. It is supposed file contains
    '---binary representation of a sequence of doubles (8 bytes each)
    String s = Load_File(APP_SourcePath & "FileOfDoubleBinaryNumbers.txt")
    '---Create a virtual variable d (virtual because it uses the same memory area
    '---of another variable, in this case string s
    Double d(Len(s) / SizeOf(Double)) At StrPtr(s)
    
    
    '---d is now an array of double
    '---Because d is dimensioned using the same memory area of another
    '---variable, it cannot be re-dimmed but just used
    
    
    '---Save back s to file. If d() elements have been changed,
    '---those changes will be reflected to string s
    Save_File(APP_SourcePath & "FileOfDoubleBinaryNumbers.txt", s)
    
    Problem is when you have an array of dynamic string or arrays of pointers to memory areas and you want to save/load from file.
    In this case you need to write your own procedure because dynamically allocated memory areas/strings must be re-allocated at run-time.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #49
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,533
    Rep Power
    171
    it was just an idea and of course it would not make sense to save array-elemtents that contain handles, string- or other memory-pointers.
    it's meant for "simple data" only which is usually stored in a 1-dimensional array with fixed sized elements (as color-palettes, vertex-lists etc.)
    I think there are missing some Forum-sections as beta-testing and support

  10. #50
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,780
    Rep Power
    10
    As usual, yours are always very interesting ideas and quite sure I will develop your request because I think it is general purpose and useful.
    Just discussing on how this can be done and what cases it can be applied.
    I think the only limitation is when arrays has some dynamic allocated data but in all other cases, even UDT with no dynamic data inside.

    I think we can think more general and think to object (arrays, UDT, ...) serialization: http://en.wikipedia.org/wiki/Serialization
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Page 5 of 6 FirstFirst ... 3456 LastLast

Similar Threads

  1. OOP ideas
    By ErosOlmi in forum Suggestions/Ideas discussions
    Replies: 13
    Last Post: 26-08-2013, 20:26
  2. Ideas for dynamic Array-constructor on the fly
    By ReneMiner in forum Suggestions/Ideas discussions
    Replies: 0
    Last Post: 16-07-2013, 08:53
  3. copy array of UDT to another array in one step?
    By ReneMiner in forum thinBasic General
    Replies: 3
    Last Post: 02-11-2012, 01:15
  4. More Ideas
    By peter in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 3
    Last Post: 27-10-2012, 14:47

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •