loadFromDataSet($dataSet); } else { $this->hashedArray = array(); $this->arrayDataSet = array(); $this->dataSet = ""; } } /** * @param $dataSet */ public function loadFromDataSet($dataSet){ $this->arrayDataSet = $this->deserialize($dataSet); if(!isset($this->arrayDataSet['n']) || !isset($this->arrayDataSet['p'])) throw new Exception("Missing range factors"); $this->n = $this->arrayDataSet['n']; $this->p = $this->arrayDataSet['p']; $this->opti = (bool)$this->arrayDataSet['o']; $this->hashedArray = $this->arrayDataSet['a']; } /** * @return string */ public function buildDataSet(): string { //save in temp array $this->arrayDataSet['a'] = $this->hashedArray; $this->arrayDataSet['p'] = $this->p; $this->arrayDataSet['n'] = $this->n; $this->arrayDataSet['o'] = (int)$this->opti; //serialize $this->dataSet = $this->serialize($this->arrayDataSet); //empty mmry $this->arrayDataSet = array(); //return return $this->dataSet; } /** * @param $dataSet * @return mixed */ protected function deserialize($dataSet){ return json_decode($dataSet,true); } /** * @param $dataSet * @return string */ protected function serialize($dataSet){ $ret = json_encode($dataSet); return $ret; } /** * @param $word * @param int $n * @param int $p * @param bool $opti * @return bool|int|string */ protected function inner_gcs_hash($word, $n = 26, $p = 64, $opti = false){ $ret = md5($word); $ret = substr($ret,24,32); $ret = intval ($ret,16); $ret = $ret % ($n*$p); if($opti) $ret = $ret - ($n+$p); return $ret; } protected function gcs_hash($word, $n = 26, $p = 64){ return $this->inner_gcs_hash($word,$n,$p,$this->opti); } /** * @return int */ public function getN(): int { return $this->n; } /** * @param int $n * @return GhostlyDataSet */ public function setN(int $n): GhostlyDataSet { $this->n = $n; return $this; } /** * @return int */ public function getP(): int { return $this->p; } /** * @param int $p * @return GhostlyDataSet */ public function setP(int $p): GhostlyDataSet { $this->p = $p; return $this; } /** * add $str to object data * * @param $str * @return GhostlyDataSet */ public function addToData($str){ //i did not called $this->isInData here cuz it would lead to a double call to gcs_hash $hashedStr = $this->gcs_hash($str); if(!in_array($hashedStr,$this->hashedArray)) $this->hashedArray[] = $hashedStr; return $this; } /** * remove $str from object data if found return false if not found, true otherwise * * @param $str * @return bool */ public function removeFromData($str){ $hashStr = $this->gcs_hash($str); $i = array_search($hashStr,$this->hashedArray); if($i === false) return false; unset($this->hashedArray[$i]); return true; } /** * check if $str is in data object * * @param $str * @return bool */ public function isInData($str){ $hashedStr = $this->gcs_hash($str); return in_array($hashedStr,$this->hashedArray); } /** * @return bool */ public function isEmpty(){ return empty($this->hashedArray); } /** * @param bool $opti * @return GhostlyDataSet */ public function setOpti(bool $opti): GhostlyDataSet { $this->opti = $opti; return $this; } /** * @return bool */ public function isOpti(): bool { return $this->opti; } }