在Perl中如何实现数组? [英] How are arrays implemented in Perl?

查看:193
本文介绍了在Perl中如何实现数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl数组是抽象数据类型。 Perl数组的内部机制是什么?是用动态数组还是链表实现?
由于数组元素具有随机访问权限,我将假设一个动态数组的指针,或对标量的引用是有意义的。然而,在阵列的头部移动和不移动操作,阵列是否必须通过这些操作移动其所有元素?对我来说声音效率不高。任何想法?

解决方案

看看这个: http://www.perlmonks.org/?node_id=17890



(取自:)



Perl使用数组和第一个/最后一个元素偏移实现列表。数组的分配比所需要的更大,偏移量最初指向数组的中间,所以在需要重新分配底层数组之前,有两个方向增长的空间(unshifts和push / insert)。这种实现的后果是,在O(1)时间内,所有perl的原始列表运算符(插入,获取,确定数组大小,推送,弹出,移位,不移动等)都执行。


The Perl array is an abstract data type. What's the internal mechanism for the Perl array? Is it implemented with dynamic array or linked list? Since the array elements have random access, I would assume a dynamic array of pointers, or references to scalars make sense. However, with shift and unshift operation at the head of array, would the array have to move all its elements with these operations? sound inefficient to me. Any thought?

解决方案

Have a look at this: http://www.perlmonks.org/?node_id=17890

(taken from there:)

Perl implements lists with an array and first/last element offsets. The array is allocated larger than needed with the offsets originally pointing in the middle of the array so there is room to grow in both directions (unshifts and pushes/inserts) before a re-allocation of the underlying array is necessary. The consequence of this implementation is that all of perl's primitive list operators (insertion, fetching, determining array size, push, pop, shift, unshift, etc.) perform in O(1) time.

这篇关于在Perl中如何实现数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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