MCPcopy
hub / github.com/grantjenks/python-sortedcontainers / _check

Method _check

sortedcontainers/sortedlist.py:2563–2643  ·  view source on GitHub ↗

Check invariants of sorted-key list. Runtime complexity: `O(n)`

(self)

Source from the content-addressed store, hash-verified

2561
2562
2563 def _check(self):
2564 """Check invariants of sorted-key list.
2565
2566 Runtime complexity: `O(n)`
2567
2568 """
2569 try:
2570 assert self._load >= 4
2571 assert len(self._maxes) == len(self._lists) == len(self._keys)
2572 assert self._len == sum(len(sublist) for sublist in self._lists)
2573
2574 # Check all sublists are sorted.
2575
2576 for sublist in self._keys:
2577 for pos in range(1, len(sublist)):
2578 assert sublist[pos - 1] <= sublist[pos]
2579
2580 # Check beginning/end of sublists are sorted.
2581
2582 for pos in range(1, len(self._keys)):
2583 assert self._keys[pos - 1][-1] <= self._keys[pos][0]
2584
2585 # Check _keys matches _key mapped to _lists.
2586
2587 for val_sublist, key_sublist in zip(self._lists, self._keys):
2588 assert len(val_sublist) == len(key_sublist)
2589 for val, key in zip(val_sublist, key_sublist):
2590 assert self._key(val) == key
2591
2592 # Check _maxes index is the last value of each sublist.
2593
2594 for pos in range(len(self._maxes)):
2595 assert self._maxes[pos] == self._keys[pos][-1]
2596
2597 # Check sublist lengths are less than double load-factor.
2598
2599 double = self._load << 1
2600 assert all(len(sublist) <= double for sublist in self._lists)
2601
2602 # Check sublist lengths are greater than half load-factor for all
2603 # but the last sublist.
2604
2605 half = self._load >> 1
2606 for pos in range(0, len(self._lists) - 1):
2607 assert len(self._lists[pos]) >= half
2608
2609 if self._index:
2610 assert self._len == self._index[0]
2611 assert len(self._index) == self._offset + len(self._lists)
2612
2613 # Check index leaf nodes equal length of sublists.
2614
2615 for pos in range(len(self._lists)):
2616 leaf = self._index[self._offset + pos]
2617 assert leaf == len(self._lists[pos])
2618
2619 # Check index branch nodes are the sum of their children.
2620

Callers 15

test_initFunction · 0.95
test_newFunction · 0.95
test_keyFunction · 0.95
test_addFunction · 0.95
test_updateFunction · 0.95
test_containsFunction · 0.95
test_discardFunction · 0.95
test_removeFunction · 0.95
test_deleteFunction · 0.95
test_getitemFunction · 0.95
test_delitemFunction · 0.95
test_bisect_leftFunction · 0.95

Calls

no outgoing calls

Tested by 15

test_initFunction · 0.76
test_newFunction · 0.76
test_keyFunction · 0.76
test_addFunction · 0.76
test_updateFunction · 0.76
test_containsFunction · 0.76
test_discardFunction · 0.76
test_removeFunction · 0.76
test_deleteFunction · 0.76
test_getitemFunction · 0.76
test_delitemFunction · 0.76
test_bisect_leftFunction · 0.76