The Wayback Machine - https://web.archive.org/web/20200910210641/https://github.com/go-python/gpython/issues/118
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

Implement dict to receive Object as key, not only String #118

Open
HyeockJinKim opened this issue Oct 14, 2019 · 10 comments
Open

Implement dict to receive Object as key, not only String #118

HyeockJinKim opened this issue Oct 14, 2019 · 10 comments

Comments

@HyeockJinKim
Copy link
Contributor

@HyeockJinKim HyeockJinKim commented Oct 14, 2019

type StringDict map[string]Object

Change the dict to receive a hashable object as a key, not just a string.
I will change it so that the object can be looked up through the hash value of the object.

Store the object in the slice and use the map to find the index of the stored object through the hash value.

make([]Object, 0, len(default_size))  // slice to store object  (index -> Object)
type Dict map[HashIndex]int // map to store index of slice  (Hash -> index)

Is it ok to implement dict this way?

@corona10
Copy link
Member

@corona10 corona10 commented Oct 14, 2019

@HyeockJinKim
I 'd like to recommend to implement dict by wrapping the map throw using the delegate pattern.

type Dict struct {
...
}

func (d *Dict) Put(...) (Object, error) {
...
}

func (d *Dict) Get(...) (Object, error) {
...
}

cc @ncw

@corona10
Copy link
Member

@corona10 corona10 commented Oct 14, 2019

cc @sbinet

@sbinet
Copy link
Member

@sbinet sbinet commented Oct 14, 2019

if I am not mistaken, CPython dicts are not ordered (there's collections.OrderedDict for that.)

so, couldn't we just use map[Object]Object at the core of this data structure?

@corona10
Copy link
Member

@corona10 corona10 commented Oct 14, 2019

@sbinet
FYI

Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6.

He might want to implement this feature.
It is okay that we decide to broken 3.4 compatibilities.
(#74 (comment))

@corona10
Copy link
Member

@corona10 corona10 commented Oct 14, 2019

@HyeockJinKim
However, as @sbinet commented.
I'd like to recommend you implement unordered key dictionary as the 1st milestone.
That will be enough for the 1st step.

cc @ncw @sbinet

@sbinet
Copy link
Member

@sbinet sbinet commented Oct 14, 2019

I stand corrected :)

ok, then what about:

type Dict struct {
    keys map[Object]int // key to index into slice of vals
    vals []Object       // values associated with the above keys
}

(I think we can rely on Go's map hashing mechanism)

@sbinet
Copy link
Member

@sbinet sbinet commented Oct 14, 2019

and if we want to retain insertion order:

type Dict struct {
    keys  map[Object]int
    items [][2]Object // key-value pair
}
@ashermancinelli
Copy link

@ashermancinelli ashermancinelli commented Oct 19, 2019

Does anyone have a fork for this issue? Would be willing to give it a shot.

@corona10
Copy link
Member

@corona10 corona10 commented Oct 19, 2019

@ashermancinelli please ping to @HyeockJinKim
He might work on this issue :)

@HyeockJinKim
Copy link
Contributor Author

@HyeockJinKim HyeockJinKim commented Oct 19, 2019

I'm working on this issue right now.
I will work quickly and PR.

HyeockJinKim added a commit to HyeockJinKim/gpython that referenced this issue Oct 19, 2019
Implement dict struct that takes Object as key
store key and value in array together so that dict is ordered

Issue go-python#118
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.