-
Notifications
You must be signed in to change notification settings - Fork 1
/
abstract_iterator_class.f90
70 lines (54 loc) · 2.07 KB
/
abstract_iterator_class.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
!=====================================================================!
! Iterator enables to cycle through a collection, obtaining or
! removing elements.
!
! Before you can access a collection through an iterator, you must
! obtain one. Each of the collection classes provides an iterator()
! method that returns an iterator to the start of the collection. By
! using this iterator object, you can access each element in the
! collection, one element at a time.
!
! In general, to use an iterator to cycle through the contents of a
! collection, follow these steps:
!
! o Obtain an iterator to the start of the collection by calling the
! collection's iterator( ) method.
!
! o Set up a loop that makes a call to has_next(). Have the loop
! iterate as long as has_next() returns true.
!
! o Within the loop, obtain each element by calling next().
!
! Author: Komahan Boopathy ([email protected])
!=====================================================================!
module iterator_interface
use object_class, only : object
implicit none
private
public :: iterator
type, abstract :: iterator
contains
procedure(has_next), deferred :: has_next
procedure(next) , deferred :: next
! procedure :: remove ! optional
end type iterator
interface
!----------------------------------------------------------------!
! tells if there is a next element in collection
!----------------------------------------------------------------!
pure type(logical) function has_next(this)
import iterator
class(iterator), intent(in) :: this
end function has_next
!----------------------------------------------------------------!
! Returns the next element in collection
!----------------------------------------------------------------!
pure function next(this) result(next_entry)
import iterator
import object
class(iterator), intent(in) :: this
class(object), allocatable :: next_entry
end function next
end interface
contains
end module iterator_interface