@@ -36,8 +36,9 @@ StructTypes.StructType(::Type{PostData}) = StructTypes.Struct()
36
36
function fastmaxindex! (xs:: Vector{Int64} , topn, maxn, maxv)
37
37
maxn .= 1
38
38
maxv .= 0
39
+ top = maxv[1 ]
39
40
for (i, x) in enumerate (xs)
40
- if x > maxv[ 1 ]
41
+ if x > top
41
42
maxv[1 ] = x
42
43
maxn[1 ] = i
43
44
for j in 2 : topn
@@ -46,6 +47,7 @@ function fastmaxindex!(xs::Vector{Int64}, topn, maxn, maxv)
46
47
maxn[j- 1 ], maxn[j] = maxn[j], maxn[j- 1 ]
47
48
end
48
49
end
50
+ top = maxv[1 ]
49
51
end
50
52
end
51
53
56
58
57
59
function related (posts)
58
60
topn = 5
61
+ # key is every possible "tag" used in all posts
62
+ # value is indicies of all "post"s that used this tag
59
63
tagmap = Dict {String,Vector{Int64}} ()
60
64
for (idx, post) in enumerate (posts)
61
65
for tag in post. tags
@@ -69,27 +73,30 @@ function related(posts)
69
73
relatedposts = Vector {RelatedPost} (undef, length (posts))
70
74
taggedpostcount = Vector {Int64} (undef, length (posts))
71
75
72
- maxn = Vector { Int64} (undef, topn )
73
- maxv = Vector { Int64} (undef, topn )
76
+ maxn = MVector {topn, Int64} (undef)
77
+ maxv = MVector {topn, Int64} (undef)
74
78
75
79
for (i, post) in enumerate (posts)
76
80
taggedpostcount .= 0
81
+ # for each post (`i`-th)
82
+ # and every tag used in the `i`-th post
83
+ # give all related post +1 in `taggedpostcount` shadow vector
77
84
for tag in post. tags
78
85
for idx in tagmap[tag]
79
86
taggedpostcount[idx] += 1
80
87
end
81
88
end
82
89
90
+ # don't self count
83
91
taggedpostcount[i] = 0
84
92
85
93
fastmaxindex! (taggedpostcount, topn, maxn, maxv)
86
94
87
- relatedpost = RelatedPost (post. _id, post. tags, SVector {topn} (posts[ix] for ix in maxn))
95
+ relatedpost = RelatedPost (post. _id, post. tags, SVector {topn} (@view posts[maxn] ))
88
96
relatedposts[i] = relatedpost
89
97
end
90
98
91
99
return relatedposts
92
100
end
93
101
94
102
const res = relatedIO ()
95
-
0 commit comments