473,385 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Details about pythons set implementation

Hi,

I'm interested in details about how sets are implemented in python.
They seem to be quite fast and I found some remarks who state, that
the implementation is highly optimized. I need to implemented sets in
C/C++ and need a starting point on how to do it right. Could somebody
give me a starting point?

regards,
Achim
Jan 4 '08 #1
6 2588
Achim Domma <do***@procoders.netwrites:
I'm interested in details about how sets are implemented in python.
They seem to be quite fast and I found some remarks who state, that
the implementation is highly optimized. I need to implemented sets
in C/C++ and need a starting point on how to do it right. Could
somebody give me a starting point?
You can simply look at the implementation, Objects/setobject.c in the
Python source code. Most that it's mostly copy-paste from the dict
implementation (dictobject.c) and that both are quite involved and
optimized for the use by Python. They're not general implementation
of sets from use in C.

The "highly optimized" remarks should be understood in the context of
Python, not in the context of other C and C++ set libraries. I don't
know how well Python sets compare to other set libraries, but I doubt
that it's much faster than the median (which "highly optimized" could
be understood to imply).

BTW if you're using C++, why not simply use std::set? If you need it
called from C, you can wrap the needed methods in a C-accessible API.
Jan 4 '08 #2
Hrvoje Niksic <hn*****@xemacs.orgwrote:
>BTW if you're using C++, why not simply use std::set?
Because ... how to be polite about this? No, I can't. std::set is
crap. The implementation is a sorted sequence -- if you're lucky,
this is a heap or a C array, and you've got O(log n) performance.
But the real killer is that requirement for a std::set<Tis that
T::operator< exists. Which means, for instance, that you can't
have a set of complex numbers....

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Jan 4 '08 #3
On Jan 4, 9:08 am, Sion Arrowsmith <si...@chiark.greenend.org.uk>
wrote:
Hrvoje Niksic <hnik...@xemacs.orgwrote:
BTW if you're using C++, why not simply use std::set?

Because ... how to be polite about this? No, I can't. std::set is
crap. The implementation is a sorted sequence -- if you're lucky,
this is a heap or a C array, and you've got O(log n) performance.
But the real killer is that requirement for a std::set<Tis that
T::operator< exists. Which means, for instance, that you can't
have a set of complex numbers....

--
\S -- si...@chiark.greenend.org.uk --http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Why cant you implement < for complex numbers? Maybe I'm being naive,
but isn't this the normal definition?
a + bi < c + di iff sqrt(a**2 + b**2) < sqrt(c**2, d**2)

How do you implement a set without sorting?

Are you expecting better than O(log n)?

--Buck
Jan 4 '08 #4
bukzor schrieb:
On Jan 4, 9:08 am, Sion Arrowsmith <si...@chiark.greenend.org.uk>
wrote:
>Hrvoje Niksic <hnik...@xemacs.orgwrote:
>>BTW if you're using C++, why not simply use std::set?
Because ... how to be polite about this? No, I can't. std::set is
crap. The implementation is a sorted sequence -- if you're lucky,
this is a heap or a C array, and you've got O(log n) performance.
But the real killer is that requirement for a std::set<Tis that
T::operator< exists. Which means, for instance, that you can't
have a set of complex numbers....

--
\S -- si...@chiark.greenend.org.uk --http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump

Why cant you implement < for complex numbers? Maybe I'm being naive,
but isn't this the normal definition?
a + bi < c + di iff sqrt(a**2 + b**2) < sqrt(c**2, d**2)

How do you implement a set without sorting?

Are you expecting better than O(log n)?
Of course, hashing does O(1) (most of the time, with a sane hash of course.)

Diez
Jan 4 '08 #5
On Jan 4, 6:08 pm, Sion Arrowsmith <si...@chiark.greenend.org.uk>
wrote:
Hrvoje Niksic <hnik...@xemacs.orgwrote:
BTW if you're using C++, why not simply use std::set?

Because ... how to be polite about this? No, I can't. std::set is
crap. The implementation is a sorted sequence -- if you're lucky,
this is a heap or a C array, and you've got O(log n) performance.
But the real killer is that requirement for a std::set<Tis that
T::operator< exists. Which means, for instance, that you can't
have a set of complex numbers....

--
Hallo and Sorry for being OT.
As Arnaud pointed out, you must only overload the < Operator for the
requested type.
Something like
bool operator < ( const Type& fir, const Type& sec )....
similar to python with __lt__ .
The rest of magic will be done by the compiler/interpreter.
Assoziative Arrays (set,map,multi_set,multi_map) in the classical STL
are implemented as binary trees. Therefore the keys must be comparable
and the access time is O(log n ).
To get a dictionary with O(1), the most STL implementation support a
extension called hash_set.
The new standard TR1 support unsorted_set ... . You can download it
from www.boost.org. Newer gcc runtimes also including the new
subnamespace tr1.
There is no need to implement set in c++ to get O(1).
Greetings Rainer
Jan 5 '08 #6
Sion Arrowsmith:
Because ... how to be polite about this? No, I can't. std::set is
crap. The implementation is a sorted sequence
What about using hash_map instead? You can use it with GCC too (but
you have to use a trick if you want to use string keys).

Bye,
bearophile
Jan 5 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: ForHimself Every Man | last post by:
What's better about Rattlesnakes than Python. I'm sure there's something. What is it? This is not a troll. I'm a snake shooping and I want people's answers. I don't know beans about...
12
by: f29 | last post by:
I don't believe that noone has yet spotted that python is becoming java. Each new version is fully equipped with more garbage than before. Classes are great, but once there are 1000 of them,...
2
by: pvinodhkumar | last post by:
I am reading Lippman's Inside C++ object model. I feel lonely because the Microsoft C++ compiler which I use does not provide me an implementation details manual, describing how they implement...
1
by: P Vinodh Kumar | last post by:
Reposting, please give your ideas/suggestions/comments. I am reading Lippman's Inside C++ object model. I feel lonely because the Microsoft C++ compiler which I use does not provide me an...
5
by: Mathias Panzenboeck | last post by:
Hi. I wrote a small hashlib for C. Because I'm new to hashes I looked at pythons implementation and reused *some* of the code... or more the mathematical "hash-function", not really the code. ...
1
by: tedpottel | last post by:
Hi, I am creating a library of functions. I would like to have them saved in a sub folder of pythons LIB folder, but I cannot get it to work. I have a script called test.py I stored it in...
6
by: tedpottel | last post by:
Hi, I'm trying to create my own lib of functions, but it seems like I can only import them if they are in pythons lib folder. Example I have a folder called K:\mypython Now in the...
2
by: tedpottel | last post by:
Hi, Is their a version of pythons IDLE that will run in a dos command line? The reason is that I would like to be able to run python code interactively from my parable by connecting to my desktop...
6
by: Ralph | last post by:
Hi, I was reading effictive C++ and some other books again and they all tell you about hiding implementation details (proxy/pimpl/inheritance) but they never really explain when to use it. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.