PHP SPL 資料結構筆記摘要

來源:互聯網
上載者:User
php SPL(Standard PHP Library) 主要用到的資料結構有:雙向鏈表(SplDoublyLinkedLis),棧(SplStack),隊列(SplQueue),堆(SplHeap),大根堆(SplMaxHeap),小根堆(SplMinHeap), 優先順序隊列(SplPriorityQueue),固定數組(SplFixedArray),Object Storage Service(SplStorageObject)。

SplDoublyLinkedList

資料結構:(c代碼參考PHP7.0.0)

typedef struct _spl_ptr_llist_element {    struct _spl_ptr_llist_element *prev;    struct _spl_ptr_llist_element *next;    int                            rc;      // 引用次數    void                          *data;    // 資料類型} spl_ptr_llist_element;typedef struct _spl_ptr_llist {    spl_ptr_llist_element   *head;      spl_ptr_llist_element   *tail;    spl_ptr_llist_dtor_func  dtor;    // 刪除元素   引用-1    spl_ptr_llist_ctor_func  ctor;    // 建立元素   引用+1    int count;                        // 元素個數} spl_ptr_llist;struct _spl_dllist_object {    zend_object            std;    spl_ptr_llist         *llist;    int                    traverse_position;    spl_ptr_llist_element *traverse_pointer;    zval                  *retval;    int                    flags;    zend_function         *fptr_offset_get;    zend_function         *fptr_offset_set;    zend_function         *fptr_offset_has;    zend_function         *fptr_offset_del;    zend_function         *fptr_count;    zend_class_entry      *ce_get_iterator;    HashTable             *debug_info;};

雙向鏈表SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {/* 方法 */public __construct ( void )public void add ( mixed $index , mixed $newval )public mixed bottom ( void )public int count ( void )public mixed current ( void )public int getIteratorMode ( void )public bool isEmpty ( void )public mixed key ( void )public void next ( void )public bool offsetExists ( mixed $index )public mixed offsetGet ( mixed $index )public void offsetSet ( mixed $index , mixed $newval )public void offsetUnset ( mixed $index )public mixed pop ( void )public void prev ( void )public void push ( mixed $value )public void rewind ( void )public string serialize ( void )public void setIteratorMode ( int $mode )public mixed shift ( void )      // 刪除第一個元素public mixed top ( void )public void unserialize ( string $serialized )public void unshift ( mixed $value )   //將value插入的第一個元素,原來的第一個元素不刪除public bool valid ( void )}

需要注意的是:

add 是5.5+版本添加的函數,其它都是5.3+

從資料結構中可以看出,雖然許多函數中都包含有直接索引第index 個元素,如果不在範圍則拋出OutOfRangeException異常,實際實現時時間複雜度為O(n)。

具體函數詳細可參考php官方手冊

以上就介紹了PHP SPL 資料結構筆記摘要,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.