MODFLOW 6  version 6.7.0.dev0
USGS Modular Hydrologic Model
KeyValueList.f90
Go to the documentation of this file.
2  use kindmodule, only: i4b
7  implicit none
8  private
9 
10  public :: keyvaluelisttype
11 
12  !> @brief A list that stores items as a key-value pair
13  !!
14  !! Items in this list can be retrieved by using a key.
15  !<
17  private
18  type(keyvaluenodetype), pointer :: first => null() !< first item in the list
19  type(keyvaluenodetype), pointer :: last => null() !< first item in the list
20  integer(I4B) :: cnt = 0 !< number of items in the list
21  contains
22  procedure :: iterator
23  procedure :: add
24  procedure :: get
25  procedure :: count
26  procedure :: clear
27  end type keyvaluelisttype
28 
29 contains
30 
31  function iterator(this) result(itr)
32  class(keyvaluelisttype) :: this
33  class(iteratortype), allocatable :: itr
34 
35  itr = keyvaluelistiteratortype(this%first)
36  end function
37 
38  !> @brief Add a key-value pair to the list
39  !!
40  !! The list uses an 'append to end' approach for adding items
41  !<
42  subroutine add(this, key, val)
43  class(keyvaluelisttype) :: this
44  character(len=*), intent(in) :: key
45  class(*), pointer, intent(in) :: val
46  ! -- local
47 
48  if (.not. associated(this%first)) then
49  allocate (this%first)
50  this%last => this%first
51  else
52  allocate (this%last%next)
53  this%last => this%last%next
54  end if
55 
56  allocate (this%last%key)
57  this%last%key = key
58  this%last%value => val
59  this%last%next => null()
60 
61  this%cnt = this%cnt + 1
62 
63  end subroutine add
64 
65  !> @brief Get a value using a key
66  !!
67  !! If the key can't be found the return value will be a null pointer
68  !<
69  function get(this, key) result(val)
70  class(keyvaluelisttype) :: this
71  character(len=*), intent(in) :: key
72  class(*), pointer :: val
73  ! -- local
74  type(keyvaluenodetype), pointer :: node !< current node in the list
75 
76  val => null()
77  node => this%first
78 
79  do while (associated(node))
80  if (node%key == key) then
81  val => node%value
82  exit
83  end if
84  node => node%next
85  end do
86 
87  end function
88 
89  !> @brief The nummer of items in the list
90  !!
91  !<
92  function count(this) result(val)
93  class(keyvaluelisttype) :: this
94  integer(I4B) :: val
95 
96  val = this%cnt
97  end function
98 
99  !> @brief Clears the list
100  !!
101  !! clears all the nodes of the list
102  !<
103  subroutine clear(this)
104  class(keyvaluelisttype) :: this
105 
106  if (associated(this%first)) call clear_node(this%first)
107  this%first => null()
108  this%last => null()
109  this%cnt = 0
110 
111  end subroutine
112 
113  !> @brief Clears the node
114  !!
115  !! Recursive method that clears the next node before clearing itself
116  !<
117  recursive subroutine clear_node(node)
118  type(keyvaluenodetype), pointer :: node
119 
120  if (associated(node)) then
121  call clear_node(node%next)
122 
123  deallocate (node%key)
124 
125  nullify (node%key)
126  nullify (node%value)
127  nullify (node%next)
128 
129  deallocate (node)
130  end if
131 
132  end subroutine clear_node
133 
134 end module keyvaluelistmodule
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter lenmemaddress
maximum length of the full memory address, including variable name
Definition: Constants.f90:31
class(*) function, pointer get(this, key)
Get a value using a key.
integer(i4b) function count(this)
The nummer of items in the list.
class(iteratortype) function, allocatable iterator(this)
subroutine clear(this)
Clears the list.
recursive subroutine clear_node(node)
Clears the node.
subroutine add(this, key, val)
Add a key-value pair to the list.
This module defines variable data types.
Definition: kind.f90:8
An iterator used to iterate through a KeyValueList.
A list that stores items as a key-value pair.