Skip to content

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

Merged
merged 1 commit into from
Dec 10, 2022
Merged

Support optional capacity on creation #15

merged 1 commit into from
Dec 10, 2022

Conversation

fenollp
Copy link
Contributor

@fenollp fenollp commented Dec 4, 2022

No description provided.

@wk8
Copy link
Owner

wk8 commented Dec 6, 2022

Thanks for the PR @fenollp , might adding a test too?

@SVilgelm
Copy link
Contributor

SVilgelm commented Dec 7, 2022

I think it would be great to have more general solution for the options.
something like

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

@SVilgelm
Copy link
Contributor

SVilgelm commented Dec 7, 2022

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)
  ...
}

@wk8
Copy link
Owner

wk8 commented Dec 7, 2022

@SVilgelm : I agree in general, but in this specific case:

  1. it's nice to have an API that stays as close as possible to that of maps
  2. I can't really think off the top of my head of any other options that could make sense in the future

So I'm inclined to keep this as is? Thoughts?

Copy link
Owner

@wk8 wk8 left a 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)
Copy link
Owner

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?

Copy link
Contributor Author

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

Copy link
Owner

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())
}
Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting 👍

@fenollp
Copy link
Contributor Author

fenollp commented Dec 9, 2022

Rebased!

So I'm inclined to keep this as is? Thoughts?

Agreed!

@wk8 wk8 merged commit a449e02 into wk8:master Dec 10, 2022
@fenollp fenollp deleted the new-cap branch December 10, 2022 09:30
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

Successfully merging this pull request may close these issues.

3 participants