I have a 2D array of Word, what is the fastest way to fill with the word max
value (65535) using fillchar?
thanks
Steve
The best answer will depend on whether it is a
dynamic array or a fixed size array? Rgds, JohnH
Note the syntax, as it is shown wrong in other answer.
This works as 65535 is hex $FFFF, the same as 2 bytes of $FF
Christen Fihl
http://HSPascal.Fihl.net/
- or -
type
PMyArray := ^TMyArray;
TMyArray := array[1..65535, 1..65535] of word;
var
MyArray := PMyArray;
...
new(MyArray);
fillchar(MyArray^, 0, SizeOf(TMyArray));
DaveH
"Steve" <nos...@nospam.com> wrote in message
news:3fd9...@newsgroups.borland.com...
Remember to use a Fastcode FillChar.
http://home3.gvdnet.dk/~marianndkc/FillcharChallenge.htm
Regards
Dennis
I use a 2D dynamic array.
Does this also apply to Dynamic array?
thanks
Steve
with a dynamic array I have done this but it crashes.
FillChar(Matrix, Width * Height * 2, -1);
I have TMatrix = array of array of word;
SetLength(Matrix,Width , Height );
If I do FillChar(Matrix, Width * Height * 2, -1); it freezes the app
Steve
In dynamic arrays, 2 dimensionals are saved with one dimension as an array
of "pointers" to the other dimension.
In plan arrays, the data is contigous, not here. You can only FillChar one
dimension at a time.
In Strings you could do: FillChar(S[1], Length(S), 'F');
But not: FillChar(S, Length(S), 'F'); !!!
This example works.
Type Elem= Word;
TMatrix = array of array of Elem;
var Matrix: TMatrix;
N: Integer;
Const Width=2; Height=3;
begin
SetLength(Matrix, Width, Height);
for N:=Low(Matrix) to High(Matrix) do
FillChar(Matrix[N,0], Height * SizeOf(Elem), -1);
end.
Remember to use: Matrix[N,0] in FillChar.
Your example would fill the 4 bytes (the Matrix pointer) and much more
around it, kill the stack contenst.
--
Christen Fihl
http://HSPascal.fihl.net
That's why sometimes it might be an idea to use a one-dimensional array, set
its length to width * height, and address its single elements with index :=
width * y + x (where x in [0..width-1] and y in [0..height-1]). It makes the
use of FillChar much easier. And although it looks like a one-dimensional
array, it comes closer to a static two-dimensional array, than a dynamic
array of array (from the "behind the scenes" point of view). But it all
depends. Using an array of array tends to be more readable and is a must if
you want to change the length of a single row.
Jens
Steve
That is hard to fill efficiently since the array memory is not in one
block. A 2D dynamic array is in fact an array of pointers, each of
which points at a 1D dynamic array. If the rightmost dimension of the
array is small you will not get much improvement over the standard loop
over all elements. If it is larger you may benefit from something like
this:
type
T2DWordarray = array of array of Word;
procedure Init2DArrayOfWord( A: T2DWordArray );
var
i: Integer;
begin
for i:= 0 to High(A) do
FillChar( A[i,0], Length( A[i] )*Sizeof(A[i,0]), $FF );
end;
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be