下面小編就為大家帶來一篇老生常談PHP中的資料結構:DS擴充。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
PHP7以上才能安裝並使用該資料結構擴展,安裝比較簡單:
1. 運行指令pecl install ds
2. 在php.ini新增extension=ds.so
3. 重新啟動PHP或重載設定
#Collection Interface:包含本庫中所有資料結構通用功能的基本interface。 It guarantees that all structures are traversable, countable, and can be converted to json using json_encode().
Ds\Collection implements Traversable , Countable , JsonSerializable { /* 方法 */ abstract public void clear ( void ) abstract public Ds\Collection copy ( void ) abstract public bool isEmpty ( void ) abstract public array toArray ( void ) }
#Hashable Interface:which allows objects to be
Hashable Interface:which allows objects to be Hashable . Sequence Interface:A Sequence 相當於一個一維的數字key陣列, with the
exceptionof a few characteristics:
#Values will always be indexed as [0, 1, 2, …, size - 1].
Only allowed to access values by index in the range [0, size - 1].
Use cases:
Wherever you would use an array as a list (not concerned with keys).
Vector Class:##Vector Class:##Vector Class:
Vector是自動增長和收縮的連續緩衝區中的一系列值。它是最有效的順序結構,值的索引直接映射到緩衝區中索引,增長因子不綁定到特定的倍數或指數。其有以下優缺點:
Supports array syntax (square brackets).
Uses less overall memory than an array for the same number of values.
#Automatically frees allocated memory when its size drops low enough.
Capacity does not have to be a power of 2.
get(), set(), push(), pop() are all O(1 ).
但是shift(), unshift(), insert() and remove() are all O(n).Ds\Hashable {
/* 方法 */
abstract public bool equals ( object $obj )
abstract public mixed hash ( void )
}
「雙端佇列」的縮寫,也用於Ds\Queue中,擁有head、tail兩個指標。 The pointers can “wrap around” the end of the buffer, which avoids the need to move other values around to make room. This makes shift and unshift very fast — something a Ds\Vector can't compete with. 其具有以下優缺點:
Supports array syntax (square brackets).
Uses less overall memory than an array for the same number of values.
Automatically frees allocated memory when ops ize
Automatically frees allocated memory when ops size 一樣enough.
get(), set(), push(), pop(), shift(), and unshift() are all O(1).但Capacity must be a power of 2.insert() and remove() are O(n).
#Map Class:鍵值對的連續集合,幾乎與陣列相同。鍵可以是任何類型,但必須是唯一的。如果使用相同的鍵新增到map中,則將替換值。其擁有以下優缺點:
Keys and values can be any type, including objects.
Supports array syntax (square brackets).
##Insertion order is preserved.Performance and memory efficiency is very similar to an array.Automatically frees allocated memory when its size drops low enough.
Can't be converted to anopsy whenen objects are used as keys.
Pair Class:A pair is used by Ds\Map to pair keys with values.
Ds\Vector::allocate — Allocates enough memory for a required capacity. Ds\Vector::apply — Updates all values by applying a callback function to each value. Ds\Vector::capacity — Returns the current capacity. Ds\Vector::clear — Removes all values. Ds\Vector::construct — Creates a new instance. Ds\Vector::contains — Determines if the vector contains given values. Ds\Vector::copy — Returns a shallow copy of the vector. Ds\Vector::count — Returns the number of values in the collection. Ds\Vector::filter — Creates a new vector using a callable to determine which values to include. Ds\Vector::find — Attempts to find a value's index. Ds\Vector::first — Returns the first value in the vector. Ds\Vector::get — Returns the value at a given index. Ds\Vector::insert — Inserts values at a given index. Ds\Vector::isEmpty — Returns whether the vector is empty Ds\Vector::join — Joins all values together as a string. Ds\Vector::jsonSerialize — Returns a representation that can be converted to JSON. Ds\Vector::last — Returns the last value. Ds\Vector::map — Returns the result of applying a callback to each value. Ds\Vector::merge — Returns the result of adding all given values to the vector. Ds\Vector::pop — Removes and returns the last value. Ds\Vector::push — Adds values to the end of the vector. Ds\Vector::reduce — Reduces the vector to a single value using a callback function. Ds\Vector::remove — Removes and returns a value by index. Ds\Vector::reverse — Reverses the vector in-place. Ds\Vector::reversed — Returns a reversed copy. Ds\Vector::rotate — Rotates the vector by a given number of rotations. Ds\Vector::set — Updates a value at a given index. Ds\Vector::shift — Removes and returns the first value. Ds\Vector::slice — Returns a sub-vector of a given range. Ds\Vector::sort — Sorts the vector in-place. Ds\Vector::sorted — Returns a sorted copy. Ds\Vector::sum — Returns the sum of all values in the vector. Ds\Vector::toArray — Converts the vector to an array. Ds\Vector::unshift — Adds values to the front of the vector.
Set Class :
唯一值序列。 This implementation uses the same hash table as Ds\Map, where values are used as keys and the mapped value is ignored.其擁有以下優缺點:Values can be any type, including objects.Supports array syntax (square brackets).Insertion order is preserved.Automatically frees allocated memory when its size drops low enough.##add(), remove ) and contains() are all O(1).
但Doesn't support push(), pop(), insert(), shift(), or unshift(). get() is O(n) if there are deleted values in the buffer before the accessed index, O(1) otherwise.Stack Class:
“last in, first out”集合,只允許在結構頂部進行訪問和迭代。 Ds\Pair implements JsonSerializable {
/* 方法 */
public construct ([ mixed $key [, mixed $value ]] )
}
「first in, first out」集合,只允許在結構前端進行存取和迭代。 Ds\Stack implements Ds\Collection {
/* 方法 */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public Ds\Stack copy ( void )
public bool isEmpty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ([ mixed $...values ] )
public array toArray ( void )
}
Ds\Queue implements Ds\Collection { /* Constants */ const int MIN_CAPACITY = 8 ; /* 方法 */ public void allocate ( int $capacity ) public int capacity ( void ) public void clear ( void ) public Ds\Queue copy ( void ) public bool isEmpty ( void ) public mixed peek ( void ) public mixed pop ( void ) public void push ([ mixed $...values ] ) public array toArray ( void ) }
以上是php資料結構中的DS擴充實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!