PHP的SplDoublyLinkedList类是什么意思,更重要的是一般的链接列表? [英] What is the point of PHP's SplDoublyLinkedList class, and more importantly, Linked Lists in general?

查看:132
本文介绍了PHP的SplDoublyLinkedList类是什么意思,更重要的是一般的链接列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了扩展我的编程能力,我已经深入研究了标准PHP库。这导致我发现了 SplDoublyLinkedList 类。从那里我阅读了链接列表维基百科上的双重链接列表

On a quest to expand my programming prowess, I've delved ever-so-slightly into The Standard PHP Library. This led to my discovery of the SplDoublyLinkedList class. From there I read the descriptions of Linked Lists and Doubly Linked Lists on Wikipedia.

我了解它们是如何工作的...但是,由于我们在PHP中编制了索引和关联数组,所以我无法想象为什么我们需要它,或者更好地实现 SplDoublyLinkedList 的实际示例。

I understand how they work... But I cannot conceive of a reason WHY we need it—or better yet a practical example of SplDoublyLinkedList since we have indexed and associative arrays in PHP.

如何在PHP中正常使用链接列表?

How are Linked Lists normally used in-and-out of PHP?

推荐答案

SPL数据结构降低了内存消耗并提高了性能。良好的解释:

The SPL data structures reduce memory consumption and improve performance. Good explanations:


数据结构本质上是与语言无关的,作为一组基于数学的逻辑概念而存在。这些容器使用不同的算法来最大限度地提高效率。

Data structures are inherently language-independent and exist as a set of logical concepts based in mathematics. These containers use different algorithms as appropriate to maximize efficiency.

例如,如果不需要关联数组的哈希映射功能 - 也就是说,如果你没有为特定目的使用数组键,只需要枚举数组 - SplFixedArray(以前的SplFastArray,当前未记录)可能是一个合适的替代。唯一需要注意的是,数组的大小是固定的,这意味着您必须在实例化类时指定大小,并且如果您尝试存储超过该数量的元素,则会发生错误。这就是说,平均来说,它比标准PHP数组更好。

For example, if you don't need the hash map capabilities of an associative array -- that is, if you aren't using the array key for a specific purpose and only need an enumerated array -- SplFixedArray (formerly SplFastArray, currently undocumented) may be a suitable replacement. The only caveat is that the size of the array is fixed, meaning that you must specify the size when you instantiate the class and an error will occur if you attempt to store more than that number of elements. This is the reason that, on average, it performs better than standard PHP arrays.

http://web.archive.org/web/20130805120049/http:/ /blueparabola.com/blog/spl-deserves-some-reiteration


在构成PHP解释器的C代码中,数组被实现为称为散列表或散列映射的数据结构。当数组中包含的值由其索引引用时,PHP使用哈希函数将该索引转换为表示阵列中相应值的位置的唯一哈希。

Within the C code that makes up the PHP interpreter, arrays are implemented as a data structure called a hash table or hash map. When a value contained within an array is referenced by its index, PHP uses a hashing function to convert that index into a unique hash representing the location of the corresponding value within the array.

这个哈希映射实现使数组能够存储任意数量的元素,并使用数字或字符串键同时访问所有这些元素。数组对于它们提供的功能非常快,并且是一个很好的通用数据结构。

This hash map implementation enables arrays to store an arbitrary number of elements and provide access to all of those elements simultaneously using either numeric or string keys. Arrays are extremely fast for the capabilities they provide and are an excellent general purpose data structure.

在计算机科学中,列表被定义为有序的值集合。链接列表是数据结构,其中列表中的每个元素包括对列表中任一侧上的元素中的一个或两个元素的引用。术语双链表用于指后一种情况。在SPL中,它采用SplDoublyLinkedList类的形式....当要存储的元素数量不被提前知道时,使用列表是有意义的,并且元素只需要按顺序位置访问。

In computer science, a list is defined as an ordered collection of values. A linked list is a data structure in which each element in the list includes a reference to one or both of the elements on either side of it within the list. The term "doubly-linked list" is used to refer to the latter case. In the SPL, this takes the form of the class SplDoublyLinkedList.... It makes sense to use lists when the number of elements to be stored is not known in advance and the elements only need to be accessed by sequential position.

http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/

这篇关于PHP的SplDoublyLinkedList类是什么意思,更重要的是一般的链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆