Ec72b0ab3eb1855c56fd04bcc886f65c

To get this Section started, here are a couple of Array functions which I'm certain are hugely inefficient. All of the Arrays are the Array of Byte type.

- The CopyIntoArray function takes a Destination array and Start Index and copies the Source array into the DestArray starting at the SourceIndex. I don't like having to loop through the whole array each time, there must be an easier way?

- The Append to Array function takes a Destination array as a var and then resizes it and copies the Source Array into the newly increased space. I don't like the way this array has to be passed as a var.

Any suggestions or help refactoring would be much appreciated!

procedure CopyIntoArray(var DestArray: Array of Byte; SourceArray: Array of Byte; StartIndex: integer);
var
  i: integer;
begin
  for i := 0 to SizeOf(SourceArray) - 1 do
    DestArray[StartIndex + i] := SourceArray[i];
end;

procedure AppendToArray(var Dest: Array of Byte; Source: Array of Byte);
var
  i,j: integer;
begin
  i := Length(Dest);
  SetLength(Dest, i + Length(Source));
  for j := 0 to  Length(Source) - 1 do
    begin
      Dest[i + j] := Source[j];
    end;
end;

Refactorings

No refactoring yet !

C99bb2b0dfcf7dba9aabf244a3330ac2

DarkAxi0m

February 3, 2009, February 03, 2009 23:14, permalink

No rating. Login to rate!

Using Delphi 7;

Move seems to be fast,

CopyIntoArray, after a quick test looks like it returns the same results.

AppendToArray, I couldn't get to compile either versions.
Can not seem to do setlength on 'var Dest: Array of Byte;'

procedure CopyIntoArray(var DestArray: Array of Byte; SourceArray: Array of Byte; StartIndex: integer);
begin
 Move(SourceArray,DestArray[StartIndex],SizeOf(SourceArray));
end;

procedure AppendToArray(var Dest: Array of Byte; Source: Array of Byte);
begin
  SetLength(Dest,SizeOf(dest)+SizeOf(Source));
  Move(Source,Dest[SizeOf(dest)],SizeOf(Source));
end;
D41d8cd98f00b204e9800998ecf8427e

Andreas Hausladen

February 4, 2009, February 04, 2009 11:26, permalink

4 ratings. Login to rate!

How did you get this to compile. I always get "Incompatible types" because you can't use SetLength() on an open array parameter (Dest). You must declare a type lile TDynByteArray or TBytes (as it is in Delphi 2009).

@DarkAxi0m: Your AppendToArray copies the Source-data beyond the Dest-array end.

{$IF not declared(TBytes)}
type
  TBytes = array of Byte;
{$IFEND}

procedure CopyIntoArray(var DestArray: TBytes; const SourceArray: array of Byte; StartIndex: integer);
begin
  Assert(StartIndex >= 0);
  Assert(Length(DestArray) <= StartIndex + Length(SourceArray));

  Move(SourceArray[0], DestArray[StartIndex], Length(SourceArray));
end;

procedure AppendToArray(var Dest: TBytes; Source: array of Byte);
var
  DestLen: Integer;
begin
  DestLen := Length(Dest);
  SetLength(Dest, DestLen + Length(Source));
  Move(Source, Dest[DestLen], Length(Source));
end;
Ec72b0ab3eb1855c56fd04bcc886f65c

jamiei

February 4, 2009, February 04, 2009 11:48, permalink

No rating. Login to rate!

Apologies to all - @Andreas Hausladen - you're absolutely correct. I changed the type of the Dest argument in AppendToArray after I pasted the code into this site to make it simpler to refactor at a glance and forgot about not being able to SetLegnth on the open array parameter.

Thanks for the refactorings!

Your refactoring





Format Copy from initial code

or Cancel