A non-contiguous view of an ndarray that synchronizes with the original. Note: this class should not be instantiated directly.
| 153 | |
| 154 | |
| 155 | class SynchronizingArrayWrapper(np.ndarray): |
| 156 | """A non-contiguous view of an ndarray that synchronizes with the original. |
| 157 | |
| 158 | Note: this class should not be instantiated directly. |
| 159 | """ |
| 160 | __slots__ = ( |
| 161 | '_backing_array', |
| 162 | '_backing_index', |
| 163 | '_backing_index_is_array_like', |
| 164 | '_physics', |
| 165 | '_triggers_dirty', |
| 166 | '_disable_on_write', |
| 167 | ) |
| 168 | |
| 169 | def __new__(cls, |
| 170 | backing_array, |
| 171 | backing_index, |
| 172 | physics, |
| 173 | triggers_dirty, |
| 174 | disable_on_write): |
| 175 | obj = backing_array[backing_index].view(SynchronizingArrayWrapper) |
| 176 | # pylint: disable=protected-access |
| 177 | obj._backing_array = backing_array |
| 178 | obj._backing_index = backing_index |
| 179 | # Performance optimization: avoid repeatedly checking the type of the |
| 180 | # backing index. |
| 181 | backing_index_type = _get_index_type(backing_index) |
| 182 | obj._backing_index_is_array_like = backing_index_type is _ARRAY_LIKE |
| 183 | obj._physics = physics |
| 184 | obj._triggers_dirty = triggers_dirty |
| 185 | obj._disable_on_write = disable_on_write |
| 186 | # pylint: enable=protected-access |
| 187 | return obj |
| 188 | |
| 189 | def _synchronize_from_backing_array(self): |
| 190 | if self._physics.is_dirty and not self._triggers_dirty: |
| 191 | self._physics.forward() |
| 192 | updated_values = self._backing_array[self._backing_index] |
| 193 | # Faster than `super(...).__setitem__(slice(None), updated_values)` |
| 194 | np.copyto(self, updated_values) |
| 195 | |
| 196 | def copy(self, order='C'): |
| 197 | return np.copy(self, order=order) |
| 198 | |
| 199 | def __copy__(self): |
| 200 | return self.copy() |
| 201 | |
| 202 | def __deepcopy__(self, memo): |
| 203 | return self.copy() |
| 204 | |
| 205 | def __reduce__(self): |
| 206 | raise NotImplementedError(_PICKLING_NOT_SUPPORTED.format(type=type(self))) |
| 207 | |
| 208 | def __setitem__(self, index, value): |
| 209 | if self._physics.is_dirty and not self._triggers_dirty: |
| 210 | self._physics.forward() |
| 211 | super().__setitem__(index, value) |
| 212 |
no outgoing calls
no test coverage detected
searching dependent graphs…