Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
96f3410e
Kaydet (Commit)
96f3410e
authored
Ara 15, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 10667: Fast path for collections.Counter
üst
779f19e7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
4 deletions
+81
-4
collections.py
Lib/collections.py
+12
-3
NEWS
Misc/NEWS
+2
-0
_collectionsmodule.c
Modules/_collectionsmodule.c
+67
-1
No files found.
Lib/collections.py
Dosyayı görüntüle @
96f3410e
...
@@ -334,6 +334,17 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
...
@@ -334,6 +334,17 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
### Counter
### Counter
########################################################################
########################################################################
def
_count_elements
(
mapping
,
iterable
):
'Tally elements from the iterable.'
mapping_get
=
mapping
.
get
for
elem
in
iterable
:
mapping
[
elem
]
=
mapping_get
(
elem
,
0
)
+
1
try
:
# Load C helper function if available
from
_collections
import
_count_elements
except
ImportError
:
pass
class
Counter
(
dict
):
class
Counter
(
dict
):
'''Dict subclass for counting hashable items. Sometimes called a bag
'''Dict subclass for counting hashable items. Sometimes called a bag
or multiset. Elements are stored as dictionary keys and their counts
or multiset. Elements are stored as dictionary keys and their counts
...
@@ -476,9 +487,7 @@ class Counter(dict):
...
@@ -476,9 +487,7 @@ class Counter(dict):
else
:
else
:
dict
.
update
(
self
,
iterable
)
# fast path when counter is empty
dict
.
update
(
self
,
iterable
)
# fast path when counter is empty
else
:
else
:
self_get
=
self
.
get
_count_elements
(
self
,
iterable
)
for
elem
in
iterable
:
self
[
elem
]
=
1
+
self_get
(
elem
,
0
)
if
kwds
:
if
kwds
:
self
.
update
(
kwds
)
self
.
update
(
kwds
)
...
...
Misc/NEWS
Dosyayı görüntüle @
96f3410e
...
@@ -17,6 +17,8 @@ Core and Builtins
...
@@ -17,6 +17,8 @@ Core and Builtins
Library
Library
-------
-------
- Issue #10667: Fast path for collections.Counter().
- Issue #10695: passing the port as a string value to telnetlib no longer
- Issue #10695: passing the port as a string value to telnetlib no longer
causes debug mode to fail.
causes debug mode to fail.
...
...
Modules/_collectionsmodule.c
Dosyayı görüntüle @
96f3410e
...
@@ -1518,6 +1518,68 @@ static PyTypeObject defdict_type = {
...
@@ -1518,6 +1518,68 @@ static PyTypeObject defdict_type = {
PyObject_GC_Del
,
/* tp_free */
PyObject_GC_Del
,
/* tp_free */
};
};
/* helper function for Counter *********************************************/
PyDoc_STRVAR
(
_count_elements_doc
,
"_count_elements(mapping, iterable) -> None
\n
\
\n
\
Count elements in the iterable, updating the mappping"
);
static
PyObject
*
_count_elements
(
PyObject
*
self
,
PyObject
*
args
)
{
PyObject
*
it
,
*
iterable
,
*
mapping
,
*
oldval
;
PyObject
*
newval
=
NULL
;
PyObject
*
key
=
NULL
;
PyObject
*
one
=
NULL
;
if
(
!
PyArg_UnpackTuple
(
args
,
"_count_elements"
,
2
,
2
,
&
mapping
,
&
iterable
))
return
NULL
;
if
(
!
PyDict_Check
(
mapping
))
{
PyErr_SetString
(
PyExc_TypeError
,
"Expected mapping argument to be a dictionary"
);
return
NULL
;
}
it
=
PyObject_GetIter
(
iterable
);
if
(
it
==
NULL
)
return
NULL
;
one
=
PyLong_FromLong
(
1
);
if
(
one
==
NULL
)
{
Py_DECREF
(
it
);
return
NULL
;
}
while
(
1
)
{
key
=
PyIter_Next
(
it
);
if
(
key
==
NULL
)
{
if
(
PyErr_Occurred
()
&&
PyErr_ExceptionMatches
(
PyExc_StopIteration
))
PyErr_Clear
();
break
;
}
oldval
=
PyDict_GetItem
(
mapping
,
key
);
if
(
oldval
==
NULL
)
{
if
(
PyDict_SetItem
(
mapping
,
key
,
one
)
==
-
1
)
break
;
}
else
{
newval
=
PyNumber_Add
(
oldval
,
one
);
if
(
newval
==
NULL
)
break
;
if
(
PyDict_SetItem
(
mapping
,
key
,
newval
)
==
-
1
)
break
;
Py_CLEAR
(
newval
);
}
Py_DECREF
(
key
);
}
Py_DECREF
(
it
);
Py_XDECREF
(
key
);
Py_XDECREF
(
newval
);
Py_DECREF
(
one
);
if
(
PyErr_Occurred
())
return
NULL
;
Py_RETURN_NONE
;
}
/* module level code ********************************************************/
/* module level code ********************************************************/
PyDoc_STRVAR
(
module_doc
,
PyDoc_STRVAR
(
module_doc
,
...
@@ -1526,13 +1588,17 @@ PyDoc_STRVAR(module_doc,
...
@@ -1526,13 +1588,17 @@ PyDoc_STRVAR(module_doc,
- defaultdict: dict subclass with a default value factory
\n
\
- defaultdict: dict subclass with a default value factory
\n
\
"
);
"
);
static
struct
PyMethodDef
module_functions
[]
=
{
{
"_count_elements"
,
_count_elements
,
METH_VARARGS
,
_count_elements_doc
},
{
NULL
,
NULL
}
/* sentinel */
};
static
struct
PyModuleDef
_collectionsmodule
=
{
static
struct
PyModuleDef
_collectionsmodule
=
{
PyModuleDef_HEAD_INIT
,
PyModuleDef_HEAD_INIT
,
"_collections"
,
"_collections"
,
module_doc
,
module_doc
,
-
1
,
-
1
,
NULL
,
module_functions
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment