MCPcopy Create free account
hub / github.com/grantjenks/python-sortedcontainers / count

Method count

sortedcontainers/sortedlist.py:1233–1270  ·  view source on GitHub ↗

Return number of occurrences of `value` in the sorted list. Runtime complexity: `O(log(n))` -- approximate. >>> sl = SortedList([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]) >>> sl.count(3) 3 :param value: value to count in sorted list :return: count

(self, value)

Source from the content-addressed store, hash-verified

1231
1232
1233 def count(self, value):
1234 """Return number of occurrences of `value` in the sorted list.
1235
1236 Runtime complexity: `O(log(n))` -- approximate.
1237
1238 >>> sl = SortedList([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
1239 >>> sl.count(3)
1240 3
1241
1242 :param value: value to count in sorted list
1243 :return: count
1244
1245 """
1246 _maxes = self._maxes
1247
1248 if not _maxes:
1249 return 0
1250
1251 pos_left = bisect_left(_maxes, value)
1252
1253 if pos_left == len(_maxes):
1254 return 0
1255
1256 _lists = self._lists
1257 idx_left = bisect_left(_lists[pos_left], value)
1258 pos_right = bisect_right(_maxes, value)
1259
1260 if pos_right == len(_maxes):
1261 return self._len - self._loc(pos_left, idx_left)
1262
1263 idx_right = bisect_right(_lists[pos_right], value)
1264
1265 if pos_left == pos_right:
1266 return idx_right - idx_left
1267
1268 right = self._loc(pos_right, idx_right)
1269 left = self._loc(pos_left, idx_left)
1270 return right - left
1271
1272
1273 def copy(self):

Callers 1

test_countFunction · 0.95

Calls 1

_locMethod · 0.95

Tested by 1

test_countFunction · 0.76