How are arrays implemented in Perl?

前端 未结 2 565
死守一世寂寞
死守一世寂寞 2021-01-03 23:57

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 ha

2条回答
  •  温柔的废话
    2021-01-04 00:51

    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.

提交回复
热议问题