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?
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")
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
精彩评论