Author Topic: The Redim Macro  (Read 1466 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
The Redim Macro
« on: June 21, 2018, 01:35:49 AM »
Redim is a core macro for creating dynamic, resizable arrays, and this is an enhancement to provide immediate deallocation of string and bstring elements when the array is down-sized.

This version will be included in the next o2 update:

Code: [Select]
def redim
=========
  '
  'redim %1 %2(%3)
  '%1  type of array
  '%2  name of array
  '%3  number of elements
  '
  #ifndef %2_buffer
    dim static string %2_buffer = ""
    dim static int %2_count
    dim static %1 byref %2
  #endif
  %2_count=%3
  scope
    dim sys %2_qn = len(%2_buffer) / sizeof(%2)
    if %2_count > %2_qn 'EXTEND ARRAY
      %2_buffer += nuls( sizeof(%2) * (%2_count - %2_qn) )
    elseif %2_count <  %2_qn 'REDUCE ARRAY
      #if (typecodeof(%2)>0xc0)and(typecodeof(%2)<0x100)
        string *%2_str : @%2_str = @%2
        'DISPOSE OF BSTRING AND STRING ELEMENTS
        do
          if %2_qn<=%2_count then exit do
          %2_qn--
          freememory strptr %2_str
          strptr(%2_str) = 0
          @%2_str+=sizeof sys
        loop
      #endif
      %2_buffer = left( %2_buffer, sizeof(%2) * %2_count )
    end if
    @%2=strptr %2_buffer
  end scope
end def
'
'TESTS:
=======
int n=1000
redim string s(n)
s(100)="ABC"
s(101)="abc"
print s(101)
redim string s(200)
'print s(100)
function f()
  n=100
  redim string s(n)
  print s[100]
  s[100]="DEF"
end function
'
f()
print s[100]
print "ok"

Arnold

  • Guest
Re: The Redim Macro
« Reply #1 on: June 21, 2018, 02:59:50 AM »
Hi Charles,

I assume redim will not work completely with wstring arrays?

Roland

Charles Pegge

  • Guest
Re: The Redim Macro
« Reply #2 on: June 21, 2018, 03:24:07 AM »
Redim will work for any type.

The typecode for string is 0xe1, and for wstring it is 0xe2
Similarly, bstrings: 0xc1 and 0xc2

Arnold

  • Guest
Re: The Redim Macro
« Reply #3 on: June 21, 2018, 03:50:55 AM »
Thank you, Charles. I was confused because of s(101) which shows a curious output when using wstring. But s(101) is out of range and should not be used anyway.

Edit: I was totally wrong. I overlooked n=1000
« Last Edit: June 21, 2018, 04:22:42 AM by Arnold »