private function find_routes($org, $dest, &$flights)
 {
     $result = array();
     $queue = new SplPriorityQueue();
     foreach ($flights as $flight) {
         if ($flight['org_id'] == $org) {
             $route = new Route($this->route_opts);
             $num_seats = Flight::get_open_seats_on_flight($flight['flight_id'], $this->user);
             $route->add_flight($flight, $num_seats);
             $queue->insert($route, $route->get_joy());
         }
     }
     //BFS to find all routes that take < 10 hours
     $count = 0;
     while ($queue->count() > 0 && $count < $this->opts['max_results']) {
         $cur_route = $queue->extract();
         if ($cur_route->get_dest() == $dest) {
             $result[] = $cur_route;
             $count++;
             continue;
         }
         foreach ($flights as $flight) {
             if (!array_key_exists($flight['dest_id'], $cur_route->visited) && $flight['org_id'] == $cur_route->get_dest() && $flight['e_depart_time'] > 30 * 60 + $cur_route->get_arrival_time()) {
                 $new_route = $cur_route->copy();
                 $num_seats = Flight::get_open_seats_on_flight($flight['flight_id'], $this->user);
                 $new_route->add_flight($flight, $num_seats);
                 if ($new_route->get_trip_time() < 24 * 60 * 60 && $new_route->seats >= $this->opts['passengers']) {
                     $queue->insert($new_route, $new_route->get_joy());
                 }
             }
         }
     }
     return $result;
 }
 private function find_routes($org, $dest, &$flights)
 {
     $result = array();
     $queue = new SplPriorityQueue();
     foreach ($flights as $flight) {
         if ($flight['org_id'] == $org) {
             $route = new Route($this->route_opts);
             $route->add_flight($flight);
             array_push($this->id, $flight['flight_id']);
             $queue->insert($route, $route->get_joy());
         }
     }
     //BFS to find all routes that take < 10 hours
     $count = 0;
     while ($queue->count() > 0 && $count < $this->opts['max_results']) {
         $cur_route = $queue->extract();
         if ($cur_route->get_dest() == $dest) {
             $result[] = $cur_route;
             $count++;
             continue;
         }
         foreach ($flights as $flight) {
             if ($flight['org_id'] == $cur_route->get_dest() && $flight['e_depart_time'] > 30 * 60 + $cur_route->get_arrival_time()) {
                 $new_route = $cur_route->copy();
                 $new_route->add_flight($flight);
                 array_push($this->id, $flight['flight_id']);
                 if ($new_route->get_trip_time() < 24 * 60 * 60) {
                     $queue->insert($new_route, $new_route->get_joy());
                 }
             }
         }
     }
     return $result;
 }
Beispiel #3
0
function encode($symb2freq)
{
    $heap = new SplPriorityQueue();
    // Instancia fila de prioridades utilizando max heap.
    $heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
    // define o modo de extração no caso extrai array com a prioridade
    foreach ($symb2freq as $sym => $wt) {
        $heap->insert(array($sym => ''), -$wt);
    }
    while ($heap->count() > 1) {
        $lo = $heap->extract();
        // extraio o minimo
        $hi = $heap->extract();
        // extraio o minimo
        foreach ($lo['data'] as &$x) {
            $x = '0' . $x;
        }
        foreach ($hi['data'] as &$x) {
            $x = '1' . $x;
        }
        $heap->insert($lo['data'] + $hi['data'], $lo['priority'] + $hi['priority']);
    }
    $result = $heap->extract();
    return $result['data'];
}
Beispiel #4
0
	/**
	 * Get best matching locking method
	 *
	 * @param string $id ID to identify this lock in the system
	 * @param int $capabilities LockingStrategyInterface::LOCK_CAPABILITY_* elements combined with bit-wise OR
	 * @return LockingStrategyInterface Class name for a locking method
	 * @throws LockCreateException if no locker could be created with the requested capabilities
	 */
	public function createLocker($id, $capabilities = LockingStrategyInterface::LOCK_CAPABILITY_EXCLUSIVE) {
		$queue = new \SplPriorityQueue();

		/** @var LockingStrategyInterface $method */
		foreach ($this->lockingStrategy as $method => $_) {
			$supportedCapabilities = $capabilities & $method::getCapabilities();
			if ($supportedCapabilities === $capabilities) {
				$queue->insert($method, $method::getPriority());
			}
		}
		if ($queue->count() > 0) {
			$className = $queue->top();
			// We use 'new' here on purpose!
			// Locking might be used very early in the bootstrap process, where makeInstance() does not work
			return new $className($id);
		}
		throw new LockCreateException('Could not find a matching locking method with requested capabilities.', 1425990190);
	}
function encode($symb2freq)
{
    $heap = new SplPriorityQueue();
    $heap->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
    foreach ($symb2freq as $sym => $wt) {
        $heap->insert(array($sym => ''), -$wt);
    }
    while ($heap->count() > 1) {
        $lo = $heap->extract();
        $hi = $heap->extract();
        foreach ($lo['data'] as &$x) {
            $x = '0' . $x;
        }
        foreach ($hi['data'] as &$x) {
            $x = '1' . $x;
        }
        $heap->insert($lo['data'] + $hi['data'], $lo['priority'] + $hi['priority']);
    }
    $result = $heap->extract();
    return $result['data'];
}
Beispiel #6
0
 private function findNearestAttributesBruteForce(ElementInterface $query, array $dataSet, $count = 1)
 {
     $smallestDistance = INF;
     $nearestElement = NULL;
     $cycles = 0;
     $nearestElsQueue = new \SplPriorityQueue();
     $nearestElements = array();
     foreach ($dataSet as $element) {
         $distance = $this->node->distance($query, $element);
         if ($distance < $smallestDistance) {
             $smallestDistance = $distance;
             $nearestElement = $element;
         }
         $priority = 1 / (1 + $distance);
         $nearestElsQueue->insert($element, $priority);
         $cycles++;
     }
     while ($count > 0 && $nearestElsQueue->count() > 0) {
         $nearestElements[] = $nearestElsQueue->extract();
         $count--;
     }
     return new \ArrayObject(array('distance' => $smallestDistance, 'element' => $nearestElement, 'elements' => $nearestElements, 'cycles' => $cycles));
 }
 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;
 }
Beispiel #8
0
<?php

$h = new SplPriorityQueue();
$h->count(1);
Beispiel #9
0
<?php

$queue = new SplPriorityQueue();
$queue->insert('first', 1);
$queue->insert('second', 2);
$queue->insert('third', 3);
$clone = clone $queue;
echo "Queue items: " . $queue->count() . PHP_EOL;
echo "Clone items: " . $clone->count() . PHP_EOL;
echo "Queue:" . PHP_EOL;
for ($i = 0; $i < 3; $i++) {
    echo ' ' . $queue->extract() . PHP_EOL;
}
echo "Queue items: " . $queue->count() . PHP_EOL;
echo "Clone items: " . $clone->count() . PHP_EOL;
echo "Clone:" . PHP_EOL;
for ($i = 0; $i < 3; $i++) {
    echo ' ' . $clone->extract() . PHP_EOL;
}
 /**
  * {@inheritdoc}
  */
 public function count()
 {
     return $this->queue->count();
 }
Beispiel #11
0
 /**
  * Returns the number of listeners in this queue.
  * 
  * @return int
  */
 public function count()
 {
     return $this->innerQueue->count();
 }
Beispiel #12
0
 private function extractNearestByCount(\SplPriorityQueue $elementsQueue, $count)
 {
     if ($count == 1) {
         return array($elementsQueue->extract()->getElement());
     }
     $elementsArray = array();
     while ($count > 0 && $elementsQueue->count() > 0) {
         $node = $elementsQueue->extract();
         if ($node->isLeaf()) {
             $elementsArray[] = $node->getElement();
             $count--;
         }
     }
     return $elementsArray;
 }