Page 3 of 3 FirstFirst 123
Results 21 to 25 of 25

Thread: some arrays goodies

  1. #21
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Hi,

    primo basically nailed the stringBuilder usage, here a little example:
    Uses "console", "stringBuilder"
     
    ' this will store the numbers:
    Dim builder as new StringBuilder()
     
    ' access the numbers virtually:
    Dim myNumber() as Number At 0
     
    Dim i As Long ' just to list our arrays elements
     
    ' add a few elements, type Ext equals Number, that's why MKE$:
    builder.Add(MKE$(0.12345)) ' the first element be 0.12345
    ' just append a bunch of new elements:
    builder.Add(MKE$(1,2,3,4,5))
    ' and some more
    builder.Add(MKE$(6.666, 7.777))
     
    ' now let's read what we have:
    ' put the virtual array onto the string:
    string builderData = builder.ToString() 
    Redim myNumber(Strptrlen(Strptr(builderData))/SizeOf(Number)) At Strptr(builderData)
     
    ' list what we have
    For i = 1 to UBound(myNumber)
      PrintL i, myNumber(i)
    Next
     
    WaitKey
    
    I may add direct pointer to string in stringBuilder as functionality, if there is interest. That would skip the step with assignment to temporary string variable, making it even faster


    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

  2. #22
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    Quote Originally Posted by Petr Schreiber View Post
    I may add direct pointer to string in stringBuilder as functionality, if there is interest
    It would be great.
    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

  3. #23
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    55
    Posts
    1,554
    Rep Power
    174
    If I understood right the stringbuilder allocates more size than the actual length of the string. But sb.Length() then returns the number of chars that are occupied.
    Then assuming sb gets a new method named DataPtr() it were like
    Redim myNumber(sb.Length/SizeOf(Number)) At sb.DataPtr
    
    ?
    However we would access the single elements of the array myNumber using parenthesis and index. No matter what type myNumber really is, even fixed-size Udt can be used.
    SizeOf(...) tells how many bytes we need, the index of myNumber(...) tells which bytes we need.

    Let's say we have the array as in the example stored to builder and we want to list the arrays elements without the use of a virtual variable.

    Cumbersome we have to calculate:
    For i = 1 To builder.Length/SizeOf(Number)
      PrintL i, CVE(Memory_Get(builder.DataPtr + (i - 1) * SizeOf(Number), SizeOf(Number)))
    
    Next
    
    How about methods to stringbuilder similar to current _.Char() but
    _.Element(index, SizeOf(datatype)) : return an elements data, means a sequence of bytes
    _.Elements(SizeOf (datatype)) : return UBound of elements stored to sb

    For i = 1 to builder.Elements(SizeOf(Number))
    PrintL i, CVE (builder.Element( i, SizeOf(Number)))
    
    Next
    
    or even better tell stringbuilder the SizeOf elements or the required datatype and it will remember it:

    Dim builder As New stringbuilder
    
    builder.SetType(Number) ' default Byte/Char
     ' or
    builder.ElementSizeSet(SizeOf(Number)) ' default 1, don't want division by 0...
    builder.Add(MKE$(0.123))
    builder.Add(MKE$(1,2,3,4))
    builder.Add(MKE$(5.5,6.6,7.7))
    For i = 1 To builder.Elements()
      PrintL i, CVE(builder.Element(i)))
    ' ...
    
    I don't know if possible for Udt if using _.SetType, but having to give the size once only instead of repeating it every time were great

    @ Eros,
    how about
    Alias MKE$ As MKNumber$
    Alias CVE As CVNumber
    For better readability and understanding?
    Last edited by ReneMiner; 25-03-2017 at 20:32.
    I think there are missing some Forum-sections as beta-testing and support

  4. #24
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10
    Quote Originally Posted by ReneMiner View Post
    @ Eros,
    how about
    Alias MKE$ As MKNumber$
    Alias CVE As CVNumber
    For better readability and understanding?
    Yes,good point.
    I will add.
    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

  5. #25
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Hi friends,

    I added the DataPtr property, which allows you to directly access the internal buffer of stringBuilder object!

    @Eros: Can we please have this in the next release of thinBasic?


    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

Page 3 of 3 FirstFirst 123

Similar Threads

  1. 3d arrays?
    By JosephE in forum thinDebug
    Replies: 2
    Last Post: 08-08-2010, 23:21
  2. Arrays
    By Charles Pegge in forum O2h Compiler
    Replies: 0
    Last Post: 17-03-2009, 16:09

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
  •