function calculate_score()
{
    global $score_board;
    global $TOTAL_P_SCORE;
    global $score_P_ratio;
    $qCur = new SplPriorityQueue();
    $qExpire = new SplPriorityQueue();
    $total_p_score = $TOTAL_P_SCORE;
    $multiplier = 0.01;
    $qCur->setExtractFlags(SplPriorityQueue::EXTR_DATA);
    $qExpire->setExtractFlags(SplPriorityQueue::EXTR_DATA);
    foreach ($score_board as $k => $r) {
        ///// f_score
        if (IsValidScoreboardEntry($k)) {
            $score_board[$k]["func"] = $score_board[$k]["check_pattern"] * pow(0.9, $score_board[$k]["mem_error"]);
            if ($score_board[$k]["mem_heap"] > 0) {
                $score_board[$k]["func"] *= ($score_board[$k]["mem_heap"] - $score_board[$k]["heap_lost"]) / $score_board[$k]["mem_heap"];
            }
            if ($r["priority"] > 0) {
                $qCur->insert($k, $r["priority"]);
            }
        } else {
            $score_board[$k]["func"] = 0;
        }
        ////// init p_score
        $score_board[$k]["p_score"] = 0;
    }
    while ($qCur->valid() && $total_p_score > 0) {
        while ($qCur->valid()) {
            $idx = $qCur->extract();
            $s = $score_board[$idx]["priority"] * $multiplier;
            // alloted score in the scheduling cycle
            if ($s > 100 - $score_board[$idx]["p_score"]) {
                $s = 100 - $score_board[$idx]["p_score"];
            }
            $score_board[$idx]["p_score"] += $s;
            $total_p_score -= $s;
            if ($score_board[$idx]["p_score"] < 100) {
                $qExpire->insert($idx, $score_board[$idx]["priority"]);
            }
        }
        $qCur = $qExpire;
        $qExpire = new SplPriorityQueue();
        $qExpire->setExtractFlags(SplPriorityQueue::EXTR_DATA);
    }
    foreach ($score_board as $k => $r) {
        $score_board[$k]["score"] = 100 * $score_board[$k]["func"] * (1 - $score_P_ratio + $score_board[$k]["p_score"] * $score_P_ratio / 100);
    }
    ////////sort by score
    $tmp = array();
    foreach ($score_board as &$ma) {
        $tmp[] =& $ma["score"];
    }
    array_multisort($tmp, SORT_DESC, $score_board);
}
示例#2
0
文件: Event.php 项目: sydes/sydes
 /**
  * @param string $event
  * @param array  $params
  * @param string $context
  */
 public function trigger($event, $params = [], $context = '')
 {
     if (empty($this->events[$event])) {
         return;
     }
     $queue = new \SplPriorityQueue();
     foreach ($this->events[$event] as $index => $action) {
         $queue->insert($index, $action['prio']);
     }
     $queue->top();
     while ($queue->valid()) {
         $index = $queue->current();
         if (!empty($context)) {
             $contexts = explode(',', $this->events[$event][$index]['contexts']);
             $current_context = false;
             foreach ($contexts as $route) {
                 if (fnmatch(trim($route), $context)) {
                     $current_context = true;
                     break;
                 }
             }
         } else {
             $current_context = true;
         }
         if ($current_context && is_callable($this->events[$event][$index]['fn'])) {
             if (call_user_func_array($this->events[$event][$index]['fn'], $params) === false) {
                 break;
             }
         }
         $queue->next();
     }
 }
示例#3
0
 public function apply($filter, $value, $args = [])
 {
     if (!isset($this->filters[$filter])) {
         return $value;
     }
     if (!count($this->filters[$filter])) {
         return $this;
     }
     $queue = new \SplPriorityQueue();
     foreach ($this->filters[$filter] as $index => $action) {
         $queue->insert($index, $action["prio"]);
     }
     $queue->top();
     while ($queue->valid()) {
         $index = $queue->current();
         if (is_callable($this->filters[$filter][$index]["fn"])) {
             $value = call_user_func_array($this->filters[$filter][$index]["fn"], [$value, $args]);
         }
         $queue->next();
     }
     return $value;
 }
 protected function cleanup(OutputInterface $output)
 {
     // Recursive directory iterator
     $dir_it = function ($dir) {
         return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
     };
     // Recursive directory deletion
     $rmdir = function ($dir) use(&$dir_it) {
         foreach ($dir_it($dir) as $file) {
             if ($file->isLink()) {
                 unlink($file->getPathname());
             } elseif ($file->isDir()) {
                 rmdir($file->getPathname());
             } else {
                 unlink($file->getPathname());
             }
         }
         rmdir($dir);
     };
     $further_cleanup_required = FALSE;
     $release_folder = $this->getReleaseDirectory();
     $distributions = array_keys($this->getConfig('distribution_info'));
     // First, release directory cleanup
     foreach (new \DirectoryIterator($release_folder) as $file) {
         if ($file->isDot() || !$file->isDir() || $file->isLink()) {
             continue;
         }
         // Remove distributions which no longer exist (have been removed from the config file).
         if (!in_array($file->getBasename(), $distributions)) {
             $rmdir($file->getPathname());
             $further_cleanup_required = TRUE;
         } else {
             // Clean up timestamped release directories within each distribution.
             // The number to keep is specified by --dirs
             $directories = new \SplPriorityQueue();
             foreach (new \DirectoryIterator($file->getPathname()) as $dir) {
                 if ($dir->isDot() || !$dir->isDir() || $dir->isLink()) {
                     continue;
                 }
                 // Store directories keeping the last modified at the top.
                 $directories->insert($dir->getPathname(), $dir->getCTime());
             }
             // No further action is required for this directory.
             if ($directories->count() <= $this->dirs) {
                 continue;
             }
             $further_cleanup_required = TRUE;
             // Timestamped release directories we want to keep
             for ($i = 0; $i < $this->dirs; $i++) {
                 $directories->extract();
             }
             // Delete all the others
             $directories->top();
             while ($directories->valid()) {
                 $dir = $directories->current();
                 $rmdir($dir);
                 $directories->next();
             }
         }
     }
     // No release directories were removed so no need to do any further cleanup.
     // (all other assets should be in use)
     if (FALSE == $further_cleanup_required) {
         return FALSE;
     }
     // Get a list of all assets that are in use (in use counts as being linked to
     // from a releases directory).
     $find_symlinks = function ($dir) use(&$dir_it) {
         $active_symlinks = [];
         foreach ($dir_it($dir) as $file) {
             // Ignore latest folder symlink
             if ($file->getBasename() != 'latest' && $file->isLink()) {
                 $active_symlinks[] = basename($file->getRealPath());
             }
         }
         return array_unique($active_symlinks);
     };
     // Find all assets that are in use
     $active_symlinks = $find_symlinks($release_folder);
     // Get a list of all assets that are downloaded
     $downloads = [];
     $base_download_dir = $this->getBaseDownloadDirectory();
     $d_it = new \DirectoryIterator($base_download_dir);
     foreach ($d_it as $file) {
         if (!$file->isDot() && $file->isDir()) {
             $downloads[] = $file->getBasename();
         }
     }
     // Calculate which asset folders need to be removed from the downloads directory.
     $to_delete = array_diff($downloads, $active_symlinks);
     if (!empty($to_delete)) {
         $assets = [];
         foreach ($to_delete as $dir) {
             $rmdir($base_download_dir . '/' . $dir);
             $parts = explode('-', $dir);
             if (count($parts) == 5) {
                 $assets[] = $parts[1] . '-' . $parts[2] . '-' . $parts[3];
             } else {
                 $assets[] = $parts[1] . '-' . $parts[2];
             }
             $hash = array_pop($parts);
             $this->removeState($parts[0], $parts[1], $hash);
         }
         $this->saveAssetsDownloadState();
         $output->writeln("<comment>Removed the following assets: " . join(',', $assets) . "</comment>");
     }
     return TRUE;
 }
示例#5
0
文件: App.php 项目: nanigashi/lime
 /**
  * Trigger event.
  * @param  String $event
  * @param  Array  $params
  * @return Boolean
  */
 public function trigger($event, $params = [])
 {
     if (!isset($this->events[$event])) {
         return $this;
     }
     if (!count($this->events[$event])) {
         return $this;
     }
     $queue = new \SplPriorityQueue();
     foreach ($this->events[$event] as $index => $action) {
         $queue->insert($index, $action["prio"]);
     }
     $queue->top();
     while ($queue->valid()) {
         $index = $queue->current();
         if (is_callable($this->events[$event][$index]["fn"])) {
             if (call_user_func_array($this->events[$event][$index]["fn"], $params) === false) {
                 break;
                 // stop Propagation
             }
         }
         $queue->next();
     }
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function valid()
 {
     return $this->queue->valid();
 }