Advertisement
parent5446

Untitled

May 11th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.27 KB | None | 0 0
  1. <?php
  2.  
  3. class SplInternalItem {
  4.     public $data = null;
  5.     public $next = null;
  6.     public $prev = null;
  7. }
  8.  
  9. class SplDoublyLinkedList implements Iterator, ArrayAccess, Countable, Serializable {
  10.     const IT_MODE_LIFO = 2;
  11.  
  12.     const IT_MODE_FIFO = 0;
  13.  
  14.     const IT_MODE_DELETE = 1;
  15.  
  16.     const IT_MODE_KEEP = 0;
  17.  
  18.     protected $head = null;
  19.  
  20.     protected $tail = null;
  21.  
  22.     protected $key = 0;
  23.  
  24.     protected $current = null;
  25.  
  26.     protected $count = 0;
  27.  
  28.     protected $mode = 0;
  29.  
  30.     public function bottom() {
  31.         if ( $head === null ) {
  32.             throw new RuntimeException( 'List is empty' );
  33.         }
  34.         return $head->data;
  35.     }
  36.  
  37.     public function top() {
  38.         if ( $tail === null ) {
  39.             throw new RuntimeException( 'List is empty' );
  40.         }
  41.         return $tail->data();
  42.     }
  43.  
  44.     public function isEmpty() {
  45.         return !$this->count;
  46.     }
  47.  
  48.     public function push( $value ) {
  49.         $node = new SplInternalItem;
  50.         $node->data = $value;
  51.  
  52.         if ( $this->isEmpty() ) {
  53.             $this->head = $this->tail = $node;
  54.         } else {
  55.             $node->prev = $this->tail;
  56.             $this->tail->next = $node;
  57.             $this->tail = $node;
  58.         }
  59.  
  60.         ++$this->count;
  61.         return;
  62.     }
  63.  
  64.     public function pop() {
  65.         $retval = $this->top();
  66.         $tail = $tail->prev;
  67.         --$this->count;
  68.         return $retval;
  69.     }
  70.  
  71.     public function unshift( $value ) {
  72.         $node = new SplInternalItem;
  73.         $node->data = $value;
  74.  
  75.         if ( $this->isEmpty() ) {
  76.             $this->head = $this->tail = $node;
  77.         } else {
  78.             $node->next = $this->head;
  79.             $this->head->prev = $node;
  80.             $this->head = $node;
  81.         }
  82.  
  83.         ++$this->count;
  84.         return;
  85.     }
  86.  
  87.     public function shift() {
  88.         $retval = $this->bottom();
  89.         $head = $head->next;
  90.         --$this->count;
  91.         return $retval;
  92.     }
  93.  
  94.  
  95.     public function current() {
  96.         return $this->current->data;
  97.     }
  98.  
  99.     public function key() {
  100.         return $this->key;
  101.     }
  102.  
  103.     public function next() {
  104.         ++$this->key;
  105.  
  106.         if ( $this->mode & IT_MODE_DELETE ) {
  107.             --$this->count;
  108.             if ( $this->current->prev !== null ) {
  109.                 $this->current->prev->next = $this->current->next;
  110.             }
  111.             if ( $this->current->next !== null ) {
  112.                 $this->current->next->prev = $this->current->prev;
  113.             }
  114.         }
  115.  
  116.         if ( $this->mode & self::IT_MODE_LIFO ) {
  117.             $this->current = $this->current->prev;
  118.         } else {
  119.             $this->current = $this->current->next;
  120.         }
  121.     }
  122.  
  123.     public function prev() {
  124.         --$this->key;
  125.  
  126.         if ( $this->mode & IT_MODE_DELETE ) {
  127.             --$this->count;
  128.             if ( $this->current->prev !== null ) {
  129.                 $this->current->prev->next = $this->current->next;
  130.             }
  131.             if ( $this->current->next !== null ) {
  132.                 $this->current->next->prev = $this->current->prev;
  133.             }
  134.         }
  135.  
  136.         if ( $this->mode & self::IT_MODE_LIFO ) {
  137.             $this->current = $this->current->next;
  138.         } else {
  139.             $this->current = $this->current->prev;
  140.         }
  141.     }
  142.  
  143.     public function rewind() {
  144.         $this->key = 0;
  145.         if ( $this->mode & self::IT_MODE_LIFO ) {
  146.             $this->current = $this->tail;
  147.         } else {
  148.             $this->current = $this->head;
  149.         }
  150.     }
  151.  
  152.     public function valid() {
  153.         return $this->current !== null;
  154.     }
  155.  
  156.     public function getIteratorMode() {
  157.         return $this->mode;
  158.     }
  159.  
  160.     public function setIteratorMode( $mode ) {
  161.         $this->mode = mode;
  162.     }
  163.  
  164.  
  165.     public function offsetExists( $index ) {
  166.         return $index < $this->count;
  167.     }
  168.  
  169.     public function offsetGet( $index ) {
  170.         $node = $this->head;
  171.         for ( $i = 0; $i < $index && $node !== null; ++$i ) {
  172.             $node = $node->next;
  173.         }
  174.         return $node ? $node->data : null;
  175.     }
  176.  
  177.     public function offsetSet( $index, $newval ) {
  178.         $node = $this->head;
  179.         if ( $index === null ) {
  180.             $index = $this->count;
  181.         }
  182.         for ( $i = 0; $i < index; ++$i ) {
  183.             if ( $node->next === null ) {
  184.                 ++$this->count;
  185.                 $node->next = new SplInternalItem;
  186.                 $node->next->prev = $node;
  187.             }
  188.             $node = $node->next;
  189.         }
  190.         $node->data = $newval;
  191.     }
  192.  
  193.     public function offsetUnset( $index ) {
  194.         $node = $this->head;
  195.         for ( $i = 0; $i < $index && $node !== null; ++$i ) {
  196.             $node = $node->next;
  197.         }
  198.         if ( $node ) {
  199.             --$this->count;
  200.             $node->prev->next = $node->next;
  201.             $node->next->prev = $node->prev;
  202.         }
  203.     }
  204.  
  205.  
  206.     public function count() {
  207.         return $this->count;
  208.     }
  209.  
  210.  
  211.     public function serialize() {
  212.         $data = array();
  213.         while ( !$this->isEmpty() ) {
  214.             $data[] = $this->shift();
  215.         }
  216.         return serialize( $data );
  217.     }
  218.  
  219.     public function unserialize( $serialized ) {
  220.         $obj = new self;
  221.         foreach ( unserialize( $serialized ) as $data ) {
  222.             $this->push( $data );
  223.         }
  224.     }
  225. }
  226.  
  227. class SplQueue extends SplDoublyLinkedList implements Iterator, ArrayAccess, Countable {
  228.     public function dequeue() {
  229.         return $this->pop();
  230.     }
  231.  
  232.     public function enqueue( $value ) {
  233.         $this->unshift( $value );
  234.     }
  235.  
  236.     public function setIteratorMode( $mode ) {
  237.         if ( $mode & self::IT_MODE_LIFO ) {
  238.             throw new RuntimeException( 'SplQueue can only be used in FIFO mode.' );
  239.         }
  240.         parent::setIteratorMode( $mode );
  241.     }
  242. }
  243.  
  244. class SplStack extends SplDoublyLinkedList implements Iterator, ArrayAccess, Countable {
  245.     public function __construct() {
  246.         $this->setIteratorMode( self::IT_MODE_LIFO | self::IT_MODE_KEEP );
  247.     }
  248.  
  249.     public function setIteratorMode( $mode ) {
  250.         if ( $mode & self::IT_MODE_FIFO ) {
  251.             throw new RuntimeException( 'SplStack can only be used in LIFO mode.' );
  252.         }
  253.         parent::setIteratorMode( $mode );
  254.     }
  255. }
  256.  
  257. class SplFixedArray implements Iterator, ArrayAccess, Countable {
  258.     protected $size = 0;
  259.  
  260.     protected $data = array();
  261.  
  262.     protected $current = 0;
  263.  
  264.     public function __construct( $size = 0 ) {
  265.         $this->size = $size;
  266.         if ( $size ) {
  267.             $this->data = array_fill( 0, $size, null );
  268.         }
  269.     }
  270.  
  271.     public function current() {
  272.         return current( $this->data );
  273.     }
  274.  
  275.     public function key() {
  276.         return key( $this->data );
  277.     }
  278.  
  279.     public function next() {
  280.         next( $this->data );
  281.     }
  282.  
  283.     public function rewind() {
  284.         reset( $this->data );
  285.     }
  286.  
  287.     public function valid() {
  288.         return $this->current < $this->size;
  289.     }
  290.  
  291.  
  292.     public function offsetExists( $index ) {
  293.         return $index < $this->size;
  294.     }
  295.  
  296.     public function offsetGet( $index ) {
  297.         return $this->data[$index];
  298.     }
  299.  
  300.     public function offsetSet( $index, $newval ) {
  301.         if ( $index === null ) {
  302.             throw new RuntimeException( 'Must specify key for fixed array' );
  303.         }
  304.         $this->data[$index] = $newval;
  305.     }
  306.  
  307.     public function offsetUnset( $index ) {
  308.         $this->data[$index] = null;
  309.     }
  310.  
  311.  
  312.     public function count() {
  313.         return count( $this->data );
  314.     }
  315. }
  316.  
  317. abstract class SplHeap implements Iterator, Countable {
  318.     protected $head = null;
  319.  
  320.     protected $count = 0;
  321.  
  322.     protected $key = 0;
  323.  
  324.     abstract protected function compare( $value1, $value2 );
  325.  
  326.     public function extract() {
  327.     }
  328.  
  329.     public function insert( $value ) {
  330.     }
  331.  
  332.     public function isEmpty() {
  333.         return $this->head === null;
  334.     }
  335.  
  336.     public function recoverFromCorruption() {
  337.     }
  338.  
  339.     public function top() {
  340.         if ( $this->head === null ) {
  341.             return null;
  342.         } else {
  343.             return $this->head->data;
  344.         }
  345.     }
  346.  
  347.  
  348.     public function current() {
  349.         return $this->top();
  350.     }
  351.  
  352.     public function key() {
  353.         return $this->key;
  354.     }
  355.  
  356.     public function next() {
  357.         ++$this->key;
  358.         $this->extract();
  359.     }
  360.  
  361.     public function rewind() {
  362.         $this->key = 0;
  363.     }
  364.  
  365.     public function valid() {
  366.         return $this->head !== null;
  367.     }
  368.  
  369.  
  370.     public function count() {
  371.         return $this->count;
  372.     }
  373. }
  374.  
  375. class SplMaxHeap extends SplHeap {
  376.     protected function compare( $value1, $value2 ) {
  377.         if ( $value1 < $value2 ) {
  378.             return -1;
  379.         } elseif ( $value1 > $value2 ) {
  380.             return 1;
  381.         } else {
  382.             return 0;
  383.         }
  384.     }
  385. }
  386.  
  387. class SplMinHeap extends SplHeap {
  388.     protected function compare( $value1, $value2 ) {
  389.         if ( $value1 < $value2 ) {
  390.             return 1;
  391.         } elseif ( $value1 > $value2 ) {
  392.             return -1;
  393.         } else {
  394.             return 0;
  395.         }
  396.     }
  397. }
  398.  
  399. class SplPriorityQueue implements Iterator, Countable {
  400.     const EXTR_DATA = 1;
  401.  
  402.     const EXTR_PRIORITY = 2;
  403.  
  404.     const EXTR_BOTH = 3;
  405.  
  406.     protected $flags = 1;
  407.  
  408.     protected $heap;
  409.  
  410.     public function __construct() {
  411.         $this->heap = new SplMaxHeap;
  412.     }
  413.  
  414.     public function extract() {
  415.         $info = $this->heap->extract();
  416.         if ( $this->flags === self::EXTR_DATA ) {
  417.             return $info[1];
  418.         } elseif ( $this->flags === self::EXTR_PRIORITY ) {
  419.             return $info[0];
  420.         } else {
  421.             return $info;
  422.         }
  423.     }
  424.  
  425.     public function insert( $value, $priority ) {
  426.         $this->heap->insert( array( $value, $priority ) );
  427.     }
  428.  
  429.     public function isEmpty() {
  430.         return $this->heap->isEmpty();
  431.     }
  432.  
  433.     public function recoverFromCorruption() {
  434.         $this->heap->recoverFromCorruption();
  435.     }
  436.  
  437.     public function setExtractFlags( $flags ) {
  438.         $this->flags = $flags;
  439.     }
  440.  
  441.     public function top() {
  442.         return $this->heap->top();
  443.     }
  444.  
  445.  
  446.     public function current() {
  447.         $info = $this->heap->current();
  448.         if ( $this->flags === self::EXTR_DATA ) {
  449.             return $info[1];
  450.         } elseif ( $this->flags === self::EXTR_PRIORITY ) {
  451.             return $info[0];
  452.         } else {
  453.             return $info;
  454.         }
  455.     }
  456.  
  457.     public function key() {
  458.         return $this->heap->key();
  459.     }
  460.  
  461.     public function next() {
  462.         return $this->heap->next();
  463.     }
  464.  
  465.     public function rewind() {
  466.         return $this->heap->rewind();
  467.     }
  468.  
  469.     public function valid() {
  470.         return $this->heap->valid();
  471.     }
  472.  
  473.  
  474.     public function count() {
  475.         return $this->heap->count();
  476.     }
  477. }
  478.  
  479. class SplFileInfo {
  480.     protected $fileClass = 'SplFileObject';
  481.  
  482.     protected $infoClass = 'SplFileInfo';
  483.  
  484.     protected $filename;
  485.  
  486.     public function __construct( $filename ) {
  487.         $this->filename = $filename;
  488.     }
  489.  
  490.     public function getATime() {
  491.         return fileatime( $this->filename );
  492.     }
  493.  
  494.     public function getBasename( $suffix = '' ) {
  495.         return basename( $this->filename, $suffix );
  496.     }
  497.  
  498.     public function getCTime() {
  499.         return filectime( $this->filename );
  500.     }
  501.  
  502.     public function getExtension() {
  503.         return pathinfo( $this->filename, PATHINFO_EXTENSION );
  504.     }
  505.  
  506.     public function getFileInfo( $className = 'SplFileInfo' ) {
  507.         return $className ? new $className( $this->filename ) : new $this->infoClass( $this->filename );
  508.     }
  509.  
  510.     public function getFilename() {
  511.         return pathinfo( $this->filename, PATHINFO_FILENAME );
  512.     }
  513.  
  514.     public function getGroup() {
  515.         return filegroup( $this->filename );
  516.     }
  517.  
  518.     public function getInode() {
  519.         return fileinode( $this->filename );
  520.     }
  521.  
  522.     public function getLinkTarget() {
  523.         return readlink( $this->filename );
  524.     }
  525.  
  526.     public function getMTime() {
  527.         return filemtime( $this->filename );
  528.     }
  529.  
  530.     public function getOwner() {
  531.         return fileowner( $this->path );
  532.     }
  533.  
  534.     public function getPath() {
  535.         return pathinfo( $this->filename, PATHINFO_DIRNAME );
  536.     }
  537.  
  538.     public function getPathInfo( $className = 'SplFileInfo' ) {
  539.         return $className ? new $className( $this->getPath() ) : new $this->infoClass( $this->getPath() );
  540.     }
  541.  
  542.     public function getPathname() {
  543.         return $this->filename;
  544.     }
  545.  
  546.     public function getPerms() {
  547.         return fileperms( $this->filename );
  548.     }
  549.  
  550.     public function getRealPath() {
  551.         return realpath( $this->filename );
  552.     }
  553.  
  554.     public function getSize() {
  555.         return filesize( $this->filename );
  556.     }
  557.  
  558.     public function getType() {
  559.         return filetype( $this->filename );
  560.     }
  561.  
  562.     public function isDir() {
  563.         return is_dir( $this->filename );
  564.     }
  565.  
  566.     public function isExecutable() {
  567.         return is_executable( $this->filename );
  568.     }
  569.  
  570.     public function isFile() {
  571.         return is_file( $this->filename );
  572.     }
  573.  
  574.     public function isLink() {
  575.         return is_link( $this->filename );
  576.     }
  577.  
  578.     public function isReadable() {
  579.         return is_readable( $this->filename );
  580.     }
  581.  
  582.     public function isWritable() {
  583.         return is_writeable( $this->filename );
  584.     }
  585.  
  586.     public function openFile( $mode = 'r', $usePath = false, $context = null ) {
  587.         return new $this->fileClass( $this->filename, $mode, $usePath, $context );
  588.     }
  589.  
  590.     public function setFileClass( $className = 'SplFileObject' ) {
  591.         $this->fileClass = $className;
  592.     }
  593.  
  594.     public function setInfoClass( $className = 'SplFileInfo' ) {
  595.         $this->infoClass = $infoClass;
  596.     }
  597.  
  598.     public function __toString() {
  599.         return $this->getPathname();
  600.     }
  601. }
  602.  
  603. class SplFileObject extends SplFileInfo implements RecursiveIterator, SeekableIterator {
  604.     const DROP_NEW_LINE = 1;
  605.  
  606.     const READ_AHEAD = 2;
  607.  
  608.     const SKIP_EMPTY = 4;
  609.  
  610.     const READ_CSV = 8;
  611.  
  612.     protected $fp;
  613.  
  614.     protected $delimiter = ',';
  615.  
  616.     protected $enclosure = '"';
  617.  
  618.     protected $escape = '\\';
  619.  
  620.     protected $flags = 0;
  621.  
  622.     protected $maxLineLen = 0;
  623.    
  624.     protected $key = 0;
  625.    
  626.     protected $line = null;
  627.  
  628.     public function __construct( $filename, $mode = 'r', $usePath = false, $context = null ) {
  629.         $this->fp = fopen( $filename, $mode, $usePath, $context );
  630.     }
  631.  
  632.     public function __call( $function, array $args ) {
  633.         return call_user_func_array( $function, array( $this->fp ) + $args );
  634.     }
  635.  
  636.     public function eof() {
  637.         return feof( $this->fp );
  638.     }
  639.  
  640.     public function getCsvControl() {
  641.         return array( $delimiter, $enclosure );
  642.     }
  643.  
  644.     public function setCsvControl( $delimiter = ',', $enclosure = '"', $escape = '\\' ) {
  645.         $this->delimiter = $delimiter;
  646.         $this->enclosure = $enclosure;
  647.         $this->escape = $escape;
  648.     }
  649.  
  650.     public function getFlags() {
  651.         return $this->flags;
  652.     }
  653.  
  654.     public function setFlags( $flags ) {
  655.         $this->flags = $flags;
  656.     }
  657.  
  658.     public function getMaxLineLen() {
  659.         return $this->maxLineLen;
  660.     }
  661.  
  662.     public function setMaxLinkeLen( $maxLen ) {
  663.         $this->maxLineLen = $maxLen;
  664.     }
  665.  
  666.     public function __toString() {
  667.         return $this->current();
  668.     }
  669.  
  670.  
  671.     public function current() {
  672.         if ( $this->line === null ) {
  673.             if ( $this->flags & self::READ_CSV ) {
  674.                 $this->line = $this->fgetcsv( $this->delimiter, $this->enclosure, $this->escape );
  675.             } else {
  676.                 do {
  677.                     $this->line = $this->fgets();
  678.                 } while ( $this->flags & self::SKIP_EMPTY && $this->line === "\n" );
  679.             }
  680.  
  681.             if ( $this->flags & self::DROP_NEW_LINE ) {
  682.                 $this->line = substr( $this->line, 0, -1 );
  683.             }
  684.         }
  685.  
  686.         return $this->line;
  687.     }
  688.  
  689.     public function key() {
  690.         return $this->key;
  691.     }
  692.  
  693.     public function next() {
  694.         ++$this->key;
  695.         $this->line = null;
  696.         if ( $this->flags & self::READ_AHEAD ) {
  697.             $this->current();
  698.         }
  699.     }
  700.  
  701.     public function rewind() {
  702.         $this->key = 0;
  703.         $this->line = null;
  704.         if ( $this->flags & self::READ_AHEAD ) {
  705.             $this->current();
  706.         }
  707.     }
  708.  
  709.     public function valid() {
  710.         return $this->line !== false && $this->line !== null;
  711.     }
  712.  
  713.     public function hasChildren() {
  714.         return false;
  715.     }
  716.  
  717.     public function getChildren() {
  718.         return null;
  719.     }
  720.  
  721. }
  722.  
  723. class SplTempFileObject extends SplFileObject {
  724.     public function __construct( $maxMemory ) {
  725.         parent::construct( "php://temp/maxmemory:$maxMemory", 'r+' );
  726.     }
  727. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement