/** * Initialization * * @param array parameters * @return object HashObject */ public function __construct($setup = null, $hashes = null) { /** * Default parameters */ $params = array('strtolower' => true); /** * Applying income user parameters */ $params = Map::apply($this->map, $params, $setup); $this->params = $params; /** * Creating unique seed */ $seeds = array(); if ($hashes) { foreach ($hashes as $hash) { $seeds = array_merge((array) $seeds, (array) $hash->seed); } } do { $hash = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6); } while (in_array($hash, $seeds)); $this->seed[] = $hash; }
/** * Hash constructor. * * @param null $setup * @param null $hashes */ public function __construct($setup = null, $hashes = null) { /* * Default parameters */ $params = array('strtolower' => true); /* * Applying income user parameters */ $params = Map::apply($this->map, $params, $setup); $this->params = $params; /* * Creating unique seed */ $seeds = array(); if ($hashes) { foreach ($hashes as $hash) { $seeds = array_merge((array) $seeds, (array) $hash->seed); } } do { $hash = substr(str_shuffle(BloomFilter::alphabet), 0, 6); } while (in_array($hash, $seeds)); $this->seed[] = $hash; }
/** * Class initiation * * @param array * @return BloomObject */ public function __construct($setup = []) { /** * Default parameters */ $params = array('entries_max' => 100, 'error_chance' => 0.001, 'counter' => false, 'hash' => array('strtolower' => true)); /** * Applying income user parameters */ $params = Map::apply($this->map, $params, $setup); /** * Setting every parameters as properties */ foreach ($params as $key => $value) { $this->{$key} = $value; } /** * Calculating size of set using maximum number of entries and error chance */ if (!$this->set_size) { $this->set_size = -round($this->entries_max * log($this->error_chance) / pow(log(2), 2)); } /** * Calculating number of hashes using maximum number of entries and set size */ if (!$this->hash_count) { $this->hash_count = round($this->set_size * log(2) / $this->entries_max); } /** * Setting up HashObjects to hashes property */ for ($i = 0; $i < $this->hash_count; $i++) { $this->hashes[] = new Hash($params['hash'], $this->hashes); } /** * Initiation set */ $this->set = str_repeat('0', $this->set_size); return $this; }