Code Monkey home page Code Monkey logo

Comments (6)

kmike avatar kmike commented on September 14, 2024

Hi @superbobry,

I'm fine with adding __eq__ and __ne__ methods to Trie and other classes; pull requests are welcome :)

As for the hash, it looks like a consequence of a free list-based allocation indeed; two objects that are in memory at the same time are getting different hashes:

In [7]: from marisa_trie import Trie
In [8]: trie1 = Trie()
In [9]: trie2 = Trie()
In [10]: hash(trie1)
Out[10]: 281853959
In [11]: hash(trie2)
Out[11]: 281853967

from marisa-trie.

superbobry avatar superbobry commented on September 14, 2024

I'll try to submit a PR during the weekend.

two objects that are in memory at the same time are getting different hashes

I can add __hash__ as well, if it's ok with you.

from marisa-trie.

kmike avatar kmike commented on September 14, 2024

Thanks!

I think adding __hash__ is not necessary - what problem adding it solves? Current implementation doesn't e.g. affect dict collisions because hash is the same only for objects which are not alive at the same time.

from marisa-trie.

superbobry avatar superbobry commented on September 14, 2024

Good point. Having a trie as a dictionary key doesn't make much sense anyway :)

from marisa-trie.

superbobry avatar superbobry commented on September 14, 2024

There's one more edge-case: Trie implements __getitem__ and doesn't override __iter__. This (unfortunately) makes it iterable in CPython:

>>> t = Trie()
>>> iter(t)
<iterator object at 0x102939f28>
>>> for key in t:
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Argument 'key' has incorrect type (expected str, got int)

Is it possible to make the Trie yield keys in __iter__ just like dict does?

from marisa-trie.

kmike avatar kmike commented on September 14, 2024

Ouch. Didn't know about that Python feature. Overriding __iter__ to return keys makes sense.

from marisa-trie.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.