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

Add generic set type #21408

Merged
merged 7 commits into from
Oct 12, 2022
Merged

Add generic set type #21408

merged 7 commits into from
Oct 12, 2022

Conversation

KN4CK3R
Copy link
Member

@KN4CK3R KN4CK3R commented Oct 11, 2022

This PR adds a generic set type to get rid of maps used as sets.

@KN4CK3R KN4CK3R added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Oct 11, 2022
@KN4CK3R KN4CK3R added this to the 1.18.0 milestone Oct 11, 2022
models/repo/user_repo.go Outdated Show resolved Hide resolved
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 11, 2022
}

// AddMultiple adds the specified elements to a set.
func (s Set[T]) AddMultiple(values ...T) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just use Add to also add multiple? Just like you can use append adding single or multiple values

Copy link
Member Author

Choose a reason for hiding this comment

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

Add returns true if the element was added.

Copy link
Member

Choose a reason for hiding this comment

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

It can still return bool and work for both use cases:

// Add adds the specified element to a set.
// Returns true if at least one element is added; false if all the elements is already present.
func (s Set[T]) Add(value ...T) bool {
        var added bool
	for _, v := range value {
		if _, has := s[v]; !has {
			s[v] = struct{}{}
			added = true
		}
	}
	return added
}

Copy link
Contributor

Choose a reason for hiding this comment

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

But it's ambiguous when reading code Add(a,b) to set [a], only b is added. What if one day there is another function Add with comment "true if every element is added". The return value for adding multiple values is meaningless

I stand the current approach.

Copy link
Member

Choose a reason for hiding this comment

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

That's discutable but I won't block. For our use case single Add would be more than enough and easier to use imho

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, different frameworks have different choices. For C++ stl, pair<iterator,bool> insert(elem) vs void insert(begin, end) (like current approach), while in Java bool add(elem) vs bool addAll(elemList) (return true if the set is changed).

Personally I like the current approach 😁

modules/container/set.go Outdated Show resolved Hide resolved
@lafriks
Copy link
Member

lafriks commented Oct 11, 2022

Would be nice to create func NewSet[T comparable]() Set[T] and use that instead of make, would make it more flexible in future imho

@KN4CK3R
Copy link
Member Author

KN4CK3R commented Oct 11, 2022

I removed the NewSet method some commits ago. Which advantages do you see?

@lafriks
Copy link
Member

lafriks commented Oct 11, 2022

If for whatever reason we want to replace base struct for Set to some other instead of map[T]struct{}

@KN4CK3R
Copy link
Member Author

KN4CK3R commented Oct 11, 2022

That's not possible because there is code which uses for ... := range set. So if there may be another set type in future you are required to change these places too.

@lafriks
Copy link
Member

lafriks commented Oct 11, 2022

That's not possible because there is code which uses for ... := range set. So if there may be another set type in future you are required to change these places too.

why .Values() is not used there?

@wxiaoguang
Copy link
Contributor

why .Values() is not used there?

It copies the keys into a new array, it's inefficient

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Oct 11, 2022
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Oct 11, 2022
@wxiaoguang wxiaoguang merged commit 0e57ff7 into go-gitea:main Oct 12, 2022
zjjhot added a commit to zjjhot/gitea that referenced this pull request Oct 13, 2022
* upstream/main: (25 commits)
  [skip ci] Updated translations via Crowdin
  Respect user's locale when rendering the date range in the repo activity page (go-gitea#21410)
  Consolidate more CSS colors into variables (go-gitea#21402)
  Add HEAD fix to gitea doctor (go-gitea#21352)
  Contribution guidelines (go-gitea#21425)
  Refactor Gitpod configuration to improve quick spin up of automated dev environments (go-gitea#21411)
  Support instance-wide OAuth2 applications (go-gitea#21335)
  Case-insensitive NuGet symbol file GUID (go-gitea#21409)
  Add generic set type (go-gitea#21408)
  Improve OAuth integration tests (go-gitea#21390)
  Make e-mail sanity check more precise (go-gitea#20991)
  Fix broken link to frontend guidelines in hacking guidelines (go-gitea#21382)
  Use Name instead of DisplayName in LFS Lock (go-gitea#21415)
  [skip ci] Updated translations via Crowdin
  feat(pr review): add more space on mobile (go-gitea#21326)
  Bump `golang.org/x/text` (go-gitea#21412)
  Update gitea.service (go-gitea#21399)
  Do DB update after merge in hammer context (go-gitea#21401)
  add gitpod config (go-gitea#20995)
  Remove cancel button in repo creation page (go-gitea#21381)
  ...
@wolfogre wolfogre mentioned this pull request Jan 4, 2023
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants