def end_update(self, domain): if domain.is_empty or self._end_update_interval.is_superset_of(domain): return self._end_update_interval = Interval.union( [self._end_update_interval, domain]) if not self._end_update_interval.is_superset_of( self._begin_update_interval): # Keep collecting updates return # Updates complete update_interval = self._end_update_interval self._begin_update_interval = Interval.empty() self._end_update_interval = Interval.empty() self.set_needs_interval_update() for token in list(self._ordered_observer_tokens): _, callback_interval, _, callback, _, callback_with_interval = self._observer_data[ token] if callback_interval is None or update_interval.intersects( callback_interval): if callback is not None: if callback_with_interval: callback(update_interval) else: callback()
def begin_update(self, domain): if domain.is_empty or self._begin_update_interval.is_superset_of( domain): return self._begin_update_interval = Interval.union( [self._begin_update_interval, domain]) for token in self._ordered_observer_tokens: _, callback_interval, callback, _, callback_with_interval, _ = self._observer_data[ token] if callback_interval is None or domain.intersects( callback_interval): if callback is not None: if callback_with_interval: callback(domain) else: callback()
def _update_extension_interval(self): if not self._extension_interval_stale: return self._extension_interval_stale = False if self._extension_stale: self._update_extension_if_needed() if self.start and self.start_valid and self.end and self.end_valid: self.extension_interval = Interval.union([self.start_func.domain, self.end_func.domain]) elif self.start and self.start_valid: self.extension_interval = Interval.intersection([self.start_func.domain, self.curve.domain.get_lt()]) elif self.end and self.end_valid: self.extension_interval = Interval.intersection([self.end_func.domain, self.curve.domain.get_gt()]) else: self.extension_interval = Interval.empty() self.update_extension_interval() if (self.start and not self.extension_interval.is_negative_infinite) or (self.end and not self.extension_interval.is_positive_infinite): if self.raise_on_empty: raise Exception('Unable to extend func')
def set(self, points, update_domain=None): """ `points` are assumed to be strictly ordered in ascending order w.r.t. to `x`. """ if update_domain is None: points_len = len(points) if points_len == 0: update_domain = self.domain else: update_domain = Interval.union([ self.domain, Interval.closed(points[0][0], points[-1][0]) ]) self.begin_update(update_domain) self._is_equally_spaced = self._points_equally_spaced([], points) if self._force_equally_spaced and not self._is_equally_spaced: raise Exception( 'Attempting to set points at non-regular intervals') self._points = list(points) self._did_change_points() self.end_update(update_domain)
def get_domain(self): domains = list(map(lambda f: f.domain, self.funcs)) if self.is_union: return Interval.union(domains) else: return Interval.intersection(domains)
def get_domain(self): self._update_extension_interval() return Interval.union([self.curve.domain, self.extension_interval])
def list_domain(cls, quotes): if len(quotes) == 0: return Interval.empty() return Interval.union([quotes[0].domain, quotes[-1].domain])
def test_intersection(): # closed, closed d1 = Interval(0, 2, start_open=False, end_open=False) d2 = Interval(1, 3, start_open=False, end_open=False) assert d1.contains(0) assert d1.contains(1) assert d1.contains(2) d = Interval.intersection([d1, d2]) assert d.start == 1 assert d.end == 2 assert not d.start_open assert not d.end_open d = Interval.union([d1, d2]) assert d.start == 0 assert d.end == 3 assert not d.start_open assert not d.end_open # closed, open d1 = Interval(0, 2, start_open=False, end_open=False) d2 = Interval(1, 3, start_open=True, end_open=True) d = Interval.intersection([d1, d2]) assert d.start == 1 assert d.end == 2 assert d.start_open assert not d.end_open d = Interval.union([d1, d2]) assert d.start == 0 assert d.end == 3 assert not d.start_open assert d.end_open # open, open d1 = Interval(0, 2, start_open=True, end_open=True) d2 = Interval(1, 3, start_open=True, end_open=True) assert not d1.contains(0) assert d1.contains(1) assert not d1.contains(2) d = Interval.intersection([d1, d2]) assert d.start == 1 assert d.end == 2 assert d.start_open assert d.end_open d = Interval.union([d1, d2]) assert d.start == 0 assert d.end == 3 assert d.start_open assert d.end_open d = Interval.intersection([Interval(0, 1), Interval(2, 3)]) assert d.is_empty d = Interval.intersection([Interval(0, 1, end_open=True), Interval(1, 3, start_open=True)]) assert d.is_empty d = Interval.intersection([Interval(0, 1), Interval.empty()]) assert d.is_empty d = Interval.union([Interval.empty(), 1]) assert d.start == 1 assert d.end == 1