MCPcopy Create free account
hub / github.com/numpy/numpy / PyArrayIdentityHash_SetItem

Function PyArrayIdentityHash_SetItem

numpy/core/src/common/npy_hashtable.c:187–213  ·  view source on GitHub ↗

* Add an item to the identity cache. The storage location must not change * unless the cache is cleared. * * @param tb The mapping. * @param key The key, must be a C-array of pointers of the length * corresponding to the mapping. * @param value Normally a Python object, no reference counting is done. * use NULL to clear an item. If the item does not exist, no * actio

Source from the content-addressed store, hash-verified

185 * the RuntimeError.
186 */
187NPY_NO_EXPORT int
188PyArrayIdentityHash_SetItem(PyArrayIdentityHash *tb,
189 PyObject *const *key, PyObject *value, int replace)
190{
191 if (value != NULL && _resize_if_necessary(tb) < 0) {
192 /* Shrink, only if a new value is added. */
193 return -1;
194 }
195
196 PyObject **tb_item = find_item(tb, key);
197 if (value != NULL) {
198 if (tb_item[0] != NULL && !replace) {
199 PyErr_SetString(PyExc_RuntimeError,
200 "Identity cache already includes the item.");
201 return -1;
202 }
203 tb_item[0] = value;
204 memcpy(tb_item+1, key, tb->key_len * sizeof(PyObject *));
205 tb->nelem += 1;
206 }
207 else {
208 /* Clear the bucket -- just the value should be enough though. */
209 memset(tb_item, 0, (tb->key_len + 1) * sizeof(PyObject *));
210 }
211
212 return 0;
213}
214
215
216NPY_NO_EXPORT PyObject *

Callers 2

_resize_if_necessaryFunction · 0.85

Calls 2

_resize_if_necessaryFunction · 0.85
find_itemFunction · 0.85

Tested by

no test coverage detected