0

Well, first of all, my question is directly related with computer science and some mathematics, rahter than programming. Suppose that i have 4 values which are for example 100,200,300,400, and those values directly map to an array index that correspond to different actions (functions) to being taken by the software. Rather than doing this with switch case or if case (since it may be a lot more) i want to point the functions at the proper locations of the array. At this point my question is that if it is possible to map those non-adjacent values mentioned above to the adjacent values, for the purpose of minimizing the size of the array, with a specific hashing function that will work like;

f(100) = 0
f(200) = 1
f(300) = 2
.
.
.

( note that f function should be invertible) Thanks.

mecid
  • 407
  • 4
  • 12
  • Is this question technology agnostic ? – Walt Jan 10 '15 at 18:44
  • I am not sure whether this kind of hashing function exist or not. But if it is, a keyword Just works for me. – mecid Jan 10 '15 at 18:47
  • `f(x) = x / 100 - 1` - or am I missing a greater point here? – 500 - Internal Server Error Jan 10 '15 at 18:54
  • The values do not need to be 100 200 300 and so on. They could be abything basically in the range of 0 and 65535 but the result routine of the hashing function should be the same as shown in the question. – mecid Jan 10 '15 at 18:59
  • You are looking for a minimal perfect hash. If your keys are known at compile-time [this post](http://stackoverflow.com/questions/6743316/minimal-perfect-hash-function) may be of interest to you. – M Oehm Jan 10 '15 at 19:22
  • Thanks @MOehm. You're right. It seems like it is what I had been looking for. – mecid Jan 10 '15 at 20:21

0 Answers0