(git:b2ae3e3)
Loading...
Searching...
No Matches
list_routinestat.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief An array-based list which grows on demand.
10!> When the internal array is full, a new array of twice the size will be
11!> allocated and the items are copied over.
12!>
13!> This list can also be used as a stack.
14!> Have look at list_push(), list_pop() and list_peek().
15!> \par History
16!> 12.2012 first version [ole]
17!> \author Ole Schuett
18! **************************************************************************************************
19
22
23
24#include "../base/base_uses.f90"
25 IMPLICIT NONE
26 PRIVATE
27
31
32!this is an internal type
33 TYPE private_item_type_routinestat
34 PRIVATE
35 TYPE(routine_stat_type), POINTER :: value => null()
36 END TYPE private_item_type_routinestat
37
38!this is an internal type
39 TYPE private_item_p_type_routinestat
40 PRIVATE
41 TYPE(private_item_type_routinestat), POINTER :: p => null()
42 END TYPE private_item_p_type_routinestat
43
44! this is the public type, which holds a list-instance
46 PRIVATE
47 TYPE(private_item_p_type_routinestat), DIMENSION(:), POINTER :: arr => null()
48 INTEGER :: size = -1
50
51 CONTAINS
52
53! **************************************************************************************************
54!> \brief Test if the given list has been initialized.
55!> \param list ...
56!> \return ...
57!> \par History
58!> 12.2012 created [ole]
59!> \author Ole Schuett
60! **************************************************************************************************
61 FUNCTION list_routinestat_isready(list) RESULT(res)
62 TYPE(list_routinestat_type), intent(in) :: list
63 LOGICAL :: res
64 res = ASSOCIATED(list%arr)
65 END FUNCTION list_routinestat_isready
66
67! **************************************************************************************************
68!> \brief Allocates the internal data-structures of the given list.
69!> This has to be called before any of the other routines.
70!> For deallocation call list_[valuetype]_destroy.
71!> \param list ...
72!> \param initial_capacity The initial size of the internal array (default=11).
73!> \par History
74!> 12.2012 created [ole]
75!> \author Ole Schuett
76! **************************************************************************************************
77 SUBROUTINE list_routinestat_init(list, initial_capacity)
78 TYPE(list_routinestat_type), intent(inout) :: list
79 INTEGER, INTENT(in), OPTIONAL :: initial_capacity
80 INTEGER :: stat
81 INTEGER :: initial_capacity_
82
83 initial_capacity_ = 11
84 If (PRESENT(initial_capacity)) initial_capacity_ = initial_capacity
85
86 IF (initial_capacity_ < 0) THEN
87 cpabort("list_routinestat_create: initial_capacity < 0")
88 END IF
89
90 IF (ASSOCIATED(list%arr)) THEN
91 cpabort("list_routinestat_create: list is already initialized.")
92 END IF
93
94 ALLOCATE (list%arr(initial_capacity_), stat=stat)
95 IF (stat /= 0) THEN
96 cpabort("list_routinestat_init: allocation failed")
97 END IF
98
99 list%size = 0
100 END SUBROUTINE list_routinestat_init
101
102! **************************************************************************************************
103!> \brief Deallocated the internal data-structures of the given list.
104!> Caution: If the stored values are pointers, their targets will
105!> not get deallocated by this routine.
106!> \param list ...
107!> \par History
108!> 12.2012 created [ole]
109!> \author Ole Schuett
110! **************************************************************************************************
112 TYPE(list_routinestat_type), intent(inout) :: list
113 INTEGER :: i
114 IF (.not. ASSOCIATED(list%arr)) THEN
115 cpabort("list_routinestat_destroy: list is not initialized.")
116 END IF
117
118 do i = 1, list%size
119 deallocate (list%arr(i)%p)
120 end do
121 deallocate (list%arr)
122 list%size = -1
123 END SUBROUTINE list_routinestat_destroy
124
125! **************************************************************************************************
126!> \brief Assings the given value to the given position in the list.
127!> Thereby, the former value at that position gets overwritten.
128!> If the position is out of bounds, the program stops.
129!> \param list ...
130!> \param value ...
131!> \param pos Position in the list - musst fulfill 0 < pos < list_size+1.
132!> \par History
133!> 12.2012 created [ole]
134!> \author Ole Schuett
135! **************************************************************************************************
136 SUBROUTINE list_routinestat_set(list, value, pos)
137 TYPE(list_routinestat_type), intent(inout) :: list
138 TYPE(routine_stat_type), POINTER, intent(in) :: value
139 INTEGER, intent(in) :: pos
140 IF (.not. ASSOCIATED(list%arr)) THEN
141 cpabort("list_routinestat_set: list is not initialized.")
142 END IF
143 IF (pos < 1) THEN
144 cpabort("list_routinestat_set: pos < 1")
145 END IF
146 IF (pos > list%size) THEN
147 cpabort("list_routinestat_set: pos > size")
148 END IF
149 list%arr(pos)%p%value =>value
150 END SUBROUTINE list_routinestat_set
151
152! **************************************************************************************************
153!> \brief Appends the given value at the end of the list.
154!> \param list ...
155!> \param value ...
156!> \par History
157!> 12.2012 created [ole]
158!> \author Ole Schuett
159! **************************************************************************************************
160 SUBROUTINE list_routinestat_push(list, value)
161 TYPE(list_routinestat_type), intent(inout) :: list
162 TYPE(routine_stat_type), POINTER, intent(in) :: value
163 INTEGER :: stat
164
165 IF (.not. ASSOCIATED(list%arr)) THEN
166 cpabort("list_routinestat_push: list is not initialized.")
167 END IF
168 IF (list%size == size(list%arr)) THEN
169 CALL change_capacity_routinestat (list, 2*size(list%arr) + 1)
170 END IF
171
172 list%size = list%size + 1
173 ALLOCATE (list%arr(list%size)%p, stat=stat)
174 IF (stat /= 0) THEN
175 cpabort("list_routinestat_push: allocation failed")
176 END IF
177 list%arr(list%size)%p%value =>value
178 END SUBROUTINE list_routinestat_push
179
180! **************************************************************************************************
181!> \brief Inserts the given value at the givenn position within the list.
182!> Values which lay behind the insertion-position move one position up.
183!> \param list ...
184!> \param value ...
185!> \param pos Position in the list - musst fulfill 0 < pos < list_size+2 .
186!> \par History
187!> 12.2012 created [ole]
188!> \author Ole Schuett
189! **************************************************************************************************
190 SUBROUTINE list_routinestat_insert(list, value, pos)
191 TYPE(list_routinestat_type), intent(inout) :: list
192 TYPE(routine_stat_type), POINTER, intent(in) :: value
193 INTEGER, intent(in) :: pos
194 INTEGER :: i, stat
195
196 IF (.not. ASSOCIATED(list%arr)) THEN
197 cpabort("list_routinestat_insert: list is not initialized.")
198 END IF
199 IF (pos < 1) THEN
200 cpabort("list_routinestat_insert: pos < 1")
201 END IF
202 IF (pos > list%size + 1) THEN
203 cpabort("list_routinestat_insert: pos > size+1")
204 END IF
205
206 if (list%size == size(list%arr)) THEN
207 call change_capacity_routinestat (list, 2*size(list%arr) + 1)
208 END IF
209
210 list%size = list%size + 1
211 do i = list%size, pos + 1, -1
212 list%arr(i)%p => list%arr(i - 1)%p
213 end do
214
215 ALLOCATE (list%arr(pos)%p, stat=stat)
216 IF (stat /= 0) THEN
217 cpabort("list_routinestat_insert: allocation failed.")
218 END IF
219 list%arr(pos)%p%value =>value
220 END SUBROUTINE list_routinestat_insert
221
222! **************************************************************************************************
223!> \brief Returns the last element in the list.
224!> Is equivalent to: list_routinestat_get(list, list_routinestat_size(list))
225!> \param list ...
226!> \return ...
227!> \par History
228!> 12.2012 created [ole]
229!> \author Ole Schuett
230! **************************************************************************************************
231 FUNCTION list_routinestat_peek(list) RESULT(value)
232 TYPE(list_routinestat_type), intent(inout) :: list
233 TYPE(routine_stat_type), POINTER :: value
234
235 IF (.not. ASSOCIATED(list%arr)) THEN
236 cpabort("list_routinestat_peek: list is not initialized.")
237 END IF
238 IF (list%size < 1) THEN
239 cpabort("list_routinestat_peek: list is empty.")
240 END IF
241
242 value =>list%arr(list%size)%p%value
243 END FUNCTION list_routinestat_peek
244
245! **************************************************************************************************
246!> \brief Returns the last element in the list and removes it.
247!> Is equivialent to:
248!> value = list_routinestat_get(list, list_routinestat_size(list))
249!> call list_routinestat_del(list, list_routinestat_size(list))
250!>
251!> \param list ...
252!> \return ...
253!> \par History
254!> 12.2012 created [ole]
255!> \author Ole Schuett
256! **************************************************************************************************
257 FUNCTION list_routinestat_pop(list) RESULT(value)
258 TYPE(list_routinestat_type), intent(inout) :: list
259 TYPE(routine_stat_type), POINTER :: value
260
261 IF (.NOT. ASSOCIATED(list%arr)) THEN
262 cpabort("list_routinestat_pop: list is not initialized.")
263 END IF
264 IF (list%size < 1) THEN
265 cpabort("list_routinestat_pop: list is empty.")
266 END IF
267
268 value =>list%arr(list%size)%p%value
269 deallocate (list%arr(list%size)%p)
270 list%size = list%size - 1
271 END FUNCTION list_routinestat_pop
272
273! **************************************************************************************************
274!> \brief Removes all values from the list. The list itself is not deallocated.
275!> \param list ...
276!> \par History
277!> 12.2012 created [ole]
278!> \author Ole Schuett
279! **************************************************************************************************
280 SUBROUTINE list_routinestat_clear(list)
281 TYPE(list_routinestat_type), intent(inout) :: list
282 INTEGER :: i
283
284 IF (.not. ASSOCIATED(list%arr)) THEN
285 cpabort("list_routinestat_clear: list is not initialized.")
286 END IF
287
288 DO i = 1, list%size
289 deallocate (list%arr(i)%p)
290 END DO
291 list%size = 0
292 END SUBROUTINE list_routinestat_clear
293
294!
295! **************************************************************************************************
296!> \brief Returns the value at the given position from the list.
297!> \param list ...
298!> \param pos Position in the list - musst fulfill 0 < pos < list_size+1 .
299!> \return ...
300!> \par History
301!> 12.2012 created [ole]
302!> \author Ole Schuett
303! **************************************************************************************************
304 FUNCTION list_routinestat_get(list, pos) RESULT(value)
305 TYPE(list_routinestat_type), intent(in) :: list
306 INTEGER, intent(in) :: pos
307 TYPE(routine_stat_type), POINTER :: value
308
309 IF (.NOT. ASSOCIATED(list%arr)) THEN
310 cpabort("list_routinestat_get: list is not initialized.")
311 END IF
312 IF (pos < 1) THEN
313 cpabort("list_routinestat_get: pos < 1")
314 END IF
315 IF (pos > list%size) THEN
316 cpabort("list_routinestat_get: pos > size")
317 END IF
318
319 value =>list%arr(pos)%p%value
320
321 END FUNCTION list_routinestat_get
322
323! **************************************************************************************************
324!> \brief Removes the value at the given position from the list.
325!> \param list ...
326!> \param pos Position in the list - musst fulfill 0 < pos < list_size+1 .
327!> \par History
328!> 12.2012 created [ole]
329!> \author Ole Schuett
330! **************************************************************************************************
331 SUBROUTINE list_routinestat_del(list, pos)
332 TYPE(list_routinestat_type), intent(inout) :: list
333 INTEGER, intent(in) :: pos
334 INTEGER :: i
335
336 IF (.NOT. ASSOCIATED(list%arr)) THEN
337 cpabort("list_routinestat_del: list is not initialized.")
338 END IF
339 IF (pos < 1) THEN
340 cpabort("list_routinestat_det: pos < 1")
341 END IF
342 IF (pos > list%size) THEN
343 cpabort("list_routinestat_det: pos > size")
344 END IF
345
346 deallocate (list%arr(pos)%p)
347 DO i = pos, list%size - 1
348 list%arr(i)%p => list%arr(i + 1)%p
349 END DO
350
351 list%size = list%size - 1
352
353 END SUBROUTINE list_routinestat_del
354
355! **************************************************************************************************
356!> \brief Returns the current size of the list.
357!> \param list ...
358!> \return ...
359!> \par History
360!> 12.2012 created [ole]
361!> \author Ole Schuett
362! **************************************************************************************************
363 FUNCTION list_routinestat_size(list) RESULT(size)
364 TYPE(list_routinestat_type), intent(in) :: list
365 INTEGER :: size
366
367 IF (.NOT. ASSOCIATED(list%arr)) THEN
368 cpabort("list_routinestat_size: list is not initialized.")
369 END IF
370
371 size = list%size
372 END FUNCTION list_routinestat_size
373
374! **************************************************************************************************
375!> \brief Internal routine for changing the size of the internal array.
376!> \param list ...
377!> \param new_capacity ...
378!> \par History
379!> 12.2012 created [ole]
380!> \author Ole Schuett
381! **************************************************************************************************
382 SUBROUTINE change_capacity_routinestat (list, new_capacity)
383 TYPE(list_routinestat_type), intent(inout) :: list
384 INTEGER, intent(in) :: new_capacity
385 INTEGER :: i, new_cap, stat
386 TYPE(private_item_p_type_routinestat), DIMENSION(:), POINTER :: old_arr
387
388 new_cap = new_capacity
389 IF (new_cap < 0) THEN
390 cpabort("list_routinestat_change_capacity: new_capacity < 0")
391 END IF
392 IF (new_cap < list%size) THEN
393 cpabort("list_routinestat_change_capacity: new_capacity < size")
394 END IF
395 IF (new_cap > huge(i)) THEN
396 IF (size(list%arr) == huge(i)) THEN
397 cpabort("list_routinestat_change_capacity: list has reached integer limit.")
398 END IF
399 new_cap = huge(i) ! grow as far as possible
400 END IF
401
402 old_arr => list%arr
403 allocate (list%arr(new_cap), stat=stat)
404 IF (stat /= 0) THEN
405 cpabort("list_routinestat_change_capacity: allocation failed")
406 END IF
407
408 DO i = 1, list%size
409 allocate (list%arr(i)%p, stat=stat)
410 IF (stat /= 0) THEN
411 cpabort("list_routinestat_change_capacity: allocation failed")
412 END IF
413 list%arr(i)%p%value =>old_arr(i)%p%value
414 DEALLOCATE (old_arr(i)%p)
415 END DO
416 DEALLOCATE (old_arr)
417
418 END SUBROUTINE change_capacity_routinestat
419
420END MODULE list_routinestat
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
subroutine, public list_routinestat_init(list, initial_capacity)
Allocates the internal data-structures of the given list. This has to be called before any of the oth...
integer function, public list_routinestat_size(list)
Returns the current size of the list.
subroutine, public list_routinestat_set(list, value, pos)
Assings the given value to the given position in the list. Thereby, the former value at that position...
logical function, public list_routinestat_isready(list)
Test if the given list has been initialized.
subroutine, public list_routinestat_insert(list, value, pos)
Inserts the given value at the givenn position within the list. Values which lay behind the insertion...
subroutine, public list_routinestat_clear(list)
Removes all values from the list. The list itself is not deallocated.
type(routine_stat_type) function, pointer, public list_routinestat_pop(list)
Returns the last element in the list and removes it. Is equivialent to: value = list_routinestat_get(...
subroutine, public list_routinestat_push(list, value)
Appends the given value at the end of the list.
subroutine, public list_routinestat_destroy(list)
Deallocated the internal data-structures of the given list. Caution: If the stored values are pointer...
subroutine, public list_routinestat_del(list, pos)
Removes the value at the given position from the list.
type(routine_stat_type) function, pointer, public list_routinestat_peek(list)
Returns the last element in the list. Is equivalent to: list_routinestat_get(list,...
type(routine_stat_type) function, pointer, public list_routinestat_get(list, pos)
Returns the value at the given position from the list.
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
Definition list.F:24
Types used by timings.F and timings_report.F The types in this module are used within dict or list,...