@@ -6,7 +6,7 @@ set是无序且不重复的集合,是可变的,通常用来从列表中删
66
77在set中,对应的set的值的存储是通过结构setentry来保存数据值的;
88
9- 源文件:[ include/setobject.h] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Include/setobject.h#L26 )
9+ ` 源文件: ` [ include/setobject.h] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Include/setobject.h#L26 )
1010
1111```
1212typedef struct {
@@ -17,7 +17,7 @@ typedef struct {
1717
1818key就是保存的数据,hash就是保存的数据的hash,便于查找,set也是基于hash表来实现。对应的setentry所对应的set的数据结构如下;
1919
20- 源文件:[ include/setobject.h] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Include/setobject.h#L42 )
20+ ` 源文件: ` [ include/setobject.h] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Include/setobject.h#L42 )
2121
2222```
2323
@@ -122,7 +122,7 @@ python -m dis set_test.py
122122
123123查找BUILD_SET的虚拟机执行函数如下;
124124
125- 源文件:[ Python/ceval.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Python/ceval.c#L2318 )
125+ ` 源文件: ` [ Python/ceval.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Python/ceval.c#L2318 )
126126
127127```
128128// Python/ceval.c
@@ -152,7 +152,7 @@ python -m dis set_test.py
152152
153153此时继续查看PySet_New函数的执行流程;
154154
155- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L2286 )
155+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L2286 )
156156
157157
158158```
@@ -199,7 +199,7 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
199199
200200在本例的初始化过程中,由于传入了初始值1,2,所以会在执行字节码指令的时候,执行PySet_Add,该函数的本质与set_a.add(3)本质都调用了更底层set_add_key函数;
201201
202- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L2338 )
202+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L2338 )
203203
204204```
205205
@@ -217,7 +217,7 @@ PySet_Add(PyObject *anyset, PyObject *key)
217217
218218继续查看set_add_key函数的执行过程;
219219
220- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L419 )
220+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L419 )
221221
222222```
223223static int
@@ -237,7 +237,7 @@ set_add_key(PySetObject *so, PyObject *key)
237237
238238该函数主要就是检查传入的key是否能够被hash,如果能够被hash则直接返回,如果能被hash则继续调用set_add_entry函数将值加入到set中;
239239
240- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L136 )
240+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L136 )
241241
242242```
243243
@@ -392,7 +392,7 @@ s.add(7) // index = 9 & 7 = 1
392392
393393set的删除操作主要集中在set_remove()函数上,如下示例;
394394
395- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L1921 )
395+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L1921 )
396396
397397```
398398
@@ -426,7 +426,7 @@ set_remove(PySetObject *so, PyObject *key)
426426
427427此时就会调用set_discard_key方法来讲对应的entry设置为dummy;set_discard_key方法如下;
428428
429- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L447 )
429+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L447 )
430430
431431```
432432
@@ -447,7 +447,7 @@ set_discard_key(PySetObject *so, PyObject *key)
447447
448448该函数主要就是做了检查key是否可用hash的检查,此时如果可用hash则调用set_discard_entry方法;
449449
450- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L400 )
450+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L400 )
451451
452452```
453453
@@ -477,7 +477,7 @@ set_discard_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
477477
478478set的resize主要依靠set_table_reseize函数来实现;
479479
480- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L302 )
480+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L302 )
481481
482482```
483483static int
@@ -563,7 +563,7 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)
563563
564564主要是检查是否table相同并且需要重新resize的值,然后判断是否fill与used相同,如果相同则全部插入,如果不同,则遍历旧table讲不为空并且不为dummy的值插入到新表中;
565565
566- 源文件:[ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L267 )
566+ ` 源文件: ` [ Objects/setobject.c] ( https://github.com/python/cpython/blob/1bf9cc509326bc42cd8cb1650eb9bf64550d817e/Objects/setobject.c#L267 )
567567
568568```
569569static void
0 commit comments