Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | public, | allocatable | :: | count(:) | |||
character(len=:), | public, | allocatable | :: | key(:) | |||
character(len=:), | public, | allocatable | :: | value(:) |
clr(3f) - [M_list::dictionary::OOPS] clear basic dictionary
(LICENSE:PD)
type(dictionary) :: dict
call dict%clr()
clear a basic dictionary.
DICT the dictionary.
create and clear a basic dictionary
program demo_clr
use M_list, only : dictionary
implicit none
type(dictionary) :: caps
integer :: i
! create a character string dictionary
call caps%set('A','aye')
call caps%set('B','bee')
call caps%set('C','see')
call caps%set('D','dee')
! show current dictionary
write(*,'("DICTIONARY BEFORE CLEARED")')
write(*,101)(trim(caps%key(i)),trim(caps%value(i)),i=1,size(caps%key))
call caps%clr()
write(*,'("DICTIONARY AFTER CLEARED")')
! show current dictionary
write(*,101)(trim(caps%key(i)),trim(caps%value(i)),i=1,size(caps%key))
101 format (1x,*(a,"='",a,"'",:,","))
end program demo_clr
Results
> DICTIONARY BEFORE CLEARED
> D='dee',C='see',B='bee',A='aye'
> DICTIONARY AFTER CLEARED
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(dictionary), | intent(inout) | :: | self |
del(3f) - [M_list::dictionary::OOPS] delete entry by key name from
a basic dictionary
(LICENSE:PD)
type(dictionary) :: dict
character(len=*),intent(in) :: key
dict%del(key)
Delete an entry from a basic dictionary if it is present.
DICT the dictionary.
KEY the key name to find and delete from the dictionary.
Delete an entry from a dictionary by key name.
program demo_del
use M_list, only : dictionary
implicit none
type(dictionary) :: caps
integer :: i
! create a character string dictionary
call caps%set('A','aye')
call caps%set('B','bee')
call caps%set('C','see')
call caps%set('D','dee')
! show current dictionary
write(*,101)(trim(caps%key(i)),trim(caps%value(i)),i=1,size(caps%key))
! delete dictionary entries
call caps%del('A')
call caps%del('C')
call caps%del('z') ! a noop as there is no key of 'z'
! show current dictionary
write(*,101)(trim(caps%key(i)),trim(caps%value(i)),i=1,size(caps%key))
101 format (1x,*(a,"='",a,"'",:,","))
end program demo_del
Results
> D='dee',C='see',B='bee',A='aye'
> D='dee',B='bee'
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(dictionary), | intent(in) | :: | self | |||
character(len=*), | intent(in) | :: | key |
get(3f) - [M_list::dictionary::OOPS] get value of key-value pair in
a dictionary given key
(LICENSE:PD)
type(dictionary) :: dict
character(len=*),intent(in) :: key
character(len=*),intent(in) :: VALUE
value=dict%get(key)
get a value given a key from a key-value dictionary
If key is not found in dictionary , return a blank
DICT is the dictionary.
KEY key name
VALUE value associated with key
Sample program:
program demo_get
use M_list, only : dictionary
implicit none
type(dictionary) :: table
character(len=:),allocatable :: val
integer :: i
call table%set('A','value for A')
call table%set('B','value for B')
call table%set('C','value for C')
call table%set('D','value for D')
call table%set('E','value for E')
call table%set('F','value for F')
call table%set('G','value for G')
write(*,*)'A=',table%get('A')
write(*,*)'B=',table%get('B')
write(*,*)'C=',table%get('C')
write(*,*)'D=',table%get('D')
write(*,*)'E=',table%get('E')
write(*,*)'F=',table%get('F')
write(*,*)'G=',table%get('G')
write(*,*)'H=',table%get('H')
end program demo_get
Results
> A=value for A
> B=value for B
> C=value for C
> D=value for D
> E=value for E
> F=value for F
> G=value for G
> H=
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(dictionary), | intent(in) | :: | self | |||
character(len=*), | intent(in) | :: | key |
ifdef(3f) - [M_list::dictionary::OOPS] return whether name is present
in dictionary or not
(LICENSE:PD)
type(dictionary) :: dict
character(len=*),intent(in) :: key
logical :: value
value=dict%ifdef(key)
determine if name is already defined in dictionary or not
DICT is the dictionary.
KEY key name
VALUE .FALSE. if name not defined, .TRUE if name is defined.
Sample program:
program demo_ifdef
use M_list, only : dictionary
implicit none
type(dictionary) :: table
character(len=:),allocatable :: val
integer :: i
call table%set('A','value for A')
call table%set('B','value for B')
call table%set('C','value for C')
call table%set('D','value for D')
call table%set('E','value for E')
call table%set('F','value for F')
call table%set('G','value for G')
call table%del('F')
call table%del('D')
write(*,*)'A=',table%ifdef('A')
write(*,*)'B=',table%ifdef('B')
write(*,*)'C=',table%ifdef('C')
write(*,*)'D=',table%ifdef('D')
write(*,*)'E=',table%ifdef('E')
write(*,*)'F=',table%ifdef('F')
write(*,*)'G=',table%ifdef('G')
write(*,*)'H=',table%ifdef('H')
end program demo_ifdef
Results:
> A= T
> B= T
> C= T
> D= F
> E= T
> F= F
> G= T
> H= F
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(dictionary), | intent(in) | :: | self | |||
character(len=*), | intent(in) | :: | key |
set(3f) - [M_list::dictionary::OOPS] add or replace a key-value pair
in a dictionary
(LICENSE:PD)
type(dictionary) :: dict
character(len=*),intent(in) :: key
character(len=*),intent(in) :: VALUE
call dict%rep(key,value)
Add or replace a key-value pair in a dictionary.
DICT is the dictionary.
key key name
VALUE value associated with key
Add or replace a key and value pair in a dictionary
program demo_set
use M_list, only : dictionary
implicit none
type(dictionary) :: dict
integer :: i
call dict%set('A','b')
call dict%set('B','^')
call dict%set('C',' ')
call dict%set('D','c')
call dict%set('E','ZZ')
call dict%set('F','ZZZZ')
call dict%set('G','z')
call dict%set('A','new value for A')
write(*,'(*(a,"==>","[",a,"]",/))') &
& (trim(dict%key(i)), &
& dict%value(i)(:dict%count(i)), &
& i=1,size(dict%key))
end program demo_set
Results
> G==>[z]
> F==>[ZZZZ]
> E==>[ZZ]
> D==>[c]
> C==>[]
> B==>[^]
> A==>[new value for A]
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(dictionary), | intent(inout) | :: | self | |||
character(len=*), | intent(in) | :: | key | |||
character(len=*), | intent(in) | :: | value |
type dictionary
character(len=:),allocatable :: key(:)
character(len=:),allocatable :: value(:)
integer,allocatable :: count(:)
contains
procedure,public :: get => dict_get ! get value associated with a key in a dictionary or return blank
procedure,public :: set => dict_add ! insert or replace entry by name into a dictionary
procedure,public :: del => dict_delete ! delete entry by name from a dictionary if entry is present
procedure,public :: clr => dict_clear ! clear dictionary
procedure,public :: ifdef => dict_ifdef ! return if defined or not
end type dictionary