paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Integer objects. |
paul@6 | 5 | |
paul@6 | 6 | Copyright (C) 2015, 2016 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@303 | 22 | from __builtins__.operator import _negate |
paul@472 | 23 | from __builtins__.unicode import utf8string |
paul@356 | 24 | from native import isinstance as _isinstance, get_maxint, get_minint, \ |
paul@356 | 25 | int_add, int_and, int_div, int_eq, int_gt, int_lt, int_mod, \ |
paul@356 | 26 | int_mul, int_ne, int_neg, int_not, int_or, int_pow, \ |
paul@356 | 27 | int_str, int_sub, int_xor |
paul@6 | 28 | |
paul@355 | 29 | class int: |
paul@182 | 30 | |
paul@182 | 31 | "An integer abstraction." |
paul@182 | 32 | |
paul@6 | 33 | def __init__(self, number_or_string=None): |
paul@182 | 34 | |
paul@182 | 35 | "Initialise the integer with the given 'number_or_string'." |
paul@182 | 36 | |
paul@356 | 37 | if _isinstance(number_or_string, int): |
paul@182 | 38 | self.__data__ = number_or_string.__data__ |
paul@182 | 39 | else: |
paul@182 | 40 | # NOTE: To be implemented. |
paul@182 | 41 | self.__data__ = None |
paul@6 | 42 | |
paul@283 | 43 | def __hash__(self): |
paul@303 | 44 | |
paul@283 | 45 | "Return a value for hashing purposes." |
paul@303 | 46 | |
paul@283 | 47 | return self |
paul@283 | 48 | |
paul@303 | 49 | def _binary_op(self, op, other): |
paul@303 | 50 | |
paul@303 | 51 | "Perform 'op' on this int and 'other' if appropriate." |
paul@303 | 52 | |
paul@356 | 53 | if _isinstance(other, int): |
paul@303 | 54 | return op(self.__data__, other.__data__) |
paul@303 | 55 | else: |
paul@303 | 56 | return NotImplemented |
paul@303 | 57 | |
paul@303 | 58 | def _binary_op_rev(self, op, other): |
paul@303 | 59 | |
paul@303 | 60 | "Perform 'op' on 'other' and this int if appropriate." |
paul@303 | 61 | |
paul@356 | 62 | if _isinstance(other, int): |
paul@303 | 63 | return op(other.__data__, self.__data__) |
paul@303 | 64 | else: |
paul@303 | 65 | return NotImplemented |
paul@303 | 66 | |
paul@6 | 67 | def __iadd__(self, other): |
paul@303 | 68 | |
paul@303 | 69 | "Return a new int for the addition of this int and 'other'." |
paul@303 | 70 | |
paul@356 | 71 | return self._binary_op(int_add, other) |
paul@6 | 72 | |
paul@6 | 73 | def __isub__(self, other): |
paul@303 | 74 | |
paul@303 | 75 | "Return a new int for the subtraction of this int and 'other'." |
paul@303 | 76 | |
paul@356 | 77 | return self._binary_op(int_sub, other) |
paul@6 | 78 | |
paul@6 | 79 | def __imul__(self, other): |
paul@303 | 80 | |
paul@303 | 81 | "Return a new int for the multiplication of this int and 'other'." |
paul@303 | 82 | |
paul@356 | 83 | return self._binary_op(int_mul, other) |
paul@6 | 84 | |
paul@6 | 85 | def __idiv__(self, other): |
paul@303 | 86 | |
paul@303 | 87 | "Return a new int for the division of this int and 'other'." |
paul@303 | 88 | |
paul@356 | 89 | return self._binary_op(int_div, other) |
paul@6 | 90 | |
paul@6 | 91 | def __imod__(self, other): |
paul@303 | 92 | |
paul@303 | 93 | "Return a new int for the modulo of this int by 'other'." |
paul@303 | 94 | |
paul@356 | 95 | return self._binary_op(int_mod, other) |
paul@6 | 96 | |
paul@6 | 97 | def __ipow__(self, other): |
paul@303 | 98 | |
paul@303 | 99 | "Return a new int for the exponentiation of this int by 'other'." |
paul@303 | 100 | |
paul@356 | 101 | return self._binary_op(int_pow, other) |
paul@6 | 102 | |
paul@6 | 103 | def __iand__(self, other): |
paul@303 | 104 | |
paul@303 | 105 | "Return a new int for the binary-and of this int and 'other'." |
paul@303 | 106 | |
paul@356 | 107 | return self._binary_op(int_and, other) |
paul@6 | 108 | |
paul@6 | 109 | def __ior__(self, other): |
paul@303 | 110 | |
paul@303 | 111 | "Return a new int for the binary-or of this int and 'other'." |
paul@303 | 112 | |
paul@356 | 113 | return self._binary_op(int_or, other) |
paul@6 | 114 | |
paul@6 | 115 | def __ixor__(self, other): |
paul@303 | 116 | |
paul@303 | 117 | "Return a new int for the exclusive-or of this int and 'other'." |
paul@303 | 118 | |
paul@356 | 119 | return self._binary_op(int_xor, other) |
paul@6 | 120 | |
paul@307 | 121 | def __invert__(self): |
paul@307 | 122 | |
paul@307 | 123 | "Return the inversion of this int." |
paul@307 | 124 | |
paul@356 | 125 | return int_not(self.__data__) |
paul@307 | 126 | |
paul@6 | 127 | __add__ = __radd__ = __iadd__ |
paul@6 | 128 | __sub__ = __isub__ |
paul@6 | 129 | |
paul@6 | 130 | def __rsub__(self, other): |
paul@303 | 131 | |
paul@303 | 132 | "Return a new int for the subtraction of this int from 'other'." |
paul@303 | 133 | |
paul@356 | 134 | return self._binary_op_rev(int_sub, other) |
paul@6 | 135 | |
paul@6 | 136 | __mul__ = __rmul__ = __imul__ |
paul@6 | 137 | __div__ = __idiv__ |
paul@6 | 138 | |
paul@6 | 139 | def __rdiv__(self, other): |
paul@303 | 140 | |
paul@303 | 141 | "Return a new int for the division of this int into 'other'." |
paul@303 | 142 | |
paul@356 | 143 | return self._binary_op_rev(int_div, other) |
paul@6 | 144 | |
paul@6 | 145 | def __floordiv__(self, other): pass |
paul@6 | 146 | def __rfloordiv__(self, other): pass |
paul@6 | 147 | def __ifloordiv__(self, other): pass |
paul@6 | 148 | |
paul@6 | 149 | __mod__ = __imod__ |
paul@6 | 150 | |
paul@6 | 151 | def __rmod__(self, other): |
paul@303 | 152 | |
paul@303 | 153 | "Return a new int for the modulo of 'other' by this int." |
paul@303 | 154 | |
paul@356 | 155 | return self._binary_op_rev(int_mod, other) |
paul@6 | 156 | |
paul@6 | 157 | __pow__ = __ipow__ |
paul@6 | 158 | |
paul@6 | 159 | def __rpow__(self, other): |
paul@303 | 160 | |
paul@303 | 161 | "Return a new int for the exponentiation of 'other' by this int." |
paul@303 | 162 | |
paul@356 | 163 | return self._binary_op_rev(int_pow, other) |
paul@6 | 164 | |
paul@6 | 165 | __and__ = __rand__ = __iand__ |
paul@6 | 166 | __or__ = __ror__ = __ior__ |
paul@6 | 167 | __xor__ = __rxor__ = __ixor__ |
paul@6 | 168 | |
paul@6 | 169 | def __lt__(self, other): |
paul@303 | 170 | |
paul@303 | 171 | "Return whether this int is less than 'other'." |
paul@303 | 172 | |
paul@356 | 173 | return self._binary_op(int_lt, other) |
paul@6 | 174 | |
paul@6 | 175 | def __gt__(self, other): |
paul@303 | 176 | |
paul@303 | 177 | "Return whether this int is greater than 'other'." |
paul@303 | 178 | |
paul@356 | 179 | return self._binary_op(int_gt, other) |
paul@6 | 180 | |
paul@6 | 181 | def __le__(self, other): |
paul@303 | 182 | |
paul@303 | 183 | "Return whether this int is less than or equal to 'other'." |
paul@303 | 184 | |
paul@6 | 185 | return _negate(self.__gt__(other)) |
paul@6 | 186 | |
paul@6 | 187 | def __ge__(self, other): |
paul@303 | 188 | |
paul@303 | 189 | "Return whether this int is greater than or equal to 'other'." |
paul@303 | 190 | |
paul@6 | 191 | return _negate(self.__lt__(other)) |
paul@6 | 192 | |
paul@6 | 193 | def __eq__(self, other): |
paul@303 | 194 | |
paul@303 | 195 | "Return whether this int is equal to 'other'." |
paul@303 | 196 | |
paul@356 | 197 | return self._binary_op(int_eq, other) |
paul@6 | 198 | |
paul@6 | 199 | def __ne__(self, other): |
paul@303 | 200 | |
paul@303 | 201 | "Return whether this int is not equal to 'other'." |
paul@303 | 202 | |
paul@6 | 203 | return _negate(self.__eq__(other)) |
paul@6 | 204 | |
paul@207 | 205 | def __neg__(self): |
paul@303 | 206 | |
paul@207 | 207 | "Apply the unary negation operator." |
paul@303 | 208 | |
paul@356 | 209 | return int_neg(self.__data__) |
paul@207 | 210 | |
paul@207 | 211 | def __pos__(self): |
paul@303 | 212 | |
paul@207 | 213 | "Apply the unary positive operator." |
paul@303 | 214 | |
paul@207 | 215 | return self |
paul@198 | 216 | |
paul@198 | 217 | def __str__(self): |
paul@303 | 218 | |
paul@198 | 219 | "Return a string representation." |
paul@303 | 220 | |
paul@472 | 221 | return utf8string(int_str(self.__data__)) |
paul@198 | 222 | |
paul@222 | 223 | __repr__ = __str__ |
paul@222 | 224 | |
paul@6 | 225 | def __lshift__(self): pass |
paul@6 | 226 | def __rlshift__(self): pass |
paul@6 | 227 | def __rshift__(self): pass |
paul@6 | 228 | def __rrshift__(self): pass |
paul@6 | 229 | def __ilshift__(self): pass |
paul@6 | 230 | def __irshift__(self): pass |
paul@6 | 231 | |
paul@6 | 232 | def __bool__(self): |
paul@303 | 233 | |
paul@6 | 234 | "Return whether this int is non-zero." |
paul@303 | 235 | |
paul@303 | 236 | zero = 0 |
paul@356 | 237 | return int_ne(self.__data__, zero.__data__) |
paul@6 | 238 | |
paul@311 | 239 | # Limits. |
paul@311 | 240 | |
paul@356 | 241 | maxint = get_maxint() |
paul@356 | 242 | minint = get_minint() |
paul@311 | 243 | |
paul@6 | 244 | # vim: tabstop=4 expandtab shiftwidth=4 |