http://www.perlmonks.org?node_id=382895


in reply to Re^3: Re-orderable keyed access structure?
in thread Re-orderable keyed access structure?

If the hash values are references to the array elements (not indices), then even if I shuffle the array, I can still find the correct array elements directly through their hash keys. The references to the individual elements of the array don't change when the hash is re-ordered.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
  • Comment on Re^4: Re-orderable keyed access structure?

Replies are listed 'Best First'.
Re^5: Re-orderable keyed access structure?
by etcshadow (Priest) on Aug 14, 2004 at 04:21 UTC
    OK, so if you need to be able to get from key to payload, but not from key to index, then how about:
    • a hash of $key => $data
    • a hash of $data => $key (that is the stringification of the ref stored in $data... maybe consider making this a Tie::RefHash, if you want)
    • an array of hash keys
    ------------ :Wq Not an editor command: Wq

      Yes. That will do it. And it's cheap to maintain. Thankyou.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

      This is essentially the same as I proposed: a main data structure carrying the payload, and two auxiliary ones for bidirectionality — except you the bidirectionality in your setup is between the main and one auxiliary structure, while in mine it is between the two auxiliary structures. By doing it that there's no need for references at all.

      Makeshifts last the longest.

      I was premature in accepting this. Please see 382970 for the details.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon