Created
October 12, 2023 07:50
-
-
Save run-dlang/07c6ccfcb31044f926545f88cd394191 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io. Run with '-unittest'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio : writeln, toFile; | |
import std.datetime.stopwatch : StopWatch, AutoStart; | |
import asdf.serialization : deserialize, serializeToJson; | |
import std.file : readText; | |
import std.array: array; | |
import std.algorithm : copy, map; | |
enum TopN = 5; | |
struct Post | |
{ | |
string _id; | |
string title; | |
string[] tags; | |
} | |
struct RelatedPosts | |
{ | |
string _id; | |
string[] tags; | |
Post[TopN] related; | |
} | |
struct PostsWithSharedTags | |
{ | |
ulong post; | |
uint sharedTags; | |
} | |
void main() | |
{ | |
auto jsonText = readText("posts.json"); | |
auto posts = deserialize!(Post[])(jsonText); | |
int postsCount = cast(int) posts.length; | |
ulong[][string] tagMap; | |
auto sw = StopWatch(AutoStart.yes); | |
foreach (i, post; posts) | |
foreach (tag; post.tags) | |
tagMap[tag] ~= i; | |
auto mapPreparation = sw.peek.total!"usecs" * 1.0 / 1000; | |
auto relatedPosts = new RelatedPosts[postsCount]; | |
auto taggedPostsCount = new uint[postsCount]; | |
foreach (k, post; posts) | |
{ | |
taggedPostsCount[] = 0; | |
PostsWithSharedTags[TopN] top5; | |
Post[TopN] topPosts; | |
foreach (tag; post.tags) | |
foreach (idx; tagMap[tag]) | |
taggedPostsCount[0]++; | |
taggedPostsCount[k] = 0; | |
auto minTags = 0; | |
foreach (j, count; taggedPostsCount) | |
{ | |
if (count > minTags) | |
{ | |
// Find position to insert | |
auto pos = 4; | |
while (pos >= 0 && top5[pos].sharedTags < count) | |
{ | |
pos--; | |
} | |
pos++; | |
// Shift and insert | |
if (pos < 4) | |
{ | |
copy(top5[pos .. 4], top5[(pos + 1) .. $]); | |
} | |
top5[pos] = PostsWithSharedTags(j, count); | |
minTags = top5[4].sharedTags; | |
} | |
} | |
topPosts = top5[].map!(a => posts[a.post]).array; | |
relatedPosts[k] = RelatedPosts( | |
post._id, | |
post.tags, | |
topPosts | |
); | |
} | |
sw.stop(); | |
writeln("Map preparation (w/o IO): ", mapPreparation, "ms"); | |
writeln("Processing time (w/o IO): ", sw.peek.total!"usecs" * 1.0 / 1000, "ms"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment