Collections API — TColligoList, TColligoArray, TFluentDictionary
Colligo ships three concrete collection classes in Colligo.Collections that implement their interface counterparts and integrate natively with IColligoEnumerable<T>.
TColligoList<T>
Implements IFluentList<T>. Wraps a TList<T> and optionally owns it.
Constructors
constructor Create(const AOwnerships: Boolean = False); overload;
constructor Create(const AComparer: IComparer<T>; const AOwnerships: Boolean = False); overload;
constructor Create(const ACollection: TEnumerable<T>; ...); overload;
constructor Create(const ACollection: IEnumerable<T>; ...); overload;
constructor Create(const AValues: array of T; ...); overload;
constructor Create(const AList: TList<T>; const AOwnsList: Boolean = False; ...); overload;
When AOwnerships = True and T is a class, TColligoList<T> frees owned objects on removal/destruction.
Key methods
procedure Add(const AValue: T);
procedure AddRange(const AValues: array of T); overload;
procedure AddRange(const ACollection: IEnumerable<T>); overload;
procedure Insert(const AIndex: NativeInt; const AValue: T);
procedure Delete(const AIndex: NativeInt);
procedure DeleteRange(const AIndex, ACount: NativeInt);
procedure Sort; overload;
procedure Sort(const AComparer: IComparer<T>); overload;
function Remove(const AValue: T): Boolean;
function Extract(const AValue: T): T;
function BinarySearch(const AItem: T; out FoundIndex: NativeInt): Boolean;
function IndexOf(const AValue: T): NativeInt;
function Count: NativeInt;
function IsEmpty: Boolean;
function AsEnumerable: IColligoEnumerable<T>;
function ToArray: IFluentArray<T>;
Factory class methods
class function From(const AList: TList<T>): IColligoEnumerable<T>;
class function From(const AArray: TArray<T>): IColligoEnumerable<T>;
TColligoArray<T>
Implements IFluentArray<T>. Wraps a TArray<T>.
Key members
constructor Create(const AArray: TArray<T>; AOwnsArray: Boolean = False);
function Length: Integer;
function AsEnumerable: IColligoEnumerable<T>;
function GetEnumerator: IFluentEnumerator<T>;
property Items[AIndex: NativeInt]: T read GetItem write SetItem; default;
property ArrayData: TArray<T> read _GetArray;
Factory class methods
class function From(const AArray: TArray<T>): IColligoEnumerable<T>; overload;
class function From(const AList: TList<T>): IColligoEnumerable<T>; overload;
TColligoArray (non-generic utility record)
Static utility methods mirroring System.Generics.Collections.TArray:
class procedure Sort<T>(var AValues: array of T [, AComparer, AIndex, Count]);
class function BinarySearch<T>(const AValues: array of T; const AItem: T; out FoundIndex: NativeInt [, ...]): Boolean;
class procedure Copy<T>(const Source: array of T; var Destination: array of T; ...);
class function Concat<T>(const Args: array of TArray<T>): IFluentArray<T>;
class function IndexOf<T>(const AValues: array of T; const AItem: T [, ...]): NativeInt;
class function LastIndexOf<T>(...): NativeInt;
class function Contains<T>(const AValues: array of T; const AItem: T [, AComparer]): Boolean;
class function ToString<T>(const AValues: array of T; const ASeparator: string = ','): string;
class procedure FreeValues<T>(const AValues: array of T);
class function From<T>(const AArray: array of T): IColligoEnumerable<T>;
TFluentDictionary<K, V>
Implements IFluentDictionary<K, V>. Wraps TDictionary<K, V> (or TObjectDictionary when ownerships are specified).
Constructors
constructor Create(const AOwnerships: TDictionaryOwnerships = []);
constructor Create(const ACapacity: NativeInt; ...);
constructor Create(const AComparer: IEqualityComparer<K>; ...);
constructor Create(const ACollection: TEnumerable<TPair<K, V>>);
constructor Create(const AItems: array of TPair<K, V> [, AComparer]);
Key methods
procedure Add(const AKey: K; const AValue: V);
procedure AddOrSetValue(const AKey: K; const AValue: V);
function Remove(const AKey: K): Boolean;
function TryGetValue(const AKey: K; var AValue: V): Boolean;
function TryAdd(const AKey: K; const AValue: V): Boolean;
function ContainsKey(const AKey: K): Boolean;
function ContainsValue(const AValue: V): Boolean;
function Count: NativeInt;
function IsEmpty: Boolean;
function AsEnumerable: IColligoEnumerable<TPair<K, V>>;
function ToArray: IFluentArray<TPair<K, V>>;
property Items[const AKey: K]: V read GetItem write SetItem; default;
property Keys: TDictionary<K, V>.TKeyCollection;
property Values: TDictionary<K, V>.TValueCollection;
Iterating a dictionary as a pipeline
var LDict: IFluentDictionary<string, Integer>;
// ...
var LResult := LDict.AsEnumerable
.Where(function(const P: TPair<string, Integer>): Boolean
begin Result := P.Value > 10 end)
.Select<string>(function(const P: TPair<string, Integer>): string
begin Result := P.Key end)
.ToArray;