예제 #1
0
파일: huffman.php 프로젝트: alphaxxl/hhvm
 public static function buildTree($s)
 {
     $t = new Tree();
     $seenSymbols = array();
     $pq = new PriorityQueue();
     // Add leaf nodes for each symbol to the priority queue.
     for ($i = 0; $i < strlen($s); ++$i) {
         $c = $s[$i];
         if (array_key_exists($c, $seenSymbols)) {
             continue;
         }
         $occurrences = 0;
         for ($j = $i; $j < strlen($s); ++$j) {
             if ($s[$j] != $c) {
                 continue;
             }
             $occurrences++;
         }
         $node = new Node($c, $occurrences);
         $pq->enqueue($node);
         $t->leaves[$c] = $node;
         $seenSymbols[$c] = true;
     }
     if ($pq->size() == 0) {
         return $t;
     }
     // While there is more than one node left in the priority queue:
     while ($pq->size() > 1) {
         // Remove the two nodes of highest priority (lowest probability).
         $node1 = $pq->dequeue();
         $node2 = $pq->dequeue();
         // Create a new internal node with the two nodes as children with
         // p_total = p_1 + p_2
         $newNode = new Node(null, $node1->weight + $node2->weight);
         $newNode->left = $node1;
         $newNode->right = $node2;
         $node1->parent = $newNode;
         $node2->parent = $newNode;
         // Add the new node to the queue.
         $pq->enqueue($newNode);
     }
     // The final node in the priority queue is the root.
     $t->root = $pq->dequeue();
     return $t;
 }
예제 #2
0
             unset($in_progress[$k]);
         }
     }
     $md5top = md5($top);
     $text = curl_multi_getcontent($ch);
     Error::disableLogging();
     db_query("REPLACE INTO primitive_cache (url, content) VALUES ('%s', '%s')", $md5top, $text);
     Error::enableLogging();
     db_end_transaction('primitive_cache', $md5top);
     curl_multi_remove_handle($mch, $ch);
     curl_close($ch);
     $n_reqs_procd++;
     fputs($fp, "Daemon {$GLOBALS['client']} processed {$top}\r\n");
     Error::generate('debug', "Processed {$top}");
 } else {
     if (count($in_progress) < $max_simult_queries && $curl_stack->size() > 0) {
         $ch = $curl_stack->pop();
         $top = $completed->pop();
         $md5top = md5($top);
         db_query("INSERT IGNORE INTO primitive_cache_lock (id,locked) VALUES ('%s','0')", $md5top);
         $exists = db_affected_rows() == 0;
         // TODO: CURLOPT_TIMEVALUE and CURLOPT_TIMECONDITION on cache timestamp for Last-Modified if exists
         if (!$exists) {
             $locked = db_start_transaction('primitive_cache', $md5top, $blocking);
         } else {
             $locked = false;
         }
         if ($locked) {
             // we've got a lock, we can either start or finish
             $in_progress[] = array('ch' => $ch, 'top' => $top);
             curl_multi_add_handle($mch, $ch);