|
| 1 | + |
| 2 | +# dictionay 타입에 대해서 알아보자 |
| 3 | +### dict get |
| 4 | + |
| 5 | +```c |
| 6 | +static inline Py_ssize_t |
| 7 | +dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i) |
| 8 | +{ |
| 9 | + Py_ssize_t s = DK_SIZE(keys); |
| 10 | + Py_ssize_t ix; |
| 11 | + |
| 12 | + if (s <= 0xff) { |
| 13 | + const int8_t *indices = (const int8_t*)(keys->dk_indices); |
| 14 | + ix = indices[i]; |
| 15 | + } |
| 16 | + else if (s <= 0xffff) { |
| 17 | + const int16_t *indices = (const int16_t*)(keys->dk_indices); |
| 18 | + ix = indices[i]; |
| 19 | + } |
| 20 | +#if SIZEOF_VOID_P > 4 |
| 21 | + else if (s > 0xffffffff) { |
| 22 | + const int64_t *indices = (const int64_t*)(keys->dk_indices); |
| 23 | + ix = indices[i]; |
| 24 | + } |
| 25 | +#endif |
| 26 | + else { |
| 27 | + const int32_t *indices = (const int32_t*)(keys->dk_indices); |
| 28 | + ix = indices[i]; |
| 29 | + } |
| 30 | + assert(ix >= DKIX_DUMMY); |
| 31 | + return ix; |
| 32 | +} |
| 33 | +``` |
| 34 | +
|
| 35 | +### dict set |
| 36 | +```c |
| 37 | +static inline void |
| 38 | +dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix) |
| 39 | +{ |
| 40 | + Py_ssize_t s = DK_SIZE(keys); |
| 41 | +
|
| 42 | + assert(ix >= DKIX_DUMMY); |
| 43 | +
|
| 44 | + if (s <= 0xff) { |
| 45 | + int8_t *indices = (int8_t*)(keys->dk_indices); |
| 46 | + assert(ix <= 0x7f); |
| 47 | + indices[i] = (char)ix; |
| 48 | + } |
| 49 | + else if (s <= 0xffff) { |
| 50 | + int16_t *indices = (int16_t*)(keys->dk_indices); |
| 51 | + assert(ix <= 0x7fff); |
| 52 | + indices[i] = (int16_t)ix; |
| 53 | + } |
| 54 | +#if SIZEOF_VOID_P > 4 |
| 55 | + else if (s > 0xffffffff) { |
| 56 | + int64_t *indices = (int64_t*)(keys->dk_indices); |
| 57 | + indices[i] = ix; |
| 58 | + } |
| 59 | +#endif |
| 60 | + else { |
| 61 | + int32_t *indices = (int32_t*)(keys->dk_indices); |
| 62 | + assert(ix <= 0x7fffffff); |
| 63 | + indices[i] = (int32_t)ix; |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### dict new |
| 69 | +```c |
| 70 | +static PyObject * |
| 71 | +new_dict(PyDictKeysObject *keys, PyObject **values) |
| 72 | +{ |
| 73 | + PyDictObject *mp; |
| 74 | + assert(keys != NULL); |
| 75 | +#if PyDict_MAXFREELIST > 0 |
| 76 | + if (numfree) { |
| 77 | + mp = free_list[--numfree]; |
| 78 | + assert (mp != NULL); |
| 79 | + assert (Py_IS_TYPE(mp, &PyDict_Type)); |
| 80 | + _Py_NewReference((PyObject *)mp); |
| 81 | + } |
| 82 | + else |
| 83 | +#endif |
| 84 | + { |
| 85 | + mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 86 | + if (mp == NULL) { |
| 87 | + dictkeys_decref(keys); |
| 88 | + if (values != empty_values) { |
| 89 | + free_values(values); |
| 90 | + } |
| 91 | + return NULL; |
| 92 | + } |
| 93 | + } |
| 94 | + mp->ma_keys = keys; |
| 95 | + mp->ma_values = values; |
| 96 | + mp->ma_used = 0; |
| 97 | + mp->ma_version_tag = DICT_NEXT_VERSION(); |
| 98 | + ASSERT_CONSISTENT(mp); |
| 99 | + return (PyObject *)mp; |
| 100 | +} |
| 101 | +``` |
| 102 | +
|
0 commit comments