Solved

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

Posted on 2014-09-13
4
183 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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Activex loadlibrary and show parented form issue 6 286
Internet Explorer View Settings Question 15 107
Delphi : could not find program, '...exe' 2 149
Dev Express grid collapse 2 39
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

861 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

23 Experts available now in Live!

Get 1:1 Help Now