Skip to content

Instantly share code, notes, and snippets.

@haiiro-shimeji
Last active December 20, 2015 12:47
Show Gist options
  • Save haiiro-shimeji/4c126a880ac418a9f18b to your computer and use it in GitHub Desktop.
Save haiiro-shimeji/4c126a880ac418a9f18b to your computer and use it in GitHub Desktop.
<?php
class ReadLineIterator implements Iterator {
private $fp;
private $currentId = 0;
private $current = null;
public function __construct($fp) {
$this->fp = $fp;
$line = fgets($this->fp);
$this->current = false !== $line ? (float) $line : null;
}
public function current () {
return $this->current;
}
public function key () {
return $this->currentId;
}
public function next () {
$this->currentId++;
$line = fgets($this->fp);
$this->current = false !== $line ? (float) $line : null;
}
public function rewind () {
//not support
}
public function valid () {
return $this->current !== null;
}
}
class AssembleIterator implements Iterator {
private $it;
private $currentId = 0;
private $current = null;
private $size = 0;
private $callback = null;
public function __construct($it, $size, $callback) {
$this->it = $it;
$this->size = $size;
$this->callback = $callback;
$this->current = false !== $this->it->valid() ? $this->assemble($this->size) : null;
}
private function assemble($size) {
$block = array();
for ($i=0; $size > $i && $this->it->valid(); $i++) {
array_push($block, $this->it->current());
$this->it->next();
}
return call_user_func($this->callback, $block);
}
public function current () {
return $this->current;
}
public function key () {
return $this->currentId;
}
public function next () {
$this->currentId++;
$this->current = false !== $this->it->valid() ? $this->assemble($this->size) : null;
}
public function rewind () {
$this->it->rewind();
}
public function valid () {
return $this->current !== null;
}
}
class ConvolveIterator implements Iterator {
private $it;
private $currentId = 0;
private $current = null;
private $size = 0;
private $callback = null;
private $window = array();
public function __construct($it, $size, $callback) {
$this->it = $it;
$this->size = $size;
$this->callback = $callback;
$this->current = false !== $this->it->valid() ? $this->convolve($this->size) : null;
}
private function convolve($size) {
array_shift($this->window);
while ($size > count($this->window) && $this->it->valid()) {
array_push($this->window, $this->it->current());
$this->it->next();
}
return call_user_func($this->callback, $this->window);
}
public function current () {
return $this->current;
}
public function key () {
return $this->currentId;
}
public function next () {
$this->currentId++;
$this->current = false !== $this->it->valid() ? $this->convolve($this->size) : null;
}
public function rewind () {
$this->it->rewind();
}
public function valid () {
return $this->current !== null;
}
}
function read($fp) {
return new ReadLineIterator($fp);
}
function intervalMax(Iterator $it) {
return new AssembleIterator($it, 10, function($a) {
return max($a);
});
}
function movingAverage(Iterator $it) {
return new ConvolveIterator($it, 10, function($a) {
return array_sum($a)/count($a);
});
}
$fp = isset($argv[1]) ? fopen($argv[1], 'r') : STDIN;
foreach (intervalMax(movingAverage(read($fp))) as $value) {
echo $value."\r\n";
}
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment