N
none
(Wow, did someone's spam-bot explode in this group, or what? I think it's
trying to become self aware...)
Hi all, I am wondering if anyone has implemented something like this, and
whether they'd be willing to share the code.
In perl, "hash" is a basic type. And the language supports some really
nice (and yes, easy to abuse) functionality, like:
1) Easy lookup of "nested" hashes:
$result = $my_hash{"root"}{"branch1"}{"branch2"}{"leaf"};
2) Initializing an element forces it into existence:
$my_hash{"root"}{"branch1"}{"branch2"}{"leaf"} = 5;
That second one will work even if "root", "branch1", "branch2", and "leaf"
don't exist before the assignment, or even if the entire hash is empty, or
even if "root" and "branch1" already exist with lots of other
children/siblings, but "branch2" doesn't yet exist. It will also work for
any assignment -- you could store an int, a float, a string, or even an
array of something as a leaf. And if "leaf" already existed as some other
type, it would just be replaced with the int assignment.
I'm sure something similar could be done in C++, probably using a wrapper
around std::map that provided operator overloading for maybe the []
operator, and I guess you'd need to overload the = operator to accept any
type of data that you might want to store in a leaf. Templates, maybe?
Before I set off on a week of banging my head against a wall, I thought I'd
ask here.
Thanks!
trying to become self aware...)
Hi all, I am wondering if anyone has implemented something like this, and
whether they'd be willing to share the code.
In perl, "hash" is a basic type. And the language supports some really
nice (and yes, easy to abuse) functionality, like:
1) Easy lookup of "nested" hashes:
$result = $my_hash{"root"}{"branch1"}{"branch2"}{"leaf"};
2) Initializing an element forces it into existence:
$my_hash{"root"}{"branch1"}{"branch2"}{"leaf"} = 5;
That second one will work even if "root", "branch1", "branch2", and "leaf"
don't exist before the assignment, or even if the entire hash is empty, or
even if "root" and "branch1" already exist with lots of other
children/siblings, but "branch2" doesn't yet exist. It will also work for
any assignment -- you could store an int, a float, a string, or even an
array of something as a leaf. And if "leaf" already existed as some other
type, it would just be replaced with the int assignment.
I'm sure something similar could be done in C++, probably using a wrapper
around std::map that provided operator overloading for maybe the []
operator, and I guess you'd need to overload the = operator to accept any
type of data that you might want to store in a leaf. Templates, maybe?
Before I set off on a week of banging my head against a wall, I thought I'd
ask here.
Thanks!