Solved

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

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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