![]() |
Delphi-Version: 7
How to reverse array of any type?
Hi, is possible to reverse (I actually need this, but I mean processing in any way) array of any type (numbers, string, booleans, etc.) by one procedure?
|
AW: How to reverse array of any type?
By 'reverse' you mean: ABCD => DCBA?
Well, that's simple: All you need is the Address of the first element, the size of an element and the total number of elements (n), then you exchange elements 0 with n-1, 1 with n-2 and so on. To exchange an element you will need a temporary storage of ElementSize Bytes. The signature of the function could be:
Delphi-Quellcode:
Procedure SwapArrayElements(aStartElement : Pointer; aElementSize, aElementCount : Integer);
|
AW: How to reverse array of any type?
That is what
![]() |
Re: How to reverse array of any type?
Exactly, you are correct!
I thought about pointers, but I don't know how to access fields? :( AData: Pointer Inc(AData) - to get next one, usage not possible :( |
AW: How to reverse array of any type?
As you have Delphi 2010 in your profile, generics should be not problem, are they?
Delphi-Quellcode:
Example usage:
TArrayUtil = class
public class procedure Swap<T>(var Value1, Value2: T); class procedure Reverse<T>(var Value: array of T); end; { TArrayUtil } class procedure TArrayUtil.Reverse<T>(var Value: array of T); var i: Integer; begin if Length(Value) > 0 then for i := Low(Value) to High(Value) div 2 do Swap<T>(Value[i], Value[High(Value) - i]); end; class procedure TArrayUtil.Swap<T>(var Value1, Value2: T); var Temp: T; begin Temp := Value1; Value1 := Value2; Value2 := Temp; end;
Delphi-Quellcode:
// EDIT:
var
Test: array of Integer; i: Integer; begin SetLength(Test, 0); for i := Low(Test) to High(Test) do Test[i] := i; TArrayUtil.Reverse<Integer>(Test); for i := Low(Test) to High(Test) do ShowMessage(IntToStr(Test[i])); end; Of course it would be faster to swap directly in procedure Reverse. // EDIT2: Ok, translated to English and I did not see Delphi version 7 in the post. Then this won't work. |
AW: How to reverse array of any type?
Zitat:
|
Re: How to reverse array of any type?
Ok, nice, nice, but how to access array elements if parameter is Pointer only? Array can be in any type.
|
AW: Re: How to reverse array of any type?
Zitat:
|
Re: How to reverse array of any type?
Pointer is procedure parameter (see @Furtbichler post).
|
AW: Re: How to reverse array of any type?
Zitat:
Delphi-Quellcode:
(NB: These are only some building blocks of the routine, not the whole thing. :mrgreen:)
procedure SwapArrayElements(aStartElement : Pointer; aElementSize, aElementCount : Integer);
var p: PByte; begin p := AStartElement; // p points to first element Inc(p, aElementSize); // Now p points to second element Move(p^, ..., aElementSize); // Moves the element p points to end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:24 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