[futurebasic] Re: [FB] Dynamic Array

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 2002 : Group Archive : Group : All Groups

From: Jay <jktr@...>
Date: Fri, 29 Mar 02 00:26:57 -0600
>Thank you. You are quite right. This is the first time I have used Dynamic
>arrays and I didn't even think to calculate the allocation because I thought
>it was a theoretical number. Now I understand.
>Many thanks again
>Barrie  
>
Barrie,

You are correct!--it IS a theoretical number. The compiler doesn't care 
to what size you dimension a dynamic array. Most of them are DIMmed to 
_Maxlong, which is also more ram than any of us have on our machines.

The problem is with the 2-D array. Dynamic arrays are limited to one 
dimension. To get around this, I have always used a dynamic array of 
dynamic handles and the following fn. I can send a short demo 
illustrating its use if you like.

 e-e
 =J= a  y
  "

goto "End of DynamicXREF@.INCL"
'~'6
#if 0

      DYNAMIC XREF@ INCL

        by Jay Reeve

       April 27, 2001

#endif
'~'6
/*

Adding this INCL to your program allows you to store DYNAMIC ARRAY 
handles and replace them in the array, just the way you do with XREF@.

There are only two FN's here. 

FN ArrayCount is a simple 1-line utility you can use to get the index 
of the top element in any dynamic array. You no longer need to keep 
your own record of this! 

FN InstallHandle( DynamicArrayName, theHandle ) is what you use for
Creating and installing Dynamic handles in an array.

Manipulation of dynamic handles MUST ALWAYS be done through this 
FN to avoid loss or corruption of data.

NOTE 1: Header data is only saved when a handle is
about to be replaced. That's why it is essential that
you ALWAYS use FN InstallXHandle for changing handles.

NOTE 2: To create a new handle for your dynamic array, 
use FN InstallXHandle( dynamicArray, _Nil )

You may immediately begin writing elements to the new 
array. There will not be a valid handle in the array
until you have referenced (read or written) at least 
one element in the array. 

Be sure to store the new handle after it is created:
myHandleArray&(ID) = myDynamicArray

NOTE 3: Remember that these handles have extra data 
appended when they are not installed in the array. 
Be careful how you use them.

When you want to use the handle for something, it is 
usually best to install it in the array.

While it is installed in the array, it is a normal 
DYNAMIC array, and may be used as such.

NOTE 4: DON'T FORGET to use FN InstallXHandle to create, 
install and replace handles in a DYNAMIC array. Reverting 
to the XREF@ syntax of myArray = myHandle can create big 
problems. FN InstallXHandle has error checking for this, 
but it is not foolproof!

*/
'~'6
_SaveHeaderSize = _AutoXREFSpare'12
'~'6
/*

This FN actually returns the top index rather than the count, 
which is 1 more.

*/

def fn ArrayCount( @theDynamicArray as ptr ) = 
theDynamicArray.autoXREFCurr& - 1
'~'6
/*

This FN appends header info to the handle being replaced (if any), and 
removes 
it from the incoming handle.

If there is no incoming handle, it clears the header for creation of a 
new one.

*/
local mode
local fn InstallXHandle( @address as ptr, NewHndl& )
'Call as: FN InstallXHandle( ArrayName, HandleToInstall ) or
'         FN InstallXHandle( ArrayName, _Nil ) to initialize a new handle
dim OldHndl&
dim HSize&
dim OSErr
'
oldHndl = address.autoXREFHndl&
long if oldHndl'Append header data to current handle before replacing it.
long if fn gethandleSize( oldHndl ) mod address.autoXREFMax&
stop "Handle-size error in FN InstallXHandle"
end if'Check for acceptable size
long if FN PtrAndHand (@address.autoXREFCurr&,oldHndl,_SaveHeaderSize)
stop "MemErr in FN InstallXHandle"
end if
end if
'
long if NewHndl'Retrieve saved header data from handle
HSize = fn gethandlesize( NewHndl ) - _SaveHeaderSize
address.autoXREFHndl& = NewHndl'Replace handle
address.autoXREFCurr&;_SaveHeaderSize = [NewHndl] + HSize 'Restore header 
info
long if HSize mod address.autoXREFMax&'Check for acceptable size
stop "Wrong handle size in FN InstallXHandle"
end if
sethandlesize( NewHndl, HSize )'Get rid of appendage
xelse'Clear out dynamic header to create new handle
def blockfill( @address.autoXREFHndl&, _SaveHeaderSize + sizeof(handle), 
0 )
end if
end fn
'~'6
"End of DynamicXREF@.INCL"