1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4 ! Description: This module implements a queue of user-defined data types and
5 ! a set of routines related to the maintenance and manipulation of the queue.
6 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12 type q_data ! The user-defined datatype to store in the queue
16 integer :: depth ! Used by 'search' interpolation method
21 character (len=128) :: units, description, stagger
22 integer :: depth ! Used by 'search' interpolation method
26 type q_item ! Wrapper for item to be stored in the queue
28 type (q_item), pointer :: next
31 type queue ! The queue object, defined by a head and tail pointer
32 type (q_item), pointer :: head, tail
40 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43 ! Purpose: To initialize a queue
44 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
50 type (queue), intent(inout) :: q
59 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
62 ! Purpose: To insert an item in the tail of the queue
63 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
64 subroutine q_insert(q, qitem)
69 type (queue), intent(inout) :: q
70 type (q_data), intent(in) :: qitem
73 type (q_item), pointer :: newitem
78 if (.not.associated(q%tail)) then
86 q%length = q%length + 1
88 end subroutine q_insert
91 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
94 ! Purpose: This function returns FALSE if the queue is empty and TRUE otherwise
95 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
101 type (queue), intent(in) :: q
108 if (associated(q%head) .and. (q%length >= 1)) then
112 end function q_isdata
115 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
118 ! Purpose: To return the item in the head of the queue, without
119 ! actually removing the item
120 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
126 type (queue), intent(in) :: q
129 type (q_data) :: q_peek
131 if (associated(q%head)) then
134 call mprintf(.true.,ERROR,'q_peek(): Trying to peek at an empty queue')
140 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
143 ! Purpose: To return the number of items currently in the queue
144 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
150 type (queue), intent(in) :: q
153 ! type (q_item), pointer :: cursor
158 ! USE THE FOLLOWING TO COUNT THE LENGTH BY ACTUALLY TRAVERSING THE LINKED LIST
159 ! REPRESENTATION OF THE QUEUE
160 ! if (associated(q%head)) then
161 ! q_length = q_length + 1
163 ! do while(associated(cursor%next))
164 ! cursor=>cursor%next
165 ! q_length = q_length + 1
169 end function q_length
172 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
175 ! Purpose: To return the item stored at the head of the queue
176 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
182 type (queue), intent(inout) :: q
185 type (q_data) :: q_remove
186 type (q_item), pointer :: cursor
188 if (associated(q%head)) then
189 if (associated(q%head%next)) then
191 q_remove = q%head%data
195 q_remove = q%head%data
200 q%length = q%length - 1
202 call mprintf(.true.,ERROR,'q_remove(): Trying to remove from an empty queue')
205 end function q_remove
208 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
211 ! Purpose: To free all memory allocated by the queue, thus destroying any
212 ! items that have not been removed
213 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
214 subroutine q_destroy(q)
219 type (queue), intent(inout) :: q
222 type (q_item), pointer :: cursor
226 if (associated(q%head)) then
227 do while(associated(q%head%next))
235 end subroutine q_destroy
237 end module queue_module