Comments (3)
I investigated the issue a bit and found the underlaying problem which in turn raises several different issues. The problem arises from the fact that we keep a pointer in our underlaying map structure for every Set when we call for example parent.Add(s1) when s1 is a Set, thus Add() over threadUnsafeSet cannot possibly distinguish between these two same and equal sets passed to it as they have different memory addresses. Now, here comes the interesting part. What do you expect from the outcome of the following code?
a := NewThreadUnsafeSet()
b := NewThreadUnsafeSet()
a.Add(1)
a.Add(2)
b.Add(a)
b.Add(2)
fmt.Println(b)
a.Add(3)
fmt.Println(b)
Here is the output:
Set{Set{1, 2}, 2}
Set{Set{2, 3, 1}, 2}
Interestingly, after adding 3 to set a, although invisibe to the eyes of the programmer, b changes which in my humble opinion is not a good scenario to happen here as you do not possibly expect from mapset to keep references to temporary objects in your code. We did nothing to b to expect a change in it's elements. One might possibly (and that is very likely to happen) decides to say reuse a set, change it or destroy it altogether in which case every set that temporary object has been added to gets corrupted. I think making a copy from the object that we're tying to add to our set and adding that copy is the only viable solution here. What do we do?
from golang-set.
This looks to me like a potentially deep conflict. Go does not like to use containers as keys to maps.
Maps are containers, so if we use a map as a key....
I actually dug into this package to see exactly how this problem is being addressed. :(
It seems to me that the only real option is to use pointers for all structs, maps, arrays, and pointers. Comparison between these items is then required to be deep.
As for the a & b sets above, the paradox exists only because of appearances. The set b contains the set a. The display of a changes because a changes, but b continues to include a. Note that an additional set c, containing a and 2, is equal to b both before and after a is modified.
from golang-set.
This boils down to the fact that keys MUST be comparable types which is limited in Go to simple value types and struct types that don’t contain reference types themselves...ie, they only contain value types or further embed structs which only contain value types.
So the feature request isn’t easily supported in Go given this implementation. I’m going to say this is a fair trade off since the built-in types have the same limitations when it comes to keys and the nature of comparison.
from golang-set.
Related Issues (20)
- Missing support Len()/Count()/Size() method? HOT 2
- iterate with random sequence? HOT 1
- Feature request: Support for immutable sets HOT 1
- [Q] How to print the contents of a Set? HOT 1
- Getting error when instantiating a set: "invalid operation: cannot index mapset.NewSet (value of type func(s ...interface{}) mapset.Set)" HOT 6
- No installation guide
- [Question] Contains() parameters escape to heap HOT 4
- How I can get every elements in my set? HOT 2
- Unmarshalling threadUnsafeSet with json.Unmarshal panics HOT 4
- Document thread-safe set usage HOT 1
- Issue: Contains HOT 2
- [Question] Why isn't `defer` used? HOT 6
- Non-mutating helpers to add/remove HOT 2
- Guidance/suggestions on Iterators and Stop() HOT 4
- Add a way to distinguish thread safe and unsafe sets HOT 3
- Is there any method to know whether a set is empty? HOT 2
- unmarshal json fails when set is part of struct HOT 2
- Suggestion: option to restrict comparable to only exported values. HOT 1
- why isn't there a `mapset.NewSetFromSlice` method? HOT 1
- Allow capacity to be specified when initializing a Set HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from golang-set.