[fpc-devel] RussianlocaleinformationnotcompatiblewithFPClocalevariables

Boian Mitov mitov at mitov.com
Thu Jul 31 18:50:27 CEST 2008


And the same code will work both with normal array and with object 
implementing iterators ?
I am not sure it will.
How I can call your sort for a linked list container instead on array?
Am I missing something or your code works with arrays only.
The STL sort will work with arrays or with STL containers or with any class 
that implements the concept (I.E. defines iretator and reference types, and 
has iterator generating function etc.)
As the best of my knowledge this is not doable with generics.
C++ is doing it, but there is no validation mechanism aside from just plain 
old testing all possible combinations ;-) .

  With best regards,
    Boian Mitov

--------------------------------------------------------------------
Mitov Software
http://www.mitov.com
--------------------------------------------------------------------


----- Original Message ----- 
From: "Ales Katona" <almindor at gmail.com>
To: "FPC developers' list" <fpc-devel at lists.freepascal.org>
Sent: Thursday, July 31, 2008 9:47 AM
Subject: Re: [fpc-devel] 
RussianlocaleinformationnotcompatiblewithFPClocalevariables


> Boian Mitov  wrote / napĂ­sal(a):
>> Sort for example:
>>
>> It can work with C type array:
>>
>> int a[7] = {23, 1, 33, -20, 6, 6, 9};
>> sort(a, a+7);
>>
>> or it can work with a container such as linked list:
>>
>> list<Something> v1;
>>
>> sort(v1.begin(), v1.end());
>>
>>
>>  With best regards,
>>    Boian Mitov
>>
>> --------------------------------------------------------------------
>> Mitov Software
>> http://www.mitov.com
>> --------------------------------------------------------------------
> This you mean?
>
> template <class RandomAccessIterator>
>  void sort ( RandomAccessIterator first, RandomAccessIterator last );
>
> You can do this with generics. Just use sizeof() to get to next element 
> and overloaded comparator functions for base types. I don't really see 
> much new here, it's a bit non-typical generics usage.
>
> Here's a pascal example (OOP, true but that's a current limitation, afaik 
> "generic function" should be possible):
>
> program Project1;
>
> {$mode objfpc}{$H+}
>
> type
>
>  { TSorter }
>
>  generic TSorter<T> = class
>   public
>    procedure Sort(var First, Last: T);
>  end;
>
> { TSorter }
>
> procedure TSorter.Sort(var First, Last: T);
> var
>  x, y: ^T;
>  z: T;
> begin
>  x := @First;
>  y := @First;
>
>  while x^ <> Last do begin
>    while y^ <> Last do begin
>      Inc(y);
>      if x^ > y^ then begin
>        z := x^;
>        x^ := y^;
>        y^ := z;
>      end;
>    end;
>    Inc(x);
>    y := x;
>  end;
> end;
>
> type
>  TIntSorter = specialize TSorter<Integer>;
>
> var
>  s: TIntSorter;
>  a: array[1..5] of Integer = (5, 4, 3, 2, 1);
>  i: Integer;
> begin
>  s := TIntSorter.Create;
>
>  s.Sort(a[1], a[5]);
>
>  for i := 1 to 5 do
>    Writeln(a[i]);
>
>  s.Free;
> end.
>
>
> Ales
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel 




More information about the fpc-devel mailing list