Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why cannot index priorityqueue.NewWith (value of type func(comparator utils.Comparator) *priorityqueue.Queue)? #245

Open
aldalee opened this issue Mar 14, 2024 · 1 comment

Comments

@aldalee
Copy link

aldalee commented Mar 14, 2024

This is a leetcode problem:merge k sorted lists, I want to use a heap to solve it, and the following code can be AC.

package main

import pq "github.com/emirpasic/gods/queues/priorityqueue"

type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeKLists(lists []*ListNode) *ListNode {
	heap := pq.NewWith(func(a, b any) int {
		return a.(*ListNode).Val - b.(*ListNode).Val
	})
	for _, list := range lists {
		if list != nil {
			heap.Enqueue(list)
		}
	}
	if heap.Empty() {
		return nil
	}
	v, _ := heap.Dequeue()
	head := v.(*ListNode)
	cur, pre := head, head
	for !heap.Empty() {
		if cur.Next != nil {
			heap.Enqueue(cur.Next)
		}
		v, _ := heap.Dequeue()
		cur = v.(*ListNode)
		pre.Next = cur
		pre = cur
	}
	return head
}

But type assertion is required, and I want to add generics to solve this problem. I noticed that the following libraries have already introduced generics: https://github.com/emirpasic/gods/blob/master/queues/priorityqueue/priorityqueue.go
So I adjusted my code and added generics:

package main

import "github.com/emirpasic/gods/queues/priorityqueue"

type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeKLists(lists []*ListNode) *ListNode {
	heap := priorityqueue.NewWith[*ListNode](func(a, b *ListNode) int {
		return a.Val - b.Val
	})

	for _, list := range lists {
		if list != nil {
			heap.Enqueue(list)
		}
	}
	if heap.Empty() {
		return nil
	}
	head, _ := heap.Dequeue()
	cur, pre := head, head
	for !heap.Empty() {
		if cur.Next != nil {
			heap.Enqueue(cur.Next)
		}
		cur, _ = heap.Dequeue()
		pre.Next = cur
		pre = cur
	}
	return nil
}

But when I import "github.com/emirpasic/gods/queues/priorityqueue" and build the code on https://go.dev/play, I meet the error:

go: finding module for package github.com/emirpasic/gods/queues/priorityqueue
go: downloading github.com/emirpasic/gods v1.18.1
go: found github.com/emirpasic/gods/queues/priorityqueue in github.com/emirpasic/gods v1.18.1
./prog.go:11:31: invalid operation: cannot index priorityqueue.NewWith (value of type func(comparator utils.Comparator) *priorityqueue.Queue) 

Here are some source codes:

// NewWith instantiates a new empty queue with the custom comparator.
func NewWith[T comparable](comparator utils.Comparator[T]) *Queue[T] {
	return &Queue[T]{heap: binaryheap.NewWith(comparator), Comparator: comparator}
}
// comparable is an interface that is implemented by all comparable types
// (booleans, numbers, strings, pointers, channels, arrays of comparable types,
// structs whose fields are all comparable types).
// The comparable interface may only be used as a type parameter constraint,
// not as the type of a variable.
type comparable interface{ comparable }

But, I have added generics and implemented comparators, why are there still errors?

Meanwhile, while compiling the code locally, I encountered this error again:

D:\space\algorithm\leetcode-top-interview-questions\src> go build .\problem_0023_merge_k_sorted_lists.go
# github.com/emirpasic/gods/queues/priorityqueue
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\iterator.go:13:7: type instantiation requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\iterator.go:16:15: type parameter requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\iterator.go:16:17: predeclared comparable requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\iterator.go:17:12: type instantiation requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\priorityqueue.go:32:12: type parameter requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\priorityqueue.go:32:14: predeclared comparable requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\priorityqueue.go:33:14: type instantiation requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\priorityqueue.go:34:13: type instantiation requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\priorityqueue.go:37:10: type parameter requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\priorityqueue.go:42:14: type parameter requires go1.18 or later (-lang was set to go1.2; check go.mod)
D:\opt\go1.22\gopath\pkg\mod\github.com\emirpasic\[email protected]\queues\priorityqueue\iterator.go:13:7: too many errors

My Go version is go version go1.22.0 windows/amd64. And this is my go.mod

module leetcode-top-interview-questions
go 1.22
require github.com/emirpasic/gods v1.18.1
require github.com/emirpasic/gods/v2 v2.0.0-alpha // indirect

How can I solve the above two problems? Thank you!

@PapaCharlie
Copy link
Collaborator

Hey @aldalee, now that you're using the new major version with generics, you need to import it like this:

import "github.com/emirpasic/gods/v2/queues/priorityqueue"

In all your error messages you're still pulling in v1.18.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants