Skip to content

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

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No 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? No Sign in to your account

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

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
Collaborator

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
Collaborator

cc @sbinet

@sbinet
Copy link
Member

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
Collaborator

@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
Collaborator

@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 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 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 commented Oct 19, 2019

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

@corona10
Copy link
Collaborator

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

@HyeockJinKim
Copy link
Contributor Author

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
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants