EIRAM
atomic and molecular data in form of polynomial fits
dictionary.f90
Go to the documentation of this file.
1 ! dictionary.f90 --
2 ! Include file for defining dictionaries:
3 ! a mapping of strings to some data
4 !
5 ! See the example/test program for the way to use this
6 !
7 ! Note:
8 ! Use is made of a linked list, as we do not
9 ! sort the elements by means of the strings.
10 ! (That will be for another version)
11 !
12 ! Note:
13 ! - Define the length of the strings as
14 ! parameter "DICT_KEY_LENGTH"
15 ! - Define a derived type for the data
16 ! to be stored
17 ! - Also define a "null" value - DICT_NULL
18 ! of type DICT_DATA, for use when the
19 ! key is not found.
20 ! - Put both in a separate module, that
21 ! will be used.
22 !
23 ! $Id: dictionary.f90,v 1.2 2006/03/26 19:03:53 arjenmarkus Exp $
24 !
25 type list_data
26  character(len=DICT_KEY_LENGTH) :: key
27  type(dict_data) :: value
28 end type list_data
29 
30 type dict_struct
31  private
32  type(linked_list), pointer :: list
33 end type dict_struct
34 
35 !
36 ! We do not want everything to be public
37 !
38 private :: list_data
39 private :: linked_list
40 private :: list_create
41 private :: list_destroy
42 private :: list_count
43 private :: list_next
44 private :: list_insert
45 private :: list_insert_head
46 private :: list_delete_element
47 private :: list_get_data
48 private :: list_put_data
49 private :: dict_get_elem
50 
51 include 'linkedlist.f90'
52 
53 !
54 ! Routines and functions specific to dictionaries
55 !
56 
57 ! dict_create --
58 ! Create and initialise a dictionary
59 ! Arguments:
60 ! dict Pointer to new dictionary
61 ! key Key for the first element
62 ! value Value for the first element
63 ! Note:
64 ! This version assumes a shallow copy is enough
65 ! (that is, there are no pointers within the data
66 ! to be stored)
67 ! It also assumes the argument list does not already
68 ! refer to a list. Use dict_destroy first to
69 ! destroy up an old list.
70 !
71 subroutine dict_create( dict, key, value )
72  type(dict_struct), pointer :: dict
73  character(len=*), intent(in) :: key
74  type(dict_data), intent(in) :: value
75 
76  type(list_data) :: data
77 
78  allocate( dict )
79 
80  data%key = key
81  data%value = value
82 
83  call list_create( dict%list, data )
84 
85 end subroutine dict_create
86 
87 ! dict_destroy --
88 ! Destroy an entire dictionary
89 ! Arguments:
90 ! dict Pointer to the dictionary to be destroyed
91 ! Note:
92 ! This version assumes that there are no
93 ! pointers within the data that need deallocation
94 !
95 subroutine dict_destroy( dict )
96  type(dict_struct), pointer :: dict
97 
98  call list_destroy( dict%list )
99  deallocate( dict )
100 
101 end subroutine dict_destroy
102 
103 ! dict_add_key
104 ! Add a new key
105 ! Arguments:
106 ! dict Pointer to the dictionary
107 ! key Key for the new element
108 ! value Value for the new element
109 ! Note:
110 ! If the key already exists, the
111 ! key's value is simply replaced
112 !
113 subroutine dict_add_key( dict, key, value )
114  type(dict_struct), pointer :: dict
115  character(len=*), intent(in) :: key
116  type(dict_data), intent(in) :: value
117 
118  type(list_data) :: data
119  type(linked_list), pointer :: elem
120 
121  elem => dict_get_elem( dict, key )
122 
123  if ( associated(elem) ) then
124  elem%data%value = value
125  else
126  data%key = key
127  data%value = value
128  call list_insert( dict%list, data )
129  endif
130 end subroutine dict_add_key
131 
132 ! dict_delete_key
133 ! Delete a key-value pair from the dictionary
134 ! Arguments:
135 ! dict Dictionary in question
136 ! key Key to be removed
137 !
138 subroutine dict_delete_key( dict, key )
139  type(dict_struct), pointer :: dict
140  character(len=*), intent(in) :: key
141 
142  type(linked_list), pointer :: elem
143 
144  elem => dict_get_elem( dict, key )
145 
146  if ( associated(elem) ) then
147  call list_delete_element( dict%list, elem )
148  endif
149 end subroutine dict_delete_key
150 
151 ! dict_get_key
152 ! Get the value belonging to a key
153 ! Arguments:
154 ! dict Pointer to the dictionary
155 ! key Key for which the values are sought
156 !
157 function dict_get_key( dict, key ) result(value)
158  type(dict_struct), pointer :: dict
159  character(len=*), intent(in) :: key
160  type(dict_data) :: value
161 
162  type(list_data) :: data
163  type(linked_list), pointer :: elem
164 
165  elem => dict_get_elem( dict, key )
166 
167  if ( associated(elem) ) then
168  value = elem%data%value
169  else
170  value = dict_null
171  endif
172 end function dict_get_key
173 
174 ! dict_has_key
175 ! Check if the dictionary has a particular key
176 ! Arguments:
177 ! dict Pointer to the dictionary
178 ! key Key to be sought
179 !
180 function dict_has_key( dict, key ) result(has)
181  type(dict_struct), pointer :: dict
182  character(len=*), intent(in) :: key
183  logical :: has
184 
185  type(linked_list), pointer :: elem
186 
187  elem => dict_get_elem( dict, key )
188 
189  has = associated(elem)
190 end function dict_has_key
191 
192 ! dict_get_elem
193 ! Find the element with a particular key
194 ! Arguments:
195 ! dict Pointer to the dictionary
196 ! key Key to be sought
197 !
198 function dict_get_elem( dict, key ) result(elem)
199  type(dict_struct), pointer :: dict
200  character(len=*), intent(in) :: key
201 
202  type(linked_list), pointer :: elem
203 
204  elem => dict%list
205  do while ( associated(elem) )
206  if ( elem%data%key .eq. key ) then
207  exit
208  else
209  elem => list_next( elem )
210  endif
211  enddo
212 end function dict_get_elem
213