in reply to Encoding function needed
You might consider simply using a hash. You don't specify any requirements on how the mapping is done, so it's possible you don't actually care about the actual number you're mapping to. If so, then there's little difference between:
my ($idx, @array, $a_big_num, $some_data); # Store the data $idx = map_func($a_big_num); $array[$idx] = $some_data; # Fetch the data $idx = map_func($a_big_num); $some_data = $array[$idx];
and
my (%hash, $a_big_num, $some_data); # Store $hash{$a_big_num} = $some_data; # Fetch $some_data = $hash{$a_big_num};
The only real differences between them pop up if (a) you need to retain the order of insertion/deletion or (b) if you actually need to know the slot number ($idx) somewhere in your code. I'm guessing you don't need either, though, because you have absolutely no stated requirements on your mapping function that indicate it.
In fact, you have *so* *few) requirements specified on your mapping function, that this one would meet all stated requirements:
sub map_func { return 7 }
Before you complain about the fact that there are collisions, you'd have to deal with that in any case because when you map a billion different integers into a thousand, your mapping function will always return collisions unless it's something like:
sub map_func { state %indexes; state $idx; my $a_big_num = shift; return $indexes{$a_big_num} if exists $indexes{$a_big_num}; die "Ran out of slots!" if keys %indexes > 1000; return $indexes{$a_big_num}=$idx++; }
But in this case, we're using a hash anyway, so there's little to be gained over just using the hash directly. (Again, assuming you don't need the aforementioned conditions (a) or (b).)
...roboticus
When your only tool is a hammer, all problems look like your thumb.
|
|---|