Author Topic: array in type  (Read 6962 times)

0 Members and 1 Guest are viewing this topic.

Offline Ivan

  • Newbie
  • Posts: 17
  • Mild dyslexia
    • View Profile
Re: array in type
« Reply #15 on: June 14, 2021, 07:42:36 am »
Is arrays of types also missing?
Started with BBCBASIC in 1984 and UniComal. Some expirence wth c++ and database. For the last two years I had coded almost every day mostly in Basic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: array in type
« Reply #16 on: June 14, 2021, 07:44:02 am »
You can have arrays of types. You just can't have arrays IN types.
Shuwatch!

Offline Ivan

  • Newbie
  • Posts: 17
  • Mild dyslexia
    • View Profile
Re: array in type
« Reply #17 on: June 14, 2021, 01:29:47 pm »
You can have arrays of types. You just can't have arrays IN types.

The basic i'm currently using don't have types but can have structs of arrays and can have arrays in structs, and I think I mostly can use structs like types.

Exepct that types in QB64 are more precise and more nicely listed and much easier to read.

I' in midst of programming a relational database with one to many relations using arrays with four different files.
Started with BBCBASIC in 1984 and UniComal. Some expirence wth c++ and database. For the last two years I had coded almost every day mostly in Basic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: array in type
« Reply #18 on: June 14, 2021, 02:24:31 pm »
To me, this seems absolutely simple enough for an array in type:

Code: QB64: [Select]
  1.  
  2. Type arrTest
  3.     As _MEM array
  4.  
  5. Dim As arrTest arrTest
  6.  
  7. Dim As Long array(1 To 3)
  8.  
  9. array(1) = 100
  10. array(2) = 5
  11. array(3) = 34
  12.  
  13. arrTest.array = _Mem(array())
  14.  
  15. Dim As Long array2(1 To Val(Str$(arrTest.array.SIZE)) / Val(Str$(arrTest.array.ELEMENTSIZE)))
  16. _MemGet arrTest.array, arrTest.array.OFFSET, array2()
  17. '_MemFree arrTest.array
  18.  
  19. Dim x
  20. For x = LBound(array2) To UBound(array2)
  21.     Print array2(x)
Shuwatch!

Offline Ivan

  • Newbie
  • Posts: 17
  • Mild dyslexia
    • View Profile
Re: array in type
« Reply #19 on: June 15, 2021, 03:19:47 pm »
Most code I have just done seems maybe not simple, but clear. Not many days later, when i have look at the same code I think, what is going on...

I think if I dig into your code I would understand how to use it.

But I wish someting like this pseudo code in QB64:

type vector(array)
  x(array)
  y(array)
  z(array)
end type

or the basic I use (not pseudo):

dim vector_{ a(9,9,9), x(9), y(9), z(9) }
Started with BBCBASIC in 1984 and UniComal. Some expirence wth c++ and database. For the last two years I had coded almost every day mostly in Basic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: array in type
« Reply #20 on: June 15, 2021, 04:29:18 pm »
Here is how to do it (use variable length String Arrays instead of number arrays) in a Type.

Demo of building a Double Spiral without resort to arrays, storing all the points into a Type Variable:
Code: QB64: [Select]
  1. _Title "Array strings in Type Demo" ' b+ 2021-06-15
  2. Const xmx = 640
  3. Const ymx = 640
  4.  
  5. Screen _NewImage(xmx, ymx, 32)
  6. _Delay .25
  7.  
  8. Type points
  9.     xA As String
  10.     yA As String
  11. Dim sq, s2, points, ga, scale, n
  12. sq = ymx: s2 = sq / 2: points = 100: ga = 137.5
  13. scale = sq * 30 / 640
  14. Dim dblSpiral As points
  15.  
  16. ' build it into string arrays
  17. For n = 1 To points
  18.     'compare to xA(n) = s2 + scale * Sqr(n) * Cos(_D2R(n * ga))
  19.     'compare to yA(n) = s2 + scale * Sqr(n) * Sin(_D2R(n * ga))
  20.     SetLong dblSpiral.xA, n, s2 + scale * Sqr(n) * Cos(_D2R(n * ga))
  21.     SetLong dblSpiral.yA, n, s2 + scale * Sqr(n) * Sin(_D2R(n * ga))
  22.  
  23. ''display it
  24. For n = 3 To points
  25.     Line (GetLong&(dblSpiral.xA, n - 2), GetLong&(dblSpiral.yA, n - 2))-(GetLong&(dblSpiral.xA, n), GetLong&(dblSpiral.yA, n))
  26.  
  27. Sub SetLong (array$, index As Long, value&) ' Luke's Method except option explicit requires mod, no variables needed for one type
  28.     If Len(array$) < 4 * (index + 1) Then array$ = array$ + String$(4 * (index + 1) - Len(array$), Chr$(0))
  29.     Mid$(array$, index * 4 + 1) = _MK$(Long, value&)
  30.  
  31. Function GetLong& (array$, index As Long)
  32.     GetLong& = _CV(Long, Mid$(array$, index * 4 + 1, 4))
  33.  
  34.  

Of course you could display the Double Spiral immediately but you wouldn't have the points saved for further processing.
« Last Edit: June 15, 2021, 04:32:03 pm by bplus »

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: array in type
« Reply #21 on: July 02, 2021, 10:01:18 am »
It must be time for my YEARLY or MULTI-YEARLY petition for ARRAYs in UDT!

ARRAYs IN UDTs PLEASE!

I second this motion! :-D

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: array in type
« Reply #22 on: July 02, 2021, 10:06:00 am »
To me, this seems absolutely simple enough for an array in type:

Two questions for you Spriggs
  • Is this cross-platform? Will it work in Windows, Mac, and Linux?
  • Someone mentioned it doesn't do garbage collection. If that is true, would you update your example to show how to release the array from memory when done with it?

Thanks

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: array in type
« Reply #23 on: July 02, 2021, 10:07:04 am »
@madscijr The example does show how to release it from memory. I just have it commented out. And of course it is cross-platform. It's just a regular QB64 function.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: array in type
« Reply #24 on: July 02, 2021, 10:28:21 am »
The way that arrays are passed in Windows API from QB64 in TYPEs are always through an _Offset variable because we are passing it as a pointer in memory. What I'm showing is similar, although I'm instead passing the whole _MEM block rather than just the reference _MEM.OFFSET.

In my Task Dialog library, one passes an array of buttons in a variable called pButtons; an element of the TASKDIALOGCONFIG struct/type. The "p" in pButtons means "pointer" or, in our case, an _Offset. I pass the entire array using _Offset(btnArray()) and the Windows API handles it. I assume they are doing something similar on their end by copying the referenced memory to a newly defined array on the API side. That is why I suggested something similar for us since it works so well in all the APIs that have required an array in a struct.
Shuwatch!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: array in type
« Reply #25 on: July 02, 2021, 10:45:40 am »
@madscijr The example does show how to release it from memory. I just have it commented out. And of course it is cross-platform. It's just a regular QB64 function.

Aha, thanks. I see the _MEMFREE now, not sure how that escaped my attention, maybe I need to adjust the colors on my monitor (I see it just fine on the iPhone screen!) Or maybe I just need coffee.
Either way thanks!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: array in type
« Reply #26 on: July 04, 2021, 02:13:48 pm »
yes, but mem requires a lot of house keeping, not user friendly
I thinking of faking an array using a string*size

Use a function similar to PHP "serialize"
Quote
https://www.php.net/manual/en/function.serialize.php

when I get time, i'll dig around in one of my libs and post it. I made a "kludgy" version as I too was disappointed at the arrays in TYPED vars. :-(

However - ALWAYS follow Spriggs tips!
« Last Edit: July 04, 2021, 02:15:32 pm by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!