I've been toying a bit with C++. That language is probably better suited for what I want. Here's the code (for gcc):
#include <iostream> #include <ext/hash_map> #include <vector> using namespace std; /* * hash function, borrowed from * http://burtleburtle.net/bob/c/lookup3.c */ #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) #define mix(a,b,c) \ { \ a -= c; a ^= rot(c, 4); c += b; \ b -= a; b ^= rot(a, 6); a += c; \ c -= b; c ^= rot(b, 8); b += a; \ a -= c; a ^= rot(c,16); c += b; \ b -= a; b ^= rot(a,19); a += c; \ c -= b; c ^= rot(b, 4); b += a; \ } #define final(a,b,c) \ { \ c ^= b; c -= rot(b,14); \ a ^= c; a -= rot(c,11); \ b ^= a; b -= rot(a,25); \ c ^= b; c -= rot(b,16); \ a ^= c; a -= rot(c,4); \ b ^= a; b -= rot(a,14); \ c ^= b; c -= rot(b,24); \ } struct hashword { size_t operator()(const vector<uint32_t>* k) const { uint32_t a, b, c; size_t length = (*k).size(); uint32_t initval = 0; unsigned int ki = 0; /* Set up the internal state */ a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval; /*------------------------------ handle most of the key */ while (length > 3) { a += (*k)[ki++]; b += (*k)[ki++]; c += (*k)[ki++]; mix(a,b,c); length -= 3; } ki = (*k).size() - 1; /*-------------------------------- handle the last 3 uint32_t's */ switch (length) /* all the case statements fall through */ { case 3: c += (*k)[ki--]; case 2: b += (*k)[ki--]; case 1: a += (*k)[ki]; final(a,b,c) ; case 0: /* case 0: nothing left to add */ break; } /*-------------------------------------- report the result */ return c; } }; /* compare 2 vectors */ struct eqintarr { bool operator()(const vector<uint32_t>* s1, const vector<uint32_t>* +s2) const { return (*s1 == *s2); } }; int main() { __gnu_cxx::hash_map<const vector<uint32_t>*, int, hashword, eqintarr +> preds; uint32_t a[] = {1,2,3,4,5}; uint32_t b[] = {0,34567,3,5,256001}; vector<uint32_t> k1 (a, a + sizeof(a) / sizeof(uint32_t) ); vector<uint32_t> k2 (k1); vector<uint32_t> k3 (b, b + sizeof(b) / sizeof(uint32_t) ); vector<uint32_t> k4 (k3); preds[&k1] = 7; preds[&k3] = 100; cout << "k1 -> " << preds[&k2] << endl; cout << "k2 -> " << preds[&k4] << endl; }

In reply to Re: non-scalar hash key by kdejonghe
in thread non-scalar hash key by kdejonghe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.