diff --git a/go/go.mod b/go/go.mod index e158984b..cbadf11b 100644 --- a/go/go.mod +++ b/go/go.mod @@ -2,6 +2,6 @@ module g.io/related go 1.21.1 -require github.com/emirpasic/gods v1.18.1 +require github.com/goccy/go-json v0.10.2 -require github.com/goccy/go-json v0.10.2 // indirect +require github.com/ugurcsen/gods-generic v0.10.4 // indirect diff --git a/go/go.sum b/go/go.sum index bd866945..8fc9ff9d 100644 --- a/go/go.sum +++ b/go/go.sum @@ -1,4 +1,4 @@ -github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= -github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/ugurcsen/gods-generic v0.10.4 h1:OomH3R2MdzZxpnEPijaD/ncLzV6rpDXd5ruEkWsw0vo= +github.com/ugurcsen/gods-generic v0.10.4/go.mod h1:mGYOa88Y5sbw+ADXLpScxjJ7s5iHoWya/YHyeQ4f6c4= diff --git a/go/main.go b/go/main.go index c76995ca..4a9cd4e1 100644 --- a/go/main.go +++ b/go/main.go @@ -6,8 +6,8 @@ import ( "os" "time" - "github.com/emirpasic/gods/trees/binaryheap" "github.com/goccy/go-json" + "github.com/ugurcsen/gods-generic/trees/binaryheap" ) type Post struct { @@ -16,26 +16,25 @@ type Post struct { Tags []string `json:"tags"` } +type PostWithSharedTags struct { + Post int + SharedTags int +} + type RelatedPosts struct { ID string `json:"_id"` Tags []string `json:"tags"` Related []*Post `json:"related"` } -type PostWithSharedTags struct { - Post int - SharedTags int -} - func main() { - - file, err := os.Open("../posts.json") + f, err := os.Open("../posts.json") if err != nil { log.Panicln(err) } var posts []Post - err = json.NewDecoder(file).Decode(&posts) + err = json.NewDecoder(f).Decode(&posts) if err != nil { log.Panicln(err) @@ -52,16 +51,15 @@ func main() { } allRelatedPosts := make([]RelatedPosts, 0, len(posts)) - taggedPostCount := make([]int, len(posts)) + t5 := binaryheap.NewWith[PostWithSharedTags](PostComparator) - for i, post := range posts { - // luckily this is optimized to a memset - for i := range taggedPostCount { - taggedPostCount[i] = 0 + for i := range posts { + for j := range taggedPostCount { + taggedPostCount[j] = 0 } - for _, tag := range post.Tags { + for _, tag := range posts[i].Tags { for _, otherPostIdx := range tagMap[tag] { if otherPostIdx != i { taggedPostCount[otherPostIdx]++ @@ -69,17 +67,16 @@ func main() { } } - t5 := binaryheap.NewWith(PostComparator) + t5.Clear() // Clear the heap for next post for v, count := range taggedPostCount { if t5.Size() < 5 { t5.Push(PostWithSharedTags{Post: v, SharedTags: count}) } else { - if t, _ := t5.Peek(); t.(PostWithSharedTags).SharedTags < count { + if t, _ := t5.Peek(); t.SharedTags < count { t5.Pop() t5.Push(PostWithSharedTags{Post: v, SharedTags: count}) } - } } @@ -87,14 +84,14 @@ func main() { topPosts := make([]*Post, num) for i := 0; i < num; i++ { - if t, _ := t5.Pop(); t != nil { - topPosts[i] = &posts[t.(PostWithSharedTags).Post] + if t, ok := t5.Pop(); ok { + topPosts[i] = &posts[t.Post] } } allRelatedPosts = append(allRelatedPosts, RelatedPosts{ - ID: post.ID, - Tags: post.Tags, + ID: posts[i].ID, + Tags: posts[i].Tags, Related: topPosts, }) } @@ -103,7 +100,7 @@ func main() { fmt.Println("Processing time (w/o IO)", end.Sub(start)) - file, err = os.Create("../related_posts_go.json") + file, err := os.Create("../related_posts_go.json") if err != nil { log.Panicln(err) @@ -114,21 +111,14 @@ func main() { if err != nil { log.Panicln(err) } - } -func PostComparator(a, b interface{}) int { - aAsserted := a.(PostWithSharedTags) - bAsserted := b.(PostWithSharedTags) - - if aAsserted.SharedTags > bAsserted.SharedTags { +func PostComparator(a, b PostWithSharedTags) int { + if a.SharedTags > b.SharedTags { return 1 - - } else if aAsserted.SharedTags < bAsserted.SharedTags { + } + if a.SharedTags < b.SharedTags { return -1 - - } else { - return 0 } - + return 0 }