Dave Roberts <·············@re-move.droberts.com> wrote in message news:<·······················@attbi_s01>...
> I was editing a bit of my resolver library today and decided to finally deal
> with something I have been skirting for a while. Simply, what is the best
> way to create a nice mapping table from integers<->symbols.
Whenever I see the word "mapping", I think hash tables.
> In my case, I
> want to map from symbols representing DNS types (A, MX, TXT, etc.) to the
> corresponding type codes. I need to do this both directions, from symbols
> to codes when creating a query, and from codes back to symbols when
> decoding the response. I have tried this a couple ways before this, but
> they always felt kludgy.
Here's my stab at it:
(let ((code-to-name-table (make-hash-table :test #'eql))
(name-to-code-table (make-hash-table :test #'eq)))
(defun code->name (code)
(nth-value 0 (gethash code code-to-name-table)))
(defun name->code (name)
(nth-value 0 (gethash name name-to-code-table)))
(defun add-code-to-name-mapping (code name)
(setf (gethash code code-to-name-table) name
(gethash name name-to-code-table) code)
nil))
(loop for (code . name) in '((1 . A) (2 . NS) (3 . MD)
(4 . MF) (5 . CNAME) (6 . SOA)
(7 . MB) (8 . MG) (9 . MR)
(10 . NULL) (11 . WKS) (12 . PTR)
(13 . HINFO) (14 . MINFO) (15 . MX)
(16 . TXT) (252 . AXFR) (253 . MAILB)
(254 . MAILA) (255 . ALL))
doing (add-code-to-name-mapping code name))
I probably wouldn't use macros for this problem. In general I think
people tend to only use macros when a function just won't do the job
(ie. when you need to control what gets evaluated, when and how).
Anyway... there's one young and inexperienced lisper's opinion for
you. :-)
Good luck with your project.
Justin Dubs
Justin Dubs wrote:
> Whenever I see the word "mapping", I think hash tables.
Depends. The space for each hashtable is probably large, however. If the
mapping table is huge then I agree with you. If it's smaller, you're
probably wasting lots of space. I don't know how space efficient the
hashtable implementations are in most CL dialects, but if they're anything
like Java, you only want to use them when you really have a reasonable
sized amount of stuff to store.
--
Dave Roberts, ·············@re-move.droberts.com
Slowly but surely, the programming world is finding Lisp...
http://www.findinglisp.com/blog