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
    6,795
    Blog Entries
    3
    Rep Power
    693
    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 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  2. #22
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,183
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #23
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Philippines
    Age
    47
    Posts
    1,218
    Blog Entries
    1
    Rep Power
    133
    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.
    [TABLE="class: tblf1 tblf-fullwidth tblf-alternate"]
    [TR]
    [TD][COLOR=#b22222][B][SIZE=1]The limit is[/SIZE][/B][/COLOR][/TD]
    [TD="class: isRelinked"] [COLOR=#0000cd][B][SIZE=1]Imagination[/SIZE][/B][/COLOR][/TD]
    [/TR]
    [/TABLE]

  4. #24
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,183
    Blog Entries
    2
    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/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #25
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,795
    Blog Entries
    3
    Rep Power
    693
    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 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

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

Posting Permissions

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