D
Don Bruder
A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I've got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.
Dictionaries/"dict" types...
Am I understanding/interpreting correctly when I go with the idea that a
"dict" variable can be looked at as (effectively) two parallel arrays?
Something like this C construct:
struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}
Where "ValType" would be likely be a union, to allow
assigning/retrieving the actual values correctly depending on what their
individiual types were, and "<somenumber>" is just that: Some
more-or-less arbitrarily selected value that provides enough space to
handle all expected numbers of key/value pairs?
Similarly, would something like:
struct DictStruct
{
char Key[25];
ValType Value;
DictStruct *PrevDictEntry;
DictStruct *NextDictEntry;
}
(again, ValType would probably be a union to cover each type of value
that might be wanted in the dictionary - but in this case, Key directly
contains the Key's name) then building a double-linked list of struct
DictStructs end up behaving at least reasonably like a Python "dict"?
I expect that either will be a reasonable facsimile of how Python
handles dicts, with, of course, the corresponding overhead (which may be
surprisingly little, I'm thinking... but I have yet to code that part,
so I may find out it's going to be a nightmare) of "We want the value
that goes with key, so we've got to walk the KeyArray[] until we find a
match, then extract the value from the other array", or a similar
procedure for the doubly-linked list.
Or am I way out in left field with the way I'm RTFM?
(Note 1)
Along with quite a bit of entirely unwelcome "Python evangelism" --
Which part of "I don't care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn't clear enough for those of
you who took it upon yourselves to preach (including the idiot who
decided to spew a relatively impressive (as such things go) string of
insults about how stupid I am, how far up my ass my head is, and how
pathetic my family all the way back to Adam must be for producing such a
throwback because I've got no interest in adopting "the perfect
language...") at me on the merits of Python, both on group and
in-mailbox?
answers (Note 1) but now I've got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.
Dictionaries/"dict" types...
Am I understanding/interpreting correctly when I go with the idea that a
"dict" variable can be looked at as (effectively) two parallel arrays?
Something like this C construct:
struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}
Where "ValType" would be likely be a union, to allow
assigning/retrieving the actual values correctly depending on what their
individiual types were, and "<somenumber>" is just that: Some
more-or-less arbitrarily selected value that provides enough space to
handle all expected numbers of key/value pairs?
Similarly, would something like:
struct DictStruct
{
char Key[25];
ValType Value;
DictStruct *PrevDictEntry;
DictStruct *NextDictEntry;
}
(again, ValType would probably be a union to cover each type of value
that might be wanted in the dictionary - but in this case, Key directly
contains the Key's name) then building a double-linked list of struct
DictStructs end up behaving at least reasonably like a Python "dict"?
I expect that either will be a reasonable facsimile of how Python
handles dicts, with, of course, the corresponding overhead (which may be
surprisingly little, I'm thinking... but I have yet to code that part,
so I may find out it's going to be a nightmare) of "We want the value
that goes with key, so we've got to walk the KeyArray[] until we find a
match, then extract the value from the other array", or a similar
procedure for the doubly-linked list.
Or am I way out in left field with the way I'm RTFM?
(Note 1)
Along with quite a bit of entirely unwelcome "Python evangelism" --
Which part of "I don't care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn't clear enough for those of
you who took it upon yourselves to preach (including the idiot who
decided to spew a relatively impressive (as such things go) string of
insults about how stupid I am, how far up my ass my head is, and how
pathetic my family all the way back to Adam must be for producing such a
throwback because I've got no interest in adopting "the perfect
language...") at me on the merits of Python, both on group and
in-mailbox?