老生常谈PHP中的数据结构:DS扩展
PHP7以上才能安装和使用该数据结构扩展,安装比较简单:
1.运行命令peclinstallds
2.在php.ini中添加extension=ds.so
3.重启PHP或重载配置
CollectionInterface:包含本库中所有数据结构通用功能的基本interface。Itguaranteesthatallstructuresaretraversable,countable,andcanbeconvertedtojsonusingjson_encode().
Ds\CollectionimplementsTraversable,Countable,JsonSerializable{ /*方法*/ abstractpublicvoidclear(void) abstractpublicDs\Collectioncopy(void) abstractpublicboolisEmpty(void) abstractpublicarraytoArray(void) }
HashableInterface:whichallowsobjectstobeusedaskeys.
Ds\Hashable{ /*方法*/ abstractpublicboolequals(object$obj) abstractpublicmixedhash(void) }
SequenceInterface:ASequence相当于一个一维的数字key数组,withtheexceptionofafewcharacteristics:
Valueswillalwaysbeindexedas[0,1,2,…,size-1].
Onlyallowedtoaccessvaluesbyindexintherange[0,size-1].
Usecases:
Whereveryouwoulduseanarrayasalist(notconcernedwithkeys).
AmoreefficientalternativetoSplDoublyLinkedListandSplFixedArray.
VectorClass:Vector是自动增长和收缩的连续缓冲区中的一系列值。它是最有效的顺序结构,值的索引直接映射到缓冲区中索引,增长因子不绑定到特定的倍数或指数。其具有以下优缺点:
Supportsarraysyntax(squarebrackets).
Useslessoverallmemorythananarrayforthesamenumberofvalues.
Automaticallyfreesallocatedmemorywhenitssizedropslowenough.
Capacitydoesnothavetobeapowerof2.
get(),set(),push(),pop()areallO(1).
但是shift(),unshift(),insert()andremove()areallO(n).
Ds\Vector::allocate—Allocatesenoughmemoryforarequiredcapacity. Ds\Vector::apply—Updatesallvaluesbyapplyingacallbackfunctiontoeachvalue. Ds\Vector::capacity—Returnsthecurrentcapacity. Ds\Vector::clear—Removesallvalues. Ds\Vector::__construct—Createsanewinstance. Ds\Vector::contains—Determinesifthevectorcontainsgivenvalues. Ds\Vector::copy—Returnsashallowcopyofthevector. Ds\Vector::count—Returnsthenumberofvaluesinthecollection. Ds\Vector::filter—Createsanewvectorusingacallabletodeterminewhichvaluestoinclude. Ds\Vector::find—Attemptstofindavalue'sindex. Ds\Vector::first—Returnsthefirstvalueinthevector. Ds\Vector::get—Returnsthevalueatagivenindex. Ds\Vector::insert—Insertsvaluesatagivenindex. Ds\Vector::isEmpty—Returnswhetherthevectorisempty Ds\Vector::join—Joinsallvaluestogetherasastring. Ds\Vector::jsonSerialize—ReturnsarepresentationthatcanbeconvertedtoJSON. Ds\Vector::last—Returnsthelastvalue. Ds\Vector::map—Returnstheresultofapplyingacallbacktoeachvalue. Ds\Vector::merge—Returnstheresultofaddingallgivenvaluestothevector. Ds\Vector::pop—Removesandreturnsthelastvalue. Ds\Vector::push—Addsvaluestotheendofthevector. Ds\Vector::reduce—Reducesthevectortoasinglevalueusingacallbackfunction. Ds\Vector::remove—Removesandreturnsavaluebyindex. Ds\Vector::reverse—Reversesthevectorin-place. Ds\Vector::reversed—Returnsareversedcopy. Ds\Vector::rotate—Rotatesthevectorbyagivennumberofrotations. Ds\Vector::set—Updatesavalueatagivenindex. Ds\Vector::shift—Removesandreturnsthefirstvalue. Ds\Vector::slice—Returnsasub-vectorofagivenrange. Ds\Vector::sort—Sortsthevectorin-place. Ds\Vector::sorted—Returnsasortedcopy. Ds\Vector::sum—Returnsthesumofallvaluesinthevector. Ds\Vector::toArray—Convertsthevectortoanarray. Ds\Vector::unshift—Addsvaluestothefrontofthevector.
DequeClass:“双端队列”的缩写,也用于Ds\Queue中,拥有head、tail两个指针。Thepointerscan“wraparound”theendofthebuffer,whichavoidstheneedtomoveothervaluesaroundtomakeroom.Thismakesshiftandunshiftveryfast — somethingaDs\Vectorcan'tcompetewith.其具有以下优缺点:
Supportsarraysyntax(squarebrackets).
Useslessoverallmemorythananarrayforthesamenumberofvalues.
Automaticallyfreesallocatedmemorywhenitssizedropslowenough.
get(),set(),push(),pop(),shift(),andunshift()areallO(1).
但Capacitymustbeapowerof2.insert()andremove()areO(n).
MapClass:键值对的连续集合,几乎与数组相同。键可以是任何类型,但必须是唯一的。如果使用相同的键添加到map中,则将替换值。其拥有以下优缺点:
Keysandvaluescanbeanytype,includingobjects.
Supportsarraysyntax(squarebrackets).
Insertionorderispreserved.
Performanceandmemoryefficiencyisverysimilartoanarray.
Automaticallyfreesallocatedmemorywhenitssizedropslowenough.
Can'tbeconvertedtoanarraywhenobjectsareusedaskeys.
PairClass:ApairisusedbyDs\Maptopairkeyswithvalues.
Ds\PairimplementsJsonSerializable{ /*方法*/ public__construct([mixed$key[,mixed$value]]) }
SetClass:唯一值序列。ThisimplementationusesthesamehashtableasDs\Map,wherevaluesareusedaskeysandthemappedvalueisignored.其拥有以下优缺点:
Valuescanbeanytype,includingobjects.
Supportsarraysyntax(squarebrackets).
Insertionorderispreserved.
Automaticallyfreesallocatedmemorywhenitssizedropslowenough.
add(),remove()andcontains()areallO(1).
但Doesn'tsupportpush(),pop(),insert(),shift(),orunshift().get()isO(n)iftherearedeletedvaluesinthebufferbeforetheaccessedindex,O(1)otherwise.
StackClass:“lastin,firstout”集合,只允许在结构顶部进行访问和迭代。
Ds\StackimplementsDs\Collection{ /*方法*/ publicvoidallocate(int$capacity) publicintcapacity(void) publicvoidclear(void) publicDs\Stackcopy(void) publicboolisEmpty(void) publicmixedpeek(void) publicmixedpop(void) publicvoidpush([mixed$...values]) publicarraytoArray(void) }
QueueClass:“firstin,firstout”集合,只允许在结构前端进行访问和迭代。
Ds\QueueimplementsDs\Collection{ /*Constants*/ constintMIN_CAPACITY=8; /*方法*/ publicvoidallocate(int$capacity) publicintcapacity(void) publicvoidclear(void) publicDs\Queuecopy(void) publicboolisEmpty(void) publicmixedpeek(void) publicmixedpop(void) publicvoidpush([mixed$...values]) publicarraytoArray(void) }
PriorityQueueClass:优先级队列与队列是非常相似的,但值以指定的优先级被推入队列,优先级最高的值总是位于队列的前面,同优先级元素“先入先出”顺序任然保留。在一个PriorityQueue上递代是具有破坏性的,相当于连续弹出操作直到队列为空。Implementedusingamaxheap.
Ds\PriorityQueueimplementsDs\Collection{ /*Constants*/ constintMIN_CAPACITY=8; /*方法*/ publicvoidallocate(int$capacity) publicintcapacity(void) publicvoidclear(void) publicDs\PriorityQueuecopy(void) publicboolisEmpty(void) publicmixedpeek(void) publicmixedpop(void) publicvoidpush(mixed$value,int$priority) publicarraytoArray(void) }
以上这篇老生常谈PHP中的数据结构:DS扩展就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。