Forum > Beginners

Generic Array Sorting

(1/4) > >>

Nitorami:
I converted a C routine to Pascal via CodeConvert. It uses generics which I don't necessarily need but thought this might be a chance to learn about them. I got to the point below but now get an error in the last line where the array should be sorted - "Syntax Error, Create expected but Sort found".
I am a bit lost here. I assume CodeConvert put the TArray in as placeholder for some sort of generic array. The docs say TArray (under sysutils) is "not needed in Free Pascal, where 2 array types are equal if their element types are equal." WTF ? And why in sysutils? Is there another TArray in Generics.Collection I should use ?

Maybe remove sysutils from the uses clause - aha, now the error message changes to "Generics without specialization cannot be used as a type for variables". That seems to make more sense but I can't get the specialization right. I beg for a hint.



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function count_repeats(var rng: TRandom; expanse: SizeInt): SizeInt;type  rngval_t = Integer; // Assuming RNG_TYPE::result_type is an Integervar  values:  specialize TArray<rngval_t>;  prev, curr: rngval_t;  i, duplicates: SizeInt;begin  // Allocate an array to hold all the output.  SetLength(values, expanse);  Values := specialize TArray <integer>.Create;  // Store all the output from the generator.  for i := 0 to expanse - 1 do    values[i] := rng.Next; // Assuming rng() is equivalent to rng.Next  // Sort the array  TArray.Sort<rngval_t>(values); 

TRon:
Specialize as a new type, see also wiki.

In casw you have competing typenames for generics you can prefix them with the name of the unit ,e.g.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyValues:  specialize sysutils.TArray<integer>; 

Nitorami:
Nope, that does not work. Not in FPC 3.2.2. "<" expected but "." found.

And this is not the problem. I need an Array I can specialize to various integer types, and sort it. That shouldn't be difficult. The wiki says there is a Generics Collections (fully compatible with Delphi generics library) which offers a TArray with methods for searching and sorting. No further help is available:

https://wiki.lazarus.freepascal.org/Data_Structures,_Containers,_Collections

It seems my TArray is just an ordinary dynamic array, as no sort method is known. So maybe I need something different than TArray, but I can't find it. Where are the sortable array in generics collections ?


TRon:

--- Quote from: Nitorami on February 09, 2024, 06:24:52 pm ---Nope, that does not work. Not in FPC 3.2.2. "<" expected but "." found.

--- End quote ---
Drats you are right. Sorry about that. It should be though.


--- Quote ---And this is not the problem. I need an Array I can specialize to various integer types, and sort it. That shouldn't be difficult. The wiki says there is a Generics Collections (fully compatible with Delphi generics library) which offers a TArray with methods for searching and sorting. No further help is available:

--- End quote ---
There is but because anonymous procedures and functions are not yet implemented (they are in trunk but just recently) you need to use tarrayhelper for sorting (unit generics.collections).

In the rtl-generics package examples directory are some examples and/or have a look at this post.

Zvoni:
Err.... is it just me, or is he trying to call a Class Procedure in his Line 16?!?!?
Would have expected "Values.Sort;" or some such thing

EDIT: Just looked at the source: It actually IS a class Function....doh me  :-[

Navigation

[0] Message Index

[#] Next page

Go to full version