블룸 필터를 검색하는 동안 GitHub의이 간단한 PHP 클래스를 보았습니다.이 이름은 "블룸 필터"로 명명되었지만 호기심이 많은 "해시 테이블"이라고 생각합니다. 이해하기가 매우 쉽습니다.PHP 해시 키 배열
단어 파일을 읽고 각 단어에 대해 해시 배열 키를 만든 다음 해당 단어가 해시 배열에 있는지 확인할 수 있습니다.
내가 궁금한 점은 배열 키 또는 값으로 실제 단어를 저장하고 그 단어가 배열에 존재하는지 검사하는 것보다 이론적으로는 오버 헤드를 추가하고 동일한 작업을 수행하는 것입니다. 내가 빠진 것을 이해하도록 도와주세요?
<?php
class Dictionary {
private $words;
private $wordsHash;
public $hashLength;
public function __construct($filepath, $hashLength) {
$this->words = file($filepath);
$this->hashLength = $hashLength;
foreach($this->words as $word){
$this->wordsHash[$this->createHash($word)] = true;
}
echo 'words: ' . count($this->words) . ' hashes: ' . count($this->wordsHash) . "\n";
}
public function createHash($str){
$hash = substr(md5(trim($str)), 0, $this->hashLength);
return $hash;
}
public function checkDictionary($str){
$hash = $this->createHash(trim($str));
if(array_key_exists ($hash , $this->wordsHash)){
return true;
}
return false;
}
}
?>
하는 dictionary.txt 파일이 10,000 단어를 가지고, 난 그냥 데모에 대한 몇 가지가 표시됩니다
der
die
und
in
den
von
zu
das
mit
sich
des
auf
für
ist
사용 예제 :
<?php
$dictionary = new Dictionary('dictionary.txt', 30);
if($dictionary->checkDictionary('den')){
echo 'The Word den Exist in the Hash Table';
}else{
echo 'The Word den DOES NOT Exist in the Hash Table';
}
?>
해시처럼 작동하는 일반적인 php 배열로 할 수있는 것처럼 보입니다. – hackartist
@hackartist : 그게 전부였습니다.하지만 누군가이 문제를 해결해야하는 이유가있을 것이라고 생각 했습니까? – JasonDavis