1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4 ! Purpose: This module implements a list with insert, search, and
6 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12 integer :: ikey, ivalue
13 character (len=128) :: ckey, cvalue
14 type (list_item), pointer :: next, prev
19 type (list_item), pointer :: head, tail
24 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 ! Purpose: To initialize a list type
28 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
29 subroutine list_init(l)
34 type (list), intent(inout) :: l
40 end subroutine list_init
43 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
46 ! Purpose: Given a list l, a key, and a value to be stored with that key,
47 ! this routine adds (key, value) to the table.
49 ! NOTE: If the key already exists in the list, a second copy of a list item
50 ! with that key is added, possibly with a different associated value.
51 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
52 subroutine list_insert(l, ikey, ivalue, ckey, cvalue)
57 integer, intent(in), optional :: ikey, ivalue
58 character (len=128), intent(in), optional :: ckey, cvalue
59 type (list), intent(inout) :: l
62 type (list_item), pointer :: lp
67 if (present(ikey) .and. present(ivalue)) then
70 else if (present(ckey) .and. present(cvalue)) then
74 call mprintf(.true.,ERROR,'list_insert() called without proper arguments.')
77 if (associated(l%tail)) then
88 end subroutine list_insert
91 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
95 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
96 function list_get_keys(l)
101 type (list), intent(in) :: l
104 type (list_item), pointer, dimension(:) :: list_get_keys
108 type (list_item), pointer :: lp
110 allocate(list_get_keys(l%l_len))
115 do while (associated(lp))
116 list_get_keys(i)%ikey = lp%ikey
117 list_get_keys(i)%ivalue = lp%ivalue
118 list_get_keys(i)%ckey = lp%ckey
119 list_get_keys(i)%cvalue = lp%cvalue
126 end function list_get_keys
129 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
132 ! Purpose: If key k is found in the list, this function returns TRUE and sets
133 ! value equal to the value stored with k. If the k is not found, this
134 ! function returns FALSE, and value is undefined.
135 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
136 function list_search(l, ikey, ivalue, ckey, cvalue)
141 integer, intent(in), optional :: ikey
142 integer, intent(out), optional :: ivalue
143 character (len=128), intent(in), optional :: ckey
144 character (len=128), intent(out), optional :: cvalue
145 type (list), intent(inout) :: l
148 logical :: list_search
151 type (list_item), pointer :: lp
153 list_search = .false.
157 do while (associated(lp))
158 if (present(ikey) .and. present(ivalue)) then
159 if (lp%ikey == ikey) then
164 else if (present(ckey) .and. present(cvalue)) then
165 if (lp%ckey == ckey) then
171 call mprintf(.true.,ERROR,'list_search() called without proper arguments.')
176 end function list_search
179 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
180 ! Name: list_get_first_item
182 ! Purpose: Sets k and v equal to the key and value, respectively, of the
183 ! first item in the list. The list should be thought of as a queue, so that
184 ! the first item refers to the least recently inserted item that has not yet
185 ! been removed or retrieved. This item is also removed from the list before
186 ! the subroutine returns.
187 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
188 subroutine list_get_first_item(l, ikey, ivalue, ckey, cvalue)
193 integer, intent(out), optional :: ikey, ivalue
194 character (len=128), intent(out), optional :: ckey, cvalue
195 type (list), intent(inout) :: l
198 type (list_item), pointer :: lp
202 if (associated(lp)) then
203 if (present(ikey) .and. present(ivalue)) then
206 else if (present(ckey) .and. present(cvalue)) then
210 call mprintf(.true.,ERROR,'list_get_first_item() called without proper arguments.')
213 if (associated(lp%next)) nullify(lp%next%prev)
215 l%l_len = l%l_len - 1
218 end subroutine list_get_first_item
221 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
224 ! Purpose: Deletes the entry with key k from the list. If multiple entries
225 ! have the specified key, only the first encountered entry is deleted.
226 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
227 subroutine list_remove(l, ikey, ckey)
232 integer, intent(in), optional :: ikey
233 character (len=128), intent(in), optional :: ckey
234 type (list), intent(inout) :: l
237 type (list_item), pointer :: lp
241 do while (associated(lp))
242 if (present(ikey)) then
243 if (lp%ikey == ikey) then
245 if (.not. associated(lp%prev)) then
247 if (.not. associated(l%head)) nullify(l%tail)
248 if (associated(lp%next)) nullify(lp%next%prev)
250 else if (.not. associated(lp%next)) then
252 if (.not. associated(l%tail)) nullify(l%head)
253 if (associated(lp%prev)) nullify(lp%prev%next)
256 lp%prev%next => lp%next
257 lp%next%prev => lp%prev
260 l%l_len = l%l_len - 1
266 else if (present(ckey)) then
268 if (lp%ckey == ckey) then
270 if (.not. associated(lp%prev)) then
272 if (.not. associated(l%head)) nullify(l%tail)
273 if (associated(lp%next)) nullify(lp%next%prev)
275 else if (.not. associated(lp%next)) then
277 if (.not. associated(l%tail)) nullify(l%head)
278 if (associated(lp%prev)) nullify(lp%prev%next)
281 lp%prev%next => lp%next
282 lp%next%prev => lp%prev
285 l%l_len = l%l_len - 1
291 call mprintf(.true.,ERROR,'list_remove() called without proper arguments.')
297 end subroutine list_remove
300 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
303 ! Purpose: Returns the number of items in the list l.
304 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
305 function list_length(l)
310 type (list), intent(in) :: l
313 integer :: list_length
315 list_length = l%l_len
319 end function list_length
322 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
325 ! Purpose: Frees all memory associated with list l. This routine may be
326 ! used to remove all entries from a list.
327 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
328 subroutine list_destroy(l)
333 type (list), intent(inout) :: l
336 type (list_item), pointer :: lp
340 do while (associated(lp))
350 end subroutine list_destroy
352 end module list_module