Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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