[UP]


Manual Reference Pages  - M_list (3)

NAME

M_list(3f) - [M_list] maintain simple lists (LICENSE:PD)

CONTENTS

Synopsis
Description
Example
Author
License

SYNOPSIS

use M_list, only : insert, replace, remove use M_list, only : dictionary

DESCRIPTION

The M_list(3fm) module allows for maintaining an array as a sorted list. An example is given that creates a keyword-value dictionary using the lists.

The lists are maintained as simple allocatable arrays. Each time an entry is added or deleted the array is re-allocated. Because of the expense of reallocating the data these routines are best suited for maintaining small lists that do not change size frequently.

The advantage is that the dictionary components are simple arrays of intrinsic types which can be easily accessed with standard routines.

    BASIC LIST

subroutine locate(list,value,place,ier,errmsg)
  finds the index where a value is found or should be in a sorted array and flag if the value exists already
subroutine insert(list,value,place)
  insert entry into an allocatable array at specified position
subroutine replace(list,value,place)
  replace entry in an allocatable array at specified position
subroutine remove(list,place)
  remove entry from an allocatable array at specified position

    BASIC DICTIONARY

Due to bugs in gfortran up to at least 7.4.0, this next section does not work.

type dictionary

      character(len=:),allocatable :: key(:)
      character(len=:),allocatable :: value(:)
      integer,allocatable          :: count(:)

%get get value from type(dictionary) given an existing key
%set set or replace value for type(dictionary) given a key
%del delete an existing key from type(dictionary)
%clr empty a type(dictionary)

EXAMPLE

Sample program

   program demo_M_list
   use M_list, only : insert, locate, replace, remove
   ! create a dictionary with character keywords, values, and value lengths
   ! using the routines for maintaining a list

use M_list, only : locate, insert, replace implicit none character(len=:),allocatable :: keywords(:) character(len=:),allocatable :: values(:) integer,allocatable :: counts(:) integer :: i ! insert and replace entries call update(’b’,’value of b’) call update(’a’,’value of a’) call update(’c’,’value of c’) call update(’c’,’value of c again’) call update(’d’,’value of d’) call update(’a’,’value of a again’) ! show array write(*,’(*(a,"==>","[",a,"]",/))’)(trim(keywords(i)),values(i)(:counts(i)),i=1,size(keywords)) ! remove some entries call update(’a’) call update(’c’) write(*,’(*(a,"==>","[",a,"]",/))’)(trim(keywords(i)),values(i)(:counts(i)),i=1,size(keywords)) ! get some values write(*,*)’get b=>’,get(’b’) write(*,*)’get d=>’,get(’d’) write(*,*)’get notthere=>’,get(’notthere’)

contains subroutine update(key,valin) character(len=*),intent(in) :: key character(len=*),intent(in),optional :: valin integer :: place integer :: ilen character(len=:),allocatable :: val if(present(valin))then val=valin ilen=len_trim(val) ! find where string is or should be call locate(keywords,key,place) ! if string was not found insert it if(place.lt.1)then call insert(keywords,key,iabs(place)) call insert(values,val,iabs(place)) call insert(counts,ilen,iabs(place)) else call replace(values,val,place) call replace(counts,ilen,place) endif else call locate(keywords,key,place) if(place.gt.0)then call remove(keywords,place) call remove(values,place) call remove(counts,place) endif endif end subroutine update function get(key) result(valout) character(len=*),intent(in) :: key character(len=:),allocatable :: valout integer :: place ! find where string is or should be call locate(keywords,key,place) if(place.lt.1)then valout=’’ else valout=values(place)(:counts(place)) endif end function get

end program demo_M_list

Results:

   d==>[value of d]
   c==>[value of c again]
   b==>[value of b]
   a==>[value of a again]

d==>[value of d] b==>[value of b]

get b=>value of b get d=>value of d get notthere=>

AUTHOR

John S. Urban

LICENSE

Public Domain


M_list (3) March 11, 2021
Generated by manServer 1.08 from c672d770-25be-4659-9342-eea09bca93af using man macros.