-
Notifications
You must be signed in to change notification settings - Fork 7
TEnumerator
Ivan Semenkov edited this page Feb 13, 2021
·
2 revisions
Class adds counter to an iterable object and returns it like in a Python language.
uses
utils.enumerate;
type
generic TEnumerator<V; Iterator> = class
Iterator
is a forward iterator implemented object.
If macro {$USE_OPTIONAL}
is defined, then all methods return a TOptionalValue wrapper, otherwise V.
uses
utils.optional;
type
TOptionalValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<V>;
A new enumerator can be created by call its constructor.
constructor Create (AIterator : Iterator; AStartIndex : Integer = 0);
AIterator
is a forward iterator implemented object.
AStartIndex
is counter start index value.
uses
container.list, utils.functor, utils.enumerate;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TIntegerListEnumerator = {$IFDEF FPC}specialize{$ENDIF} TEnumerator<Integer, TIntegerList.TIterator>;
var
list : TIntegerList;
enumerator : TIntegerListEnumerator;
begin
list := TIntegerList.Create;
enumerator := TIntegerListEnumerator.Create(list.FirstEntry, 0);
FreeAndNil(enumerator);
FreeAndNil(list);
end;
It is possible to iterate for TEnumerator values using in
operator. Each value would present as TEnumerator.TIterator object.
uses
utils.enumerate;
type
generic TEnumerator<V; Iterator> = class
type
TIterator = class
end;
uses
container.list, utils.functor, utils.enumerate;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TIntegerListEnumerator = {$IFDEF FPC}specialize{$ENDIF} TEnumerator<Integer, TIntegerList.TIterator>;
var
list : TIntegerList;
enumerator : TIntegerListEnumerator;
iterator : TIntegerListEnumerator.TIterator;
begin
list := TIntegerList.Create;
enumerator := TIntegerListEnumerator.Create(list.FirstEntry, 0);
for iterator in enumerator do
;
FreeAndNil(enumerator);
FreeAndNil(list);
end;
Retrive the first entry.
function FirstEntry : TIterator;
uses
container.list, utils.functor, utils.enumerate;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TIntegerListEnumerator = {$IFDEF FPC}specialize{$ENDIF} TEnumerator<Integer, TIntegerList.TIterator>;
var
list : TIntegerList;
enumerator : TIntegerListEnumerator;
iterator : TIntegerListEnumerator.TIterator;
begin
list := TIntegerList.Create;
enumerator := TIntegerListEnumerator.Create(list.FirstEntry, 0);
iterator := enumerator.FirstEntry;
while iterator.HasValue do
begin
iterator := iterator.Next;
end;
FreeAndNil(enumerator);
FreeAndNil(list);
end;
Return true if iterator has correct value.
function HasValue : Boolean;
uses
container.list, utils.functor, utils.enumerate;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TIntegerListEnumerator = {$IFDEF FPC}specialize{$ENDIF} TEnumerator<Integer, TIntegerList.TIterator>;
var
list : TIntegerList;
enumerator : TIntegerListEnumerator;
iterator : TIntegerListEnumerator.TIterator;
begin
list := TIntegerList.Create;
enumerator := TIntegerListEnumerator.Create(list.FirstEntry, 0);
for iterator in enumerator do
begin
if iterator.HasValue then
;
end;
FreeAndNil(enumerator);
FreeAndNil(list);
end;
Retrieve the iterator for next entry.
function Next : TIterator;
uses
container.list, utils.functor, utils.enumerate;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TIntegerListEnumerator = {$IFDEF FPC}specialize{$ENDIF} TEnumerator<Integer, TIntegerList.TIterator>;
var
list : TIntegerList;
enumerator : TIntegerListEnumerator;
iterator : TIntegerListEnumerator.TIterator;
begin
list := TIntegerList.Create;
enumerator := TIntegerListEnumerator.Create(list.FirstEntry, 0);
for iterator in enumerator do
begin
if iterator.Next.HasValue then
;
end;
FreeAndNil(enumerator);
FreeAndNil(list);
end;
To get value use Value property.
property Value : {$IFNDEF USE_OPTIONAL}V{$ELSE}TOptionalValue{$ENDIF};
uses
container.list, utils.functor, utils.enumerate;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TIntegerListEnumerator = {$IFDEF FPC}specialize{$ENDIF} TEnumerator<Integer, TIntegerList.TIterator>;
var
list : TIntegerList;
enumerator : TIntegerListEnumerator;
iterator : TIntegerListEnumerator.TIterator;
begin
list := TIntegerList.Create;
enumerator := TIntegerListEnumerator.Create(list.FirstEntry, 0);
for iterator in enumerator do
begin
writeln(iterator.Value);
end;
FreeAndNil(enumerator);
FreeAndNil(list);
end;
Get current item index.
property Index : Integer;
uses
container.list, utils.functor, utils.enumerate;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TIntegerListEnumerator = {$IFDEF FPC}specialize{$ENDIF} TEnumerator<Integer, TIntegerList.TIterator>;
var
list : TIntegerList;
enumerator : TIntegerListEnumerator;
iterator : TIntegerListEnumerator.TIterator;
begin
list := TIntegerList.Create;
enumerator := TIntegerListEnumerator.Create(list.FirstEntry, 0);
for iterator in enumerator do
begin
writeln(iterator.Value);
writeln(iterator.Index);
end;
FreeAndNil(enumerator);
FreeAndNil(list);
end;