assoc_list_insert Subroutine

public subroutine assoc_list_insert(a, key, value, stat)

Arguments

Type IntentOptional AttributesName
type(assoc_list_t), intent(inout) :: a
character(len=*), intent(in) :: key
character(len=*), intent(in) :: value
integer, intent(out) :: stat

Contents

Source Code


Source Code

subroutine assoc_list_insert(a,key,value,stat)
type(assoc_list_t), intent(inout) :: a
character(len=*), intent(in)      :: key, value
integer, intent(out)              :: stat

integer :: i
character(len=1000), allocatable  :: b(:)

if (.not. allocated(a%key)) then
   call assoc_list_init(a,4,stat)
   if (stat /= 0) return
endif

! Replace if key exists already
do i = 1, a%nitems
   if (a%key(i) == key) then
      a%value(i) = value
      stat = 0
      return
   endif
enddo
!
! Add at the end
a%nitems = a%nitems + 1
if (a%nitems > a%nslots)  then
   ! Enlarge a%data by 4 slots
   !
   allocate(b(a%nslots))
   !
   b = a%key
   deallocate(a%key)
   allocate(a%key(a%nslots + 4))
   a%key(1:a%nslots) = b
   !
   b = a%value
   deallocate(a%value)
   allocate(a%value(a%nslots + 4))
   a%value(1:a%nslots) = b
   deallocate(b)
   !
   a%nslots = size(a%key)
endif
i = a%nitems
a%key(i) = key
a%value(i) = value
stat = 0

end subroutine assoc_list_insert