{"id":1082,"date":"2010-03-16T09:22:00","date_gmt":"2010-03-16T01:22:00","guid":{"rendered":"\/?p=1082"},"modified":"2010-03-16T09:22:00","modified_gmt":"2010-03-16T09:22:00","slug":"1082","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/php\/1082.html","title":{"rendered":"\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u7684PHP\u5b9e\u73b0"},"content":{"rendered":"
\u53d1\u73b0\u4e86\u6bb5\u4f18\u79c0\u7684\u4ee3\u7801\uff0c\u8bfb\u4e86\u4e00\u4e0b\uff0c\u987a\u624b\u5199\u4e86\u4e00\u5199\u6ce8\u91ca\uff0c\u9632\u6b62\u4ee5\u540e\u518d\u770b\u7684\u65f6\u5019\u5fd8\u8bb0\u3002
\u4e3b\u8981\u5b9e\u73b0\u4e86\u4e00\u81f4\u6027\u54c8\u5e0c\u7684\u7b97\u6cd5\uff0cphp\u754c\u76f8\u5f53\u4f18\u79c0\u7684\u4ee3\u7801\u3002\u8bfb\u5b8c\u5fc3\u65f7\u795e\u6021\u3002<\/p>\n
view plaincopy to clipboardprint? \/** \/** \/** \/** \/** \/** \/** \/** \/** $this->_targetToPositions[$target] = array(); <\/p>\n \/\/ hash the target into multiple positions $this->_positionToTargetSorted = false; return $this; \/** return $this; \/** foreach ($this->_targetToPositions[$target] as $position) unset($this->_targetToPositions[$target]); <\/p>\n $this->_targetCount–; <\/p>\n return $this; \/** \/** \/** \/\/ handle no targets \/\/ optimize single target \/\/ hash resource to a position $results = array(); $this->_sortPositionTargets(); <\/p>\n \/\/ search values above the resourcePosition \/\/ only collect the first instance of any target \/\/ return when enough results, or list exhausted \/\/ loop to start – search values below the resourcePosition \/\/ return when enough results, or list exhausted \/\/ return results after iterating through both "parts" public function __toString() \/\/ —————————————- \/** } <\/p>\n \/** \/** } <\/p>\n \/** \/* (non-phpdoc) } <\/p>\n \/** \/* (non-phpdoc) \/\/ 4 bytes of binary md5 data could also be used, but } <\/p>\n \/** \/** \/** \/** \/** \/** \/** \/** \/** \/** $this->_targetToPositions[$target] = array();<\/p>\n \/\/ hash the target into multiple positions $this->_positionToTargetSorted = false; return $this; \/** return $this; \/** foreach ($this->_targetToPositions[$target] as $position) unset($this->_targetToPositions[$target]);<\/p>\n $this->_targetCount–;<\/p>\n return $this; \/** \/** \/** \/\/ handle no targets \/\/ optimize single target \/\/ hash resource to a position $results = array(); $this->_sortPositionTargets();<\/p>\n \/\/ search values above the resourcePosition \/\/ only collect the first instance of any target \/\/ return when enough results, or list exhausted \/\/ loop to start – search values below the resourcePosition \/\/ return when enough results, or list exhausted \/\/ return results after iterating through both "parts" public function __toString() \/\/ —————————————- \/** }<\/p>\n \/** \/** }<\/p>\n \/** \/* (non-phpdoc) }<\/p>\n \/** \/* (non-phpdoc) \/\/ 4 bytes of binary md5 data could also be used, but }<\/p>\n \/**
<?php
\/**
* Flexihash – A simple consistent hashing implementation for PHP.
*
* The MIT License
*
* Copyright (c) 2008 Paul Annesley
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Paul Annesley
* @link http:\/\/paul.annesley.cc\/<\/a>
* @copyright Paul Annesley, 2008
* @comment by MyZ (http:\/\/blog.csdn.net\/mayongzhan<\/a>)
*\/<\/p>\n
* A simple consistent hashing implementation with pluggable hash algorithms.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash
{ <\/p>\n
* The number of positions to hash each target to.
*
* @var int
* @comment \u865a\u62df\u8282\u70b9\u6570,\u89e3\u51b3\u8282\u70b9\u5206\u5e03\u4e0d\u5747\u7684\u95ee\u9898
*\/
private $_replicas = 64; <\/p>\n
* The hash algorithm, encapsulated in a Flexihash_Hasher implementation.
* @var object Flexihash_Hasher
* @comment \u4f7f\u7528\u7684hash\u65b9\u6cd5 : md5,crc32
*\/
private $_hasher; <\/p>\n
* Internal counter for current number of targets.
* @var int
* @comment \u8282\u70b9\u8bb0\u6570\u5668
*\/
private $_targetCount = 0; <\/p>\n
* Internal map of positions (hash outputs) to targets
* @var array { position => target, … }
* @comment \u4f4d\u7f6e\u5bf9\u5e94\u8282\u70b9,\u7528\u4e8elookup\u4e2d\u6839\u636e\u4f4d\u7f6e\u786e\u5b9a\u8981\u8bbf\u95ee\u7684\u8282\u70b9
*\/
private $_positionToTarget = array(); <\/p>\n
* Internal map of targets to lists of positions that target is hashed to.
* @var array { target => [ position, position, … ], … }
* @comment \u8282\u70b9\u5bf9\u5e94\u4f4d\u7f6e,\u7528\u4e8e\u5220\u9664\u8282\u70b9
*\/
private $_targetToPositions = array(); <\/p>\n
* Whether the internal map of positions to targets is already sorted.
* @var boolean
* @comment \u662f\u5426\u5df2\u6392\u5e8f
*\/
private $_positionToTargetSorted = false; <\/p>\n
* Constructor
* @param object $hasher Flexihash_Hasher
* @param int $replicas Amount of positions to hash each target to.
* @comment \u6784\u9020\u51fd\u6570,\u786e\u5b9a\u8981\u4f7f\u7528\u7684hash\u65b9\u6cd5\u548c\u9700\u62df\u8282\u70b9\u6570,\u865a\u62df\u8282\u70b9\u6570\u8d8a\u591a,\u5206\u5e03\u8d8a\u5747\u5300,\u4f46\u7a0b\u5e8f\u7684\u5206\u5e03\u5f0f\u8fd0\u7b97\u8d8a\u6162
*\/
public function __construct(Flexihash_Hasher $hasher = null, $replicas = null)
{
$this->_hasher = $hasher ? $hasher : new Flexihash_Crc32Hasher();
if (!emptyempty($replicas)) $this->_replicas = $replicas;
} <\/p>\n
* Add a target.
* @param string $target
* @chainable
* @comment \u6dfb\u52a0\u8282\u70b9,\u6839\u636e\u865a\u62df\u8282\u70b9\u6570,\u5c06\u8282\u70b9\u5206\u5e03\u5230\u591a\u4e2a\u865a\u62df\u4f4d\u7f6e\u4e0a
*\/
public function addTarget($target)
{
if (isset($this->_targetToPositions[$target]))
{
throw new Flexihash_Exception("Target ‘$target’ already exists.");
} <\/p>\n
for ($i = 0; $i < $this->_replicas; $i++)
{
$position = $this->_hasher->hash($target . $i);
$this->_positionToTarget[$position] = $target; \/\/ lookup
$this->_targetToPositions[$target] []= $position; \/\/ target removal
} <\/p>\n
$this->_targetCount++; <\/p>\n
} <\/p>\n
* Add a list of targets.
* @param array $targets
* @chainable
*\/
public function addTargets($targets)
{
foreach ($targets as $target)
{
$this->addTarget($target);
} <\/p>\n
} <\/p>\n
* Remove a target.
* @param string $target
* @chainable
*\/
public function removeTarget($target)
{
if (!isset($this->_targetToPositions[$target]))
{
throw new Flexihash_Exception("Target ‘$target’ does not exist.");
} <\/p>\n
{
unset($this->_positionToTarget[$position]);
} <\/p>\n
} <\/p>\n
* A list of all potential targets
* @return array
*\/
public function getAllTargets()
{
return array_keys($this->_targetToPositions);
} <\/p>\n
* Looks up the target for the given resource.
* @param string $resource
* @return string
*\/
public function lookup($resource)
{
$targets = $this->lookupList($resource, 1);
if (emptyempty($targets)) throw new Flexihash_Exception(‘No targets exist’);
return $targets[0];
} <\/p>\n
* Get a list of targets for the resource, in order of precedence.
* Up to $requestedCount targets are returned, less if there are fewer in total.
*
* @param string $resource
* @param int $requestedCount The length of the list to return
* @return array List of targets
* @comment \u67e5\u627e\u5f53\u524d\u7684\u8d44\u6e90\u5bf9\u5e94\u7684\u8282\u70b9,
* \u8282\u70b9\u4e3a\u7a7a\u5219\u8fd4\u56de\u7a7a,\u8282\u70b9\u53ea\u6709\u4e00\u4e2a\u5219\u8fd4\u56de\u8be5\u8282\u70b9,
* \u5bf9\u5f53\u524d\u8d44\u6e90\u8fdb\u884chash,\u5bf9\u6240\u6709\u7684\u4f4d\u7f6e\u8fdb\u884c\u6392\u5e8f,\u5728\u6709\u5e8f\u7684\u4f4d\u7f6e\u5217\u4e0a\u5bfb\u627e\u5f53\u524d\u8d44\u6e90\u7684\u4f4d\u7f6e
* \u5f53\u5168\u90e8\u6ca1\u6709\u627e\u5230\u7684\u65f6\u5019,\u5c06\u8d44\u6e90\u7684\u4f4d\u7f6e\u786e\u5b9a\u4e3a\u6709\u5e8f\u4f4d\u7f6e\u7684\u7b2c\u4e00\u4e2a(\u5f62\u6210\u4e00\u4e2a\u73af)
* \u8fd4\u56de\u6240\u627e\u5230\u7684\u8282\u70b9
*\/
public function lookupList($resource, $requestedCount)
{
if (!$requestedCount)
throw new Flexihash_Exception(‘Invalid count requested’); <\/p>\n
if (emptyempty($this->_positionToTarget))
return array(); <\/p>\n
if ($this->_targetCount == 1)
return array_unique(array_values($this->_positionToTarget)); <\/p>\n
$resourcePosition = $this->_hasher->hash($resource); <\/p>\n
$collect = false; <\/p>\n
foreach ($this->_positionToTarget as $key => $value)
{
\/\/ start collecting targets after passing resource position
if (!$collect && $key > $resourcePosition)
{
$collect = true;
} <\/p>\n
if ($collect && !in_array($value, $results))
{
$results []= $value;
} <\/p>\n
if (count($results) == $requestedCount || count($results) == $this->_targetCount)
{
return $results;
}
} <\/p>\n
foreach ($this->_positionToTarget as $key => $value)
{
if (!in_array($value, $results))
{
$results []= $value;
} <\/p>\n
if (count($results) == $requestedCount || count($results) == $this->_targetCount)
{
return $results;
}
} <\/p>\n
return $results;
} <\/p>\n
{
return sprintf(
‘%s{targets:[%s]}’,
get_class($this),
implode(‘,’, $this->getAllTargets())
);
} <\/p>\n
\/\/ private methods <\/p>\n
* Sorts the internal mapping (positions to targets) by position
*\/
private function _sortPositionTargets()
{
\/\/ sort by key (position) if not already
if (!$this->_positionToTargetSorted)
{
ksort($this->_positionToTarget, SORT_REGULAR);
$this->_positionToTargetSorted = true;
}
} <\/p>\n
* Hashes given values into a sortable fixed size address space.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
interface Flexihash_Hasher
{ <\/p>\n
* Hashes the given string into a 32bit address space.
*
* Note that the output may be more than 32bits of raw data, for example
* hexidecimal characters representing a 32bit value.
*
* The data must have 0xFFFFFFFF possible values, and be sortable by
* PHP sort functions using SORT_REGULAR.
*
* @param string
* @return mixed A sortable format with 0xFFFFFFFF possible values
*\/
public function hash($string); <\/p>\n
* Uses CRC32 to hash a value into a signed 32bit int address space.
* Under 32bit PHP this (safely) overflows into negatives ints.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash_Crc32Hasher
implements Flexihash_Hasher
{ <\/p>\n
* @see Flexihash_Hasher::hash()
*\/
public function hash($string)
{
return crc32($string);
} <\/p>\n
* Uses CRC32 to hash a value into a 32bit binary string data address space.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash_Md5Hasher
implements Flexihash_Hasher
{ <\/p>\n
* @see Flexihash_Hasher::hash()
*\/
public function hash($string)
{
return substr(md5($string), 0, 8); \/\/ 8 hexits = 32bit <\/p>\n
\/\/ performance seems to be the same.
} <\/p>\n
* An exception thrown by Flexihash.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash_Exception extends Exception
{
}
<?php
\/**
* Flexihash – A simple consistent hashing implementation for PHP.
*
* The MIT License
*
* Copyright (c) 2008 Paul Annesley
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Paul Annesley
* @link http:\/\/paul.annesley.cc\/<\/a>
* @copyright Paul Annesley, 2008
* @comment by MyZ (http:\/\/blog.csdn.net\/mayongzhan<\/a>)
*\/<\/p>\n
* A simple consistent hashing implementation with pluggable hash algorithms.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash
{<\/p>\n
* The number of positions to hash each target to.
*
* @var int
* @comment \u865a\u62df\u8282\u70b9\u6570,\u89e3\u51b3\u8282\u70b9\u5206\u5e03\u4e0d\u5747\u7684\u95ee\u9898
*\/
private $_replicas = 64;<\/p>\n
* The hash algorithm, encapsulated in a Flexihash_Hasher implementation.
* @var object Flexihash_Hasher
* @comment \u4f7f\u7528\u7684hash\u65b9\u6cd5 : md5,crc32
*\/
private $_hasher;<\/p>\n
* Internal counter for current number of targets.
* @var int
* @comment \u8282\u70b9\u8bb0\u6570\u5668
*\/
private $_targetCount = 0;<\/p>\n
* Internal map of positions (hash outputs) to targets
* @var array { position => target, … }
* @comment \u4f4d\u7f6e\u5bf9\u5e94\u8282\u70b9,\u7528\u4e8elookup\u4e2d\u6839\u636e\u4f4d\u7f6e\u786e\u5b9a\u8981\u8bbf\u95ee\u7684\u8282\u70b9
*\/
private $_positionToTarget = array();<\/p>\n
* Internal map of targets to lists of positions that target is hashed to.
* @var array { target => [ position, position, … ], … }
* @comment \u8282\u70b9\u5bf9\u5e94\u4f4d\u7f6e,\u7528\u4e8e\u5220\u9664\u8282\u70b9
*\/
private $_targetToPositions = array();<\/p>\n
* Whether the internal map of positions to targets is already sorted.
* @var boolean
* @comment \u662f\u5426\u5df2\u6392\u5e8f
*\/
private $_positionToTargetSorted = false;<\/p>\n
* Constructor
* @param object $hasher Flexihash_Hasher
* @param int $replicas Amount of positions to hash each target to.
* @comment \u6784\u9020\u51fd\u6570,\u786e\u5b9a\u8981\u4f7f\u7528\u7684hash\u65b9\u6cd5\u548c\u9700\u62df\u8282\u70b9\u6570,\u865a\u62df\u8282\u70b9\u6570\u8d8a\u591a,\u5206\u5e03\u8d8a\u5747\u5300,\u4f46\u7a0b\u5e8f\u7684\u5206\u5e03\u5f0f\u8fd0\u7b97\u8d8a\u6162
*\/
public function __construct(Flexihash_Hasher $hasher = null, $replicas = null)
{
$this->_hasher = $hasher ? $hasher : new Flexihash_Crc32Hasher();
if (!empty($replicas)) $this->_replicas = $replicas;
}<\/p>\n
* Add a target.
* @param string $target
* @chainable
* @comment \u6dfb\u52a0\u8282\u70b9,\u6839\u636e\u865a\u62df\u8282\u70b9\u6570,\u5c06\u8282\u70b9\u5206\u5e03\u5230\u591a\u4e2a\u865a\u62df\u4f4d\u7f6e\u4e0a
*\/
public function addTarget($target)
{
if (isset($this->_targetToPositions[$target]))
{
throw new Flexihash_Exception("Target ‘$target’ already exists.");
}<\/p>\n
for ($i = 0; $i < $this->_replicas; $i++)
{
$position = $this->_hasher->hash($target . $i);
$this->_positionToTarget[$position] = $target; \/\/ lookup
$this->_targetToPositions[$target] []= $position; \/\/ target removal
}<\/p>\n
$this->_targetCount++;<\/p>\n
}<\/p>\n
* Add a list of targets.
* @param array $targets
* @chainable
*\/
public function addTargets($targets)
{
foreach ($targets as $target)
{
$this->addTarget($target);
}<\/p>\n
}<\/p>\n
* Remove a target.
* @param string $target
* @chainable
*\/
public function removeTarget($target)
{
if (!isset($this->_targetToPositions[$target]))
{
throw new Flexihash_Exception("Target ‘$target’ does not exist.");
}<\/p>\n
{
unset($this->_positionToTarget[$position]);
}<\/p>\n
}<\/p>\n
* A list of all potential targets
* @return array
*\/
public function getAllTargets()
{
return array_keys($this->_targetToPositions);
}<\/p>\n
* Looks up the target for the given resource.
* @param string $resource
* @return string
*\/
public function lookup($resource)
{
$targets = $this->lookupList($resource, 1);
if (empty($targets)) throw new Flexihash_Exception(‘No targets exist’);
return $targets[0];
}<\/p>\n
* Get a list of targets for the resource, in order of precedence.
* Up to $requestedCount targets are returned, less if there are fewer in total.
*
* @param string $resource
* @param int $requestedCount The length of the list to return
* @return array List of targets
* @comment \u67e5\u627e\u5f53\u524d\u7684\u8d44\u6e90\u5bf9\u5e94\u7684\u8282\u70b9,
* \u8282\u70b9\u4e3a\u7a7a\u5219\u8fd4\u56de\u7a7a,\u8282\u70b9\u53ea\u6709\u4e00\u4e2a\u5219\u8fd4\u56de\u8be5\u8282\u70b9,
* \u5bf9\u5f53\u524d\u8d44\u6e90\u8fdb\u884chash,\u5bf9\u6240\u6709\u7684\u4f4d\u7f6e\u8fdb\u884c\u6392\u5e8f,\u5728\u6709\u5e8f\u7684\u4f4d\u7f6e\u5217\u4e0a\u5bfb\u627e\u5f53\u524d\u8d44\u6e90\u7684\u4f4d\u7f6e
* \u5f53\u5168\u90e8\u6ca1\u6709\u627e\u5230\u7684\u65f6\u5019,\u5c06\u8d44\u6e90\u7684\u4f4d\u7f6e\u786e\u5b9a\u4e3a\u6709\u5e8f\u4f4d\u7f6e\u7684\u7b2c\u4e00\u4e2a(\u5f62\u6210\u4e00\u4e2a\u73af)
* \u8fd4\u56de\u6240\u627e\u5230\u7684\u8282\u70b9
*\/
public function lookupList($resource, $requestedCount)
{
if (!$requestedCount)
throw new Flexihash_Exception(‘Invalid count requested’);<\/p>\n
if (empty($this->_positionToTarget))
return array();<\/p>\n
if ($this->_targetCount == 1)
return array_unique(array_values($this->_positionToTarget));<\/p>\n
$resourcePosition = $this->_hasher->hash($resource);<\/p>\n
$collect = false;<\/p>\n
foreach ($this->_positionToTarget as $key => $value)
{
\/\/ start collecting targets after passing resource position
if (!$collect && $key > $resourcePosition)
{
$collect = true;
}<\/p>\n
if ($collect && !in_array($value, $results))
{
$results []= $value;
}<\/p>\n
if (count($results) == $requestedCount || count($results) == $this->_targetCount)
{
return $results;
}
}<\/p>\n
foreach ($this->_positionToTarget as $key => $value)
{
if (!in_array($value, $results))
{
$results []= $value;
}<\/p>\n
if (count($results) == $requestedCount || count($results) == $this->_targetCount)
{
return $results;
}
}<\/p>\n
return $results;
}<\/p>\n
{
return sprintf(
‘%s{targets:[%s]}’,
get_class($this),
implode(‘,’, $this->getAllTargets())
);
}<\/p>\n
\/\/ private methods<\/p>\n
* Sorts the internal mapping (positions to targets) by position
*\/
private function _sortPositionTargets()
{
\/\/ sort by key (position) if not already
if (!$this->_positionToTargetSorted)
{
ksort($this->_positionToTarget, SORT_REGULAR);
$this->_positionToTargetSorted = true;
}
}<\/p>\n
* Hashes given values into a sortable fixed size address space.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
interface Flexihash_Hasher
{<\/p>\n
* Hashes the given string into a 32bit address space.
*
* Note that the output may be more than 32bits of raw data, for example
* hexidecimal characters representing a 32bit value.
*
* The data must have 0xFFFFFFFF possible values, and be sortable by
* PHP sort functions using SORT_REGULAR.
*
* @param string
* @return mixed A sortable format with 0xFFFFFFFF possible values
*\/
public function hash($string);<\/p>\n
* Uses CRC32 to hash a value into a signed 32bit int address space.
* Under 32bit PHP this (safely) overflows into negatives ints.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash_Crc32Hasher
implements Flexihash_Hasher
{<\/p>\n
* @see Flexihash_Hasher::hash()
*\/
public function hash($string)
{
return crc32($string);
}<\/p>\n
* Uses CRC32 to hash a value into a 32bit binary string data address space.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash_Md5Hasher
implements Flexihash_Hasher
{<\/p>\n
* @see Flexihash_Hasher::hash()
*\/
public function hash($string)
{
return substr(md5($string), 0, 8); \/\/ 8 hexits = 32bit<\/p>\n
\/\/ performance seems to be the same.
}<\/p>\n
* An exception thrown by Flexihash.
*
* @author Paul Annesley
* @package Flexihash
* @licence http:\/\/www.opensource.org\/licenses\/mit-license.php<\/a>
*\/
class Flexihash_Exception extends Exception
{
}<\/p>\n