SURFEX  V8_0
Surfex V8_0 release
 All Classes Files Functions Variables
modi_glt_generic_list.F90
Go to the documentation of this file.
1 !SFX_LIC Copyright 1994-2014 CNRS, Meteo-France and Universite Paul Sabatier
2 !SFX_LIC This is part of the SURFEX software governed by the CeCILL-C licence
3 !SFX_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
4 !SFX_LIC for details. version 1.
5 !GLT_LIC The GELATO model is a seaice model used in stand-alone or embedded mode.
6 !GLT_LIC It has been developed by Meteo-France. The holder of GELATO is Meteo-France.
7 !GLT_LIC
8 !GLT_LIC This software is governed by the CeCILL-C license under French law and biding
9 !GLT_LIC by the rules of distribution of free software. See the CeCILL-C_V1-en.txt
10 !GLT_LIC (English) and CeCILL-C_V1-fr.txt (French) for details. The CeCILL is a free
11 !GLT_LIC software license, explicitly compatible with the GNU GPL
12 !GLT_LIC (see http://www.gnu.org/licenses/license-list.en.html#CeCILL)
13 !GLT_LIC
14 !GLT_LIC The CeCILL-C licence agreement grants users the right to modify and re-use the
15 !GLT_LIC software governed by this free software license. The exercising of this right
16 !GLT_LIC is conditional upon the obligation to make available to the community the
17 !GLT_LIC modifications made to the source code of the software so as to contribute to
18 !GLT_LIC its evolution.
19 !GLT_LIC
20 !GLT_LIC In consideration of access to the source code and the rights to copy, modify
21 !GLT_LIC and redistribute granted by the license, users are provided only with a limited
22 !GLT_LIC warranty and the software's author, the holder of the economic rights, and the
23 !GLT_LIC successive licensors only have limited liability. In this respect, the risks
24 !GLT_LIC associated with loading, using, modifying and/or developing or reproducing the
25 !GLT_LIC software by the user are brought to the user's attention, given its Free
26 !GLT_LIC Software status, which may make it complicated to use, with the result that its
27 !GLT_LIC use is reserved for developers and experienced professionals having in-depth
28 !GLT_LIC computer knowledge. Users are therefore encouraged to load and test the
29 !GLT_LIC suitability of the software as regards their requirements in conditions enabling
30 !GLT_LIC the security of their systems and/or data to be ensured and, more generally, to
31 !GLT_LIC use and operate it in the same conditions of security.
32 !GLT_LIC
33 !GLT_LIC The GELATO sofware is cureently distibuted with the SURFEX software, available at
34 !GLT_LIC http://www.cnrm.meteo.fr/surfex. The fact that you download the software deemed that
35 !GLT_LIC you had knowledge of the CeCILL-C license and that you accept its terms.
36 !GLT_LIC Attempts to use this software in a way not complying with CeCILL-C license
37 !GLT_LIC may lead to prosecution.
38 !GLT_LIC
39 ! glt_generic_list -- A Generic Linked List Implementation in Fortran 95
40 !
41 ! Copyright (C) 2009 Jason R. Blevins
42 !
43 ! Permission is hereby granted, free of charge, to any person obtaining a copy
44 ! of this software and associated documentation files (the "Software"), to deal
45 ! in the Software without restriction, including without limitation the rights
46 ! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
47 ! copies of the Software, and to permit persons to whom the Software is
48 ! furnished to do so, subject to the following conditions:
49 !
50 ! The above copyright notice and this permission notice shall be included in
51 ! all copies or substantial portions of the Software.
52 !
53 ! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54 ! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55 ! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56 ! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57 ! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58 ! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
59 ! THE SOFTWARE.
60 
61 module data
62  implicit none
63 
64  private
65  public :: data_t
66  public :: data_ptr
67 
68  ! Data is stored in data_t
69  type :: data_t
70  real :: x
71  end type data_t
72 
73  ! A container for storing data_t pointers
74  type :: data_ptr
75  type(data_t), pointer :: p
76  end type data_ptr
77 
78 end module data
79 
80 
82  implicit none
83 
84  private
85  public :: list_node_t, list_data
86  public :: list_init, list_free
88 
89  ! A public variable used as a MOLD for transfer()
90  integer, dimension(:), allocatable :: list_data
91 
92  ! Linked list node
93  type :: list_node_t
94  private
95  integer, dimension(:), pointer :: data => null()
96  type(list_node_t), pointer :: next => null()
97  end type list_node_t
98 
99 contains
100 
101  ! Initialize a head node SELF and optionally store the provided DATA.
102  subroutine list_init(self, data)
103  type(list_node_t), pointer :: self
104  integer, dimension(:), intent(in), optional :: data
105 
106  allocate(self)
107  nullify(self%next)
108 
109  if (present(data)) then
110  allocate(self%data(size(data)))
111  self%data = data
112  else
113  nullify(self%data)
114  end if
115  end subroutine list_init
116 
117  ! Free the entire list and all data, beginning at SELF
118  subroutine list_free(self)
119  type(list_node_t), pointer :: self
120  type(list_node_t), pointer :: current
121  type(list_node_t), pointer :: next
122 
123  current => self
124  do while (associated(current))
125  next => current%next
126  if (associated(current%data)) then
127  deallocate(current%data)
128  nullify(self%data)
129  end if
130  deallocate(current)
131  nullify(current)
132  current => next
133  end do
134  end subroutine list_free
135 
136  ! Insert a list node after SELF containing DATA (optional)
137  subroutine list_insert(self, data)
138  type(list_node_t), pointer :: self
139  integer, dimension(:), intent(in), optional :: data
140  type(list_node_t), pointer :: next
141 
142  allocate(next)
143 
144  if (present(data)) then
145  allocate(next%data(size(data)))
146  next%data = data
147  else
148  nullify(next%data)
149  end if
150 
151  next%next => self%next
152  self%next => next
153  end subroutine list_insert
154 
155  ! Store the encoded DATA in list node SELF
156  subroutine list_put(self, data)
157  type(list_node_t), pointer :: self
158  integer, dimension(:), intent(in) :: data
159 
160  if (associated(self%data)) then
161  deallocate(self%data)
162  nullify(self%data)
163  end if
164  self%data = data
165  end subroutine list_put
166 
167  ! Return the DATA stored in the node SELF
168  function list_get(self) result(data)
169  type(list_node_t), pointer :: self
170  integer, dimension(:), pointer :: data
171  data => self%data
172  end function list_get
173 
174  ! Return the next node after SELF
175  function list_next(self)
176  type(list_node_t), pointer :: self
177  type(list_node_t), pointer :: list_next
178  list_next => self%next
179  end function list_next
180 
181 end module modi_glt_generic_list
182 
183 
184 program test_list
186  use data
187  implicit none
188 
189  type(list_node_t), pointer :: list => null()
190  type(data_ptr) :: ptr
191 
192  ! Allocate a new data element
193  allocate(ptr%p)
194  ptr%p%x = 2.7183
195 
196  ! Initialize the list with the first data element
197  call list_init(list, transfer(ptr, list_data))
198  print *, 'Initializing list with data:', ptr%p
199 
200  ! Allocate a second data element
201  allocate(ptr%p)
202  ptr%p%x = 0.5772
203 
204  ! Insert the second into the list
205  call list_insert(list, transfer(ptr, list_data))
206  print *, 'Inserting node with data:', ptr%p
207 
208  ! Retrieve data from the second node and free memory
209  ptr = transfer(list_get(list_next(list)), ptr)
210  print *, 'Second node data:', ptr%p
211  deallocate(ptr%p)
212 
213  ! Retrieve data from the head node and free memory
214  ptr = transfer(list_get(list), ptr)
215  print *, 'Head node data:', ptr%p
216  deallocate(ptr%p)
217 
218  ! Free the list
219  call list_free(list)
220 end program test_list
type(list_node_t) function, pointer, public list_next(self)
subroutine, public list_insert(self, data)
subroutine, public list_put(self, data)
program test_list
subroutine, public list_init(self, data)
subroutine, public list_free(self)
integer function, dimension(:), pointer, public list_get(self)