Simple Auto Dim

Share your advanced PureBasic knowledge/code with the community.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Simple Auto Dim

Post 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)
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Image
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post 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 ".
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post 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.
User avatar
idle
Always Here
Always Here
Posts: 5917
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

@einander

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