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

sets: add sets intersection #155

Merged
merged 2 commits into from
May 14, 2024
Merged

sets: add sets intersection #155

merged 2 commits into from
May 14, 2024

Conversation

marco-m-pix4d
Copy link
Contributor

Part of PCI-3790

@marco-m-pix4d marco-m-pix4d requested a review from a team as a code owner May 13, 2024 14:25
sets/sets.go Outdated
Comment on lines 87 to 99
if s.Size() < x.Size() {
for item := range s.items {
if x.Contains(item) {
result.items[item] = struct{}{}
}
}
} else {
for item := range x.items {
if s.Contains(item) {
result.items[item] = struct{}{}
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be better to just swap the variables and code the loop only once?

Suggested change
if s.Size() < x.Size() {
for item := range s.items {
if x.Contains(item) {
result.items[item] = struct{}{}
}
}
} else {
for item := range x.items {
if s.Contains(item) {
result.items[item] = struct{}{}
}
}
}
if s.Size() > x.Size() {
// Swap them to loop over the smallest set
s, x = x, s
}
for item := range s.items {
if x.Contains(item) {
result.items[item] = struct{}{}
}
}

Copy link
Contributor Author

@marco-m-pix4d marco-m-pix4d May 14, 2024

Choose a reason for hiding this comment

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

This is swapping pointers (s is a pointer):

func (s *Set[T]) Intersection(...)

but on the other hand, in Go, "assignment to the method receiver propagates only to callees but not to callers", so at the exit of the function, the contents of the set s do remain the same. Said in another way, the code proposed is safe, although confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just wanted to notice that GitHub itself decided that "marco-m-pix4d marked this conversation resolved".
I did nothing 🙄.

I added a second commit, that I thinks makes the code less confusing.

Copy link
Contributor

@aliculPix4D aliculPix4D left a comment

Choose a reason for hiding this comment

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

I remeber doing these exercises from Exercism :)

Copy link
Contributor

@iAmoric iAmoric left a comment

Choose a reason for hiding this comment

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

Just for my understanding, what is the relation between this and PCI-3790 ?

@marco-m-pix4d
Copy link
Contributor Author

marco-m-pix4d commented May 14, 2024

Just for my understanding, what is the relation between this and PCI-3790 ?

I will use set intersection in fly_helper to determine which of the required branches are present and absent.

@marco-m-pix4d marco-m-pix4d merged commit fb7c0cc into master May 14, 2024
1 check passed
@marco-m-pix4d marco-m-pix4d deleted the set-intersection branch May 14, 2024 12:24
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.

5 participants