MODFLOW 6  version 6.7.0.dev0
USGS Modular Hydrologic Model
ListIterator.f90
Go to the documentation of this file.
2  use kindmodule, only: i4b, lgp
5 
6  implicit none
7  private
8 
9  public :: listiteratortype
10 
11  !> @brief An iterator used to iterate through a List
12  !!
13  !<
14  type, extends(iteratortype) :: listiteratortype
15  private
16  type(listnodetype), pointer :: first_node => null() !< the List to iterate through
17  type(listnodetype), pointer :: current_node => null() !< the current node in the list the iterator is pointing at
18  contains
19  procedure :: has_next
20  procedure :: next
21  procedure :: value
22  end type
23 
24  interface listiteratortype
25  module procedure constructor
26  end interface listiteratortype
27 
28 contains
29 
30  !> @brief Constructor to create a ListIterator
31  !!
32  !<
33  function constructor(first_node) result(iterator)
34  type(listnodetype), pointer :: first_node
35  type(listiteratortype) :: iterator
36 
37  iterator%first_node => first_node
38  iterator%current_node => null()
39 
40  end function constructor
41 
42  !> @brief Indicates if there is a next node in the iteration chain
43  !!
44  !<
45  function has_next(this) result(res)
46  class(listiteratortype) :: this
47  logical(LGP) :: res
48 
49  if (associated(this%current_node)) then
50  res = associated(this%current_node%nextNode)
51  else
52  res = associated(this%first_node)
53  end if
54 
55  end function
56 
57  !> @brief Increment the iterator to the next node
58  !!
59  !<
60  subroutine next(this)
61  class(listiteratortype) :: this
62 
63  if (associated(this%current_node)) then
64  this%current_node => this%current_node%nextNode
65  else
66  this%current_node => this%first_node
67  end if
68  end subroutine
69 
70  !> @brief Get the value the iterator is pointing at
71  !!
72  !<
73  function value(this) result(res)
74  class(listiteratortype) :: this
75  class(*), pointer :: res
76 
77  if (associated(this%current_node)) then
78  res => this%current_node%GetItem()
79  else
80  res => null()
81  end if
82 
83  end function
84 
85 end module listiteratormodule
This module defines variable data types.
Definition: kind.f90:8
subroutine next(this)
Increment the iterator to the next node.
type(listiteratortype) function constructor(first_node)
Constructor to create a ListIterator.
logical(lgp) function has_next(this)
Indicates if there is a next node in the iteration chain.
class(*) function, pointer value(this)
Get the value the iterator is pointing at.
An iterator used to iterate through a List.