欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

老生常谈PHP中的数据结构:DS扩展

程序员文章站 2024-03-11 16:44:43
php7以上才能安装和使用该数据结构扩展,安装比较简单: 1. 运行命令 pecl install ds 2. 在php.ini中添加 extension=ds.so...

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 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.

deque class:“双端队列”的缩写,也用于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 its size drops low 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 an array when objects are used as keys.

pair class:a pair is used by ds\map to pair keys with values.

ds\pair implements jsonserializable {
/* 方法 */
public __construct ([ mixed $key [, mixed $value ]] )
}

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\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 )
}

queue class:“first in, first out”集合,只允许在结构前端进行访问和迭代。

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 )
}

priorityqueue class:优先级队列与队列是非常相似的,但值以指定的优先级被推入队列,优先级最高的值总是位于队列的前面,同优先级元素“先入先出”顺序任然保留。在一个priorityqueue上递代是具有破坏性的,相当于连续弹出操作直到队列为空。implemented using a max heap.

ds\priorityqueue implements ds\collection {
/* constants */
const int min_capacity = 8 ;
/* 方法 */
public void allocate ( int $capacity )
public int capacity ( void )
public void clear ( void )
public ds\priorityqueue copy ( void )
public bool isempty ( void )
public mixed peek ( void )
public mixed pop ( void )
public void push ( mixed $value , int $priority )
public array toarray ( void )
}

以上这篇老生常谈php中的数据结构:ds扩展就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。