老生常谈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 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扩展就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • C数据结构中串简单实例

    C数据结构中串简单实例 运行截图: 实例代码: #include "stdio.h" #include "string.h" #include "stdlib.h" #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define MAXSIZE 40 /* 存储空间初始分配量 */ typedef int Status; /* Status是函数的类型,其值是函数结果状态代码

  • java 数据结构之栈与队列

    java 数据结构之栈与队列 一:对列 队列是一种先进先出的数据结构 实现代码: package Queue; /* * 使用java构建队列,并模拟实现队列的入队和出对方法 */ public class Queue { //队列类 private int maxSize; //定义队列的长度 private int[] arrQueue; //队列 private int rear; //定义队列的尾指针 private int front; //定义队列的头指针 private int e

  • 数据结构 红黑树的详解

    数据结构 红黑树的详解 红黑树是具有下列着色性质的二叉查找树: 1.每一个节点或者着红色,或者着黑色. 2.根是黑色的. 3.如果一个节点是红色的,那么它的子节点必须是黑色. 4.从一个节点到一个NULL指针的每一条路径必须包含相同数目的黑色节点. 下面是一棵红黑树. 1.自底向上插入 通常把新项作为树叶放到树中.如果我们把该项涂成黑色,那么违反条件4,因为将会建立一条更长的黑节点路径.因此这一项必须涂成红色.如果它的父节点是黑色的,插入完成.如果父节点是红色的,那么违反条件3.在这种情况下我们

  • 数据结构 中数制转换(栈的应用)

    数据结构 中数制转换(栈的应用) 问题描述: 将一个非负的十进制整数N转换为另一个等价的基为B的B进制数的问题. 解答:按除2取余法,得到的余数依次是1.0.1.1,则十进制数转化为二进制数为1101. 分析:由于最先得到的余数是转化结果的最低位,最后得到的余数是转化结果的最高位,因此很容易用栈来解决. 代码如下: #include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct Node {

  • C++数据结构之文件压缩(哈夫曼树)实例详解

    C++数据结构之文件压缩(哈夫曼树)实例详解 概要: 项目简介:利用哈夫曼编码的方式对文件进行压缩,并且对压缩文件可以解压 开发环境:windows vs2013 项目概述:         1.压缩 a.读取文件,将每个字符,该字符出现的次数和权值构成哈夫曼树 b.哈夫曼树是利用小堆构成,字符出现次数少的节点指针存在堆顶,出现次数多的在堆底 c.每次取堆顶的两个数,再将两个数相加进堆,直到堆被取完,这时哈夫曼树也建成 d.从哈夫曼树中获取哈夫曼编码,然后再根据整个字符数组来获取出现了得字符的编

  • 数据结构 双机调度问题的实例详解

    数据结构 双机调度问题的实例详解 1.问题描述 双机调度问题,又称独立任务最优调度:用两台处理机A和B处理n个作业.设第i个作业交给机器A处理时所需要的时间是a[i],若由机器B来处理,则所需要的时间是b[i].现在要求每个作业只能由一台机器处理,每台机器都不能同时处理两个作业.设计一个动态规划算法,使得这两台机器处理完这n个作业的时间最短(从任何一台机器开工到最后一台机器停工的总的时间). 研究一个实例:n=6, a = {2, 5, 7, 10, 5, 2}, b = {3, 8, 4, 1

  • 老生常谈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_

  • 老生常谈ThinkPHP中的行为扩展和插件(推荐)

    原理分析 将标签与类之间的对应关系(如'app_init'=>array('Common\Behavior\InitHook')),通过Hook类中import或add方法,加载到Hook类中静态变量$tags中.当执行Hook中静态方法listen或者exec方法的时候(listen方法中调用了exec),实例化标签对应的类,调用相应的方法(如果是插件,调用传递的方法,如果是行为,调用run方法). Hook中exec方法定义如下: static public function exec($n

  • 老生常谈ES6中的类

    前面的话 大多数面向对象的编程语言都支持类和类继承的特性,而JS却不支持这些特性,只能通过其他方法定义并关联多个相似的对象,这种状态一直延续到了ES5.由于类似的库层出不穷,最终还是在ECMAScript 6中引入了类的特性.本文将详细介绍ES6中的类 ES5近似结构 在ES5中没有类的概念,最相近的思路是创建一个自定义类型:首先创建一个构造函数,然后定义另一个方法并赋值给构造函数的原型 function PersonType(name) { this.name = name; } Person

  • 老生常谈java中的fail-fast机制

    在JDK的Collection中我们时常会看到类似于这样的话: 例如,ArrayList: 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证.快速失败迭代器会尽最大努力抛出 ConcurrentModificationException.因此,为提高这类迭代器的正确性而编写一个依赖于此异常的程序是错误的做法:迭代器的快速失败行为应该仅用于检测 bug. HashMap中: 注意,迭代器的快速失败行为不能得到保证,一般来说,存在非同步的并发修改时

  • 老生常谈Eclipse中的BuildPath(必看篇)

    什么是Build Path? Build Path是指定Java工程所包含的资源属性集合. 在一个成熟的Java工程中,不仅仅有自己编写的源代码,还需要引用系统运行库(JRE).第三方的功能扩展库.工作空间中的其他工程,甚至外部的类文件,所有这些资源都是被这个工程所依赖的,并且只有被引用后,才能够将该工程编译成功,而Build Path就是用来配置和管理对这些资源的引用的. Build Path一般包括: JRE运行库 第三方的功能扩展库(*.jar格式文件) 其他的工程 其他的源代码或Clas

  • redis中的数据结构和编码详解

    redis中的数据结构和编码:     背景: 1>redis在内部使用redisObject结构体来定义存储的值对象. 2>每种类型都有至少两种内部编码,Redis会根据当前值的类型和长度来决定使用哪种编码实现. 3>编码类型转换在Redis写入数据时自动完成,这个转换过程是不可逆的,转换规则只能从小内存编码向大内存编码转换.     源码: 值对象redisObject: typedef struct redisObject {                 unsigned ty

  • 老生常谈C++ 中的继承

    继承 1 什么是继承 1.1 继承的概念 继承机制是面向对象程序设计使代码可以复用的最重要的手段,这个机制允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称为派生类.继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程.以前解除的都是函数复用,继承是类设计层次的复用. 代码演示如下 #include <iostream> #include <string> using namespace std; class Person { public: v

  • 老生常谈计算机中的编码问题(必看篇)

    计算机中的编码问题 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255(二进制11111111=十进制255),如果要表示更大的整数,就必须用更多的字节.比如两个字节可以表示的最大整数是65535,4个字节可以表示的最大整数是4294967295. 一.目前常用的编码 ASCII编码:由于计算机是美国人发明的,因此,最早只有127个字母被编码到计算机里,也就是大小

  • 老生常谈angularjs中的$state.go

    路由是这么定义的: $stateProvider .state('page1', { url: '/page1', templateUrl: 'views/page1.htm', controller: 'page1Ctrl' }) .state('page2', { url: '/page2/:type', templateUrl: 'views/page2.htm', controller: 'page2Ctrl' }); 用ng-href跳转的话,是这么写的: ng-href="#/pag

  • 老生常谈java中的Future模式

    jdk1.7.0_79 本文实际上是对上文<简单谈谈ThreadPoolExecutor线程池之submit方法>的一个延续或者一个补充.在上文中提到的submit方法里出现了FutureTask,这不得不停止脚步将方向转向Java的Future模式. Future是并发编程中的一种设计模式,对于多线程来说,线程A需要等待线程B的结果,它没必要一直等待B,可以先拿到一个未来的Future,等B有了结果后再取真实的结果. ExecutorService executor = Executors.

随机推荐