Oop example arraystring

Started by Frank Brübach, April 15, 2025, 11:27:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach


 Hello Charles. Whats Missing Here?

' arrayString example oop
'
def createc

class myBuffer
==============
  '
  sys bu 'buffer
  int bl 'buffer length bytes
  '
  function redim(int i)
  =====================
  sys nbu
  int nbl
  nbl=i*sizeof(sys)
  nbu=getmemory nbl
  if bl
    if bl>nbl then bl=nbl 'downsize
    copy nbu,bu,bl
    freememory bu
  endif
  bu=nbu
  bl=nbl
  end function
  '
  function constructor(int i)
  ===========================
  this.redim(i)
  end function
  '
  function destructor()
  ===================
  freememory bu
  bu=0
  bl=0
  end function
  '
end class

class ArrayString
=================
  '
  indexbase 0
  '
  has mybuffer b
  int ns 'number of strings
  '
  function redim(int i)
  =====================
  i++
  if ns>i 'downsize
    bstring s at (b.bu)
    int j
    for j=i to ns-1
      del s[j]
    next
  endif
  b.constructor(i)
  ns=i
  end function
  '
  function constructor(int i)
  ===========================
  b.constructor(i+1)
  int j
  bstring s at (b.bu)
  for j=0 to i
    s[i]=""
  next
  ns=i+1
  end function
  '
  function destructor()
  =====================
  int i
  bstring s at (b.bu)
  for i=0 to ns-1
    frees s[i]
  next
  b.destructor()
  end function
  '
  function vs(int i) as string
  ============================
  bstring s at (b.bu)
  if (i>=0)and(i<ns)
    return s(i)
  else
    'out of bounds error
  endif
  end function
  '
  function vs(int i,string t)
  ===========================
  bstring s at (b.bu)
  if (i>=0)and(i<ns)
    s(i)=t
  else
    'out of bounds error
  endif
  end function
  '
  function lbound() as int
  ========================
  return 0
  end function
  '
  function ubound() as int
  ========================
  return ns-1
  end function
  '
end class
end def

'TESTS
======
createc
new ArrayString ss(99)
ss.vs(50)="okay"
ss.redim(200)
ss.redim(70)
print ss.vs(50) ", " ss.ubound()
del ss


Charles Pegge

Hi Frank,

I've fixed this method:
  function redim(int i)
  =====================
  i++
  if ns>i 'downsize
    bstring s at (b.bu)
    int j
    for j=i to ns-1
      frees s[j] 'not del
    next
  endif
  b.constructor(i)
  ns=i
  end function
 

But redim makes it obsolete so I will remove it from the demos.


$filename "t.exe"
'uses rtl64
def createc
class Buffer
============
  '
  sys bu 'buffer
  int bl 'buffer length bytes
  '
  function redim(int i)
  =====================
  sys nbu
  int nbl
  nbl=i*sizeof(sys)
  nbu=getmemory nbl
  if bl
    if bl>nbl then bl=nbl 'downsize
    copy nbu,bu,bl
    freememory bu
  endif
  bu=nbu
  bl=nbl
  end function
  '
  function constructor(int i)
  ===========================
  this.redim(i)
  end function
  '
  function destructor()
  ===================
  freememory bu
  bu=0
  bl=0
  end function
  '
end class

class ArrayString
=================
  '
  indexbase 0
  '
  has buffer b
  int ns 'number of strings
  '
  function redim(int i)
  =====================
  i++
  if ns>i 'downsize
    bstring s at (b.bu)
    int j
    for j=i to ns-1
      frees s[j]
    next
  endif
  b.constructor(i)
  ns=i
  end function
  '
  function constructor(int i)
  ===========================
  b.constructor(i+1)
  int j
  bstring s at (b.bu)
  for j=0 to i
    s[i]=""
  next
  ns=i+1
  end function
  '
  function destructor()
  =====================
  int i
  bstring s at (b.bu)
  for i=0 to ns-1
    frees s[i]
  next
  b.destructor()
  end function
  '
  function vs(int i) as string
  ============================
  bstring s at (b.bu)
  if (i>=0)and(i<ns)
    return s(i)
  else
    'out of bounds error
  endif
  end function
  '
  function vs(int i,string t)
  ===========================
  bstring s at (b.bu)
  if (i>=0)and(i<ns)
    s(i)=t
  else
    'out of bounds error
  endif
  end function
  '
  function lbound() as int
  ========================
  return 0
  end function
  '
  function ubound() as int
  ========================
  return ns-1
  end function
  '
end class
end def

'TESTS
======
createc
new ArrayString ss(99)
ss.vs(50)="okay"
ss.redim(200)
ss.redim(70)
print ss.vs(50) ", " ss.ubound()
del ss