Solved

Is there any way to turn this function into a generic function for records?

Posted on 2014-09-13
4
187 Views
Last Modified: 2014-09-15
Hello guys

I have this function that Shaffler my array of record

procedure DoSwap(var s1, s2: TCadVb);
var
  tmp: TCadVb;
begin
  tmp := s1;
  s1  := s2;
  s2  := tmp;
end;


procedure ShuffleArrayCadVb;
var
 i, j : Integer;
Begin
  for i := Low(CadVb) to High(CadVb) do
  begin
    j := Random(High(CadVb) - Low(CadVb) + 1) + Low(CadVb);
    if i <> j then
       DoSwap(CadVb[i], CadVb[j]);
  end;
End;

Open in new window


But I'd like to turn it into in a generic function for any kind of record. Something like this:

procedure ShufflerArray(variable: record)

is it possible? if so, how to?

Thanks
Alex
0
Comment
Question by:hidrau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 40321099
If you have assignment defined for the records then the code as written should work just by adding the parameter as an array of the records.

mlmcc
0
 
LVL 1

Author Comment

by:hidrau
ID: 40321111
Could you give me an example?

thanks
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40322845
There is no need to swap large memory records - it is more efficient and faster to shuffle indexes itself:
Small example build on top of your code:
type
  TCadVb = record
    a: Integer;
  end;
  TIntArray = array of Integer;

procedure DoSwap(var s1, s2: Integer);
var
  tmp: Integer;
begin
  tmp := s1;
  s1  := s2;
  s2  := tmp;
end;

procedure ShuffleIdxArray(iArrSz: Integer; var idxArr: TIntArray);
var
 i, j : Integer;
begin
  SetLength(idxArr, iArrSz);
  //fill indexed
  for i := Low(idxArr) to High(idxArr) do
  begin
    idxArr[i] := i;
  end;
  //shuffle
  for i := Low(idxArr) to High(idxArr) do
  begin
    j := Random(High(idxArr) - Low(idxArr) + 1) + Low(idxArr);
    if i <> j then
       DoSwap(idxArr[i], idxArr[j]);
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  CadVb: array of TCadVb;
  idxArr: TIntArray;
  i: integer;
begin
  SetLength(CadVb,3);
  CadVb[0].a := 2;
  CadVb[1].a := 1;
  CadVb[2].a := 3;

  ShuffleIdxArray(Length(CadVb), idxArr); //go shuffle indexes

  Memo1.Lines.Clear;
  for i := Low(CadVb) to High(CadVb) do
  begin
    Memo1.Lines.Add(IntToStr(CadVb[idxArr[i]].a));
  end;
end;

Open in new window


... this way CadVb could be anything.
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 40323529
Thanks very much for your example
It helped me a lot
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question