unit Utils.ArrayHandler;
interface
uses
System.Generics.Defaults;
type
TArrayHandler<T> =
class
public
// Append
class procedure Append(
var AArray : TArray<T>;
const AValue : T );
overload;
class procedure Append(
var AArray : TArray<T>;
const AValues : TArray<T> );
overload;
class procedure Append(
var AArray : TArray<T>;
const AValues :
array of T );
overload;
// Contains
class function Contains(
const AArray : TArray<T>;
const AValue : T ) : Boolean;
overload;
class function Contains(
const AArray : TArray<T>;
const AValue : T; AComparer : IComparer<T> ) : Boolean;
overload;
// Shuffle
class procedure Shuffle(
var AArray : TArray<T> );
end;
implementation
{ TArrayHandler<T> }
class procedure TArrayHandler<T>.Append(
var AArray : TArray<T>;
const AValue : T );
begin
SetLength( AArray, Length( AArray ) + 1 );
AArray[high( AArray )] := AValue;
end;
class procedure TArrayHandler<T>.Append(
var AArray : TArray<T>;
const AValues : TArray<T> );
var
LStart : Integer;
LIdx : Integer;
LLow : Integer;
begin
LStart := high( AArray ) + 1;
SetLength( AArray, Length( AArray ) + Length( AValues ) );
LLow := low( AValues );
for LIdx := LLow
to high( AValues )
do
begin
AArray[LStart + LIdx - LLow] := AValues[LIdx];
end;
end;
class procedure TArrayHandler<T>.Append(
var AArray : TArray<T>;
const AValues :
array of T );
var
LStart : Integer;
LIdx : Integer;
LLow : Integer;
begin
LStart := high( AArray ) + 1;
SetLength( AArray, Length( AArray ) + Length( AValues ) );
LLow := low( AValues );
for LIdx := LLow
to high( AValues )
do
begin
AArray[LStart + LIdx - LLow] := AValues[LIdx];
end;
end;
class function TArrayHandler<T>.
Contains(
const AArray : TArray<T>;
const AValue : T; AComparer : IComparer<T> ) : Boolean;
var
LIdx : Integer;
begin
for LIdx := low( AArray )
to high( AArray )
do
if AComparer.Compare( AValue, AArray[LIdx] ) = 0
then
Exit( True );
Result := False;
end;
class procedure TArrayHandler<T>.Shuffle(
var AArray : TArray<T> );
var
LIdx : Integer;
LNewIdx : Integer;
LTmp : T;
begin
for LIdx := high( AArray )
downto low( AArray ) + 1
do
begin
LNewIdx := Random( LIdx + 1 );
if LIdx <> LNewIdx
then
begin
LTmp := AArray[LIdx];
AArray[LIdx] := AArray[LNewIdx];
AArray[LNewIdx] := LTmp;
end;
end;
end;
class function TArrayHandler<T>.
Contains(
const AArray : TArray<T>;
const AValue : T ) : Boolean;
begin
Result := Self.
Contains( AArray, AValue, TComparer<T>.
Default );
end;
end.