|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- unit uutlLinq;
-
- {$mode objfpc}{$H+}
- {$IFDEF UTL_NESTED_PROCVARS}
- {$modeswitch nestedprocvars}
- {$ENDIF}
-
- interface
-
- {$IFDEF UTL_ENUMERATORS}
- uses
- Classes, SysUtils,
- uutlTypes, uutlInterfaces, uutlComparer, uutlFilter;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlCount<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): Integer; inline;
-
- generic function utlAny<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): Boolean; inline;
-
- generic function utlToArray<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize TutlArray<T>; inline;
-
- generic function utlContains<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aElement: T): Boolean; inline; overload;
-
- generic function utlContains<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aElement: T;
- constref aComparer: specialize IutlEqualityComparer<T>): Boolean; inline; overload;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSkip<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- const aSkip: Integer): specialize IutlEnumerator<T>; inline;
-
- generic function utlTake<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- const aTake: Integer): specialize IutlEnumerator<T>; inline;
-
- generic function utlConcat<T>(
- constref aEnumerator1, aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlConcat<T>(
- constref aEnumerators: specialize TutlArray<specialize IutlEnumerator<T>>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlReverse<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>; inline;
-
- {$IFDEF UTL_ADVANCED_ENUMERATORS}
- generic function utlSort<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlSort<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize IutlFilter<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize TutlFilterEvent<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize TutlFilterEventO<T>): specialize IutlEnumerator<T>; inline; overload;
-
- {$IFDEF UTL_NESTED_PROCVARS}
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize TutlFilterEventN<T>): specialize IutlEnumerator<T>; inline; overload;
- {$ENDIF}
-
- generic function utlDistinct<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlDistinct<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlIntersect<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlIntersect<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlUnion<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlUnion<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlWithout<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlWithout<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>; inline; overload;
-
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize IutlSelector<T, S>): specialize IutlEnumerator<S>; inline; overload;
-
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEvent<T, S>): specialize IutlEnumerator<S>; inline; overload;
-
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventO<T, S>): specialize IutlEnumerator<S>; inline; overload;
-
- {$IFDEF UTL_NESTED_PROCVARS}
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventN<T, S>): specialize IutlEnumerator<S>; inline; overload;
- {$ENDIF}
-
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize IutlSelector<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>; inline; overload;
-
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEvent<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>; inline; overload;
-
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventO<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>; inline; overload;
-
- {$IFDEF UTL_NESTED_PROCVARS}
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventN<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>; inline; overload;
- {$ENDIF}
-
- generic function utlZip<T, S>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<S>): specialize IutlEnumerator<specialize TutlPair<T, S>>;
- {$ENDIF}
- {$ENDIF}
-
- implementation
-
- {$IFDEF UTL_ENUMERATORS}
- uses
- uutlEnumerator;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //utlLinq///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlCount<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): Integer;
- begin
- result := aEnumerator.Count;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlAny<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): Boolean;
- begin
- result := aEnumerator.Any;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlToArray<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize TutlArray<T>;
- begin
- result := aEnumerator.ToArray;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlContains<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aElement: T): Boolean;
- begin
- result := aEnumerator.Contains(aElement, specialize TutlEqualityComparer<T>.Create);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlContains<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aElement: T;
- constref aComparer: specialize IutlEqualityComparer<T>): Boolean;
- begin
- result := aEnumerator.Contains(aElement, aComparer);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSkip<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- const aSkip: Integer): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Skip(aSkip);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlTake<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- const aTake: Integer): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Take(aTake);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlConcat<T>(
- constref aEnumerator1, aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>;
- type
- TConcatEnumerator = specialize TutlConcatEnumerator<T>;
- begin
- result := TConcatEnumerator.Create(TConcatEnumerator.TEnumerators.Create(aEnumerator1, aEnumerator2));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlConcat<T>(
- constref aEnumerators: specialize TutlArray<specialize IutlEnumerator<T>>): specialize IutlEnumerator<T>;
- type
- TConcatEnumerator = specialize TutlConcatEnumerator<T>;
- begin
- result := TConcatEnumerator.Create(aEnumerators);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlReverse<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Reverse;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- {$IFDEF UTL_ADVANCED_ENUMERATORS}
- generic function utlSort<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Sort(specialize TutlComparer<T>.Create);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSort<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Sort(aComparer);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize IutlFilter<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Where(aFilter);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize TutlFilterEvent<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Where(specialize TutlCallbackFilter<T>.Create(aFilter));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize TutlFilterEventO<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Where(specialize TutlCallbackFilter<T>.Create(aFilter));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- {$IFDEF UTL_NESTED_PROCVARS}
- generic function utlWhere<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aFilter: specialize TutlFilterEventN<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Where(specialize TutlCallbackFilter<T>.Create(aFilter));
- end;
- {$ENDIF}
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlDistinct<T>(
- constref aEnumerator: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Distinct(specialize TutlComparer<T>.Create);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlDistinct<T>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator.Distinct(aComparer);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlIntersect<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator1.Intersect(aEnumerator2, specialize TutlComparer<T>.Create);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlIntersect<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator1.Intersect(aEnumerator2, aComparer);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlUnion<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator1.Union(aEnumerator2, specialize TutlComparer<T>.Create);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlUnion<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator1.Union(aEnumerator2, aComparer);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlWithout<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator1.Without(aEnumerator2, specialize TutlComparer<T>.Create);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlWithout<T>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<T>;
- constref aComparer: specialize IutlComparer<T>): specialize IutlEnumerator<T>;
- begin
- result := aEnumerator1.Without(aEnumerator2, aComparer);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize IutlSelector<T, S>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectEnumerator<T, S>.Create(aEnumerator, aSelector);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEvent<T, S>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectEnumerator<T, S>.Create(aEnumerator, specialize TutlCallbackSelector<T, S>.Create(aSelector));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventO<T, S>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectEnumerator<T, S>.Create(aEnumerator, specialize TutlCallbackSelector<T, S>.Create(aSelector));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- {$IFDEF UTL_NESTED_PROCVARS}
- generic function utlSelect<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventN<T, S>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectEnumerator<T, S>.Create(aEnumerator, specialize TutlCallbackSelector<T, S>.Create(aSelector));
- end;
- {$ENDIF}
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize IutlSelector<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectManyEnumerator<T, S>.Create(aEnumerator, aSelector);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEvent<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectManyEnumerator<T, S>.Create(aEnumerator, specialize TutlCallbackSelector<T, specialize IutlEnumerator<S>>.Create(aSelector));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventO<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectManyEnumerator<T, S>.Create(aEnumerator, specialize TutlCallbackSelector<T, specialize IutlEnumerator<S>>.Create(aSelector));
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- {$IFDEF UTL_NESTED_PROCVARS}
- generic function utlSelectMany<T, S>(
- constref aEnumerator: specialize IutlEnumerator<T>;
- constref aSelector: specialize TutlSelectEventN<T, specialize IutlEnumerator<S>>): specialize IutlEnumerator<S>;
- begin
- result := specialize TutlSelectManyEnumerator<T, S>.Create(aEnumerator, specialize TutlCallbackSelector<T, specialize IutlEnumerator<S>>.Create(aSelector));
- end;
- {$ENDIF}
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- generic function utlZip<T, S>(
- constref aEnumerator1: specialize IutlEnumerator<T>;
- constref aEnumerator2: specialize IutlEnumerator<S>): specialize IutlEnumerator<specialize TutlPair<T, S>>;
- begin
- result := specialize TutlZipEnumerator<T, S>.Create(aEnumerator1, aEnumerator2);
- end;
- {$ENDIF}
- {$ENDIF}
-
- end.
-
|