dictionary Derived Type

type, public :: dictionary


Contents

Source Code


Components

Type Visibility Attributes Name Initial
integer, public, allocatable :: count(:)
character(len=:), public, allocatable :: key(:)
character(len=:), public, allocatable :: value(:)

Type-Bound Procedures

procedure, public :: clr => dict_clear

  • private subroutine dict_clear(self)

    NAME

    clr(3f) - [M_list::dictionary::OOPS] clear basic dictionary
    (LICENSE:PD)
    

    SYNOPSIS

    type(dictionary) :: dict

    call dict%clr()
    

    DESCRIPTION

    clear a basic dictionary.
    

    OPTIONS

    DICT   the dictionary.
    

    EXAMPLES

    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
    

    AUTHOR

    John S. Urban
    

    LICENSE

    Public Domain
    

    Arguments

    Type IntentOptional Attributes Name
    class(dictionary), intent(inout) :: self

procedure, public :: del => dict_delete

  • private subroutine dict_delete(self, key)

    NAME

    del(3f) - [M_list::dictionary::OOPS] delete entry by key name from
              a basic dictionary
    (LICENSE:PD)
    

    SYNOPSIS

    type(dictionary) :: dict

    character(len=*),intent(in) :: key
    
    dict%del(key)
    

    DESCRIPTION

    Delete an entry from a basic dictionary if it is present.
    

    OPTIONS

    DICT   the dictionary.
    KEY    the key name to find and delete from the dictionary.
    

    EXAMPLES

    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'
    

    AUTHOR

    John S. Urban
    

    LICENSE

    Public Domain
    

    Arguments

    Type IntentOptional Attributes Name
    class(dictionary), intent(in) :: self
    character(len=*), intent(in) :: key

procedure, public :: get => dict_get

  • private function dict_get(self, key) result(value)

    NAME

    get(3f) - [M_list::dictionary::OOPS] get value of key-value pair in
              a dictionary given key
    (LICENSE:PD)
    

    SYNOPSIS

    type(dictionary) :: dict

    character(len=*),intent(in) :: key
    character(len=*),intent(in) :: VALUE
    
    value=dict%get(key)
    

    DESCRIPTION

    get a value given a key from a key-value dictionary
    
    If key is not found in dictionary , return a blank
    

    OPTIONS

    DICT     is the dictionary.
    KEY      key name
    VALUE    value associated with key
    

    EXAMPLES

    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=
    

    AUTHOR

    John S. Urban
    

    LICENSE

    Public Domain
    

    Arguments

    Type IntentOptional Attributes Name
    class(dictionary), intent(in) :: self
    character(len=*), intent(in) :: key

    Return Value character(len=:), allocatable

procedure, public :: ifdef => dict_ifdef

  • private function dict_ifdef(self, key) result(value)

    NAME

    ifdef(3f) - [M_list::dictionary::OOPS] return whether name is present
                in dictionary or not
    (LICENSE:PD)
    

    SYNOPSIS

    type(dictionary) :: dict

    character(len=*),intent(in) :: key
    logical :: value
    
    value=dict%ifdef(key)
    

    DESCRIPTION

    determine if name is already defined in dictionary or not
    

    OPTIONS

    DICT     is the dictionary.
    KEY      key name
    

    RETURNS

    VALUE    .FALSE. if name not defined, .TRUE if name is defined.
    

    EXAMPLES

    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
    

    AUTHOR

    John S. Urban
    

    LICENSE

    Public Domain
    

    Arguments

    Type IntentOptional Attributes Name
    class(dictionary), intent(in) :: self
    character(len=*), intent(in) :: key

    Return Value logical

procedure, public :: set => dict_add

  • private subroutine dict_add(self, key, value)

    NAME

    set(3f) - [M_list::dictionary::OOPS] add or replace a key-value pair
              in a dictionary
    (LICENSE:PD)
    

    SYNOPSIS

    type(dictionary) :: dict

    character(len=*),intent(in) :: key
    character(len=*),intent(in) :: VALUE
    
    call dict%rep(key,value)
    

    DESCRIPTION

    Add or replace a key-value pair in a dictionary.
    

    OPTIONS

    DICT     is the dictionary.
    key      key name
    VALUE    value associated with key
    

    EXAMPLES

    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]
    

    AUTHOR

    John S. Urban
    

    LICENSE

    Public Domain
    

    Arguments

    Type IntentOptional Attributes Name
    class(dictionary), intent(inout) :: self
    character(len=*), intent(in) :: key
    character(len=*), intent(in) :: value

Source Code

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