Initialize sorted set instance. Optional `iterable` argument provides an initial iterable of values to initialize the sorted set. Optional `key` argument defines a callable that, like the `key` argument to Python's `sorted` function, extracts a comparison key from
(self, iterable=None, key=None)
| 106 | |
| 107 | """ |
| 108 | def __init__(self, iterable=None, key=None): |
| 109 | """Initialize sorted set instance. |
| 110 | |
| 111 | Optional `iterable` argument provides an initial iterable of values to |
| 112 | initialize the sorted set. |
| 113 | |
| 114 | Optional `key` argument defines a callable that, like the `key` |
| 115 | argument to Python's `sorted` function, extracts a comparison key from |
| 116 | each value. The default, none, compares values directly. |
| 117 | |
| 118 | Runtime complexity: `O(n*log(n))` |
| 119 | |
| 120 | >>> ss = SortedSet([3, 1, 2, 5, 4]) |
| 121 | >>> ss |
| 122 | SortedSet([1, 2, 3, 4, 5]) |
| 123 | >>> from operator import neg |
| 124 | >>> ss = SortedSet([3, 1, 2, 5, 4], neg) |
| 125 | >>> ss |
| 126 | SortedSet([5, 4, 3, 2, 1], key=<built-in function neg>) |
| 127 | |
| 128 | :param iterable: initial values (optional) |
| 129 | :param key: function used to extract comparison key (optional) |
| 130 | |
| 131 | """ |
| 132 | self._key = key |
| 133 | |
| 134 | # SortedSet._fromset calls SortedSet.__init__ after initializing the |
| 135 | # _set attribute. So only create a new set if the _set attribute is not |
| 136 | # already present. |
| 137 | |
| 138 | if not hasattr(self, '_set'): |
| 139 | self._set = set() |
| 140 | |
| 141 | self._list = SortedList(self._set, key=key) |
| 142 | |
| 143 | # Expose some set methods publicly. |
| 144 | |
| 145 | _set = self._set |
| 146 | self.isdisjoint = _set.isdisjoint |
| 147 | self.issubset = _set.issubset |
| 148 | self.issuperset = _set.issuperset |
| 149 | |
| 150 | # Expose some sorted list methods publicly. |
| 151 | |
| 152 | _list = self._list |
| 153 | self.bisect_left = _list.bisect_left |
| 154 | self.bisect = _list.bisect |
| 155 | self.bisect_right = _list.bisect_right |
| 156 | self.index = _list.index |
| 157 | self.irange = _list.irange |
| 158 | self.islice = _list.islice |
| 159 | self._reset = _list._reset |
| 160 | |
| 161 | if key is not None: |
| 162 | self.bisect_key_left = _list.bisect_key_left |
| 163 | self.bisect_key_right = _list.bisect_key_right |
| 164 | self.bisect_key = _list.bisect_key |
| 165 | self.irange_key = _list.irange_key |