mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Added .first{item,value,key}() to dictionaries.
Complete with docos and tests. OKed by Guido.
This commit is contained in:
parent
827bb9fb3c
commit
1a62750eda
3 changed files with 90 additions and 0 deletions
|
@ -708,6 +708,75 @@ static PyMappingMethods dict_as_mapping = {
|
|||
(objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
|
||||
};
|
||||
|
||||
static PyObject *
|
||||
dict_firstkey(register dictobject *mp, PyObject *args)
|
||||
{
|
||||
register int i;
|
||||
|
||||
if (!PyArg_NoArgs(args))
|
||||
return NULL;
|
||||
if (mp->ma_used == 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "empty dictionary");
|
||||
return NULL;
|
||||
}
|
||||
for (i = 0; i < mp->ma_size; i++) {
|
||||
if (mp->ma_table[i].me_value != NULL) {
|
||||
PyObject *key = mp->ma_table[i].me_key;
|
||||
Py_INCREF(key);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
dict_firstvalue(register dictobject *mp, PyObject *args)
|
||||
{
|
||||
register int i;
|
||||
|
||||
if (!PyArg_NoArgs(args))
|
||||
return NULL;
|
||||
if (mp->ma_used == 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "empty dictionary");
|
||||
return NULL;
|
||||
}
|
||||
for (i = 0; i < mp->ma_size; i++) {
|
||||
if (mp->ma_table[i].me_value != NULL) {
|
||||
PyObject *value = mp->ma_table[i].me_value;
|
||||
Py_INCREF(value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
dict_firstitem(register dictobject *mp, PyObject *args)
|
||||
{
|
||||
register int i;
|
||||
|
||||
if (!PyArg_NoArgs(args))
|
||||
return NULL;
|
||||
if (mp->ma_used == 0) {
|
||||
PyErr_SetString(PyExc_ValueError, "empty dictionary");
|
||||
return NULL;
|
||||
}
|
||||
for (i = 0; i < mp->ma_size; i++) {
|
||||
if (mp->ma_table[i].me_value != NULL) {
|
||||
PyObject *key = mp->ma_table[i].me_key;
|
||||
PyObject *value = mp->ma_table[i].me_value;
|
||||
PyObject *item = PyTuple_New(2);
|
||||
if (item == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
Py_INCREF(key);
|
||||
PyTuple_SetItem(item, 0, key);
|
||||
Py_INCREF(value);
|
||||
PyTuple_SetItem(item, 1, value);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
dict_keys(register dictobject *mp, PyObject *args)
|
||||
{
|
||||
|
@ -1162,6 +1231,9 @@ static PyMethodDef mapp_methods[] = {
|
|||
{"keys", (PyCFunction)dict_keys},
|
||||
{"items", (PyCFunction)dict_items},
|
||||
{"values", (PyCFunction)dict_values},
|
||||
{"firstkey", (PyCFunction)dict_firstkey},
|
||||
{"firstitem", (PyCFunction)dict_firstitem},
|
||||
{"firstvalue", (PyCFunction)dict_firstvalue},
|
||||
{"update", (PyCFunction)dict_update},
|
||||
{"clear", (PyCFunction)dict_clear},
|
||||
{"copy", (PyCFunction)dict_copy},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue