generated from Pseudo-Lab/Jupyter-Book-Template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
|
||
# dictionay 타입에 대해서 알아보자 | ||
### dict get | ||
|
||
```c | ||
static inline Py_ssize_t | ||
dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i) | ||
{ | ||
Py_ssize_t s = DK_SIZE(keys); | ||
Py_ssize_t ix; | ||
|
||
if (s <= 0xff) { | ||
const int8_t *indices = (const int8_t*)(keys->dk_indices); | ||
ix = indices[i]; | ||
} | ||
else if (s <= 0xffff) { | ||
const int16_t *indices = (const int16_t*)(keys->dk_indices); | ||
ix = indices[i]; | ||
} | ||
#if SIZEOF_VOID_P > 4 | ||
else if (s > 0xffffffff) { | ||
const int64_t *indices = (const int64_t*)(keys->dk_indices); | ||
ix = indices[i]; | ||
} | ||
#endif | ||
else { | ||
const int32_t *indices = (const int32_t*)(keys->dk_indices); | ||
ix = indices[i]; | ||
} | ||
assert(ix >= DKIX_DUMMY); | ||
return ix; | ||
} | ||
``` | ||
### dict set | ||
```c | ||
static inline void | ||
dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix) | ||
{ | ||
Py_ssize_t s = DK_SIZE(keys); | ||
assert(ix >= DKIX_DUMMY); | ||
if (s <= 0xff) { | ||
int8_t *indices = (int8_t*)(keys->dk_indices); | ||
assert(ix <= 0x7f); | ||
indices[i] = (char)ix; | ||
} | ||
else if (s <= 0xffff) { | ||
int16_t *indices = (int16_t*)(keys->dk_indices); | ||
assert(ix <= 0x7fff); | ||
indices[i] = (int16_t)ix; | ||
} | ||
#if SIZEOF_VOID_P > 4 | ||
else if (s > 0xffffffff) { | ||
int64_t *indices = (int64_t*)(keys->dk_indices); | ||
indices[i] = ix; | ||
} | ||
#endif | ||
else { | ||
int32_t *indices = (int32_t*)(keys->dk_indices); | ||
assert(ix <= 0x7fffffff); | ||
indices[i] = (int32_t)ix; | ||
} | ||
} | ||
``` | ||
|
||
### dict new | ||
```c | ||
static PyObject * | ||
new_dict(PyDictKeysObject *keys, PyObject **values) | ||
{ | ||
PyDictObject *mp; | ||
assert(keys != NULL); | ||
#if PyDict_MAXFREELIST > 0 | ||
if (numfree) { | ||
mp = free_list[--numfree]; | ||
assert (mp != NULL); | ||
assert (Py_IS_TYPE(mp, &PyDict_Type)); | ||
_Py_NewReference((PyObject *)mp); | ||
} | ||
else | ||
#endif | ||
{ | ||
mp = PyObject_GC_New(PyDictObject, &PyDict_Type); | ||
if (mp == NULL) { | ||
dictkeys_decref(keys); | ||
if (values != empty_values) { | ||
free_values(values); | ||
} | ||
return NULL; | ||
} | ||
} | ||
mp->ma_keys = keys; | ||
mp->ma_values = values; | ||
mp->ma_used = 0; | ||
mp->ma_version_tag = DICT_NEXT_VERSION(); | ||
ASSERT_CONSISTENT(mp); | ||
return (PyObject *)mp; | ||
} | ||
``` | ||