Friday, March 20, 2009

Some common Hash Functions in Common Lisp

Here are some common hash functions in C I found online here and here. I have done the menial task of translating them to CL. I have a lot of excuses for such a lame post : blah blah blah. I made a big booper by forgetting that these functions return unsigned-byte 32 ints. After some simple pointing to this by good denizens of #lisp I changed it - that too after reading some literature. duh. If you have any suggestions how daft I am, lemme know.

;;; Hash Function by Dan Bernstein
(defun hash-DJB (str)
  (declare (type simple-string str)
    (type (unsigned-byte 32) hash)
    (optimize speed (debug 0)))
  (let ((hash 5381)) 
    (loop for x across str
     do (setf hash (ldb (byte 32 0) (+ (ldb (byte 32 0) (+ (ldb (byte 32 0) (ash hash 5)) hash)) (char-int x))))
     finally (return hash))))

;;; Hash Function by Dan Bernstein (newer)
(defun hash-DJB2 (str)
  (declare (type simple-string str)
    (type (unsigned-byte 32) hash)
    (optimize speed (debug 0)))
  (let ((hash 5381)) 
    (loop for x across str
       do (setf hash (ldb (byte 32 0) (logxor (char-int x) (* hash 33))))
       finally (return hash))))

;;; Hash Function from GAWK, a variation from the verwion from SDBM 
(defun hash-SDBM (str)
  (declare (type simple-string str)
    (type (unsigned-byte 32) hash)
    (optimize speed (debug 0)))
  (let ((hash 0))
    (loop for x across str
       do (setf hash (ldb (byte 32 0) (+ (char-int x) (ldb (byte 32 0) (ash hash 6)) (ldb (byte 32 0) (ash hash 16)) (- hash))))
       finally (return hash))))

;;; An algorithm proposed by Donald E. Knuth in The Art Of Computer Programming Volume 3
(defun hash-DEK (str)
  (declare (type simple-string str)
    (type (unsigned-byte 32) hash)
    (optimize speed (debug 0)))
    (let ((hash (length str))) 
      (loop for x across str
  do (setf hash (ldb (byte 32 0) (logxor (char-int x) (logxor (ldb (byte 32 0) (ash hash 5)) (ash hash -27)))))
  finally (return hash))))

Monday, March 16, 2009

Beware the Rhino



Finally the Rhino is here.  I named the jeep Rhino because that is what it is like - ungaily, unstoppable, difficult to control, heavy.  ..and it comes on to you. :)









My trusty old ride, the Esteem.  I have travelled far and wide with her.

Wednesday, March 11, 2009

Random scenes from the last few months.

DSC02171
Rural Scene, Devrukh.


DSC02216
Egg Bhurgi Stall, ST Stand, Kolhapur.  I reached at 0430hrs and was waiting for someone to pick me up. I had had an extremely unpleasant journey from Solapur sitting on the middle seat in the last row of an ugly bus going insanely fast on a bad road.



DSC02229
Old Building, Market, Matheran.  I had to go some place at the year end.  I went on bike to Matheran.  Tyo and Trupti had travelled by train earlier in the morning.

DSC02230
Old Red Building, Matheran.  This one I click everytime I go here.  This time from the other side and with a different tone.

DSC02231
Aman Lodge Station, Matheran.

DSC02241
Mankhurd, Mumbai.  While returning from Pune in my friend's new Esteem. hehe.

DSC02242
Sinhagad at Sunset, Sinhagad Approach Road.  I rode alone on the bike to see how it was.  The fuse on the bike blew and I did'nt have a spare.

DSC02322
Missal at Hotel Manpasand, Panhala, Kolhapur.  This is one mindblowing missal.  And he feeds with a generous heart.

DSC02328
Rajabhau Bhel Stall, Kolhapur.  Again, a mindblowing bhel.  Hez been famous for years.



All photos clicked with the Sony Ericsson K790i phone camera.