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@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to