Example #1
0
 public function setCemetery(LogootPosition $id, $vis = 1)
 {
     $sid = $id->__toString();
     if ($vis == 0) {
         unset($this->cemetery[$sid]);
     } else {
         $this->cemetery[$sid] = $vis;
     }
 }
Example #2
0
 function testPosGeneration()
 {
     $int = "5";
     $int1 = "6";
     $sid = "1";
     if ($int < $int1) {
         $id = new LogootId($int, $sid);
         $id1 = new LogootId($int1, $sid);
     } else {
         $id1 = new LogootId($int, $sid);
         $id = new LogootId($int1, $sid);
     }
     $pos = array($id);
     $pos1 = array($id1);
     $start = new LogootPosition($pos);
     $end = new LogootPosition($pos1);
     $model = manager::loadModel(0);
     $model->setPositionlist(array(0 => LogootPosition::minPosition(), 1 => $start, 2 => $end, 3 => LogootPosition::maxPosition()));
     $model->setLinelist(array(0 => "", 1 => 'start', 2 => 'end', 3 => ""));
     //$logoot = new logootEngine($model);
     $logoot = manager::getNewEngine($model);
     //insert X
     $oldContent = "start\nend";
     $newContent = "start\nline1\nend";
     $listOp1 = $logoot->generate($oldContent, $newContent);
     //$this->assertGreaterThan($end, $listOp1[0]->getLogootPosition());
     //$this->assertLessThan($end, $listOp1[0]->getLogootPosition());
     $this->assertEquals(1, $end->compareTo($listOp1[0]->getLogootPosition()));
     //$this->assertGreaterThan($start, $listOp1[0]->getLogootPosition());
     $this->assertEquals(-1, $start->compareTo($listOp1[0]->getLogootPosition()));
 }
Example #3
0
 /**
  * adapted binary search
  * -> an array with both positions in the array surrounding $position
  * @param <Object> $position LogootPosition
  * @return <array or Integer>
  */
 protected function dichoSearch(LogootPosition $position)
 {
     wfDebugLog('p2p', $this->clock . ' - function logootEngine::dichoSearch ');
     $arr = $this->model->getPositionlist();
     //avec les fausses lignes de début et de fin, on est certain
     //qu'il y a au moins deux lignes dans le tableau !
     $gauche = 0;
     $droite = count($arr) - 1;
     $val = NULL;
     // pas trouvée !
     if (count($arr) > 2) {
         $centre = round(($droite + $gauche) / 2);
         while ($centre != $droite && $centre != $gauche && !isset($val)) {
             if ($position->compareTo($arr[$centre]) == -1) {
                 $droite = $centre;
                 $centre = floor(($droite + $gauche) / 2);
             }
             if ($position->compareTo($arr[$centre]) == 1) {
                 $gauche = $centre;
                 $centre = round(($droite + $gauche) / 2);
             }
             if ($position->compareTo($arr[$centre]) == 0) {
                 $val = $centre;
             }
         }
     } else {
         /* with an array=2 */
         if ($position->compareTo($arr[$gauche]) == 0) {
             $val = $gauche;
         } elseif ($position->compareTo($arr[$droite]) == 0) {
             $val = $droite;
         }
     }
     return array(0 => $gauche, 1 => $val, 2 => $droite);
 }
Example #4
0
 /**
  * generation of a position, logoot algorithm
  * @param <LogootPosition> $p is the previous logootPosition
  * @param <LogootPosition> $q is the next logootPosition
  * @param $N number of positions generated (should be 1 in our case)
  * @param <Integer> $rep_sid session id
  * @param <Integer> $rep_clock session clock
  * @param $boundary Cf. method
  * @return <LogootPosition List> $N logootPosition(s) between $start and $end
  */
 public static function getLogootPosition(LogootPosition $p, LogootPosition $q, $nb, $rep_sid, $rep_clock = 0, $boundary = NULL)
 {
     wfDebugLog('p2p', $rep_clock . " - function LogootPosition::getLogootPosition " . $p . " / " . $q . " pour " . $nb . " position(s)");
     $one = new Math_BigInteger("1");
     // Recherche de l'interval optimal
     $index = 0;
     $interval = INT_MIN;
     $size = max($p->size(), $q->size()) + 1;
     $prefix_p = array(0 => array('cum_val' => "", 'id_str_val' => ""));
     $prefix_q = array(0 => array('cum_val' => "", 'id_str_val' => ""));
     while ($interval < $nb) {
         $index += 1;
         // recherche de prefix($p, index);
         if ($index <= $p->size()) {
             $str_val_p = str_pad($p->get($index - 1)->getInt(), DIGIT, "0", STR_PAD_LEFT);
         } else {
             $str_val_p = LPINTMINDIGIT;
         }
         $prefix_p[$index] = array('id_str_val' => $str_val_p, 'cum_val' => $prefix_p[$index - 1]['cum_val'] . $str_val_p);
         // recherche de prefix($p, index);
         if ($index <= $q->size()) {
             $str_val_q = str_pad($q->get($index - 1)->getInt(), DIGIT, "0", STR_PAD_LEFT);
         } else {
             $str_val_q = LPINTMINDIGIT;
         }
         $prefix_q[$index] = array('id_str_val' => $str_val_q, 'cum_val' => $prefix_q[$index - 1]['cum_val'] . $str_val_q);
         // Calcul de l'interval sur les nouveaux prefixes
         $BI_p = new Math_BigInteger($prefix_p[$index]['cum_val']);
         $BI_q = new Math_BigInteger($prefix_q[$index]['cum_val']);
         $BIinterval = $BI_q->subtract($BI_p)->subtract($one);
         $interval = (int) $BIinterval->__toString();
         /*wfDebugLog('p2p', $index
           . " : Prefix_p " . (string) $prefix_p[$index]['cum_val'] . '/'
           . $prefix_p[$index]['id_str_val']
           . " Prefix_q " . (string) $prefix_q[$index]['cum_val'] . '/'
           . $prefix_q[$index]['id_str_val']
           . " Interval " . $interval);*/
     }
     // Construction des identifiants
     //wfDebugLog('p2p', "N " . $nb . " Interval " . $interval . " index " . $index);
     $step = (int) $interval / $nb;
     if (isset($boundary)) {
         $step = $boundary < $step ? $boundary : $step;
     }
     $BI_step = new Math_BigInteger($step);
     $BI_r = new Math_BigInteger($prefix_p[$index]['cum_val']);
     $list = array();
     //wfDebugLog('p2p', "Step :" . $step . "/" . $boundary);
     for ($j = 1; $j <= $nb; $j++) {
         $BI_nr = $BI_r->add(new Math_BigInteger(rand(1, $step)));
         //wfDebugLog('p2p', "nr " . (string) $BI_nr . " r " . (string) $BI_r);
         // pour découper une chaine en paquets de N car : str_split($cdc, $N) !
         $str_nr0 = (string) $BI_nr;
         // on fait en sorte que le découpage soit un multiple de DIGIT pour ne pas créer de décallage
         if (strlen($str_nr0) % ($index * DIGIT) != 0) {
             $str_nr = str_pad($str_nr0, strlen($str_nr0) + ($index * DIGIT - strlen($str_nr0) % ($index * DIGIT)), "0", STR_PAD_LEFT);
         } else {
             $str_nr = $str_nr0;
         }
         //wfDebugLog('p2p', "str_nr0 " . $str_nr0 . " str_nr " . $str_nr);
         $tab_nr = str_split($str_nr, DIGIT);
         $pos = new LogootPosition();
         for ($i = 1; $i <= count($tab_nr); $i++) {
             $d = $tab_nr[$i - 1];
             //wfDebugLog('p2p', "$i#" . $prefix_p[$i]['id_str_val'] . "#" . $prefix_q[$i]['id_str_val'] . "#" . $d);
             if ($i <= $p->size() && $prefix_p[$i]['id_str_val'] == $d) {
                 $id = new LogootId($d, $p->get($i - 1)->getSessionId(), $p->get($i - 1)->getClock());
             } elseif ($i <= $q->size() && $prefix_q[$i]['id_str_val'] == $d) {
                 $id = new LogootId($d, $q->get($i - 1)->getSessionId(), $q->get($i - 1)->getClock());
             } else {
                 $id = new LogootId($d, $rep_sid, $rep_clock);
             }
             $pos->addId($id);
         }
         wfDebugLog('p2p', "===========>" . $pos->__toString());
         $list[] = $pos;
         $BI_r = $BI_r->add($BI_step);
     }
     return $list;
 }
Example #5
0
 function testNEquals()
 {
     $pos = new LogootPosition(array(LogootId::IdMin(), LogootId::IdMin()));
     $pos1 = new LogootPosition(array(LogootId::IdMax(), LogootId::IdMax()));
     $this->assertEquals('0', $pos->nEquals($pos1));
     $pos1 = new LogootPosition(array(LogootId::IdMin(), LogootId::IdMin()));
     $this->assertEquals('1', $pos->nEquals($pos1));
     $pos1 = new LogootPosition(array(LogootId::IdMin(), LogootId::IdMin(), LogootId::IdMin()));
     $this->assertEquals('0', $pos->nEquals($pos1));
 }
Example #6
0
 public function __construct()
 {
     $this->lineList = array("", "");
     $this->positionList = array(LogootPosition::minPosition(), LogootPosition::maxPosition());
 }
 /**
  * generation of a position, logoot algorithm
  * @param <Object> $start is the previous logootPosition
  * @param <Object> $end is the next logootPosition
  * @param <Integer> $N number of positions generated (should be 1 in our case)
  * @param <Object> $sid session id
  * @return <Object> a logootPosition between $start and $end
  */
 private function getLogootPosition($start, $end, $N, $sid)
 {
     $result = array();
     $Id_Max = LogootId::IdMax();
     $Id_Min = LogootId::IdMin();
     $i = 0;
     $pos = array();
     $currentPosition = new LogootPosition($pos);
     // voir constructeur
     $inf = new Math_BigInteger("0");
     $sup = new Math_BigInteger("0");
     $isInf = false;
     while (true) {
         $inf = new Math_BigInteger($start->get($i)->getInt());
         if ($isInf == true) {
             $sup = new Math_BigInteger(INT_MAX);
         } else {
             $sup = new Math_BigInteger($end->get($i)->getInt());
         }
         $tmpVal = $sup->subtract($inf);
         $tmpVal1 = $tmpVal->subtract(new Math_BigInteger("1"));
         if ($tmpVal1->compare($N) > 0) {
             //				inf = start.get(i).getInteger();
             //				sup = end.get(i).getInteger();
             break;
         }
         $currentPosition->add($start->get($i));
         $i++;
         if ($i == $start->size()) {
             $start->add($Id_Min);
         }
         if ($i == $end->size()) {
             $end->add($Id_Max);
         }
         if ($inf->compare($sup) < 0) {
             $isInf = true;
         }
     }
     $binf = $inf->add(new Math_BigInteger("1"));
     $bsup = $sup->subtract(new Math_BigInteger("1"));
     $slot = $bsup->subtract($binf);
     $stepTmp = $slot->divide($N);
     $step = $stepTmp[0];
     // quotient, [1] is the remainder
     $old = clone $currentPosition;
     if ($step->compare(new Math_BigInteger(INT_MAX)) > 0) {
         $lstep = new Math_BigInteger(INT_MAX);
         $r = clone $currentPosition;
         $tmpVal2 = $inf->random($inf, $sup);
         $r->set($i, $tmpVal2->toString(), $sid);
         $result[] = $r;
         // result est une arraylist<Position>
         return $result;
     } else {
         $lstep = $step;
     }
     if ($lstep->compare(new Math_BigInteger("0")) == 0) {
         $lstep = new Math_BigInteger("1");
     }
     $p = clone $currentPosition;
     $p->set($i, $inf->toString(), $sid);
     $tmpVal3 = (int) $N->toString();
     for ($j = 0; $j < $tmpVal3; $j++) {
         $r = clone $p;
         if (!($lstep->compare(new Math_BigInteger("1")) == 0)) {
             $tmpVal4 = new Math_BigInteger($p->get($i)->getInt());
             $tmpVal5 = $tmpVal4->add($lstep);
             // max
             $tmpVal6 = new Math_BigInteger($p->get($i)->getInt());
             // min
             $add = $tmpVal6->random($tmpVal6, $tmpVal5);
             $r->set($i, $add->toString(), $sid);
         } else {
             $r->add1($i, new Math_BigInteger("1"), $sid);
         }
         $result[] = clone $r;
         // voir
         $old = clone $r;
         $tmpVal7 = new Math_BigInteger($p->get($i)->getInt());
         $tmpVal7 = $tmpVal7->add($lstep);
         $p->set($i, $tmpVal7->toString(), $sid);
     }
     return $result;
 }
Example #8
0
 function test0()
 {
     $p = new LogootPosition(array(LogootId::IdMin()));
     $q = new LogootPosition(array(new LogootId(INT_MIN + 1, "3", 6)));
     $lp1 = LogootPosition::getLogootPosition($p, $q, 2, "3", 7, 10);
     foreach ($lp1 as $pos) {
         //echo $pos;
         $this->assertEquals('-1', $p->compareTo($pos));
         $this->assertEquals('1', $q->compareTo($pos));
     }
     $q = new LogootPosition(array(LogootId::IdMax()));
     $p = new LogootPosition(array(new LogootId(INT_MAX - 1, "3", 6)));
     $lp2 = LogootPosition::getLogootPosition($p, $q, 2, "3", 7, 10);
     foreach ($lp2 as $pos) {
         //echo $pos;
         $this->assertEquals('-1', $p->compareTo($pos));
         $this->assertEquals('1', $q->compareTo($pos));
     }
     $q = new LogootPosition(array(LogootId::IdMax()));
     $p = new LogootPosition(array(new LogootId(99, "3", 3), new LogootId(99, "3", 7)));
     $lp = LogootPosition::getLogootPosition($p, $q, 2, "3", 7, 10);
     foreach ($lp2 as $pos) {
         //echo $pos;
         $this->assertEquals('-1', $p->compareTo($pos));
         $this->assertEquals('1', $q->compareTo($pos));
     }
 }