Fast Hash Function for Strings

Posted by Chief on Jul 26, 2010 in Reference
No Comments

Pre-condition: str is null terminated.

int hash(const void * str) {
  int val = 0;
  const char *ptr;
  int tmp;
 
  while (*ptr != '\0') {
    val = (val << 4) + (*ptr);
    if (tmp = (val & 0xf0000000)) {
      val = val ^ (tmp >> 24);
      val = val ^ tmp;
    }
  }
  ptr++;
  return val;
}

Or in PHP:

function hash($str) { 
  var $val = 0;
  for($i=0,$n=strlen($str); $i<$n; $i++) {
    $val = ($val<<4) + ord($str{$i});
    if( $tmp = ($val & 0xf0000000) ) { 
      $val = $val ^ ($tmp>>24);
      $val = $val ^ $tmp;
    }
  }
  return $val;
}

Now all you have to do is mod the returned value by the size of the hash table and you have the target address for the first pass of a hash table with double-hashing.

Tags: , , , , , , , ,

Form Follows Function

Posted by Chief on Nov 3, 2009 in Socratic deductions, Thought Fragments
No Comments

What do you picture when you hear the phrase “form follows function”? Do you envision an architectural structure? An artist’s painting? A toothbrush?

Clearly, we mean that form is the after-thought and function is the primary objective. Or do we mean that the form of an object is driven by its function?

When looking for ideas, switch between the two mantras, “form follows function” and “form vs. function.” Does your product change? Try to answer the question: Does the product have to change?

Wordpress is the function, the theme is its form. The form can sometimes be a separate product entirely.

Tags: , ,

Copyright © 2010 cat brain.log | less All rights reserved.
Shades v1.2 theme from BuyNowShop.com.