-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashtable_class.f90
84 lines (59 loc) · 2.35 KB
/
hashtable_class.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
!=====================================================================!
!
!=====================================================================!
! http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Hashtable.java#Hashtable
module hashtable_class
use iso_fortran_env, only : dp => REAL64
use object_class, only : object
implicit none
private
public :: hashtable
type, extends(object) :: hashtable
class(object), allocatable, dimension(:) :: table ! table of values
type(integer) :: count ! number of keys
type(integer) :: threshold ! threshold for rehashing
type(integer) :: load_factor ! factor to increase the size
type(integer) :: mod_count ! number of rehashings happened since creation
contains
! overridden print method
procedure :: print
!procedure :: rehash
end type hashtable
! Constructor
interface hashtable
module procedure create_hashtable
end interface hashtable
contains
type(hashtable) function create_hashtable(initial_capacity, load_factor) &
& result(this)
type(integer) , intent(in) :: initial_capacity
type(real(dp)), intent(in) :: load_factor
! Sanity check in the initial capacity of hashtable
if (initial_capacity .le. 0) then
print *, "Invalid initial capacity", initial_capacity
stop
end if
! Sanity check on the load factor
if (load_factor .le. 0 .or. load_factor .ne. load_factor) then
print *, "Invalid load factor", load_factor
stop
end if
! Store the values into the object
this % load_factor = load_factor
this % threshold = int(initial_capacity*load_factor)
! Allocate space for entries
!allocate( this % table (initial_capacity) )
! Set the number of entries
this % count = size(this % table)
! Zero the number of size modifications so far (rehashing)
this % mod_count = 0
end function create_hashtable
subroutine print(this)
class(hashtable), intent(in) :: this
print *, "hashtable@ ", this % hashcode()
print *, " count: ", this % count
print *, " threshold: ", this % threshold
print *, " load_factor: ", this % load_factor
print *, " mod_count: ", this % mod_count
end subroutine print
end module hashtable_class