fork download
  1. {$MODE OBJFPC}
  2. type
  3. proc1 = procedure(var x: array of integer);
  4. proc2 = procedure(var x, y: integer);
  5. generic swtable<T> = array[boolean] of T;
  6. generic P<T> = class(TObject) public class function switch(x: array of T): specialize swtable<T>; static; end;
  7. P1 = specialize P<proc1>;
  8. P2 = specialize P<proc2>;
  9.  
  10. class function P.switch(x: array of T): specialize swtable<T>; begin switch := x end;
  11.  
  12. procedure nop2(var x, y: integer); begin end;
  13. procedure swap(var x, y: integer); begin x := x xor y; y:= y xor x; x := x xor y end;
  14.  
  15. procedure nop1(var arr: array of integer); begin end;
  16.  
  17. procedure inner_sort(var arr: array of integer);
  18. begin
  19. P2.switch([@nop2, @swap])[arr[0] > arr[1]](arr[0], arr[1]);
  20. P1.switch([@nop1, @inner_sort])[length(arr) > 2](arr[1 .. High(arr)])
  21. end;
  22.  
  23. procedure sort(var arr: array of integer);
  24. begin
  25. P1.switch([@inner_sort, @nop1])[length(arr) < 1](arr);
  26. P1.switch([@nop1, @sort])[length(arr) > 2](arr[0 .. High(arr) - 1])
  27. end;
  28.  
  29. procedure print_array(arr: array of integer);
  30. var
  31. i: integer;
  32. begin
  33. for i := Low(arr) to High(arr) do Write(arr[i], ' ');
  34. Writeln
  35. end;
  36.  
  37. var
  38. x : array[-2 .. 2] of integer = (100500, 265, 1488, 666, 13);
  39. begin
  40. sort(x);
  41. print_array(x)
  42. end.
Success #stdin #stdout 0s 336KB
stdin
Standard input is empty
stdout
13 265 666 1488 100500