(網絡學習)四、SPL鏈表相關

課程:https://www.imooc.com/video/2511/0php

一、SPL數據結構

stack overflow error 堆棧溢出錯誤

鏈表使用數據隊列的原理,在類對象調用上可謂「紛繁複雜」。無論鏈表底層實現原理,直接繼承 spldoublylinkedlist 使用就行。在實際中
SplStack、SplQueue繼承自雙向鏈表(類:SplDoublyLinkedList),這裏序列形象賴於 Obj->push()方法添加 所打印的序列。html

a.雙向鏈表

SplDoublyLinkedList
若是以【搭柴火堆操做順序】爲序列的話:node

SplDoublyLinkedList::rewind()   --重置指針操做:到初始(最下)
SplDoublyLinkedList::current()  --返回當前數組數據的指針
SplDoublyLinkedList::next()     --返回下個(上邊)數據的指針
SplDoublyLinkedList::prev()     --返回上個(下邊)數據的指針

push後(上邊)添綴、unshift前(下邊)添綴,pop後(上邊)刪出、shift前(下邊)刪出;top上、bottom下;valid鏈表當前值是否存在,count元素數。offsetSet設置從上到下0~n的節點值。git

current: push後(下邊)添綴 以後,若是指針指向空,須要調rewind才能得到 新current;若是以前指針有值,指針順延到新值(自動rewind)。若是current取不到值,則須要rewind。
SplDoublyLinkedList (  ##搭積木
     [0] => Y      bottom == beginning  重置指針位置0
     [1] => 10
     [2] => 1
     [3] => 2      top == end(pop)
 )

讀文字,從上至下 bottom -> top ,rewind回到offset=0位置、next-prev是+-。github

SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
        )

)
is valid ? bool(false)
push:1-2-3; unshift:10: 
SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 10
            [1] => 1
            [2] => 2
            [3] => 3
        )

)
current:--next node:
rewind current:10--next node:1

current pop:3
current:1

push:X; unshift:Y:
current pop:X
current:1
SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => Y
            [1] => 10
            [2] => 1
            [3] => 2
        )

)

offsetGet: 3 == 2; top:2; bottom:Y
SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => Y
            [1] => 10
            [2] => 1
            [3] => 2
        )

)

b.堆棧

SplStack
一樣地,【搭積木之取柴】:sql

函數方法繼承於雙向鏈表:push下壓添綴,pop上邊刪出。top上、bottom下;offsetSet設置從上到下0~n的節點值。
SplStack (  
     [0] => Y      bottom == beginning
     [1] => 10
     [2] => 1
     [3] => 2      top == end(pop)  重置指針位置
 )

疊羅漢,先進後出 top -> bottom ,rewind回到offset=n位置、next-prev是-+、offset、offget,除這5個函數外其餘與雙向鏈表一致。數組

SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
        )

)
is valid ? bool(false)
push:1-2-3; unshift:10: 
SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 10
            [1] => 1
            [2] => 2
            [3] => 3
        )

)
current:--next node:
rewind current:3--next node:2

current pop:3
current:2

push:X; unshift:Y: 
current pop:X
current:2
SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => Y
            [1] => 10
            [2] => 1
            [3] => 2
        )

)

offsetGet: 3 == Y; top:2; bottom:Y
SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => Y
            [1] => 10
            [2] => 1
            [3] => 2
        )

)

c.隊列

SplQueue
等效於雙向鏈表
SplQueue::enqueue == SplQueue::push
SplQueue::dequeue == SplQueue::shift數據結構

SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
        )

)
is valid ? bool(false)
push:1-2-3; unshift:10: 
SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 10
            [1] => 1
            [2] => 2
            [3] => 3
        )

)
current:--next node:
rewind current:10--next node:1

current pop:3
current:1

push:X; unshift:Y:
current pop:X
current:1
SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => Y
            [1] => 10
            [2] => 1
            [3] => 2
        )

)

offsetGet: 3 == 2; top:2; bottom:Y
SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => Y
            [1] => 10
            [2] => 1
            [3] => 2
        )

)

二、迭代器

a.ArrayIterator

$fuits = ['apple'=>'red apple', 'pine'=>'green pine', 'banana'=>'yellow banana'];
foreach($fruits as $key => $value){
    echo $key. ': '. $value;
}
//等效
$obj = ArrayObject($fuits);
$it = $obj->getIterator();
foreach($it as $key => $value){
    echo $key. ': '. $value;
}

$it->rewind();
while($it->vaild()){
    echo $it->key(). ': '. $it->current();
    $it->next();
}
$it->rewind();
while($it->vaild()){
    $it->seek(1); //指針移動到 offset=1
    echo $it->key(). ': '. $it->current();
    $it->next();
}
$it->ksort(); //按鍵名key排序
$it->asort(); //按鍵值value排序

b.AppendIterator

$it = new AppendIterator();
$it->append(new ArrayIterator($array_a));
$it->append(new ArrayIterator($array_b));
foreach($it as $key => $value){
    echo $key. ': '. $value;
}

c.MultipleIterator

$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mit->attachIterator(new ArrayIterator([1,2,3,4,5]), 'id');
$mit->attachIterator(new ArrayIterator(['zhangshan','lishi','wangwu','liuer','xiaqi']), 'name');
$mit->attachIterator(new ArrayIterator([23,24,32,30,26]), 'age');

$it->append();
$it->append(new ArrayIterator($array_b));
foreach($it as $key => $value){
    print_r($value);
    echo '<br/>';
}
//
Array
(
    [id] => 1
    [name] => zhangshan
    [age] => 23
)
Array
(
    [id] => 2
    [name] => lishi
    [age] => 24
)
Array
(
    [id] => 3
    [name] => wangwu
    [age] => 32
)

d.FileSystemIterator

$it = new FilesystemIterator('.');
foreach ($it as $fInfo){
    print_r([
        'mt'=>date('Y-m-d H:i:s', $fInfo->getMTime()),
        'fs'=>$fInfo->getSize()
    ]);
}

## 逐行讀取文件
$file = new \SplFileInfo('tree.php');
$fileObj = $file->openFile('r');
while($fileObj->valid()){
    echo $fileObj->fgets(); 
}
//關閉文件對象
$fileObj = null;
$file = null;

三、迭代器接口及其它

a. Countable

implements Countable => count()app

class CountMe implements Countable
{
    public function count(){ return 3;}
}
echo count(new CountMe()); //3

b.OuterTmpl

extends IteratorIterator => parent::current(),parent::key()ide

class OuterTmpl extends IteratorIterator
{
    public function current(){
        return (parent::current() * parent::current()).'_**_'. parent::key();
    }
    public function key(){
        return 'key? '. (parent::key() * 2);
    }
}
$array = [1,3,5,7,9];
$outerObj = new OuterTmpl(new ArrayIterator($array));
foreach($outerObj as $key => $value){
    echo $key. ': '. $value;
}

c.sql_autoload_register

spl_autoload_extensions(".php, .inc");
set_include_path(get_include_path() . PATH_SEPARATOR .'libs/');
var_dump(get_include_path() );
spl_autoload_register();
var_dump(new test());
var_dump(new PEople()); //類名加載不區分大小寫,這裏類名*文件名*小寫【大寫加載不了,還不清楚】

function ClassLoader($class_name){
    echo "ClassLoader load class: ". $class_name. "<br/>";
    set include_path("libs/");
    spl_autoload($class_name); //需要顯示調用
}
spl_autoload_register("ClassLoader"); //或數組['類名/類實例'=>'方法']

其餘函數

# iterator_apply
$it = new ArrayIterator(["Apples", "Bananas", "Cherries"]);
iterator_apply($it, function (Iterator $iterator) {
    echo strtoupper($iterator->current()) . "\n";
    return TRUE;
}, array($it));
foreach($it as $key => $value){
    echo '<br/>';
    echo $key. ': '. $value;
}
echo '<br/>';
# iterator_count
var_dump(iterator_count($it)); //2
#iterator_to_array
var_dump(iterator_to_array($it, true));

# class_implements — 返回指定的類實現的全部接口
# class_implements — 返回指定的類實現的全部接口

實例測試代碼上傳:
https://github.com/cffycls/cluster/tree/master/html/spl

相關文章
相關標籤/搜索