iterator Derived Type

type, public :: iterator

type~~iterator~~InheritsGraph type~iterator iterator type~container container type~container->type~iterator contents
Help


A data type which provides a collection of data to the user. Objects of this type are returned using the iter method of FIAT's other data types. The contents of the iterator are set to be the same as the iterable at the time when the iter method was called. If new items are later added to the iterable object, this will not be reflected in the iterator object.

Example

If list_obj is some sort of list which contains character strings, then the following would print all strings held in the list.

iterator_obj = list_obj%iter()
do while(iterator_obj%has_next())
    string = iterator_obj%next()
    write(*,*) string
end do

Components

TypeVisibility AttributesNameInitial
class(container), private, allocatable, dimension(:):: contents
integer, private :: location =1
logical, private :: filled =.false.

Constructor

public interface iterator

  • private pure function constructor(contents) result(new)

    Arguments

    Type IntentOptional AttributesName
    class(container), intent(in), dimension(:):: contents

    Return Value type(iterator)

    Description

    Author
    Chris MacMackin
    Date
    March 2016

    Creates an iterator from an array of containers containing the data to be returned upon iteration. The data is returned starting with the first element of the array and ending with the last.


Type-Bound Procedures

procedure, public :: has_next

  • private elemental function has_next(this)

    Arguments

    Type IntentOptional AttributesName
    class(iterator), intent(in) :: this

    Return Value logical

    Whether there are additional items to iterate through

    Description

    Author
    Chris MacMackin
    Date
    March 2016

    Returns .true. if there are any remaining objects through which to iterate, and .false. otherwise.

procedure, public :: next

  • private function next(this)

    Arguments

    Type IntentOptional AttributesName
    class(iterator), intent(inout) :: this

    Return Value class(container), allocatable

    The next item held in the iterator, if present. Otherwise an unallocated container.

    Description

    Author
    Chris MacMackin
    Date
    March 2016

    Returns the next item stored in the iterator. If there are no more items present then an empty container is returned. If there are no contents stored in this iterator then it returns an unallocated container.

procedure, public :: reset

  • private subroutine reset(this)

    Arguments

    Type IntentOptional AttributesName
    class(iterator), intent(inout) :: this

    Description

    Author
    Chris MacMackin
    Date
    March 2016

    Resets the position of the iterator to the start, so it is as though the next routine has never been called.

procedure, public :: contents_type

  • private pure function contents_type(this)

    Arguments

    Type IntentOptional AttributesName
    class(iterator), intent(in) :: this

    Return Value class(container), pointer

    A container with the dynamic type of that used to hold the contents of the iterator. It is a pointer as pointer assignment is the easiest way to hold its "value" in an abstract variable.

    Description

    Author
    Chris MacMackin
    Date
    March 2016

    Returns a container with the dynamic type of that used to hold the contents of this iterator

Source Code

  type, public :: iterator
    !* Author: Chris MacMackin
    !  Date: March 2016
    !
    ! A data type which provides a collection of data to the user. Objects
    ! of this type are returned using the [[iterable:iter]] method of
    ! FIAT's other data types. The contents of the iterator are set to be
    ! the same as the iterable at the time when the `iter` method was 
    ! called. If new items are later added to the iterable object, this
    ! will not be reflected in the iterator object.
    !
    !##Example
    ! If `list_obj` is some sort of [[list]] which contains character
    ! strings, then the following would print all strings held in the
    ! list.
    !```fortran
    !iterator_obj = list_obj%iter()
    !do while(iterator_obj%has_next())
    !    string = iterator_obj%next()
    !    write(*,*) string
    !end do
    !```
    !
    private
    class(container), allocatable, dimension(:) :: contents
    integer :: location = 1
    logical :: filled = .false.
  contains
    procedure :: has_next
    procedure :: next
    procedure :: reset
    procedure :: contents_type
  end type iterator