paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Span-related objects. |
paul@6 | 5 | |
paul@496 | 6 | Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@6 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@6 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@6 | 16 | details. |
paul@6 | 17 | |
paul@6 | 18 | You should have received a copy of the GNU General Public License along with |
paul@6 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 20 | """ |
paul@6 | 21 | |
paul@367 | 22 | class slice: |
paul@6 | 23 | |
paul@367 | 24 | "Implementation of slice." |
paul@6 | 25 | |
paul@6 | 26 | NO_END = object() |
paul@6 | 27 | |
paul@367 | 28 | def __init__(self, start_or_end=None, end=NO_END, step=1): |
paul@6 | 29 | |
paul@367 | 30 | "Initialise the slice with the given 'start_or_end', 'end' and 'step'." |
paul@6 | 31 | |
paul@412 | 32 | if end is slice.NO_END: |
paul@6 | 33 | self.start = 0 |
paul@6 | 34 | self.end = start_or_end |
paul@6 | 35 | else: |
paul@6 | 36 | self.start = start_or_end |
paul@6 | 37 | self.end = end |
paul@6 | 38 | |
paul@367 | 39 | if step == 0: |
paul@367 | 40 | raise ValueError(self.step) |
paul@367 | 41 | |
paul@6 | 42 | self.step = step |
paul@294 | 43 | |
paul@367 | 44 | def __str__(self): |
paul@367 | 45 | |
paul@367 | 46 | "Return a string representation." |
paul@367 | 47 | |
paul@499 | 48 | b = buffer([self.__parent__.__name__, ".", self.__name__, "(", self.start, ", ", self.end, ", ", self.step, ")"]) |
paul@367 | 49 | return str(b) |
paul@367 | 50 | |
paul@367 | 51 | __repr__ = __str__ |
paul@367 | 52 | |
paul@367 | 53 | class xrange(slice): |
paul@367 | 54 | |
paul@367 | 55 | "Implementation of xrange." |
paul@367 | 56 | |
paul@367 | 57 | def __init__(self, start_or_end, end=slice.NO_END, step=1): |
paul@367 | 58 | |
paul@367 | 59 | "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." |
paul@367 | 60 | |
paul@367 | 61 | get_using(slice.__init__, self)(start_or_end, end, step) |
paul@367 | 62 | |
paul@294 | 63 | # Constrain the end according to the start and step. |
paul@294 | 64 | |
paul@294 | 65 | if step > 0: |
paul@294 | 66 | self.end = _max(self.start, self.end) |
paul@294 | 67 | elif step < 0: |
paul@294 | 68 | self.end = _min(self.start, self.end) |
paul@294 | 69 | else: |
paul@294 | 70 | raise ValueError(self.step) |
paul@294 | 71 | |
paul@294 | 72 | def __len__(self): |
paul@294 | 73 | |
paul@294 | 74 | "Return the length of the range." |
paul@294 | 75 | |
paul@294 | 76 | return (self.end - self.start) / self.step |
paul@6 | 77 | |
paul@6 | 78 | def __iter__(self): |
paul@6 | 79 | |
paul@6 | 80 | "Return an iterator, currently self." |
paul@6 | 81 | |
paul@691 | 82 | return xrangeiterator(self.start, self.step, self.__len__()) |
paul@369 | 83 | |
paul@369 | 84 | class xrangeiterator: |
paul@369 | 85 | |
paul@369 | 86 | "An iterator over an xrange." |
paul@369 | 87 | |
paul@691 | 88 | def __init__(self, start, step, count): |
paul@369 | 89 | |
paul@369 | 90 | "Initialise the iterator with the given 'obj'." |
paul@369 | 91 | |
paul@691 | 92 | self.current = start |
paul@691 | 93 | self.step = step |
paul@691 | 94 | self.count = count |
paul@6 | 95 | |
paul@6 | 96 | def next(self): |
paul@6 | 97 | |
paul@6 | 98 | "Return the next item or raise a StopIteration exception." |
paul@6 | 99 | |
paul@689 | 100 | if not self.count: |
paul@369 | 101 | raise StopIteration |
paul@6 | 102 | |
paul@6 | 103 | current = self.current |
paul@691 | 104 | self.current = self.current.__add__(self.step) |
paul@691 | 105 | self.count = self.count.__sub__(1) |
paul@6 | 106 | return current |
paul@6 | 107 | |
paul@294 | 108 | def range(start_or_end, end=None, step=1): |
paul@294 | 109 | |
paul@294 | 110 | "Implementation of range." |
paul@294 | 111 | |
paul@294 | 112 | return list(xrange(start_or_end, end, step)) |
paul@294 | 113 | |
paul@421 | 114 | def _max(x, y): |
paul@421 | 115 | |
paul@421 | 116 | "Return the maximum of 'x' and 'y'." |
paul@421 | 117 | |
paul@421 | 118 | if x >= y: |
paul@421 | 119 | return x |
paul@421 | 120 | else: |
paul@421 | 121 | return y |
paul@421 | 122 | |
paul@421 | 123 | def _min(x, y): |
paul@421 | 124 | |
paul@421 | 125 | "Return the minimum of 'x' and 'y'." |
paul@421 | 126 | |
paul@421 | 127 | if x <= y: |
paul@421 | 128 | return x |
paul@421 | 129 | else: |
paul@421 | 130 | return y |
paul@421 | 131 | |
paul@6 | 132 | # vim: tabstop=4 expandtab shiftwidth=4 |