Solved

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

Posted on 2014-09-13
4
182 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
  • 2
4 Comments
 
LVL 100

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 25

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now