Dynamische Arrays mit Banks

Übersicht BlitzBasic Codearchiv

Neue Antwort erstellen

 

OJay

Betreff: Dynamische Arrays mit Banks

BeitragSo, Feb 15, 2004 2:09
Antworten mit Zitat
Benutzer-Profile anzeigen
das problem: man braucht arrays mit mehreren dimensionen, muss die aber in einem type-container speichern. was tun? blitzarrays sind ja nur ein dimensional :/

die lösung: die arrays in banks speichern, und das handle der jeweiligen bank in dem container speichern. so kann jede type-variable ihr eigenes mehrdimensionales array haben.

anwendungsbeispiel:
ein editor, in dem beliebig viele dateien mit beliebige inhalt gleichzeitig geöffnet sein sollen.

hier die bibliothek:

Code: [AUSKLAPPEN]
;//////////////////////////////////////////////////////////////////////////
;// use dynamic arrays with banks
;// by OJay Feb 2004 (ojay[at]blue-cable[dot]de)
;// currently works only with 1, 2 or 3 dimensions
;//////////////////////////////////////////////////////////////////////////

; create an array
; specify the size of the array.
; y and z are optional
; returns the handle of the bank
Function ArrayBank(x, y = -1, z = -1)
   
   If y = -1
      bank = CreateBank( (x+1) * 4 )
   ElseIf z = -1
      bank = CreateBank( 4 + (x+1) * (y+1) * 4 )
       ; save size of array
      PokeInt bank, 0, x
   Else
      bank = CreateBank( 8 + (x+1) * (y+1) * (z+1) * 4 )
       ; save size of array
      PokeInt bank, 0, x
      PokeInt bank, 4, y
   EndIf
   
   Return bank

End Function

; assign a value into an array
; put the specified value into the bank at the given koordinates
Function ArrayPush(bank, value, x, y = -1, z = -1)

   If y = -1
      PokeInt bank, x * 4, value
   ElseIf z = -1
       ; get size of array
      BankarrayXsize = PeekInt (bank, 0)
      PokeInt bank, 4 + (x + y*BankarrayXsize)*4, value
   Else
       ; get size of array
      BankarrayXsize = PeekInt (bank, 0)
      BankarrayYsize = PeekInt (bank, 4)
      PokeInt bank, 8 + (x + y*BankarrayXsize + z*(BankarrayXsize *BankarrayYsize ))*4, value
   EndIf

End Function

; get an value out of an array at the specified position
Function ArrayPull(bank, x, y = -1, z = -1)

   If y = -1
      Return PeekInt (bank, x * 4)
   ElseIf z = -1
       ; get size of array
      BankarrayXsize = PeekInt (bank, 0)
      Return PeekInt (bank, 4 + (x + y*BankarrayXsize ) * 4)
   Else
       ; get size of array
      BankarrayXsize = PeekInt (bank, 0)
      BankarrayYsize = PeekInt (bank, 4)
      Return PeekInt (bank, 8 + (x + y*BankarrayXsize + z*(BankarrayXsize *BankarrayYsize ))*4)
   EndIf

End Function

; resize the array (be careful with that!)
; all informations will be out dated and at wrong positions
; refill the array in every case!
Function ArrayDim(bank, x, y = -1, z = -1)

   If y = -1
      ResizeBank bank, (x + 1) * 4
   ElseIf z = -1
      ResizeBank bank, ( 4 + (x+1) * (y+1) * 4 )
       ; save new size of array
      PokeInt bank, 0, x
   Else
      ResizeBank bank, ( 8 + (x+1) * (y+1) * (z+1) * 4 )
       ; save new size of array
      PokeInt bank, 0, x
      PokeInt bank, 4, y
   EndIf

End Function

; free the array
; pretty useless, just for completeness purposes :-)
Function ArrayClear(bank)

   FreeBank bank

End Function



und hier noch ein beispiel:
füllt erst ein 3dimensionales array mit einem inkrementierenden wert, und vergleicht dann jeden eintrag mit jedem anderen.


Code: [AUSKLAPPEN]
Include "libs\dyn_arrays.bb"

Graphics 500,700
SeedRnd MilliSecs()


xsize=11
ysize=12
zsize=13

bank = arraybank(xsize,ysize,zsize)

For i=0 To xsize-1
For j=0 To ysize-1
For k=0 To zsize-1
   arraypush (bank, f, i, j, k)
   f=f+1
Next
Next
Next

For i=0 To xsize-1
For j=0 To ysize-1
For k=0 To zsize-1
   pull1 = arraypull(bank, i, j, k)
   For l=0 To xsize-1
   For m=0 To ysize-1
   For n=0 To zsize-1
      pull2 = arraypull (bank, l, m, n)
      If pull1 = pull2 And (i<>l And j<>m And k<>n)
         Print "dopplung: "+i+","+j+","+k+"/"+l+","+m+","+n+": "+arraypull (bank, i, j, k )
         d=d+1
      EndIf
      c=c+1
   Next
   Next
   Next
Next
Next
Next

If Not d
   Print "In "+Str(c)+" Testläufen keine Dopplungen gefunden! Algo funzt :)"
Else
   Print d+" Dopplungen in "+c+" Testläufen gefunden...so ein scheiss :("
EndIf


achja hätte ich fast vergessen: momentan kann man nur integers speichern, weil ich nicht mehr gebraucht habe...liesse sich aber leicht auf andere datentypen ändern... Razz
 

walski

Ehemaliger Admin

BeitragSo, Feb 15, 2004 2:17
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich will nicht komisch wirken, aber so viel code für dynamische Felder... da benutz ich lieber Types.
Ja, das IST eine subjektive Meinung aber ich wollt auch eigentlich nur mal fragen warum dein Code geiler ist als n Type Wink

walski
buh!
 

OJay

BeitragSo, Feb 15, 2004 2:29
Antworten mit Zitat
Benutzer-Profile anzeigen
der code ist ein ersatz für dim(x,y,z)! nicht für types. das mit den type war nur ein beispiel!

du würdest also sowas machen:
foo.bar = new bar
foo\bank = ArrayBank(x,y,z)

und hast nun ein array, das du wie ein dim-array behandeln kannst:

arraypush(foo\bank, 123, x,y,z) ; 123 in array schreiben
wert = arraypull(foo\bank, x,y,z) ; wert wieder rausholen

der einzige unterschied zu dim-arrays ist halt, das du immer das handle der bank angeben musst! aber das ist, denke ich, ein kleiner preis für dynamische mehrdimensionale arrays, die man in types speuchern kann Wink

Shadow of the night

BeitragSo, Feb 15, 2004 12:15
Antworten mit Zitat
Benutzer-Profile anzeigen
Da benutz ich eigentlich auch lieber types.

BlitzBasic: [AUSKLAPPEN]
 Graphics 500, 700


sollte wohl eher

BlitzBasic: [AUSKLAPPEN]
Graphics 500, 700, 16, 2

heissen.

MfG Shadow of the Night
User posted image
 

IonPainter

BeitragSo, Feb 15, 2004 12:19
Antworten mit Zitat
Benutzer-Profile anzeigen
nix, die zwei letzten parameter sind optional, obwohl die auflösing im vollbild wohl nicht funzen wird

Neue Antwort erstellen


Übersicht BlitzBasic Codearchiv

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group