![]() |
AW: Re: How to reverse array of any type?
I submitted an expansion proposal at Embarcadero.
![]() You may like to vote for it, so it will soon be integrated. For all Delphi versions from 2009, you can upgrade this as a class helper. Otherwise, these methods can of course also copy out and use directly. Zitat:
Delphi-Quellcode:
procedure SwapArrayElements(var TheArray; ArrTypeInfo: Pointer);
var MyArr: array of ...; SwapArrayElements(MyArr, TypeInfo(MyArr)); |
Re: How to reverse array of any type?
Delphi-Quellcode:
I don't know how replace part should works. Above is form reversing knowed type array. :(
procedure ReverseArray(AData: Pointer; const ASize, ACount: Integer);
var Left, Right, Temp: Integer; P: PCardinal; begin P := AData; Left := 0; Right := ACount; Result := Left < Right; if Result then begin while (Left < Right) do begin {Temp := AValues[Left]; AValues[Left] := AValues[Right]; AValues[Right] := Temp;} // Don't know how to exchange with pointers :( //Move(P^, ..., ASize); // Don't know how if I don't know types ???????? Inc(P); Inc(Left); Dec(Right); end; end; end; |
AW: How to reverse array of any type?
You need to understand, that you can not operate in a array-like fashion anymore, if you want a fully generic function. You have to go down an abstraction layer, and work on the actual memory.
An array simply is a sequence of values written behind each other in memory. Every element has a length (in bytes), but the length will be determined by the data type in the array. You employ pointer operations instead of the compiler magic (pascal arrays) to gain the possibility to work with data of arbitrary size. Without Generics, there just is no other way. So instead of array indexes, you point to elements using a memory address and the size of the elements. Traversing one element ahead "increase index by 1" then becomes "take pointer of the current element, and add the element-size to it". Copying "myArray[i] := myArray[m]" becomes a raw memory move-operation "Move(DestinationPointer, SourcePointer, ElementSizeInBytes)". As long as you have anything with square brackets "[]" in your reverse-function, you can be sure to think too high-level ;) |
Re: How to reverse array of any type?
Yes yes, I know ;) So I'm asking how to do it? Because I don't know how do it on pointers only? :(
BTW: @himitsu I voted for your extended TArray :) |
AW: Re: How to reverse array of any type?
Zitat:
Delphi-Quellcode:
and
PCardinal
Delphi-Quellcode:
because this would limit you to elements with the same size as Cardinal: if
Inc(P)
Delphi-Quellcode:
is of type
P
Delphi-Quellcode:
, then it points to an element of
PCardinal
Delphi-Quellcode:
bytes. If you then do
SizeOf(Cardinal)
Delphi-Quellcode:
you advance
Inc(P)
Delphi-Quellcode:
by
P
Delphi-Quellcode:
bytes. But you want
SizeOf(Cardinal)
Delphi-Quellcode:
to point to elements of size
P
Delphi-Quellcode:
bytes, so use
ASize
Delphi-Quellcode:
and
PByte
Delphi-Quellcode:
(as I already wrote :stupid:).
Inc(P, ASize)
|
AW: Re: How to reverse array of any type?
:?:
Delphi-Quellcode:
procedure ReverseArray(AData: Pointer; const ASize, ACount: Integer);
var Temp: array of Byte; Data: PAnsiChar absolute AData; // PAnsiChar verfügt über Zeiger-Arithmetik i, j: Integer; begin SetLength(Temp, ASize); i := ACount div 2 - 1; while i >= 0 do begin j := ACount - i - 1; Move(Data[i * ASize], Temp[0], ASize); Move(Data[j * ASize], Data[i * ASize], ASize); Move(Temp[0], Data[j * ASize], ASize); Dec(i); end; end; |
Re: How to reverse array of any type?
Thanks :) Now I'll try to transform Shuffle() in this way :D
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:49 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz