Author : Jasper Yangpython
School : Bupt数据结构
先挂个英文版的原文连接 Laurent Luce's Blog 这个做者仍是能够的,我又发现了他的另一篇关于dict的实现,后面的博文再进行介绍。app
在python中实现list是十分有趣的事情,list在现实使用中是那么的强大而使人喜好,因此,下面让咱们一块儿来一探究竟。less
>>> l = [] >>> l.append(1) >>> l.append(2) >>> l.append(3) >>> l [1, 2, 3] >>> for e in l: ... print e ... 1 2 3
如你所见,list是可迭代的函数
一个list对象在 CPython 中是以以下的数据结构保存的。ob_item存储了一系列指向数据的指针。allocated里面存储的是该list在内存分配的大小(slots)oop
typedef struct { PyObject_VAR_HEAD PyObject **ob_item; Py_ssize_t allocated; } PyListObject;
看看当咱们初始化一个list是会发生什么,e.g. l =[]post
arguments: size of the list = 0 returns: list object = [] PyListNew: nbytes = size * size of global Python object = 0 allocate new list object allocate list of pointers (ob_item) of size nbytes = 0 clear ob_item set list's allocated var to 0 = 0 slots return list object
很重要的一点,咱们须要注意到 allocated slots (内存分配的空间大小)和list的size的区别。list的size和 len(l) 是同样的。allocated slots的个数就是内存中分配了得slot(能够理解成内存的block)个数。你会很常常看到 allocated 比size还要大。这个是为了不屡次的从新分配内存空间(realloc c函数),后面咱们会介绍更多。spa
咱们往list里append数据后会发生什么呢~?c的内置函数 app1()会被调用。翻译
arguments: list object, new element returns: 0 if OK, -1 if not app1: n = size of list call list_resize() to resize the list to size n+1 = 0 + 1 = 1 list[n] = list[0] = new element return 0
让咱们来看看这个list_resize()。它超量地分配了比所需的内存更多的内存空间。指针
增加模式以下:0,4,8,16,25,35,46,58,72,88. . .
arguments: list object, new size returns: 0 if OK, -1 if not list_resize: new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6) = 3 new_allocated += newsize = 3 + 1 = 4 resize ob_item (list of pointers) to size new_allocated return 0
如今有4个slot分配给了这个list去存储指针,你能够看到l[0]指向了咱们刚刚append进去的整数对象。虚线方块表明了没用到的slot。
We continue by adding one more element: l.append(2). list_resize is called with n+1 = 2 but because the allocated size is 4, there is no need to allocate more memory. Same thing happens when we add 2 more integers: l.append(3), l.append(4). The following diagram shows what we have so far.
咱们继续append更多元素:l.append(2)。list_resize() 被调用了,n = n + 1。可是由于n仍是小于 allocated = 4 ,因此没有必要去从新分配内存空间,一样的咱们继续append 3 和 4。结果不变。
让咱们在位置1的地方插入整数5: l.insert(1,5),ins1() 会被调用。
如今分配的内存slot个数变成了8,这个增加的规律和咱们以前讲的如出一辙。
当咱们使用弹出:l.pop(),listpop()会被调用,而且若是list的size小于allocated的一半时,list会收缩,也就是allocated会变小。
arguments: list object returns: element popped listpop: if list empty: return null resize list with size 5 - 1 = 4. 4 is not less than 8/2 so no shrinkage set list object size to 4 return last element
你能够看到slot 4 仍然指向了原来的整数区域,可是list的size已经缩小了,也就是slot4已经不属于这个list了。
当咱们再pop一次后发现,size已经小于allocated的一半了,因而allocated变成了6。
python 的 list 还有个特殊的移除元素的方法:l.remove(5) ,这里移除的不是第5个slot而是那个指向的值是5的slot。listremove() 会被调用。
arguments: list object, element to remove returns none if OK, null if not listremove: loop through each list element: if correct element: slice list between element's slot and element's slot + 1 return none return null
切分list并移除元素(也就是上面代码中的slice),会调用list_ass_slice()。过程以下。
arguments: list object, low offset, high offset returns: 0 if OK list_ass_slice: copy integer 5 to recycle list to dereference it shift elements from slot 2 to slot 1 resize list to 5 slots return 0
但愿大家能从我翻译的这篇文章中学到东西,提升本身~
paper done 2017/04/21