Re: [cython-users] Hashtable in cython

1,036 views
Skip to first unread message

Robert Bradshaw

unread,
Oct 1, 2012, 4:39:15 PM10/1/12
to cython...@googlegroups.com
On Mon, Oct 1, 2012 at 10:52 AM, Peter <pna...@dataraker.com> wrote:
> Is there a hashtable implementation/library available to cython?
>
> I've found the map.pyx, but no hash-based implementations.

What about Python's builtin dict?

- Robert

Nathaniel Smith

unread,
Oct 2, 2012, 4:17:05 AM10/2/12
to cython...@googlegroups.com
On Mon, Oct 1, 2012 at 6:52 PM, Peter <pna...@dataraker.com> wrote:
> Is there a hashtable implementation/library available to cython?
>
> I've found the map.pyx, but no hash-based implementations.
>
> Thanks :)

If you can't use Python dicts for some reason (e.g. need to hash lots
of small objects without "boxing" them), then khash is popular. Pandas
has cython code to wrap it.

-n

Sturla Molden

unread,
Oct 2, 2012, 8:17:29 AM10/2/12
to cython...@googlegroups.com
On 01.10.2012 19:52, Peter wrote:

> I've found the map.pyx, but no hash-based implementations.

In C++11, there is an std::unsorted_map which is hash-based (std::map is
a BST). Similarly, std::unsorted_set is hash-based, whereas std::set is
BST based. The interfaces are similar, so just copy the contents of
map.pyx and replace "map" with "unsorted_map" (and ditto for set).

Also note that Pythons builtin dict and set are hash-based, but they can
only store Python objects.

If you need to hash C or C++ objects, use C++11 STL or another C or C++
hash-implementation. You can of course convert a C object to a Python
object, but it comes with a speed penalty.


Sturla



Sturla Molden

unread,
Oct 2, 2012, 8:22:27 AM10/2/12
to cython...@googlegroups.com
On 02.10.2012 14:17, Sturla Molden wrote:

> In C++11, there is an std::unsorted_map which is hash-based (std::map is
> a BST). Similarly, std::unsorted_set is hash-based, whereas std::set is
> BST based. The interfaces are similar, so just copy the contents of
> map.pyx and replace "map" with "unsorted_map" (and ditto for set).

Sorry, it's called "unordered_map", not "unsorted_map" :)

http://en.wikipedia.org/wiki/Unordered_associative_containers_(C%2B%2B)

Notice how Pythonic the C++ code becomes ;-)



Sturla

Peter

unread,
Oct 3, 2012, 5:25:41 AM10/3/12
to cython...@googlegroups.com
Thanks so much :) This is  exactly what I needed

Jérôme Kieffer

unread,
Oct 3, 2012, 2:44:06 PM10/3/12
to cython...@googlegroups.com
On Tue, 02 Oct 2012 14:22:27 +0200
Sturla Molden <sturla...@yahoo.no> wrote:

> Notice how Pythonic the C++ code becomes

I am just in a C++11 course and I have the same feeling.
--
Jérôme Kieffer <goo...@terre-adelie.org>
Reply all
Reply to author
Forward
Message has been deleted
0 new messages