1- # Copyright 2009-2013 Ram Rachum.
1+ # Copyright 2009-2013 Ram Rachum.,
22# This program is distributed under the MIT license.
33
44import operator
@@ -34,18 +34,17 @@ def __init__(self, iterable=None, **kwargs):
3434 '''
3535 super (FrozenCounter , self ).__init__ ()
3636
37+ self_get = self ._dict .get
3738 if iterable is not None :
3839 if isinstance (iterable , collections .Mapping ):
3940 if self :
40- self_get = self .get
41- for elem , count in iterable .iteritems ():
42- self [elem ] = self_get (elem , 0 ) + count
41+ for element , count in iterable .iteritems ():
42+ self ._dict [element ] = self_get (element , 0 ) + count
4343 else :
4444 super (FrozenCounter , self ).update (iterable ) # fast path when counter is empty
4545 else :
46- self_get = self .get
47- for elem in iterable :
48- self [elem ] = self_get (elem , 0 ) + 1
46+ for element in iterable :
47+ self ._dict [element ] = self_get (element , 0 ) + 1
4948 if kwargs :
5049 self .update (kwargs )
5150
@@ -129,13 +128,13 @@ def __add__(self, other):
129128 if not isinstance (other , FrozenCounter ):
130129 return NotImplemented
131130 result = collections .Counter ()
132- for elem , count in self .items ():
133- newcount = count + other [elem ]
134- if newcount > 0 :
135- result [elem ] = newcount
136- for elem , count in other .items ():
137- if elem not in self and count > 0 :
138- result [elem ] = count
131+ for element , count in self .items ():
132+ new_count = count + other [element ]
133+ if new_count > 0 :
134+ result [element ] = new_count
135+ for element , count in other .items ():
136+ if element not in self and count > 0 :
137+ result [element ] = count
139138 return FrozenCounter (result )
140139
141140 def __sub__ (self , other ):
@@ -148,13 +147,13 @@ def __sub__(self, other):
148147 if not isinstance (other , FrozenCounter ):
149148 return NotImplemented
150149 result = collections .Counter ()
151- for elem , count in self .items ():
152- newcount = count - other [elem ]
153- if newcount > 0 :
154- result [elem ] = newcount
155- for elem , count in other .items ():
156- if elem not in self and count < 0 :
157- result [elem ] = 0 - count
150+ for element , count in self .items ():
151+ new_count = count - other [element ]
152+ if new_count > 0 :
153+ result [element ] = new_count
154+ for element , count in other .items ():
155+ if element not in self and count < 0 :
156+ result [element ] = 0 - count
158157 return FrozenCounter (result )
159158
160159 def __or__ (self , other ):
@@ -167,14 +166,14 @@ def __or__(self, other):
167166 if not isinstance (other , FrozenCounter ):
168167 return NotImplemented
169168 result = Counter ()
170- for elem , count in self .items ():
171- other_count = other [elem ]
172- newcount = other_count if count < other_count else count
173- if newcount > 0 :
174- result [elem ] = newcount
175- for elem , count in other .items ():
176- if elem not in self and count > 0 :
177- result [elem ] = count
169+ for element , count in self .items ():
170+ other_count = other [element ]
171+ new_count = other_count if count < other_count else count
172+ if new_count > 0 :
173+ result [element ] = new_count
174+ for element , count in other .items ():
175+ if element not in self and count > 0 :
176+ result [element ] = count
178177 return FrozenCounter (result )
179178
180179 def __and__ (self , other ):
@@ -187,9 +186,9 @@ def __and__(self, other):
187186 if not isinstance (other , FrozenCounter ):
188187 return NotImplemented
189188 result = Counter ()
190- for elem , count in self .items ():
191- other_count = other [elem ]
192- newcount = count if count < other_count else other_count
193- if newcount > 0 :
194- result [elem ] = newcount
189+ for element , count in self .items ():
190+ other_count = other [element ]
191+ new_count = count if count < other_count else other_count
192+ if new_count > 0 :
193+ result [element ] = new_count
195194 return FrozenCounter (result )
0 commit comments