-
Notifications
You must be signed in to change notification settings - Fork 45
Support optional capacity on creation #15
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
Conversation
Thanks for the PR @fenollp , might adding a test too? |
I think it would be great to have more general solution for the options. type Option struct {
capacity int
}
func mergeOptions(opts ...Option) Option {
var res Option
for _, opt := range opts {
if opt.capacity > 0 {
res.capacity = opt.capacity
}
}
return res
}
func WithCapacity(v int) Option {
return Option{capacity: v}
}
func New[K comparable, V any](opts ...Option) *OrderedMap[K, V] {
opt := mergeOptions(opts...)
pairs = make(map[K]*Pair[K, V], opt.capacity)
...
} this is more flexible solution, the option structure can be easily extended |
or using the functions based approach: type Option func(opt *option)
type option struct {
capacity int
}
func mergeOptions(opts ...Option) option {
res := &option{}
for _, opt := range opts {
opt(res)
}
return res
}
func WithCapacity(v int) Option {
return func(opt *option) {
opt.capacity = v
}
}
func New[K comparable, V any](opts ...Option) *OrderedMap[K, V] {
opt := mergeOptions(opts...)
pairs = make(map[K]*Pair[K, V], opt.capacity)
...
} |
@SVilgelm : I agree in general, but in this specific case:
So I'm inclined to keep this as is? Thoughts? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits, plus needs rebase. Thanks :)
_ = New[int, string](1, 2, 3) | ||
}) | ||
|
||
om := New[int, string](-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually surprised that this doesn't panic...? That goes straight to make(map[K]*Pair[K, V], capacity[0])
, and make does panic when given a < 0 capacity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No panic, just min(capacity, 0)
See https://go.dev/play/p/sFwJi7omFA9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. https://go.dev/play/p/TiqBeNAUqe- does panic. I'll have to look more into that one :)
om := New[int, string](-1) | ||
om.Set(1337, "quarante-deux") | ||
assert.Equal(t, 1, om.Len()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to add a test with a > 0 capacity that also checks that om.pairs
has the expected capacity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's a way to get a map's capacity though: golang/go#52157
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting 👍
Signed-off-by: Pierre Fenoll <[email protected]>
Rebased!
Agreed! |
No description provided.