On Aug 5, 6:56 am, "Will Pierce" <
jwi...@gmail.com> wrote:
> Torbjörn, Thanks for the tip!
> This definitely will encode a dictionary, but what would the resulting
> object look like, in terms of accessor methods...? A Dictionary message
> would look like a list container/sequence, with a series of 2 element lists
> within it (just as you defined)... Duplicate keys could creep in, and if I'm
> not mistaken, access would literally require foo.key() and foo.value() to
> get the key/value pair, rather than a more native-appearing data
> hash/dictionary structure like foo[key]=value (perl: $foo{$key}=$value;)
>
> It's actually an interesting problem- how do you efficiently encode a
> dictionary/hash? I love the tight encoding of integers (the zig-zag
> method), and so it's an interesting problem to try and encode a
> dictionary/hash with as much intentionally tight encoding, versus the
> obvious angles... Any ideas?
Sorry, I guess I wasn't clear what mine actually does. Torbjörn's
dictionary encodes a list of key-value pairs, which I would recommend
for almost everything. My version uses the hash value modulo 2^16 as
the tag number for every key-value pair. This saves decoding,
rehashing, and filling a new in-memory hash, because the PB in-memory
representation itself is the hash. In other words, you can use the
interface you wanted: foo[key]=value. It also makes it much easier to
check for duplicate values (an O(n^2) operation), since you only have
to check the same bucket. Of course, if you're transmitting it over a
wire, both endpoints have to agree on a hashing algorithm, and how
many possible buckets to reserve (you can use much fewer than 2^16 -
best is twice the expected number of key-value pairs).