[Delphi (Object Pascal)] Delphi自定义动态泛型数组类代码及使用方法 →→→→→进入此内容的聊天室

来自 , 2019-05-29, 写在 Delphi (Object Pascal), 查看 111 次.
URL http://www.code666.cn/view/e1374829
  1.  
  2. unit ArrayEx;
  3.      
  4. { ******************************************************************************
  5.   泛型动态数组的扩展.
  6.      
  7.   wr960204 武稀松
  8.      
  9.   2013.6.4
  10.      
  11. http://www.raysoftware.cn/?p=278
  12.      
  13.   7.10
  14.   感谢TINTIN的完善.
  15.   加入了:排序和搜索两个重要功能
  16.      
  17. http://hi.baidu.com/tintinsoft/item/ffb1a0981dae2edd1b49df92
  18.      
  19.   sample:
  20.      
  21.   var
  22.   a, b, c: TArrayEx<Integer>;
  23.   f: TArrayEx<Single>;
  24.   s : TArrayEx<string>;
  25.   //
  26.   i: Integer;
  27.   z: array of Integer;
  28.   args:TArrayEx<string>;
  29.   begin
  30.   // assign
  31.      
  32.   a := [2, 2, 3, 4];
  33.   s := ['haha', 'hello'];
  34.      
  35.   // clone
  36.   b := a.Clone;
  37.      
  38.   // 给元素赋值
  39.   b[0] := 5;
  40.      
  41.   // operator +
  42.   c := a + b;
  43.      
  44.   c := 88 + c + 99;
  45.   a.Append([10, 20, 30]);
  46.   a.Insert(2, 1000);
  47.   a.Insert(2, [1000, 45]);
  48.      
  49.   a[0] := 7;
  50.      
  51.   // Unique
  52.   c.Unique;
  53.   // Delete
  54.   c.Delete(0, 3);
  55.      
  56.   // compare
  57.   if c = z then
  58.   // for in loop
  59.   for i in c do
  60.   if i = 0 then
  61.   begin
  62.   //
  63.   end;
  64.      
  65.   //
  66.      
  67.   f := [1, 2, 3.1415926];
  68.   f.Size := 200;
  69.   if f[0] = 1.0 then
  70.   begin
  71.   end;
  72.      
  73.      
  74.      
  75.   args := ['38inch','45inch','XL','XL2','X','38inch','45inch'];
  76.   args.Unique;
  77.   //sort
  78.   args.Sort;
  79.   //search
  80.   if args.BinarySearch('XL',i) then
  81.   ShowMessageFmt('foud index:%d',[i]);
  82.      
  83.      
  84.      
  85.   end;
  86.      
  87.   ****************************************************************************** }
  88. interface
  89.      
  90. uses System.Generics.Defaults, System.SysUtils;
  91.      
  92. type
  93.   TArrayEx<T> = record
  94.   strict private
  95.   type
  96.     TEnumerator = class
  97.     private
  98.       FValue: TArray<T>;
  99.       FIndex: NativeInt;
  100.       function GetCurrent: T;
  101.     public
  102.       constructor Create(const AValue: TArray<T>);
  103.       function MoveNext: Boolean;
  104.       property Current: T read GetCurrent;
  105.     end;
  106.   public
  107.     function GetEnumerator(): TEnumerator;
  108.   strict private
  109.     FData: TArray<T>;
  110.     function GetRawData: TArray<T>;
  111.     function GetElements(Index: Integer): T;
  112.     procedure SetElements(Index: Integer; const Value: T);
  113.   private
  114.     class function EqualArray(A, B: TArray<T>): Boolean; static;
  115.     class function CompareT(const A, B: T): Boolean; static;
  116.     class procedure CopyArray(var FromArray, ToArray: TArray<T>;
  117.       FromIndex: NativeInt = 0; ToIndex: NativeInt = 0;
  118.       Count: NativeInt = -1); static;
  119.     class procedure MoveArray(var AArray: array of T;
  120.       FromIndex, ToIndex, Count: Integer); static;
  121.     class function DynArrayToTArray(const Value: array of T): TArray<T>; static;
  122.     class function Min(A, B: NativeInt): NativeInt; static;
  123.     procedure QuickSort(const Comparer: IComparer<T>; L, R: Integer);
  124.   public // operators
  125.     class operator Implicit(Value: TArray<T>): TArrayEx<T>; overload;
  126.     class operator Implicit(Value: array of T): TArrayEx<T>; overload;
  127.     (*
  128.       这个无解,Delphi不允许array of T作为返回值.也就是这个转换是被废了.只好用AssignTo
  129.       class operator Implicit(Value:  TArrayEx<T>):array of T; overload;
  130.     *)
  131.     class operator Implicit(Value: TArrayEx<T>): TArray<T>; overload;
  132.     class operator Explicit(Value: TArrayEx<T>): TArray<T>; overload;
  133.     class operator Explicit(Value: array of T): TArrayEx<T>; overload;
  134.      
  135.     class operator Add(A, B: TArrayEx<T>): TArrayEx<T>; overload;
  136.     class operator Add(A: TArrayEx<T>; const B: T): TArrayEx<T>; overload;
  137.     class operator Add(const A: T; B: TArrayEx<T>): TArrayEx<T>; overload;
  138.     class operator Add(A: TArrayEx<T>; B: array of T): TArrayEx<T>; overload;
  139.     class operator Add(A: array of T; B: TArrayEx<T>): TArrayEx<T>; overload;
  140.     class operator In (A: T; B: TArrayEx<T>): Boolean; overload;
  141.     //
  142.     class operator Equal(A, B: TArrayEx<T>): Boolean; overload;
  143.     class operator Equal(A: TArrayEx<T>; B: TArray<T>): Boolean; overload;
  144.     class operator Equal(A: TArray<T>; B: TArrayEx<T>): Boolean; overload;
  145.     class operator Equal(A: array of T; B: TArrayEx<T>): Boolean; overload;
  146.     class operator Equal(A: TArrayEx<T>; B: array of T): Boolean; overload;
  147.      
  148.   public
  149.     procedure SetLen(Value: NativeInt); inline;
  150.     function GetLen: NativeInt; inline;
  151.     function ByteLen: NativeInt; inline;
  152.     class function Create(Value: array of T): TArrayEx<T>; overload; static;
  153.     class function Create(Value: TArrayEx<T>): TArrayEx<T>; overload; static;
  154.     class function Create(const Value: T): TArrayEx<T>; overload; static;
  155.     function Clone(): TArrayEx<T>;
  156.     procedure SetValue(Value: array of T);
  157.     function ToArray(): TArray<T>;
  158.     function SubArray(AFrom, ACount: NativeInt): TArrayEx<T>;
  159.     procedure Delete(AFrom, ACount: NativeInt); overload;
  160.     procedure Delete(AIndex: NativeInt); overload;
  161.     procedure Append(Values: TArrayEx<T>); overload;
  162.     procedure Append(const Value: T); overload;
  163.     procedure Append(Values: array of T); overload;
  164.     procedure Append(Values: TArray<T>); overload;
  165.     function Insert(AIndex: NativeInt; const Value: T): NativeInt; overload;
  166.     function Insert(AIndex: NativeInt; const Values: array of T)
  167.       : NativeInt; overload;
  168.     function Insert(AIndex: NativeInt; const Values: TArray<T>)
  169.       : NativeInt; overload;
  170.     function Insert(AIndex: NativeInt; const Values: TArrayEx<T>)
  171.       : NativeInt; overload;
  172.     procedure Unique();
  173.     // 排序
  174.     procedure Sort(); overload;
  175.     procedure Sort(const Comparer: IComparer<T>); overload;
  176.     procedure Sort(const Comparer: IComparer<T>;
  177.       Index, Count: Integer); overload;
  178.     // 搜索
  179.     function BinarySearch(const Item: T; out FoundIndex: Integer;
  180.       const Comparer: IComparer<T>; Index, Count: Integer): Boolean; overload;
  181.     function BinarySearch(const Item: T; out FoundIndex: Integer;
  182.       const Comparer: IComparer<T>): Boolean; overload;
  183.     function BinarySearch(const Item: T; out FoundIndex: Integer)
  184.       : Boolean; overload;
  185.      
  186.     property Size: NativeInt read GetLen write SetLen;
  187.     property Len: NativeInt read GetLen write SetLen;
  188.     property RawData: TArray<T> read GetRawData;
  189.     property Elements[Index: Integer]: T read GetElements
  190.       write SetElements; default;
  191.   end;
  192.      
  193. implementation
  194.      
  195. uses System.RTLConsts;
  196.      
  197. class operator TArrayEx<T>.Add(A, B: TArrayEx<T>): TArrayEx<T>;
  198. begin
  199.   Result := A.Clone;
  200.   Result.Append(B);
  201. end;
  202.      
  203. class operator TArrayEx<T>.Add(A: TArrayEx<T>; const B: T): TArrayEx<T>;
  204. begin
  205.   Result := A.Clone;
  206.   Result.Append(B);
  207. end;
  208.      
  209. class operator TArrayEx<T>.Add(const A: T; B: TArrayEx<T>): TArrayEx<T>;
  210. begin
  211.   Result.SetValue([A]);
  212.   Result.Append(B);
  213. end;
  214.      
  215. class operator TArrayEx<T>.Add(A: TArrayEx<T>; B: array of T): TArrayEx<T>;
  216. begin
  217.   Result := A.Clone;
  218.   Result.Append(B);
  219. end;
  220.      
  221. class operator TArrayEx<T>.Add(A: array of T; B: TArrayEx<T>): TArrayEx<T>;
  222. begin
  223.   Result.FData := DynArrayToTArray(A);
  224.   Result.Append(B);
  225. end;
  226.      
  227. class operator TArrayEx<T>.In(A: T; B: TArrayEx<T>): Boolean;
  228. var
  229.   Tmp: T;
  230. begin
  231.   Result := False;
  232.   for Tmp in B.FData do
  233.     if CompareT(A, Tmp) then
  234.     begin
  235.       Result := True;
  236.       Break;
  237.     end;
  238. end;
  239.      
  240. class operator TArrayEx<T>.Equal(A, B: TArrayEx<T>): Boolean;
  241. begin
  242.   Result := EqualArray(A.FData, B.FData);
  243. end;
  244.      
  245. class operator TArrayEx<T>.Equal(A: TArrayEx<T>; B: TArray<T>): Boolean;
  246. begin
  247.   Result := EqualArray(A.FData, B);
  248. end;
  249.      
  250. class operator TArrayEx<T>.Equal(A: TArray<T>; B: TArrayEx<T>): Boolean;
  251. begin
  252.   Result := EqualArray(A, B.FData);
  253. end;
  254.      
  255. class operator TArrayEx<T>.Equal(A: array of T; B: TArrayEx<T>): Boolean;
  256. begin
  257.   Result := EqualArray(DynArrayToTArray(A), B.FData);
  258. end;
  259.      
  260. class operator TArrayEx<T>.Equal(A: TArrayEx<T>; B: array of T): Boolean;
  261. begin
  262.   Result := EqualArray(A.FData, DynArrayToTArray(B));
  263. end;
  264.      
  265. function TArrayEx<T>.BinarySearch(const Item: T; out FoundIndex: Integer;
  266.   const Comparer: IComparer<T>; Index, Count: Integer): Boolean;
  267. var
  268.   L, H: Integer;
  269.   mid, cmp: Integer;
  270. begin
  271.   if (Index < Low(FData)) or ((Index > High(FData)) and (Count > 0)) or
  272.     (Index + Count - 1 > High(FData)) or (Count < 0) or (Index + Count < 0) then
  273.     raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
  274.   if Count = 0 then
  275.   begin
  276.     FoundIndex := Index;
  277.     Exit(False);
  278.   end;
  279.      
  280.   Result := False;
  281.   L := Index;
  282.   H := Index + Count - 1;
  283.   while L <= H do
  284.   begin
  285.     mid := L + (H - L) shr 1;
  286.     cmp := Comparer.Compare(FData[mid], Item);
  287.     if cmp < 0 then
  288.       L := mid + 1
  289.     else
  290.     begin
  291.       H := mid - 1;
  292.       if cmp = 0 then
  293.         Result := True;
  294.     end;
  295.   end;
  296.   FoundIndex := L;
  297. end;
  298.      
  299. function TArrayEx<T>.BinarySearch(const Item: T; out FoundIndex: Integer;
  300.   const Comparer: IComparer<T>): Boolean;
  301. begin
  302.   Result := BinarySearch(Item, FoundIndex, Comparer, Low(FData), Length(FData));
  303. end;
  304.      
  305. function TArrayEx<T>.BinarySearch(const Item: T;
  306.   out FoundIndex: Integer): Boolean;
  307. begin
  308.   Result := BinarySearch(Item, FoundIndex, TComparer<T>.Default, Low(FData),
  309.     Length(FData));
  310. end;
  311.      
  312. function TArrayEx<T>.ByteLen: NativeInt;
  313. begin
  314.   Result := Length(FData) * Sizeof(T);
  315. end;
  316.      
  317. class function TArrayEx<T>.Min(A, B: NativeInt): NativeInt;
  318. begin
  319.   Result := A;
  320.   if Result > B then
  321.     Result := B;
  322. end;
  323.      
  324. class procedure TArrayEx<T>.CopyArray(var FromArray, ToArray: TArray<T>;
  325.   FromIndex, ToIndex, Count: NativeInt);
  326. var
  327.   i: Integer;
  328. begin
  329.   if Count = 0 then
  330.     Exit;
  331.   if Count < 0 then
  332.     Count := Min(Length(FromArray), Length(ToArray));
  333.   if Length(FromArray) < (FromIndex + Count) then
  334.     Count := Length(FromArray) - FromIndex;
  335.   if Length(ToArray) < (ToIndex + Count) then
  336.     Count := Length(ToArray) - ToIndex;
  337.      
  338.   if Count > 0 then
  339.     for i := 0 to Count - 1 do
  340.       ToArray[ToIndex + i] := FromArray[FromIndex + i];
  341. end;
  342.      
  343. class procedure TArrayEx<T>.MoveArray(var AArray: array of T;
  344.   FromIndex, ToIndex, Count: Integer);
  345. var
  346.   i: Integer;
  347. begin
  348.   if Count > 0 then
  349.   begin
  350.     if FromIndex < ToIndex then
  351.       for i := Count - 1 downto 0 do
  352.         AArray[ToIndex + i] := AArray[FromIndex + i]
  353.     else if FromIndex > ToIndex then
  354.       for i := 0 to Count - 1 do
  355.         AArray[ToIndex + i] := AArray[FromIndex + i];
  356.   end;
  357. end;
  358.      
  359. procedure TArrayEx<T>.QuickSort(const Comparer: IComparer<T>; L, R: Integer);
  360. var
  361.   i, J: Integer;
  362.   pivot, temp: T;
  363. begin
  364.   if (Length(FData) = 0) or ((R - L) <= 0) then
  365.     Exit;
  366.   repeat
  367.     i := L;
  368.     J := R;
  369.     pivot := FData[L + (R - L) shr 1];
  370.     repeat
  371.       while Comparer.Compare(FData[i], pivot) < 0 do
  372.         Inc(i);
  373.       while Comparer.Compare(FData[J], pivot) > 0 do
  374.         Dec(J);
  375.       if i <= J then
  376.       begin
  377.         if i <> J then
  378.         begin
  379.           temp := FData[i];
  380.           FData[i] := FData[J];
  381.           FData[J] := temp;
  382.         end;
  383.         Inc(i);
  384.         Dec(J);
  385.       end;
  386.     until i > J;
  387.     if L < J then
  388.       QuickSort(Comparer, L, J);
  389.     L := i;
  390.   until i >= R;
  391. end;
  392.      
  393. class function TArrayEx<T>.DynArrayToTArray(const Value: array of T): TArray<T>;
  394. var
  395.   i: Integer;
  396. begin
  397.   SetLength(Result, Length(Value));
  398.   for i := Low(Value) to High(Value) do
  399.     Result[i] := Value[i];
  400. end;
  401.      
  402. class function TArrayEx<T>.EqualArray(A, B: TArray<T>): Boolean;
  403. var
  404.   i: Integer;
  405. begin
  406.   Result := True;
  407.   if A = B then
  408.     Exit;
  409.   if Length(A) <> Length(B) then
  410.   begin
  411.     Result := False;
  412.   end
  413.   else
  414.   begin
  415.     for i := Low(A) to High(A) do
  416.       if not CompareT(A[i], B[i]) then
  417.       begin
  418.         Result := False;
  419.         Break;
  420.       end;
  421.   end;
  422. end;
  423.      
  424. class function TArrayEx<T>.CompareT(const A, B: T): Boolean;
  425. var
  426.   Compare: IComparer<T>;
  427. begin
  428.   Compare := TComparer<T>.Default;
  429.   Result := Compare.Compare(A, B) = 0;
  430. end;
  431. // class function TArrayEx<T>.CompareT(const A, B: T): Boolean;
  432. // var
  433. // p1, p2: PByte;
  434. // i: Integer;
  435. // begin
  436. // Result := True;
  437. // p1 := PByte(@A);
  438. // p2 := PByte(@B);
  439. // for i := 0 to Sizeof(T) - 1 do
  440. // begin
  441. // //
  442. // if p1^ <> p2^ then
  443. // begin
  444. // Result := False;
  445. // Exit;
  446. // end;
  447. // Inc(p1);
  448. // Inc(p2);
  449. // end;
  450. // end;
  451.      
  452. function TArrayEx<T>.GetElements(Index: Integer): T;
  453. begin
  454.   Result := FData[Index];
  455. end;
  456.      
  457. function TArrayEx<T>.GetEnumerator: TEnumerator;
  458. begin
  459.   Result := TEnumerator.Create(FData);
  460. end;
  461.      
  462. function TArrayEx<T>.GetLen: NativeInt;
  463. begin
  464.   Result := Length(FData);
  465. end;
  466.      
  467. function TArrayEx<T>.GetRawData: TArray<T>;
  468. begin
  469.   Result := FData;
  470. end;
  471.      
  472. class operator TArrayEx<T>.Implicit(Value: TArrayEx<T>): TArray<T>;
  473. begin
  474.   SetLength(Result, Length(Value.FData));
  475.   CopyArray(Value.FData, Result, 0, 0, Length(Value.FData));
  476. end;
  477.      
  478. class operator TArrayEx<T>.Explicit(Value: array of T): TArrayEx<T>;
  479. begin
  480.   Result.SetValue(Value);
  481. end;
  482.      
  483. class operator TArrayEx<T>.Implicit(Value: array of T): TArrayEx<T>;
  484. begin
  485.   Result.SetValue(Value);
  486. end;
  487.      
  488. class operator TArrayEx<T>.Implicit(Value: TArray<T>): TArrayEx<T>;
  489. begin
  490.   SetLength(Result.FData, Length(Value));
  491.   CopyArray(Value, Result.FData, 0, 0, Length(Value));
  492. end;
  493.      
  494. class operator TArrayEx<T>.Explicit(Value: TArrayEx<T>): TArray<T>;
  495. begin
  496.   SetLength(Result, Length(Value.FData));
  497.   CopyArray(Value.FData, Result, 0, 0, Length(Value.FData));
  498. end;
  499.      
  500. procedure TArrayEx<T>.SetElements(Index: Integer; const Value: T);
  501. begin
  502.   FData[Index] := Value;
  503. end;
  504.      
  505. procedure TArrayEx<T>.SetLen(Value: NativeInt);
  506. begin
  507.   SetLength(FData, Value);
  508. end;
  509.      
  510. procedure TArrayEx<T>.SetValue(Value: array of T);
  511. begin
  512.   FData := DynArrayToTArray(Value);
  513. end;
  514.      
  515. procedure TArrayEx<T>.Sort;
  516. begin
  517.   QuickSort(TComparer<T>.Default, Low(FData), High(FData));
  518. end;
  519.      
  520. procedure TArrayEx<T>.Sort(const Comparer: IComparer<T>; Index, Count: Integer);
  521. begin
  522.   if (Index < Low(FData)) or ((Index > High(FData)) and (Count > 0)) or
  523.     (Index + Count - 1 > High(FData)) or (Count < 0) or (Index + Count < 0) then
  524.     raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
  525.   if Count <= 1 then
  526.     Exit;
  527.   QuickSort(Comparer, Index, Index + Count - 1);
  528. end;
  529.      
  530. procedure TArrayEx<T>.Sort(const Comparer: IComparer<T>);
  531. begin
  532.   QuickSort(Comparer, Low(FData), High(FData));
  533. end;
  534.      
  535. function TArrayEx<T>.ToArray(): TArray<T>;
  536. begin
  537.   SetLength(Result, Length(FData));
  538.   CopyArray(FData, Result, 0, 0, Length(FData));
  539. end;
  540.      
  541. class function TArrayEx<T>.Create(Value: array of T): TArrayEx<T>;
  542. begin
  543.   Result.SetValue(Value);
  544. end;
  545.      
  546. class function TArrayEx<T>.Create(Value: TArrayEx<T>): TArrayEx<T>;
  547. begin
  548.   Result := Value.Clone;
  549. end;
  550.      
  551. class function TArrayEx<T>.Create(const Value: T): TArrayEx<T>;
  552. begin
  553.   Result.SetValue([Value]);
  554. end;
  555.      
  556. function TArrayEx<T>.Clone(): TArrayEx<T>;
  557. begin
  558.   Result := SubArray(0, Length(FData));
  559. end;
  560.      
  561. function TArrayEx<T>.SubArray(AFrom, ACount: NativeInt): TArrayEx<T>;
  562. begin
  563.   SetLength(Result.FData, ACount);
  564.   CopyArray(FData, Result.FData, AFrom, 0, ACount);
  565. end;
  566.      
  567. procedure TArrayEx<T>.Delete(AFrom, ACount: NativeInt);
  568. begin
  569.   if AFrom >= Length(FData) then
  570.     Exit;
  571.   if (AFrom + ACount) > Length(FData) then
  572.     ACount := Length(FData) - AFrom;
  573.      
  574.   MoveArray(FData, AFrom + ACount, AFrom, Length(FData) - (AFrom + ACount));
  575.   SetLength(FData, Length(FData) - ACount);
  576. end;
  577.      
  578. procedure TArrayEx<T>.Delete(AIndex: NativeInt);
  579. begin
  580.   Delete(AIndex, 1);
  581. end;
  582.      
  583. procedure TArrayEx<T>.Append(Values: TArrayEx<T>);
  584. begin
  585.   Insert(Length(FData), Values);
  586. end;
  587.      
  588. procedure TArrayEx<T>.Append(Values: TArray<T>);
  589. begin
  590.   Insert(Length(FData), Values);
  591. end;
  592.      
  593. procedure TArrayEx<T>.Append(const Value: T);
  594. begin
  595.   SetLength(FData, Length(FData) + 1);
  596.   FData[High(FData)] := Value;
  597. end;
  598.      
  599. procedure TArrayEx<T>.Append(Values: array of T);
  600. begin
  601.   Insert(Length(FData), Values);
  602. end;
  603.      
  604. function TArrayEx<T>.Insert(AIndex: NativeInt; const Value: T): NativeInt;
  605. var
  606.   i: Integer;
  607. begin
  608.   Result := -1;
  609.   if (AIndex > Length(FData)) or (AIndex < 0) then
  610.     Exit;
  611.   SetLength(FData, Length(FData) + 1);
  612.   MoveArray(FData, AIndex, AIndex + 1, Length(FData) - AIndex);
  613.   FData[AIndex] := Value;
  614.   Result := AIndex;
  615. end;
  616.      
  617. function TArrayEx<T>.Insert(AIndex: NativeInt; const Values: array of T)
  618.   : NativeInt;
  619. var
  620.   i: Integer;
  621. begin
  622.   SetLength(FData, Length(Values));
  623.   MoveArray(FData, AIndex, AIndex + Length(Values), Length(FData) - AIndex);
  624.   for i := 0 to Length(Values) - 1 do
  625.     FData[AIndex + i] := Values[i];
  626.   Result := AIndex;
  627. end;
  628.      
  629. function TArrayEx<T>.Insert(AIndex: NativeInt; const Values: TArray<T>)
  630.   : NativeInt;
  631. var
  632.   i: Integer;
  633. begin
  634.   SetLength(FData, Length(FData) + Length(Values));
  635.   MoveArray(FData, AIndex, AIndex + Length(Values), Length(FData) - AIndex);
  636.   for i := 0 to Length(Values) - 1 do
  637.     FData[AIndex + i] := Values[i];
  638.   Result := AIndex;
  639. end;
  640.      
  641. function TArrayEx<T>.Insert(AIndex: NativeInt; const Values: TArrayEx<T>)
  642.   : NativeInt;
  643. begin
  644.   Result := Insert(AIndex, Values.ToArray);
  645. end;
  646.      
  647. procedure TArrayEx<T>.Unique();
  648. var
  649.   i, J: Integer;
  650.   Tmp: TArrayEx<T>;
  651.   Flag: Boolean;
  652. begin
  653.      
  654.   for i := High(FData) downto Low(FData) do
  655.   begin
  656.     Flag := False;
  657.     for J := High(Tmp.FData) downto Low(Tmp.FData) do
  658.     begin
  659.       if CompareT(FData[i], Tmp[J]) then
  660.       begin
  661.         Flag := True;
  662.         Break;
  663.       end;
  664.     end;
  665.     if not Flag then
  666.       Tmp.Append(FData[i]);
  667.   end;
  668.   FData := Tmp.FData;
  669. end;
  670.      
  671. { TArrayEx<T>.TEnumerator }
  672.      
  673. constructor TArrayEx<T>.TEnumerator.Create(const AValue: TArray<T>);
  674. begin
  675.   FValue := AValue;
  676.   FIndex := -1;
  677. end;
  678.      
  679. function TArrayEx<T>.TEnumerator.GetCurrent: T;
  680. begin
  681.   Result := FValue[FIndex];
  682. end;
  683.      
  684. function TArrayEx<T>.TEnumerator.MoveNext: Boolean;
  685. begin
  686.   Result := False;
  687.   if (FIndex >= Length(FValue)) then
  688.     Exit;
  689.      
  690.   Inc(FIndex);
  691.   Result := FIndex < Length(FValue);
  692. end;
  693.      
  694. end.
  695.  
  696.  
  697.  
  698. //delphi/9001

回复 "Delphi自定义动态泛型数组类代码及使用方法"

这儿你可以回复上面这条便签

captcha