AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

How to reverse array of any type?

Ein Thema von WojTec · begonnen am 2. Dez 2012 · letzter Beitrag vom 3. Dez 2012
Antwort Antwort
Seite 1 von 2  1 2      
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#1

Re: How to reverse array of any type?

  Alt 2. Dez 2012, 18:44
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
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.963 Beiträge
 
Delphi 12 Athens
 
#2

AW: How to reverse array of any type?

  Alt 2. Dez 2012, 19:02
As you have Delphi 2010 in your profile, generics should be not problem, are they?
Delphi-Quellcode:
  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;
Example usage:
Delphi-Quellcode:
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;
// EDIT:
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.
Sebastian Jänicke
AppCentral

Geändert von jaenicke ( 2. Dez 2012 um 20:42 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von uligerhardt
uligerhardt

Registriert seit: 19. Aug 2004
Ort: Hof/Saale
1.749 Beiträge
 
Delphi 2007 Professional
 
#3

AW: How to reverse array of any type?

  Alt 2. Dez 2012, 19:25
Da bei dir Delphi 2010 steht
Im Post selbst steht D7.
Uli Gerhardt
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#4

Re: How to reverse array of any type?

  Alt 2. Dez 2012, 19:39
Ok, nice, nice, but how to access array elements if parameter is Pointer only? Array can be in any type.
  Mit Zitat antworten Zitat
Benutzerbild von uligerhardt
uligerhardt

Registriert seit: 19. Aug 2004
Ort: Hof/Saale
1.749 Beiträge
 
Delphi 2007 Professional
 
#5

AW: Re: How to reverse array of any type?

  Alt 2. Dez 2012, 19:58
Ok, nice, nice, but how to access array elements if parameter is Pointer only? Array can be in any type.
What is a pointer? The parameter to your routine or the array element?
Uli Gerhardt
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#6

Re: How to reverse array of any type?

  Alt 2. Dez 2012, 20:04
Pointer is procedure parameter (see @Furtbichler post).
  Mit Zitat antworten Zitat
Benutzerbild von uligerhardt
uligerhardt

Registriert seit: 19. Aug 2004
Ort: Hof/Saale
1.749 Beiträge
 
Delphi 2007 Professional
 
#7

AW: Re: How to reverse array of any type?

  Alt 2. Dez 2012, 20:17
Ok, nice, nice, but how to access array elements if parameter is Pointer only? Array can be in any type.
With Furtbichler's routine signature you could have something like
Delphi-Quellcode:
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;
(NB: These are only some building blocks of the routine, not the whole thing. )
Uli Gerhardt
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu
Online

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.380 Beiträge
 
Delphi 12 Athens
 
#8

AW: Re: How to reverse array of any type?

  Alt 3. Dez 2012, 03:47
I submitted an expansion proposal at Embarcadero.
http://qc.embarcadero.com/wc/qcmain.aspx?d=110391
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:
procedure SwapArrayElements(aStartElement: Pointer; aElementSize, aElementCount: Integer);
Or you get the necessary information about the RTTI/TypeInfo.
Delphi-Quellcode:
procedure SwapArrayElements(var TheArray; ArrTypeInfo: Pointer);

var MyArr: array of ...;
SwapArrayElements(MyArr, TypeInfo(MyArr));
Ein Therapeut entspricht 1024 Gigapeut.
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#9

Re: How to reverse array of any type?

  Alt 3. Dez 2012, 10:43
Delphi-Quellcode:
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;
I don't know how replace part should works. Above is form reversing knowed type array.

Geändert von WojTec ( 3. Dez 2012 um 11:20 Uhr)
  Mit Zitat antworten Zitat
Medium

Registriert seit: 23. Jan 2008
3.688 Beiträge
 
Delphi 2007 Enterprise
 
#10

AW: How to reverse array of any type?

  Alt 3. Dez 2012, 11:04
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
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:34 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