1 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
2 | ! Module: queue_module |
---|
3 | ! |
---|
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 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
7 | |
---|
8 | MODULE queue_module |
---|
9 | |
---|
10 | USE module_debug |
---|
11 | |
---|
12 | type q_data ! The user-defined datatype to store in the queue |
---|
13 | integer :: x, y |
---|
14 | character (len=128) :: units, description, stagger |
---|
15 | end type q_data |
---|
16 | |
---|
17 | type q_item ! Wrapper for item to be stored in the queue |
---|
18 | type (q_data) :: data |
---|
19 | type (q_item), pointer :: next |
---|
20 | end type q_item |
---|
21 | |
---|
22 | type queue ! The queue object, defined by a head and tail pointer |
---|
23 | type (q_item), pointer :: head, tail |
---|
24 | integer :: length |
---|
25 | end type queue |
---|
26 | |
---|
27 | CONTAINS |
---|
28 | |
---|
29 | |
---|
30 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
31 | ! Name: q_init |
---|
32 | ! Purpose: To initialize a queue |
---|
33 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
34 | SUBROUTINE q_init(q) |
---|
35 | |
---|
36 | implicit none |
---|
37 | |
---|
38 | ! Arguments |
---|
39 | type (queue), intent(inout) :: q |
---|
40 | |
---|
41 | NULLIFY(q%head) |
---|
42 | NULLIFY(q%tail) |
---|
43 | q%length = 0 |
---|
44 | |
---|
45 | END SUBROUTINE q_init |
---|
46 | |
---|
47 | |
---|
48 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
49 | ! Name: q_insert |
---|
50 | ! Purpose: To insert an item in the tail of the queue |
---|
51 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
52 | SUBROUTINE q_insert(q, qitem) |
---|
53 | |
---|
54 | implicit none |
---|
55 | |
---|
56 | ! Arguments |
---|
57 | type (queue), intent(inout) :: q |
---|
58 | type (q_data), intent(in) :: qitem |
---|
59 | |
---|
60 | ! Local variables |
---|
61 | type (q_item), pointer :: newitem |
---|
62 | |
---|
63 | allocate(newitem) |
---|
64 | newitem%data = qitem |
---|
65 | NULLIFY(newitem%next) |
---|
66 | IF (.not.associated(q%tail)) THEN |
---|
67 | q%head=>newitem |
---|
68 | q%tail=>newitem |
---|
69 | ELSE |
---|
70 | q%tail%next=>newitem |
---|
71 | q%tail=>newitem |
---|
72 | END IF |
---|
73 | |
---|
74 | q%length = q%length + 1 |
---|
75 | |
---|
76 | END SUBROUTINE q_insert |
---|
77 | |
---|
78 | |
---|
79 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
80 | ! Name: q_isdata |
---|
81 | ! Purpose: This function returns FALSE if the queue is empty and TRUE otherwise |
---|
82 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
83 | function q_isdata(q) |
---|
84 | |
---|
85 | implicit none |
---|
86 | |
---|
87 | ! Arguments |
---|
88 | type (queue), intent(in) :: q |
---|
89 | |
---|
90 | ! Local variables |
---|
91 | logical :: q_isdata |
---|
92 | |
---|
93 | q_isdata = .false. |
---|
94 | |
---|
95 | IF (associated(q%head) .and. (q%length >= 1)) THEN |
---|
96 | q_isdata = .true. |
---|
97 | END IF |
---|
98 | |
---|
99 | END function q_isdata |
---|
100 | |
---|
101 | |
---|
102 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
103 | ! Name: q_peek |
---|
104 | ! Purpose: To return the item in the head of the queue, without |
---|
105 | ! actually removing the item |
---|
106 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
107 | function q_peek(q) |
---|
108 | |
---|
109 | implicit none |
---|
110 | |
---|
111 | ! Arguments |
---|
112 | type (queue), intent(in) :: q |
---|
113 | |
---|
114 | ! Local variables |
---|
115 | type (q_data) :: q_peek |
---|
116 | |
---|
117 | IF (associated(q%head)) THEN |
---|
118 | q_peek = q%head%data |
---|
119 | ELSE |
---|
120 | call mprintf(.true.,ERROR,'q_peek(): Trying to peek at an empty queue') |
---|
121 | END IF |
---|
122 | |
---|
123 | END function q_peek |
---|
124 | |
---|
125 | |
---|
126 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
127 | ! Name: q_length |
---|
128 | ! Purpose: To return the number of items currently in the queue |
---|
129 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
130 | FUNCTION q_length(q) |
---|
131 | |
---|
132 | implicit none |
---|
133 | |
---|
134 | ! Arguments |
---|
135 | type (queue), intent(in) :: q |
---|
136 | |
---|
137 | ! Local variables |
---|
138 | !!type (q_item), pointer :: cursor |
---|
139 | integer :: q_length |
---|
140 | |
---|
141 | q_length = q%length |
---|
142 | |
---|
143 | ! USE THE FOLLOWING TO COUNT THE LENGTH BY ACTUALLY TRAVERSING THE LINKED LIST |
---|
144 | ! REPRESENTATION OF THE QUEUE |
---|
145 | ! IF (associated(q%head)) THEN |
---|
146 | ! q_length = q_length + 1 |
---|
147 | ! cursor=>q%head |
---|
148 | ! DO WHILE(associated(cursor%next)) |
---|
149 | ! cursor=>cursor%next |
---|
150 | ! q_length = q_length + 1 |
---|
151 | ! END DO |
---|
152 | ! END IF |
---|
153 | |
---|
154 | END FUNCTION q_length |
---|
155 | |
---|
156 | |
---|
157 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
158 | ! Name: q_remove |
---|
159 | ! Purpose: To return the item stored at the head of the queue |
---|
160 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
161 | function q_remove(q) |
---|
162 | |
---|
163 | implicit none |
---|
164 | |
---|
165 | ! Arguments |
---|
166 | type (queue), intent(inout) :: q |
---|
167 | |
---|
168 | ! Local variables |
---|
169 | type (q_data) :: q_remove |
---|
170 | type (q_item), pointer :: cursor |
---|
171 | |
---|
172 | IF (associated(q%head)) THEN |
---|
173 | IF (associated(q%head%next)) THEN |
---|
174 | cursor=>q%head%next |
---|
175 | q_remove = q%head%data |
---|
176 | deallocate(q%head) |
---|
177 | q%head=>cursor |
---|
178 | ELSE |
---|
179 | q_remove = q%head%data |
---|
180 | deallocate(q%head) |
---|
181 | NULLIFY(q%head) |
---|
182 | NULLIFY(q%tail) |
---|
183 | END IF |
---|
184 | q%length = q%length - 1 |
---|
185 | ELSE |
---|
186 | CALL mprintf(.true.,ERROR,'q_remove(): Trying to remove from an empty queue') |
---|
187 | END IF |
---|
188 | |
---|
189 | END function q_remove |
---|
190 | |
---|
191 | |
---|
192 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
193 | ! Name: q_destroy |
---|
194 | ! Purpose: To free all memory allocated by the queue, thus destroying any |
---|
195 | ! items that have not been removed |
---|
196 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
197 | SUBROUTINE q_destroy(q) |
---|
198 | |
---|
199 | implicit none |
---|
200 | |
---|
201 | ! Arguments |
---|
202 | type (queue), intent(inout) :: q |
---|
203 | |
---|
204 | ! Local variables |
---|
205 | type (q_item), pointer :: cursor |
---|
206 | |
---|
207 | q%length = 0 |
---|
208 | |
---|
209 | IF (associated(q%head)) THEN |
---|
210 | DO WHILE(associated(q%head%next)) |
---|
211 | cursor=>q%head |
---|
212 | q%head=>q%head%next |
---|
213 | deallocate(cursor) |
---|
214 | END DO |
---|
215 | deallocate(q%head) |
---|
216 | END IF |
---|
217 | |
---|
218 | END SUBROUTINE q_destroy |
---|
219 | |
---|
220 | END MODULE queue_module |
---|