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

Unexpected results with Map.Items() #2

Closed
Marreck opened this issue Aug 24, 2018 · 2 comments
Closed

Unexpected results with Map.Items() #2

Marreck opened this issue Aug 24, 2018 · 2 comments

Comments

@Marreck
Copy link
Contributor

Marreck commented Aug 24, 2018

Hi,

m.Items() gives some unexpected results when you add a key with value 0.

This can be fixed by something like this:

// Items returns a channel for iterating all key-value pairs.
func (m *Map) Items() chan [2]int64 {
	c := make(chan [2]int64, 10)
	go func() {
		data := m.data
		var k int64

		if m.hasFreeKey {
			c <- [2]int64{FREE_KEY, m.freeVal}
		}

		for i := 0; i < len(data); i += 2 {
			k = data[i]
			if k == FREE_KEY {
				continue
			}
			c <- [2]int64{k, data[i+1]}
		}
		close(c)
	}()
	return c
}
@brentp
Copy link
Owner

brentp commented Aug 24, 2018

cc @shenwei356 who wrote that code.

@shenwei356
Copy link
Contributor

@Marreck you are right! I'll fix it and Keys() and tests, ASAP. You can also submit a PR 😄

shenwei356 added a commit to shenwei356/intintmap that referenced this issue Aug 25, 2018
brentp added a commit that referenced this issue Aug 26, 2018
fix bug for Keys() and Items() when 0 exists in map #2
@brentp brentp closed this as completed Aug 26, 2018
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

3 participants