Lichen

lib/__builtins__/int.py

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