diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-11-25 21:03:31 +0100 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-11-25 21:43:06 +0100 |
| commit | cdeed6969c2d10dc11346a598e7d3cba0930757b (patch) | |
| tree | e63791dadb4e767c338bf01ac6f2bf4453f3e523 | |
| parent | 18c4392a66877609ee33927ecec773a0fb535539 (diff) | |
| download | miasm-cdeed6969c2d10dc11346a598e7d3cba0930757b.tar.gz miasm-cdeed6969c2d10dc11346a598e7d3cba0930757b.zip | |
Core/Interval: Add explicit api for interval
| -rw-r--r-- | miasm/core/interval.py | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/miasm/core/interval.py b/miasm/core/interval.py index 06dc546f..172197c0 100644 --- a/miasm/core/interval.py +++ b/miasm/core/interval.py @@ -125,16 +125,26 @@ class interval(object): def __ne__(self, other): return not self.__eq__(other) - def __add__(self, i): - if isinstance(i, interval): - i = i.intervals - i = interval(self.intervals + i) - return i + def union(self, other): + """ + Return the union of intervals + @other: interval instance + """ + + if isinstance(other, interval): + other = other.intervals + other = interval(self.intervals + other) + return other + + def difference(self, other): + """ + Return the difference of intervals + @other: interval instance + """ - def __sub__(self, v): to_test = self.intervals[:] i = -1 - to_del = v.intervals[:] + to_del = other.intervals[:] while i < len(to_test) - 1: i += 1 x = to_test[i] @@ -181,12 +191,17 @@ class interval(object): raise ValueError('unknown state', rez) return interval(to_test) - def __and__(self, v): + def intersection(self, other): + """ + Return the intersection of intervals + @other: interval instance + """ + out = [] for x in self.intervals: if x[0] > x[1]: continue - for y in v.intervals: + for y in other.intervals: rez = cmp_interval(x, y) if rez == INT_DISJOIN: @@ -214,6 +229,16 @@ class interval(object): raise ValueError('unknown state', rez) return interval(out) + + def __add__(self, other): + return self.union(other) + + def __and__(self, other): + return self.intersection(other) + + def __sub__(self, other): + return self.difference(other) + def hull(self): "Return the first and the last bounds of intervals" if not self.intervals: |