(git:3d5d2e5)
Loading...
Searching...
No Matches
cp_linked_list_fm.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
10 USE cp_fm_types, ONLY: cp_fm_p_type,&
12#include "../base/base_uses.f90"
13
14
15! **************************************************************************************************
16!> \brief describes a generic linked list template.
17!> Linked list are supposed to always use pointers to the nodes for
18!> basically everything: a pointer to the node is a list, an element of
19!> the list, an iterator between the elment of the list.
20!> An empty list is represented by an unassociated pointer.
21!> \par History
22!> 1.2002 created
23!> 4.2002 changed most of the methods, by making access to the list
24!> always through pointers (identifying pointer, list and iterators)
25!> 6.2004 removed %initialized from list elements
26!> \author Fawzi Mohamed
27! **************************************************************************************************
28 IMPLICIT NONE
29 PRIVATE
30
31 LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .true.
32
33
34!API type
36
37!API common methods
38 PUBLIC :: cp_create, cp_dealloc, cp_next
39!API special get
41!API special set
42 PUBLIC :: cp_set_element_at
43!API structure manipulation
45
46 !API underlying routines
47
57
58! creation of an object (from a pointer)
59 INTERFACE cp_create
60 MODULE PROCEDURE cp_sll_fm_create
61 END INTERFACE
62! destruction of an object (from a pointer)
63 INTERFACE cp_dealloc
64 MODULE PROCEDURE cp_sll_fm_dealloc
65 END INTERFACE
66! iterates to the next element
67 INTERFACE cp_next
68 MODULE PROCEDURE cp_sll_fm_next
69 END INTERFACE
70! returns the first element
72 MODULE PROCEDURE cp_sll_fm_get_first_el
73 END INTERFACE
74! returns the rest of the list
75 INTERFACE cp_get_rest
76 MODULE PROCEDURE cp_sll_fm_get_rest
77 END INTERFACE
78! returns the length of the list
79 INTERFACE cp_get_length
80 MODULE PROCEDURE cp_sll_fm_get_length
81 END INTERFACE
82! returns the element at the given position
84 MODULE PROCEDURE cp_sll_fm_get_el_at
85 END INTERFACE
86! sets the element at the given position
88 MODULE PROCEDURE cp_sll_fm_set_el_at
89 END INTERFACE
90! inserts one element call cp_insert(list,element,...)
91 INTERFACE cp_insert
92 MODULE PROCEDURE cp_sll_fm_insert_el
93 END INTERFACE
94 INTERFACE cp_insert_at
95 MODULE PROCEDURE cp_sll_fm_insert_el_at
96 END INTERFACE
97! removes an element
98 INTERFACE cp_remove_el
99 MODULE PROCEDURE cp_sll_fm_rm_first_el, &
101 END INTERFACE
102! removes the first el
104 MODULE PROCEDURE cp_sll_fm_rm_first_el
105 END INTERFACE
106! remove all the elements
108 MODULE PROCEDURE cp_sll_fm_rm_all_el
109 END INTERFACE
110! transorms the list in array
111 INTERFACE cp_to_array
112 MODULE PROCEDURE cp_sll_fm_to_array
113 END INTERFACE
114
115! **************************************************************************************************
116!> \brief represent a single linked list that stores pointers to the elements
117!> \param first_el the element that is stored in this node.
118!> \param rest the rest of the list
119!>
120!> \param empty true if the list pointer is not associated, if it points to
121!> to a not it is always false (as there is at least the
122!> first_el in the list)
123!> \param length the number of elements in the list
124!> \note
125!> List are alway accessed through pointers, so every node of the
126!> linked list can be seen as a list, its first element
127!> a pointer to the position before itself, in a very natural way:
128!> all the insertions take place before the actual element, and
129!> you still can insert an element at the end.
130!> This way I could identify nodes, lists and pointers between the
131!> elements of the list.
132!> Indexing is 1 based.
133!> \par History
134!> none
135!> \author Fawzi Mohamed
136! **************************************************************************************************
138 type(cp_fm_type),pointer :: first_el => null()
139 TYPE(cp_sll_fm_type), POINTER :: rest => null()
140 END TYPE cp_sll_fm_type
141
142! **************************************************************************************************
143!> \brief pointer to a linked list (to make arrays of pointers)
144!> \param list the pointer to the list
145!> \par History
146!> none
147!> \author Fawzi Mohamed
148! **************************************************************************************************
150 TYPE(cp_sll_fm_type), POINTER :: list => null()
151 END TYPE cp_sll_fm_p_type
152
153 CONTAINS
154
155 ! this comment prevents trailing whitespace
156
157! =========== creation / destruction ========
158
159! **************************************************************************************************
160!> \brief allocates and initializes a single linked list
161!> \param sll the single linked list to initialize
162!> \param first_el the first element of this list
163!> \param rest the following elements (if not given: empty)
164!> \par History
165!> none
166!> \author Fawzi Mohamed
167! **************************************************************************************************
168 SUBROUTINE cp_sll_fm_create(sll, first_el, rest)
169 TYPE(cp_sll_fm_type), POINTER :: sll
170 type(cp_fm_type),pointer, OPTIONAL :: first_el
171 TYPE(cp_sll_fm_type), OPTIONAL, &
172 POINTER :: rest
173
174 IF (.NOT. PRESENT(first_el)) THEN
175 NULLIFY (sll)
176 IF (PRESENT(rest)) sll => rest
177 ELSE
178 ALLOCATE (sll)
179 sll%first_el =>first_el
180 NULLIFY (sll%rest)
181 IF (PRESENT(rest)) sll%rest => rest
182 END IF
183 END SUBROUTINE cp_sll_fm_create
184
185! **************************************************************************************************
186!> \brief deallocates the singly linked list starting at sll.
187!> Does not work if loops are present!
188!> \param sll the list to be deallocated
189!> \par History
190!> none
191!> \author Fawzi Mohamed
192!> \note
193!> does not deallocate the elements that are stored in the list
194!> check more?
195! **************************************************************************************************
196 SUBROUTINE cp_sll_fm_dealloc(sll)
197 TYPE(cp_sll_fm_type), POINTER :: sll
198
199 CALL cp_sll_fm_rm_all_el(sll)
200 END SUBROUTINE cp_sll_fm_dealloc
201
202! * low-level *
203
204! **************************************************************************************************
205!> \brief deallocates a node of a singly linked list (low level)
206!> \param sll the node to be deallocated
207!> \par History
208!> none
209!> \author Fawzi Mohamed
210! **************************************************************************************************
211 SUBROUTINE cp_sll_fm_dealloc_node(sll)
212 TYPE(cp_sll_fm_type), POINTER :: sll
213
214 DEALLOCATE (sll)
215 END SUBROUTINE cp_sll_fm_dealloc_node
216
217! ============= get/set ============
218
219! **************************************************************************************************
220!> \brief returns the first element stored in the list
221!> \param sll the single linked list to get the element from
222!> \return ...
223!> \par History
224!> none
225!> \author Fawzi Mohamed
226! **************************************************************************************************
227 FUNCTION cp_sll_fm_get_first_el(sll) RESULT(res)
228 TYPE(cp_sll_fm_type), POINTER :: sll
229 type(cp_fm_type),pointer :: res
230
231 res =>sll%first_el
232 END FUNCTION cp_sll_fm_get_first_el
233
234! **************************************************************************************************
235!> \brief returns the rest of the list
236!> \param sll the single linked list to get the rest from
237!> \param iter how many times the call to rest should be iterated,
238!> defaults to 1; -1 means till end of the list.
239!> \return ...
240!> \par History
241!> none
242!> \author Fawzi Mohamed
243!> \note
244!> split the case iter=1 to make it more optimized?
245! **************************************************************************************************
246 FUNCTION cp_sll_fm_get_rest(sll, iter) RESULT(res)
247 TYPE(cp_sll_fm_type), POINTER :: sll
248 INTEGER, OPTIONAL :: iter
249 TYPE(cp_sll_fm_type), POINTER :: res
250
251 INTEGER :: i
252
253 IF (.NOT. ASSOCIATED(sll)) THEN
254 NULLIFY (res)
255 ELSE
256 IF (PRESENT(iter)) THEN
257 res => sll
258 DO i = 1, iter
259 IF (ASSOCIATED(res%rest)) THEN
260 res => res%rest
261 ELSE
262 cpabort("tried to go past end")
263 END IF
264 END DO
265 IF (iter == -1) THEN
266 DO
267 IF (.NOT. ASSOCIATED(res%rest)) EXIT
268 res => res%rest
269 END DO
270 END IF
271 ELSE
272 res => sll%rest ! make the common case fast...
273 END IF
274 END IF
275 END FUNCTION cp_sll_fm_get_rest
276
277! **************************************************************************************************
278!> \brief returns the length of the list
279!> \param sll the list you want to know the length of
280!> \return ...
281!> \par History
282!> none
283!> \author Fawzi Mohamed
284!> \note
285!> slow (O(n))
286! **************************************************************************************************
287 FUNCTION cp_sll_fm_get_length(sll) RESULT(res)
288 TYPE(cp_sll_fm_type), POINTER :: sll
289 INTEGER :: res
290
291 TYPE(cp_sll_fm_type), POINTER :: iterator
292
293 res = 0
294 iterator => sll
295 DO
296 IF (ASSOCIATED(iterator)) THEN
297 res = res + 1
298 iterator => iterator%rest
299 ELSE
300 EXIT
301 END IF
302 END DO
303 END FUNCTION cp_sll_fm_get_length
304
305! **************************************************************************************************
306!> \brief returns the element at the given index
307!> \param sll the list you get the element from
308!> \param index the position of the element (stating at 1)
309!> \return ...
310!> \par History
311!> none
312!> \author Fawzi Mohamed
313!> \note
314!> slow (O(index))
315! **************************************************************************************************
316 FUNCTION cp_sll_fm_get_el_at(sll, index) RESULT(res)
317 TYPE(cp_sll_fm_type), POINTER :: sll
318 INTEGER, INTENT(IN) :: index
319 type(cp_fm_type),pointer :: res
320
321 TYPE(cp_sll_fm_type), POINTER :: pos
322
323 IF (index == -1) THEN
324 pos => cp_sll_fm_get_rest(sll, iter=-1)
325 ELSE
326 pos => cp_sll_fm_get_rest(sll, iter=index - 1)
327 END IF
328 cpassert(ASSOCIATED(pos))
329
330 res =>pos%first_el
331 END FUNCTION cp_sll_fm_get_el_at
332
333! **************************************************************************************************
334!> \brief sets the element at the given index
335!> \param sll the list you get the element from
336!> \param index the position of the element (stating at 1)
337!> -1 means at the end
338!> \param value the new element
339!> \par History
340!> none
341!> \author Fawzi Mohamed
342!> \note
343!> slow (O(index))
344! **************************************************************************************************
345 SUBROUTINE cp_sll_fm_set_el_at(sll, index, value)
346 TYPE(cp_sll_fm_type), POINTER :: sll
347 INTEGER, INTENT(IN) :: index
348 type(cp_fm_type),pointer :: value
349
350 TYPE(cp_sll_fm_type), POINTER :: pos
351
352 IF (index == -1) THEN
353 pos => cp_sll_fm_get_rest(sll, iter=-1)
354 ELSE
355 pos => cp_sll_fm_get_rest(sll, iter=index - 1)
356 END IF
357 cpassert(ASSOCIATED(pos))
358
359 pos%first_el =>value
360 END SUBROUTINE cp_sll_fm_set_el_at
361
362! * iteration *
363
364! **************************************************************************************************
365!> \brief returns true if the actual element is valid (i.e. iterator ont at end)
366!> moves the iterator to the next element
367!> \param iterator iterator that moves along the list
368!> \param el_att the actual element (valid only if the function returns true)
369!> \return ...
370!> \par History
371!> none
372!> \author Fawzi Mohamed
373! **************************************************************************************************
374 FUNCTION cp_sll_fm_next(iterator, el_att) RESULT(res)
375 TYPE(cp_sll_fm_type), POINTER :: iterator
376 type(cp_fm_type),pointer, OPTIONAL :: el_att
377 LOGICAL :: res
378
379 IF (ASSOCIATED(iterator)) THEN
380 res = .true.
381 IF (PRESENT(el_att)) el_att =>iterator%first_el
382 iterator => iterator%rest
383 ELSE
384 res = .false.
385 END IF
386 END FUNCTION cp_sll_fm_next
387
388! ============ structure modifications ============
389
390! **************************************************************************************************
391!> \brief insert an element at the beginning of the list
392!> \param sll the single linked list point at the beginning of which
393!> you want to add the element
394!> \param el the element to add
395!> \par History
396!> none
397!> \author Fawzi Mohamed
398!> \note
399!> fast (O(1))
400! **************************************************************************************************
401 SUBROUTINE cp_sll_fm_insert_el(sll, el)
402 TYPE(cp_sll_fm_type), POINTER :: sll
403
404 type(cp_fm_type),pointer:: el
405
406 TYPE(cp_sll_fm_type), POINTER :: newSlot
407
408 NULLIFY (newslot)
409
410 CALL cp_sll_fm_create(newslot, first_el=el, &
411 rest=sll)
412 sll => newslot
413 END SUBROUTINE cp_sll_fm_insert_el
414
415! **************************************************************************************************
416!> \brief remove the first element of the linked list
417!> \param sll the list whose first element has to be removed
418!> \par History
419!> none
420!> \author Fawzi Mohamed
421!> \note
422!> fast (O(1))
423! **************************************************************************************************
424 SUBROUTINE cp_sll_fm_rm_first_el(sll)
425 TYPE(cp_sll_fm_type), POINTER :: sll
426
427 TYPE(cp_sll_fm_type), POINTER :: node_to_rm
428
429 node_to_rm => sll
430
431 IF (ASSOCIATED(sll)) THEN
432 sll => sll%rest
433 CALL cp_sll_fm_dealloc_node(node_to_rm)
434 ELSE
435 cpabort("tried to remove first el of an empty list")
436 END IF
437 END SUBROUTINE cp_sll_fm_rm_first_el
438
439! **************************************************************************************************
440!> \brief inserts the element at the given index
441!> \param sll the list you get the element from
442!> \param el the new element
443!> \param index the position of the element (stating at 1).
444!> If it is -1, it means at end
445!> \par History
446!> none
447!> \author Fawzi Mohamed
448!> \note
449!> slow (O(index))
450! **************************************************************************************************
451 SUBROUTINE cp_sll_fm_insert_el_at(sll, el, index)
452 TYPE(cp_sll_fm_type), POINTER :: sll
453 type(cp_fm_type),pointer :: el
454 INTEGER, INTENT(IN) :: index
455
456 TYPE(cp_sll_fm_type), POINTER :: pos
457
458 IF (index == 1) THEN
459 CALL cp_sll_fm_insert_el(sll, el)
460 ELSE
461 IF (index == -1) THEN
462 pos => cp_sll_fm_get_rest(sll, iter=-1)
463 ELSE
464 pos => cp_sll_fm_get_rest(sll, iter=index - 2)
465 END IF
466 cpassert(ASSOCIATED(pos))
467 CALL cp_sll_fm_insert_el(pos%rest, el)
468 END IF
469 END SUBROUTINE cp_sll_fm_insert_el_at
470
471! **************************************************************************************************
472!> \brief removes the element at the given index
473!> \param sll the list you get the element from
474!> \param index the position of the element (stating at 1)
475!> \par History
476!> none
477!> \author Fawzi Mohamed
478!> \note
479!> slow (O(index))
480! **************************************************************************************************
481 SUBROUTINE cp_sll_fm_rm_el_at(sll, index)
482 TYPE(cp_sll_fm_type), POINTER :: sll
483 INTEGER, INTENT(IN) :: index
484
485 TYPE(cp_sll_fm_type), POINTER :: pos
486
487 IF (index == 1) THEN
488 CALL cp_sll_fm_rm_first_el(sll)
489 ELSE
490 IF (index == -1) THEN
491 pos => cp_sll_fm_get_rest(sll, iter=-1)
492 ELSE
493 pos => cp_sll_fm_get_rest(sll, iter=index - 2)
494 END IF
495 cpassert(ASSOCIATED(pos))
496 CALL cp_sll_fm_rm_first_el(pos%rest)
497 END IF
498 END SUBROUTINE cp_sll_fm_rm_el_at
499
500! **************************************************************************************************
501!> \brief removes all the elements from the list
502!> \param sll the list that should be removed
503!> \par History
504!> none
505!> \author Fawzi Mohamed
506!> \note
507!> check more?
508! **************************************************************************************************
509 SUBROUTINE cp_sll_fm_rm_all_el(sll)
510 TYPE(cp_sll_fm_type), POINTER :: sll
511
512 TYPE(cp_sll_fm_type), POINTER :: actual_node, next_node
513
514 actual_node => sll
515 DO
516 IF (.NOT. ASSOCIATED(actual_node)) EXIT
517 next_node => actual_node%rest
518 CALL cp_sll_fm_dealloc_node(actual_node)
519 actual_node => next_node
520 END DO
521 NULLIFY (sll)
522 END SUBROUTINE cp_sll_fm_rm_all_el
523
524! **************************************************************************************************
525!> \brief returns a newly allocated array with the same contents as
526!> the linked list
527!> \param sll the list to transform in array
528!> \return ...
529!> \par History
530!> 07.2002 created [fawzi]
531!> \author Fawzi Mohamed
532! **************************************************************************************************
533 FUNCTION cp_sll_fm_to_array(sll) RESULT(res)
534 TYPE(cp_sll_fm_type), POINTER :: sll
535 type(cp_fm_p_type), DIMENSION(:), POINTER :: res
536
537 INTEGER :: i, len
538 LOGICAL :: ok
539 TYPE(cp_sll_fm_type), POINTER :: iter
540
541 len = cp_sll_fm_get_length(sll)
542 ALLOCATE (res(len))
543 iter => sll
544 DO i = 1, len
545 res(i) %matrix=>iter%first_el
546 ok = cp_sll_fm_next(iter)
547 cpassert(ok .OR. i == len)
548 END DO
549 END FUNCTION cp_sll_fm_to_array
550END MODULE cp_linked_list_fm
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
integer function, public cp_sll_fm_get_length(sll)
returns the length of the list
subroutine, public cp_sll_fm_insert_el(sll, el)
insert an element at the beginning of the list
subroutine, public cp_sll_fm_rm_el_at(sll, index)
removes the element at the given index
subroutine, public cp_sll_fm_rm_all_el(sll)
removes all the elements from the list
subroutine, public cp_sll_fm_create(sll, first_el, rest)
allocates and initializes a single linked list
type(cp_sll_fm_type) function, pointer, public cp_sll_fm_get_rest(sll, iter)
returns the rest of the list
logical function, public cp_sll_fm_next(iterator, el_att)
returns true if the actual element is valid (i.e. iterator ont at end) moves the iterator to the next...
subroutine, public cp_sll_fm_insert_el_at(sll, el, index)
inserts the element at the given index
type(cp_fm_p_type) function, dimension(:), pointer, public cp_sll_fm_to_array(sll)
returns a newly allocated array with the same contents as the linked list
type(cp_fm_type) function, pointer, public cp_sll_fm_get_first_el(sll)
returns the first element stored in the list
subroutine, public cp_sll_fm_dealloc(sll)
deallocates the singly linked list starting at sll. Does not work if loops are present!
type(cp_fm_type) function, pointer, public cp_sll_fm_get_el_at(sll, index)
returns the element at the given index
subroutine, public cp_sll_fm_set_el_at(sll, index, value)
sets the element at the given index
subroutine, public cp_sll_fm_rm_first_el(sll)
remove the first element of the linked 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
just to build arrays of pointers to matrices
represent a full matrix
pointer to a linked list (to make arrays of pointers)
represent a single linked list that stores pointers to the elements