1 2 #define ASSERT_(A) if(.not.(A))call exit(1) 3 4 ! $Id: MAPL_Hash.F90,v 1.1 2008/07/15 17:25:24 f4mjs Exp $ 5 6 !============================================================================= 7 !BOP 8 9 ! !MODULE: MAPL_Hash -- A utility to manage hash tables 10 11 ! !INTERFACE: 12 13 module MAPL_HashMod 14 15 implicit none 16 private 17 18 ! !PUBLIC ROUTINES: 19 20 public MAPL_HashCreate 21 public MAPL_HashIncrement 22 public MAPL_HashDestroy 23 public MAPL_HashSize 24 public MAPL_HashDump 25 26 ! !DESCRIPTION: 27 ! 28 ! {\tt MAPL\_HashMod} is a FORTRAN binding to a simple C has facility. 29 ! The API is: 30 !\bv 31 ! 32 ! ! Create a hash table with Nbuckets 33 ! 34 ! integer function MAPL_HashCreate(Nbuckets) 35 ! integer, intent(IN) :: Nbuckets 36 ! 37 ! ! Update table Hash with integer[s] i[,j] 38 ! ! The return value is the order of occurence of the integer[s]. 39 ! ! If i is new, the return value is the new hash size. 40 ! 41 ! integer function MAPL_HashIncrement(Hash,i,j) 42 ! integer, intent(IN) :: Hash 43 ! integer, intent(IN) :: i 44 ! integer, optional, intent(IN) :: j 45 ! 46 ! ! Dump the list of integers or integer pairs in the hash. 47 ! ! The list is in no particular order. 48 ! ! If the arrays are not long enough, nothing is dumped and -1 49 ! ! is returned; otherwise it returns the current hash size 50 ! ! (the length of the list). 51 ! 52 ! integer function MAPL_HashDump(Hash,i,j) 53 ! integer, intent(IN) :: Hash 54 ! integer, intent(OUT) :: i(:) 55 ! integer, optional, intent(OUT) :: j(:) 56 ! 57 ! ! Get the size of a hash 58 ! 59 ! integer function MAPL_HashSize(Hash) 60 ! integer, intent(IN) :: Hash 61 ! 62 ! ! Destroy a hash table 63 ! 64 ! subroutine MAPL_HashDestroy(Hash) 65 ! integer, intent(IN) :: Hash 66 !\ev 67 ! 68 ! The following is a sample usage that makes a list of 69 ! unique integers in the large array II. It can similarly 70 ! be used to find ordered pairs of integers. The asserts 71 ! are put in to clarify the usage. 72 ! 73 !\bv 74 ! integer :: Hash, k, II(100000), FoundOrder(10000) 75 ! 76 ! Hash = MAPL_HashCreate(1000) 77 ! 78 ! latest = 0 79 ! do i=1,100000 80 ! k = MAPL_HashIncrement(Hash,ii(i)) 81 ! if(k>latest) then 82 ! latest = k 83 ! isnew = .true. 84 ! FoundOrder(k) = ii(i) 85 ! ASSERT_(k==MAPL_HashSize(Hash)) 86 ! else 87 ! isnew = .false. 88 ! ASSERT_(FoundOrder(k)==ii(i)) 89 ! endif 90 ! enddo 91 ! 92 ! 93 !\ev 94 95 !EOP 96 !============================================================================= 97 98 contains 99 100 integer function MAPL_HashCreate(Nbuckets) 101 integer, intent(IN) :: Nbuckets 102 103 integer CREATEHASH 104 MAPL_HashCreate = CREATEHASH(Nbuckets) 105 106 end function MAPL_HashCreate 107 108 !---------------------------------------------- 109 110 integer function MAPL_HashIncrement(Hash,i,j) 111 integer, intent(IN) :: Hash 112 integer, intent(IN) :: i 113 integer, optional, intent(IN) :: j 114 115 integer INCREMENTHASH 116 if(present(j)) then 117 MAPL_HashIncrement = INCREMENTHASH(HASH,I,J) 118 else 119 MAPL_HashIncrement = INCREMENTHASH(HASH,I,1) 120 endif 121 122 end function MAPL_HashIncrement 123 124 !---------------------------------------------- 125 126 subroutine MAPL_HashDestroy(Hash) 127 integer, intent(IN) :: Hash 128 129 call DESTROYHASH(HASH) 130 131 end subroutine MAPL_HashDestroy 132 133 !---------------------------------------------- 134 135 integer function MAPL_HashDump(Hash,i,j) 136 integer, intent(IN ) :: Hash 137 integer, intent(OUT) :: i(:) 138 integer, optional, intent(OUT) :: j(:) 139 140 integer, allocatable :: jj(:) 141 142 MAPL_HashDump = MAPL_HashSize(HASH) 143 144 if(size(i) < MAPL_HashSize(HASH)) then 145 MAPL_HashDump = -1 146 return 147 end if 148 149 if(present(j)) then 150 ASSERT_(size(i) == size(j)) 151 call DUMPHASH(HASH,I,J) 152 else 153 allocate(jj(size(i))) 154 call DUMPHASH(HASH,I,JJ) 155 deallocate(JJ) 156 end if 157 158 end function MAPL_HashDump 159 160 !---------------------------------------------- 161 162 integer function MAPL_HashSize(Hash) 163 integer, intent(IN) :: Hash 164 165 integer HASHSIZE 166 MAPL_HashSize = HASHSIZE(Hash) 167 168 end function MAPL_HashSize 169 170 !---------------------------------------------- 171 172 end module MAPL_HashMod 173