Skip to content

TEnumerator

Ivan Semenkov edited this page Feb 13, 2021 · 2 revisions

Table of contents

About

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.

TOptionalValue

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>;

Create

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.

Example
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;

Iterate

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;
Example
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;

FirstEntry

Retrive the first entry.

function FirstEntry : TIterator;
Example
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;

TIterator

HasValue

Return true if iterator has correct value.

function HasValue : Boolean;
Example
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;
Next

Retrieve the iterator for next entry.

function Next : TIterator;
Example
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;
Value

To get value use Value property.

property Value : {$IFNDEF USE_OPTIONAL}V{$ELSE}TOptionalValue{$ENDIF};
Example
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;
Index

Get current item index.

property Index : Integer;
Example
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;
Clone this wiki locally