| 1 |
from sortedcontainers import SortedDict, SortedList |
| 2 |
import gc |
| 3 |
|
| 4 |
def check(f): |
| 5 |
print('start') |
| 6 |
|
| 7 |
a = f() |
| 8 |
t = type(a) |
| 9 |
|
| 10 |
print('post-setup') |
| 11 |
for obj in gc.get_objects(): |
| 12 |
if type(obj) == t: |
| 13 |
print(obj) |
| 14 |
|
| 15 |
del a |
| 16 |
|
| 17 |
print('post-delete') |
| 18 |
for obj in gc.get_objects(): |
| 19 |
if type(obj) == t: |
| 20 |
print(obj) |
| 21 |
|
| 22 |
gc.collect() |
| 23 |
|
| 24 |
print('post-collect') |
| 25 |
for obj in gc.get_objects(): |
| 26 |
if type(obj) == t: |
| 27 |
print(obj) |
| 28 |
|
| 29 |
print('finish') |
| 30 |
|
| 31 |
check(lambda: SortedDict({'a': 1, 'b': 2})) |
| 32 |
|
| 33 |
class MyDict(dict): |
| 34 |
pass |
| 35 |
|
| 36 |
check(lambda: MyDict({'a': 1, 'b': 2})) |
| 37 |
|
| 38 |
check(lambda: SortedList([1, 2, 3])) |
| 39 |
|
| 40 |
from functools import partial |
| 41 |
|
| 42 |
class SortedDictSub(SortedDict): |
| 43 |
|
| 44 |
def __init__(self, *args, **kwargs): |
| 45 |
"""Initialize sorted dict instance. |
| 46 |
Optional key-function argument defines a callable that, like the `key` |
| 47 |
argument to the built-in `sorted` function, extracts a comparison key |
| 48 |
from each dictionary key. If no function is specified, the default |
| 49 |
compares the dictionary keys directly. The key-function argument must |
| 50 |
be provided as a positional argument and must come before all other |
| 51 |
arguments. |
| 52 |
Optional iterable argument provides an initial sequence of pairs to |
| 53 |
initialize the sorted dict. Each pair in the sequence defines the key |
| 54 |
and corresponding value. If a key is seen more than once, the last |
| 55 |
value associated with it is stored in the new sorted dict. |
| 56 |
Optional mapping argument provides an initial mapping of items to |
| 57 |
initialize the sorted dict. |
| 58 |
If keyword arguments are given, the keywords themselves, with their |
| 59 |
associated values, are added as items to the dictionary. If a key is |
| 60 |
specified both in the positional argument and as a keyword argument, |
| 61 |
the value associated with the keyword is stored in the |
| 62 |
sorted dict. |
| 63 |
Sorted dict keys must be hashable, per the requirement for Python's |
| 64 |
dictionaries. Keys (or the result of the key-function) must also be |
| 65 |
comparable, per the requirement for sorted lists. |
| 66 |
>>> d = {'alpha': 1, 'beta': 2} |
| 67 |
>>> SortedDict([('alpha', 1), ('beta', 2)]) == d |
| 68 |
True |
| 69 |
>>> SortedDict({'alpha': 1, 'beta': 2}) == d |
| 70 |
True |
| 71 |
>>> SortedDict(alpha=1, beta=2) == d |
| 72 |
True |
| 73 |
""" |
| 74 |
if args and (args[0] is None or callable(args[0])): |
| 75 |
_key = self._key = args[0] |
| 76 |
args = args[1:] |
| 77 |
else: |
| 78 |
_key = self._key = None |
| 79 |
|
| 80 |
self._list = SortedList(key=_key) |
| 81 |
|
| 82 |
# Calls to super() are expensive so cache references to dict methods on |
| 83 |
# sorted dict instances. |
| 84 |
|
| 85 |
_dict = super(SortedDict, self) |
| 86 |
# self._dict_clear = _dict.clear |
| 87 |
# self._dict_delitem = _dict.__delitem__ |
| 88 |
self._dict_iter = partial(dict.__iter__, self) # _dict.__iter__ |
| 89 |
# self._dict_pop = _dict.pop |
| 90 |
# self._dict_setitem = _dict.__setitem__ |
| 91 |
self._dict_update = partial(dict.update, self) # _dict.update |
| 92 |
|
| 93 |
# Reaching through ``self._list`` repeatedly adds unnecessary overhead |
| 94 |
# so cache references to sorted list methods. |
| 95 |
|
| 96 |
_list = self._list |
| 97 |
self._list_add = _list.add |
| 98 |
self._list_clear = _list.clear |
| 99 |
self._list_iter = _list.__iter__ |
| 100 |
self._list_reversed = _list.__reversed__ |
| 101 |
self._list_pop = _list.pop |
| 102 |
self._list_remove = _list.remove |
| 103 |
self._list_update = _list.update |
| 104 |
|
| 105 |
# Expose some sorted list methods publicly. |
| 106 |
|
| 107 |
self.bisect_left = _list.bisect_left |
| 108 |
self.bisect = _list.bisect_right |
| 109 |
self.bisect_right = _list.bisect_right |
| 110 |
self.index = _list.index |
| 111 |
self.irange = _list.irange |
| 112 |
self.islice = _list.islice |
| 113 |
self._reset = _list._reset |
| 114 |
|
| 115 |
if _key is not None: |
| 116 |
self.bisect_key_left = _list.bisect_key_left |
| 117 |
self.bisect_key_right = _list.bisect_key_right |
| 118 |
self.bisect_key = _list.bisect_key |
| 119 |
self.irange_key = _list.irange_key |
| 120 |
|
| 121 |
self._update(*args, **kwargs) |
| 122 |
|
| 123 |
check(lambda: SortedDictSub({'a': 1, 'b': 2})) |