21 character(len=:),
allocatable :: key
22 character(len=:),
allocatable :: value
28 integer :: current_size = 0
29 integer :: current_idx = 0
37 integer :: dict_size = 0
46 integer,
parameter :: BUCKET_EMPTY = -2
47 integer,
parameter :: BUCKET_ENTRY_NOT_FOUND = -4
57 function djb2(this, s) result(r)
59 character(len=*),
intent(in) :: s
69 r = r*33 + ichar(s(i:i))
72 r = modulo(r, this%dict_size)
81 subroutine set(this, k, v)
83 character(len=*),
intent(in) :: k
84 character(len=*),
intent(in) :: v
88 integer :: h, i, b_idx
92 b_idx = this%buckets(h)%find(k)
94 if (b_idx == bucket_empty)
then
97 allocate(this%buckets(h)%entries(1))
98 this%buckets(h)%current_size = 1
99 this%buckets(h)%current_idx = 1
101 this%buckets(h)%entries(1)%key = trim(k)
102 this%buckets(h)%entries(1)%value = trim(v)
107 if (b_idx == bucket_entry_not_found)
then
110 allocate(tmp_bucket%entries(this%buckets(h)%current_size + 1))
111 tmp_bucket%current_size = this%buckets(h)%current_size + 1
112 tmp_bucket%current_idx = this%buckets(h)%current_idx + 1
114 do i = 1, this%buckets(h)%current_size
115 tmp_bucket%entries(i)%key = this%buckets(h)%entries(i)%key
116 tmp_bucket%entries(i)%value = this%buckets(h)%entries(i)%value
119 deallocate(this%buckets(h)%entries)
120 allocate(this%buckets(h)%entries, source=tmp_bucket%entries)
121 deallocate(tmp_bucket%entries)
123 this%buckets(h)%current_size = tmp_bucket%current_size
124 this%buckets(h)%current_idx = tmp_bucket%current_idx
125 b_idx = this%buckets(h)%current_idx
129 this%buckets(h)%entries(b_idx)%key = k
130 this%buckets(h)%entries(b_idx)%value = v
139 subroutine init(this, dict_size)
141 integer,
intent(in) :: dict_size
143 allocate(this%buckets(dict_size))
144 this%dict_size = dict_size
158 do i = 1, this%dict_size
159 s = this%buckets(i)%current_idx
161 write(*,*)
'bucket : ', i,
' size ', s
163 write(*,*)
'key : ', this%buckets(i)%entries(j)%key
164 write(*,*)
'value : ', this%buckets(i)%entries(j)%value
179 function find(this, k) result(r)
181 character(len=*),
intent(in) :: k
186 if (this%current_size == 0)
then
191 r = bucket_entry_not_found
192 do i = 1, this%current_size
193 if (this%entries(i)%key == trim(k))
then
207 function get(this, k) result(r)
209 character(len=*),
intent(in) :: k
211 character(len=:),
allocatable :: r
217 b_idx = this%buckets(h)%find(k)
219 if ( (b_idx == bucket_empty) .or. &
220 (b_idx == bucket_entry_not_found) )
then
226 r = this%buckets(h)%entries(b_idx)%value
A bucket contains several entries.
Single entry in the dictionary.
The dictionary contains dict_size buckets (defined at run time)
Dictionary type that uses strings for the keys and values.