Another Python rookie trying to port to C/C++ question.
Tom Anderson
twic at urchin.earth.li
Thu Sep 25 19:33:46 EDT 2003
On Thu, 25 Sep 2003, Don Bruder wrote:
> In article <8yoc1wwt.fsf at python.net>,
> Thomas Heller <theller at python.net> wrote:
>
> > Don Bruder <dakidd at sonic.net> writes:
> >
> > [rewriting a Python program in C/C++]
> > > Something like this C construct:
> > >
> > > struct DictStruct
> > > {
> > > char *KeyArray[<somenumber>];
> > > ValType ValArray[<somenumber>];
> > > }
> > >
> > [...]
> > > Similarly, would something like:
> > >
> > > struct DictStruct
> > > {
> > > char Key[25];
> > > ValType Value;
> > > DictStruct *PrevDictEntry;
> > > DictStruct *NextDictEntry;
> > > }
> > >
> > > [...] end up behaving at least reasonably like a Python "dict"?
> >
> > Yes.
> >
> > Except that it would be less flexible, *much* slower, and
> > probably much more buggy.
>
> Well, since I'm not worried about "flexible", have my doubts about the
> "much slower" part,
think again. python's dictionaries use an efficient implementation, are
written in C, and have been tuned over the course of several years. your
sketched design uses a very slow implementation, and is brand new.
> and have no reason to believe that choice of language automatically
> makes a routine buggy,
it's maturity that matters in this case; you will make mistakes, just like
the python (and STL) implementers did, but they've already had years to
find and fix them.
if you don't fancy using STL (which will be the case if you're steering
clear of C++ - and might well be even if you weren't!), at least go and
read up on how to implement dictionaries; they're a very well-studied
abstract type. hashtables are simple and efficient, and binary trees are
good too. there are more complicated things like ternary trees, red-black
trees, Patricia trees (my personal favourite :) ) and skip lists, but
you'll probably be fine with hashtables. if you can lay your hands on a
good data structures and algorithms textbook, you'll be fine (Sedgewick's
'Algorithms in C' is where i learnt my craft).
tom
--
Things fall apart - it's scientific
More information about the Python-list
mailing list