Ejemplo n.º 1
0
 def iter(self, size=0):
     if self.step < 0 and self.end == -1:
         # keep negative to have 0 included
         return iter(number_range(fix(self.start, size), -1, self.step))
     return iter(
         number_range(fix(self.start, size), fix(self.end, size),
                      self.step))
Ejemplo n.º 2
0
 def repeat(self, ntimes=1):
     if ntimes == 1:
         return self
     r = []
     for i in number_range(ntimes):
         r.extend(self._elems)
     return Range1D(r)
Ejemplo n.º 3
0
    def _compress(indices):
        l = len(indices)
        if l == 0:
            return []
        elif l == 1:
            return [RangeElem.single(indices[0])]
        r = []
        deltas = [e - indices[i] for i, e in enumerate(indices[1:])]
        start = 0
        act = 1

        while act < l:
            while act < l and deltas[start] == deltas[
                    act - 1]:  # while the same delta
                act += 1
            if act == start + 1:  # just a single item used
                r.append(RangeElem.single(indices[start]))
            else:
                # +1 since end is excluded
                # fix while just +1 -1 allowed
                if abs(deltas[start]) == 1:
                    r.append(
                        RangeElem.range(indices[start],
                                        indices[act - 1] + deltas[start],
                                        deltas[start]))
                else:
                    for i in number_range(start, act):
                        r.append(RangeElem.single(indices[i]))
            start = act
            act += 1
        while start < len(
                indices
        ):  # corner case by adding act+1, it might happened that last one isn't considered
            r.append(RangeElem.single(indices[start]))
            start += 1
        return r
Ejemplo n.º 4
0
 def __getitem__(self, item):
     if len(self.dims) > item:
         return self.dims[item]
     for i in number_range(len(self.dims), item + 1):
         self.dims.append(Range1D.all())
     return self.dims[item]
Ejemplo n.º 5
0
 def iter(self, size=0):
     return iter(
         number_range(fix(self.start, size), fix(self.end, size),
                      self.step))