Page 1 of 1

Simple Auto Dim

Posted: Thu Jun 11, 2009 1:38 pm
by einander

Code: Select all

; Simple Auto Dim 
; by einander

; First array element reserved to keep array's dimension
; Optional paramenter Check.f Must be >=1
; On each successful check, array dimension is augmented by his dimension/Check

Procedure AutoDim(Index,Array Arr(1),Check.f=2)
  If Index>=Arr(0)
    Arr(0)+ArraySize(Arr())/Check+1
    Redim Arr(Arr(0))
  EndIf
EndProcedure 

;<<<<<<<<<<<<<<<<<<<<<<<<
;Test 
Dim Arr(0)

For i=1 To 123456
  AutoDim(i,Arr())
  Arr(i)=i*11
Next

For i=1 To 123456 Step 10000
  T$+"Index "+Str(i)+Chr(9)+" Value "+Str(Arr(i))+Chr(10)
Next
MessageRequester("AutoDim - Array size = "+Str(ArraySize(Arr())),T$,0)

Posted: Thu Jun 11, 2009 1:58 pm
by djes
Image

Posted: Fri Jun 12, 2009 5:14 pm
by Rescator
I don't think I understand what you are trying to do here but...
If you wanted to fill an array but have no idea how big it will be, then this is how I'd do it:

Code: Select all

Dim Arr(0)

For i=1 To 123456
  ReDim Arr(ArraySize(Arr())+1)
  Arr(i)=i*11
Next

For i=1 To 123456 Step 10000
  T$+"Index "+Str(i)+Chr(9)+" Value "+Str(Arr(i))+Chr(10)
Next
MessageRequester("AutoDim - Array size = "+Str(ArraySize(Arr())),T$,0)
PS! I used the same loop as you, personally I'd start as 0 as Arr(0) is an actual entry as well.

Posted: Sat Jun 13, 2009 3:29 pm
by dagcrack
An efficient way of resizing buckets is by duplicating their size, while you may end up wasting some RAM it is by far the most flexible way around and the most accepted as well. I personally use an increase ratio calculated on the estimated growth of the bucket.

But quite frankly, resizing on each run seems rather idiotic. No offense to Rescator.

einander's example is funny in the way that there is a known constant amount of iterations which defeats the purpose of checking bounds to resize upon requirement :P

There is hardly a case where you don't exactly (or nearly) know how many items you'll need to store/keep.

PS: The reason he starts at 1 is described in his comment "First array element reserved to keep array's dimension ".

Posted: Mon Jun 15, 2009 12:22 pm
by einander
@Rescator:
I don't think I understand what you are trying to do here
I'm decreasing the number of redimensions.
On my example: the frequency of the redimension depends on the size of the array.

For 10000000 loops
with Check.f=1 : 24 redimensions
with Check.f=2.5: 44 redimensions

On your example : the array is redimensionated on each loop
personally I'd start as 0 as Arr(0) is an actual entry as well.
The first element on the example is reserved to keep array's dimension, because is slightly fast to get the array dimension reading Arr(0) than getting the value from ArraySize(Arr()).

@Dagcrack
There is hardly a case where you don't exactly (or nearly) know how many items you'll need to store/keep.
Yes.
I found with one of these cases, counting the points of a complex spline.

Posted: Tue Jun 16, 2009 1:28 am
by idle
@einander

I didn't spot the trick the first time, nice tip.