MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
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
29contains
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 call clear_nodes(this%first)
107
108 this%first => null()
109 this%last => null()
110 this%cnt = 0
111
112 end subroutine
113
114 !> @brief Clears the node and all subsequent nodes
115 !!
116 !<
117 subroutine clear_nodes(node)
118 type(keyvaluenodetype), pointer :: node
119 type(keyvaluenodetype), pointer :: next
120
121 do while (associated(node))
122 next => node%next
123
124 call clear_node(node)
125 deallocate (node)
126
127 node => next
128 end do
129
130 end subroutine clear_nodes
131
132 !> @brief Clears a single node
133 !!
134 !<
135 subroutine clear_node(node)
136 type(keyvaluenodetype), pointer :: node
137
138 deallocate (node%key)
139
140 nullify (node%key)
141 nullify (node%value)
142 nullify (node%next)
143
144 end subroutine
145
146end 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.
subroutine clear_nodes(node)
Clears the node and all subsequent nodes.
subroutine add(this, key, val)
Add a key-value pair to the list.
subroutine clear_node(node)
Clears a single node.
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.