MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
PtrHashTableIterator.f90
Go to the documentation of this file.
3  use kindmodule, only: i4b
5 
6  implicit none
7  private
8 
10 
11  !> @brief An iterator used to iterate through a PtrHashTable
12  !!
13  !<
15  type(keyvaluelisttype), pointer :: buckets(:) => null() !< the buckets of the PtrHashTable to iterate through
16  class(iteratortype), allocatable :: current_bucket_iterator !< the iterator of the bucket to which the current iterator belongs
17  integer(I4B) :: curent_bucket_index = 1 !< the bucket in which the current iterator belongs
18  contains
19  procedure :: has_next
20  procedure :: next
21  procedure :: value
22  end type
23 
24  interface ptrhashtableiteratortype
25  module procedure constructor
26  end interface ptrhashtableiteratortype
27 
28 contains
29  !> @brief Constructor to create a PtrHashTableIterator
30  !!
31  !<
32  function constructor(buckets) Result(iterator)
33  type(keyvaluelisttype), target, dimension(:), intent(in) :: buckets
34  type(ptrhashtableiteratortype) :: iterator
35  ! -- local
36  type(keyvaluelisttype), pointer :: first_bucket
37 
38  iterator%buckets => buckets
39 
40  first_bucket => iterator%buckets(1)
41  allocate (iterator%current_bucket_iterator, source=first_bucket%iterator())
42 
43  end function constructor
44 
45  !> @brief Indicates if there is a next node in the iteration chain
46  !!
47  !<
48  function has_next(this) result(res)
49  class(ptrhashtableiteratortype) :: this
50  type(logical) :: res
51  ! -- local
52  type(keyvaluelisttype), pointer :: bucket
53  integer(I4B) :: bucket_index
54 
55  !
56  ! -- check if there are more values in the current bucket
57  if (this%current_bucket_iterator%has_next()) then
58  res = .true.
59  return
60  end if
61 
62  !
63  ! -- check if there is a next bucket which has values.
64  ! -- if so then there is more to iterate through.
65  do bucket_index = this%curent_bucket_index + 1, size(this%buckets)
66  bucket => this%buckets(bucket_index)
67  if (bucket%count() > 0) then
68  res = .true.
69  return
70  end if
71  end do
72 
73  res = .false.
74 
75  end function
76 
77  !> @brief Increment the iterator to the next node
78  !!
79  !<
80  subroutine next(this)
81  class(ptrhashtableiteratortype) :: this
82  ! -- local
83  type(keyvaluelisttype), pointer :: bucket !< a bucket
84  integer(I4B) :: bucket_index
85 
86  !
87  ! -- If the current bucket doesn't have anymore values continue to the next
88  if (.not. this%current_bucket_iterator%has_next()) then
89  do bucket_index = this%curent_bucket_index + 1, size(this%buckets)
90  bucket => this%buckets(bucket_index)
91  if (bucket%count() > 0) then
92  deallocate (this%current_bucket_iterator)
93  allocate (this%current_bucket_iterator, source=bucket%iterator())
94  this%curent_bucket_index = bucket_index
95  exit
96  end if
97  end do
98  end if
99 
100  call this%current_bucket_iterator%next()
101 
102  end subroutine
103 
104  !> @brief Get the value the iterator is pointing to
105  !!
106  !<
107  function value(this) result(res)
108  class(ptrhashtableiteratortype) :: this
109  class(*), pointer :: res
110 
111  res => this%current_bucket_iterator%value()
112 
113  end function
114 
This module defines variable data types.
Definition: kind.f90:8
subroutine next(this)
Increment the iterator to the next node.
type(logical) function has_next(this)
Indicates if there is a next node in the iteration chain.
type(ptrhashtableiteratortype) function constructor(buckets)
Constructor to create a PtrHashTableIterator.
class(*) function, pointer value(this)
Get the value the iterator is pointing to.
A list that stores items as a key-value pair.
An iterator used to iterate through a PtrHashTable.