Question

I'm working with the Google Maps API and feel there is a better way to search through the panorama images other then a massive switch statement. I would think that using an external hash table would be far more efficient and much easier to maintain. Each image has a unique panoID, which I would be able to define. Reading up on hash tables, I believe I am correct in saying I could make a table and perfect function to get the data I need in constant time. Is there a good resource on how to build this? I am not experienced with hashing at all.

My logic goes as this: Each image is saved in a directory as sometext_panoID.jpg, where sometext is a string and panoID is anything I want it to be. When initializing the data in the aforementioned switch statement, all the switching is done according to the panoID and the other metadata is accessed there. For example:

switch(panoID) {
    case "test1":
      links.push({
        description : "TEST2",
        pano : "test2",
        heading : 70
      });
      break;
    case "test2":
      links.push({
        description : "TEST1",
        pano : "test1",
        heading : 125
      });
      links.push({
        description : "TEST3",
        pano : "test3",
        heading : 0
      });
      break;
      case "test3":
      links.push({
        description : "TEST2",
        pano : "test2",
        heading : 0
      });
      break;
  }

Since I will know all the panoIDs, and will have no need to sort, add, or otherwise change anything once the table is built, I feel there is a way to make a perfect hash but don't really know where to start. Any tips? Thank's a bunch in advance

Was it helpful?

Solution

Actually, simple plain objects with their properties are implemented as hash tables underneath. So there is no need to read about hashing algorithms, you can have that work done by your JS engine:

var table = {
    "test1": [
      {
        description : "TEST2",
        pano : "test2",
        heading : 70
      }
    ],
    "test2": [
      {
        description : "TEST1",
        pano : "test1",
        heading : 125
      },
      {
        description : "TEST3",
        pano : "test3",
        heading : 0
      }
    ],
    "test3": [
      {
        description : "TEST2",
        pano : "test2",
        heading : 0
      }
    ]
};

var itemstobepushed = table[panoID];
[].push.apply(links, itemstobepushed);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top