-
Notifications
You must be signed in to change notification settings - Fork 0
/
posting.f90
195 lines (154 loc) · 4.19 KB
/
posting.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
module posting_mod
use, intrinsic :: iso_fortran_env
use dynarray_mod
use dynarray_string_mod
implicit none
type posting
type(dynarray) :: id_store, count_store
real(REAL64), allocatable :: rsv_store(:)
contains
procedure :: initialize
procedure :: initialize_length
procedure :: append
procedure :: print
procedure :: write
procedure :: read
procedure :: compute_rsv
procedure :: set_length
procedure :: merge_with
procedure :: sort
procedure :: results_print
end type posting
private :: initialize, initialize_length, append, print, write, read, compute_rsv, set_length, merge_with, sort, results_print
contains
subroutine initialize(p)
class(posting) :: p
call p%id_store%initialize()
call p%count_store%initialize()
end subroutine initialize
subroutine initialize_length(p, length)
class(posting) :: p
integer :: length
call p%id_store%initialize_length(length)
call p%count_store%initialize_length(length)
end subroutine initialize_length
subroutine append(p, docid)
class(posting) :: p
integer, intent(in) :: docid
if (p%id_store%length /= 0 .AND. p%id_store%last() == docid) then
call p%count_store%increment_last()
else
call p%id_store%append(docid)
call p%count_store%append(1)
end if
end subroutine append
subroutine print(p)
class(posting) :: p
call p%id_store%print()
call p%count_store%print()
end subroutine print
subroutine write(p)
class(posting) :: p
call p%id_store%write()
call p%count_store%write()
end subroutine write
subroutine read(p)
class(posting) :: p
call p%id_store%read()
call p%count_store%read()
end subroutine read
subroutine compute_rsv(p, count)
class(posting) :: p
integer, intent(in) :: count
integer :: i
real(REAL64) :: length, doccount
if (allocated(p%rsv_store)) then
return
end if
length = p%count_store%length
doccount = count
allocate(p%rsv_store(p%count_store%length))
do i = 1, p%count_store%length
p%rsv_store(i) = p%count_store%store(i) / (length / doccount)
end do
end subroutine compute_rsv
subroutine set_length(p, length)
class(posting) :: p
integer :: length
call p%id_store%set_length(length)
call p%count_store%set_length(length)
end subroutine set_length
function merge_with(p, with) result(out)
class(posting) :: p
type(posting) :: with
type(posting) :: out
integer :: size, left, right
if (.NOT. allocated(p%id_store%store)) then
out = with
return
end if
size = max(p%id_store%length, with%id_store%length)
call out%initialize_length(size)
allocate(out%rsv_store(size))
left = 1
right = 1
do while (left < p%id_store%length + 1 .AND. right < with%id_store%length + 1)
if (p%id_store%store(left) < with%id_store%store(right)) then
left = left + 1
else if (p%id_store%store(left) > with%id_store%store(right)) then
right = right + 1
else
out%id_store%length = out%id_store%length + 1
out%id_store%store(out%id_store%length) = p%id_store%store(left)
out%rsv_store(out%id_store%length) = p%rsv_store(left) + with%rsv_store(right)
left = left + 1
right = right + 1
end if
end do
end function merge_with
subroutine sort(p)
class(posting) :: p
call quicksort(p, 1, p%id_store%length)
end subroutine sort
recursive subroutine quicksort(p, lo, hi)
class(posting) :: p
integer, intent(in) :: lo, hi
integer :: left, right, id_tmp
real(REAL64) :: pivot, rsv_tmp
if (.NOT. lo < hi) then
return
end if
pivot = p%rsv_store((lo + hi) / 2)
left = lo - 1
right = hi + 1
do
left = left + 1
do while (p%rsv_store(left) > pivot)
left = left + 1
end do
right = right - 1
do while (p%rsv_store(right) < pivot)
right = right - 1
end do
if (left >= right) then
exit
end if
rsv_tmp = p%rsv_store(left)
p%rsv_store(left) = p%rsv_store(right)
p%rsv_store(right) = rsv_tmp
id_tmp = p%id_store%store(left)
p%id_store%store(left) = p%id_store%store(right)
p%id_store%store(right) = id_tmp
end do
call quicksort(p, lo, right)
call quicksort(p, right + 1, hi)
end subroutine quicksort
subroutine results_print(p, docnos)
class(posting) :: p
type(dynarray_string), intent(in) :: docnos
integer :: i
do i = 1, p%id_store%length
print "(A,F10.2)", docnos%store(p%id_store%store(i)), p%rsv_store(i)
end do
end subroutine results_print
end module posting_mod