Einzelnen Beitrag anzeigen

alzaimar
(Moderator)

Registriert seit: 6. Mai 2005
Ort: Berlin
4.956 Beiträge
 
Delphi 2007 Enterprise
 
#7

Re: Speichereffizientes Speichern von Boolean Array's

  Alt 25. Feb 2008, 19:23
Eigentlich ist das nicht so aufwändig. Du verwendest eben kein TBoolArray sondern eine Klasse, die das wie oben packt.
Delphi-Quellcode:
Type
  TByteArray = Array Of Byte;
  T4DimBoolArray = Class
  private
    fDim1, fDim2, fDim3, fDim4: Integer;
    fBytes: Array Of Byte;
    Function GetValue(i, j, k, l: Integer): Boolean;
    Procedure SetValue(i, j, k, l: Integer; Const Value: Boolean);
  public
    Constructor Create(dim1, dim2, dim3, dim4: Integer);
    Property Value[i, j, k, l: Integer]: Boolean read GetValue write SetValue;
    Property Bytes: TByteArray read fBytes;
  End;

Constructor T4DimBoolArray.Create(dim1, dim2, dim3, dim4: Integer);
Begin
  fDim1 := dim1;
  fDim2 := dim2;
  fDim3 := dim3;
  fDim4 := dim4;
  SetLength(fBytes, fDim1 * fDim2 * fDim3 * fDim4);
End;

Function T4DimBoolArray.GetValue(i, j, k, l: Integer): Boolean;
Var
  b: Integer;

Begin
  b := ((i * fDim1 + j) * fDim2 + k) * fDim3 + l;
  result := fBytes[b Div 8] And (1 Shl b Mod 8) <> 0;
End;

Procedure T4DimBoolArray.SetValue(i, j, k, l: Integer; Const Value: Boolean);
Var
  b, b1, b2: Integer;
  m: Byte;
Begin
  b := ((i * fDim1 + j) * fDim2 + k) * fDim3 + l;
  b1 := b Div 8;
  m := 1 Shl b Mod 8;
  If Value Then
    fBytes[b1] := fBytes Or m
  Else
    fBytes[b1] := fBytes And Not m;
End;
Ungetestet.
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat