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 used as keys.
Ds/Hashable {/* 方法 */abstract public bool equals ( object $obj )abstract public mixed hash ( void )}
Sequence Interface:A Sequence 相当于一个一维的数字key数组, with the exception of 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).
A more efficient alternative to SplDoublyLinkedList and SplFixedArray.
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/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 emptyDs/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.
新闻热点
疑难解答