views:

69

answers:

2

I'm trying to cache lat/lon address pairs from Google Maps, so I need a data structure where the key is two ints (lat and lon). What's the simplest data structure for that?

I thought of two ways so far:

Nested hash:

{37.734608 {-121.913019 "San Ramon, CA" -121.6 "Tracy, CA"}}

Concat the two to make the key:

{"37.734608,-121.913019" "San Ramon, CA" "37.734608,-121.6" "Tracy, CA"}}

Am I missing any other solutions, and which would you recommend?

+1  A: 

check out Z-order but what are you using to store this? If its an RDBMS why can't you have a 2-field primary key?

The upside of Z-order is that if you sort by it then things that are close (physically) will generally be stored close (in memory/on disk) together

tobyodavies
Hmm interesting. I'm not using an RDBMS by the way, for now I just want a simple data structure, in memory. Perhaps I misused the term "primary key".
yayitswei
depends if you need to do range/box queries or not. If you do z-order is definately the best for large disk-based datasets, otherwise a nested-hash (exact match only) or nested sorted trees (for range/box queries on in-memory small datasets) would work too...
tobyodavies
+3  A: 

As you have lisp in your tags, the simplest and most idiomatic way is to use an association-list:

;; Sample in Scheme
>  (define lat/lon (list (cons '(3.44 5.44) 
                                '("blah" "3.44,5.44" "bloo")) 
                         (cons '(37.734608 -121.913019) 
                               '("San Ramon, CA" "37.734608,-121.6" "Tracy, CA"))))

> (assoc '(3.44 5.44) lat/lon)
=> ((3.44 5.44) "blah" "3.44,5.44" "bloo")

> (assoc '(37.734608 -121.913019) lat/lon)
=> ((37.734608 -121.913019) "San Ramon, CA" "37.734608,-121.6" "Tracy, CA")
Vijay Mathew