diff options
Diffstat (limited to 'Demo/classes')
-rw-r--r-- | Demo/classes/Complex.py | 304 | ||||
-rw-r--r-- | Demo/classes/Dates.py | 227 | ||||
-rw-r--r-- | Demo/classes/README | 11 | ||||
-rw-r--r-- | Demo/classes/Range.py | 93 | ||||
-rw-r--r-- | Demo/classes/Rev.py | 93 | ||||
-rw-r--r-- | Demo/classes/Vec.py | 68 | ||||
-rw-r--r-- | Demo/classes/bitvec.py | 322 |
7 files changed, 0 insertions, 1118 deletions
diff --git a/Demo/classes/Complex.py b/Demo/classes/Complex.py deleted file mode 100644 index 06f2212..0000000 --- a/Demo/classes/Complex.py +++ /dev/null @@ -1,304 +0,0 @@ -# Complex numbers -# --------------- - -# [Now that Python has a complex data type built-in, this is not very -# useful, but it's still a nice example class] - -# This module represents complex numbers as instances of the class Complex. -# A Complex instance z has two data attribues, z.re (the real part) and z.im -# (the imaginary part). In fact, z.re and z.im can have any value -- all -# arithmetic operators work regardless of the type of z.re and z.im (as long -# as they support numerical operations). -# -# The following functions exist (Complex is actually a class): -# Complex([re [,im]) -> creates a complex number from a real and an imaginary part -# IsComplex(z) -> true iff z is a complex number (== has .re and .im attributes) -# ToComplex(z) -> a complex number equal to z; z itself if IsComplex(z) is true -# if z is a tuple(re, im) it will also be converted -# PolarToComplex([r [,phi [,fullcircle]]]) -> -# the complex number z for which r == z.radius() and phi == z.angle(fullcircle) -# (r and phi default to 0) -# exp(z) -> returns the complex exponential of z. Equivalent to pow(math.e,z). -# -# Complex numbers have the following methods: -# z.abs() -> absolute value of z -# z.radius() == z.abs() -# z.angle([fullcircle]) -> angle from positive X axis; fullcircle gives units -# z.phi([fullcircle]) == z.angle(fullcircle) -# -# These standard functions and unary operators accept complex arguments: -# abs(z) -# -z -# +z -# not z -# repr(z) == `z` -# str(z) -# hash(z) -> a combination of hash(z.re) and hash(z.im) such that if z.im is zero -# the result equals hash(z.re) -# Note that hex(z) and oct(z) are not defined. -# -# These conversions accept complex arguments only if their imaginary part is zero: -# int(z) -# float(z) -# -# The following operators accept two complex numbers, or one complex number -# and one real number (int, long or float): -# z1 + z2 -# z1 - z2 -# z1 * z2 -# z1 / z2 -# pow(z1, z2) -# cmp(z1, z2) -# Note that z1 % z2 and divmod(z1, z2) are not defined, -# nor are shift and mask operations. -# -# The standard module math does not support complex numbers. -# The cmath modules should be used instead. -# -# Idea: -# add a class Polar(r, phi) and mixed-mode arithmetic which -# chooses the most appropriate type for the result: -# Complex for +,-,cmp -# Polar for *,/,pow - -import math -import sys - -twopi = math.pi*2.0 -halfpi = math.pi/2.0 - -def IsComplex(obj): - return hasattr(obj, 're') and hasattr(obj, 'im') - -def ToComplex(obj): - if IsComplex(obj): - return obj - elif isinstance(obj, tuple): - return Complex(*obj) - else: - return Complex(obj) - -def PolarToComplex(r = 0, phi = 0, fullcircle = twopi): - phi = phi * (twopi / fullcircle) - return Complex(math.cos(phi)*r, math.sin(phi)*r) - -def Re(obj): - if IsComplex(obj): - return obj.re - return obj - -def Im(obj): - if IsComplex(obj): - return obj.im - return 0 - -class Complex: - - def __init__(self, re=0, im=0): - _re = 0 - _im = 0 - if IsComplex(re): - _re = re.re - _im = re.im - else: - _re = re - if IsComplex(im): - _re = _re - im.im - _im = _im + im.re - else: - _im = _im + im - # this class is immutable, so setting self.re directly is - # not possible. - self.__dict__['re'] = _re - self.__dict__['im'] = _im - - def __setattr__(self, name, value): - raise TypeError('Complex numbers are immutable') - - def __hash__(self): - if not self.im: - return hash(self.re) - return hash((self.re, self.im)) - - def __repr__(self): - if not self.im: - return 'Complex(%r)' % (self.re,) - else: - return 'Complex(%r, %r)' % (self.re, self.im) - - def __str__(self): - if not self.im: - return repr(self.re) - else: - return 'Complex(%r, %r)' % (self.re, self.im) - - def __neg__(self): - return Complex(-self.re, -self.im) - - def __pos__(self): - return self - - def __abs__(self): - return math.hypot(self.re, self.im) - - def __int__(self): - if self.im: - raise ValueError("can't convert Complex with nonzero im to int") - return int(self.re) - - def __float__(self): - if self.im: - raise ValueError("can't convert Complex with nonzero im to float") - return float(self.re) - - def __eq__(self, other): - other = ToComplex(other) - return (self.re, self.im) == (other.re, other.im) - - def __bool__(self): - return not (self.re == self.im == 0) - - abs = radius = __abs__ - - def angle(self, fullcircle = twopi): - return (fullcircle/twopi) * ((halfpi - math.atan2(self.re, self.im)) % twopi) - - phi = angle - - def __add__(self, other): - other = ToComplex(other) - return Complex(self.re + other.re, self.im + other.im) - - __radd__ = __add__ - - def __sub__(self, other): - other = ToComplex(other) - return Complex(self.re - other.re, self.im - other.im) - - def __rsub__(self, other): - other = ToComplex(other) - return other - self - - def __mul__(self, other): - other = ToComplex(other) - return Complex(self.re*other.re - self.im*other.im, - self.re*other.im + self.im*other.re) - - __rmul__ = __mul__ - - def __truediv__(self, other): - other = ToComplex(other) - d = float(other.re*other.re + other.im*other.im) - if not d: raise ZeroDivisionError('Complex division') - return Complex((self.re*other.re + self.im*other.im) / d, - (self.im*other.re - self.re*other.im) / d) - - def __rtruediv__(self, other): - other = ToComplex(other) - return other / self - - def __pow__(self, n, z=None): - if z is not None: - raise TypeError('Complex does not support ternary pow()') - if IsComplex(n): - if n.im: - if self.im: raise TypeError('Complex to the Complex power') - else: return exp(math.log(self.re)*n) - n = n.re - r = pow(self.abs(), n) - phi = n*self.angle() - return Complex(math.cos(phi)*r, math.sin(phi)*r) - - def __rpow__(self, base): - base = ToComplex(base) - return pow(base, self) - -def exp(z): - r = math.exp(z.re) - return Complex(math.cos(z.im)*r,math.sin(z.im)*r) - - -def checkop(expr, a, b, value, fuzz = 1e-6): - print(' ', a, 'and', b, end=' ') - try: - result = eval(expr) - except Exception as e: - print('!!\t!!\t!! error: {}'.format(e)) - return - print('->', result) - if isinstance(result, str) or isinstance(value, str): - ok = (result == value) - else: - ok = abs(result - value) <= fuzz - if not ok: - print('!!\t!!\t!! should be', value, 'diff', abs(result - value)) - -def test(): - print('test constructors') - constructor_test = ( - # "expect" is an array [re,im] "got" the Complex. - ( (0,0), Complex() ), - ( (0,0), Complex() ), - ( (1,0), Complex(1) ), - ( (0,1), Complex(0,1) ), - ( (1,2), Complex(Complex(1,2)) ), - ( (1,3), Complex(Complex(1,2),1) ), - ( (0,0), Complex(0,Complex(0,0)) ), - ( (3,4), Complex(3,Complex(4)) ), - ( (-1,3), Complex(1,Complex(3,2)) ), - ( (-7,6), Complex(Complex(1,2),Complex(4,8)) ) ) - cnt = [0,0] - for t in constructor_test: - cnt[0] += 1 - if ((t[0][0]!=t[1].re)or(t[0][1]!=t[1].im)): - print(" expected", t[0], "got", t[1]) - cnt[1] += 1 - print(" ", cnt[1], "of", cnt[0], "tests failed") - # test operators - testsuite = { - 'a+b': [ - (1, 10, 11), - (1, Complex(0,10), Complex(1,10)), - (Complex(0,10), 1, Complex(1,10)), - (Complex(0,10), Complex(1), Complex(1,10)), - (Complex(1), Complex(0,10), Complex(1,10)), - ], - 'a-b': [ - (1, 10, -9), - (1, Complex(0,10), Complex(1,-10)), - (Complex(0,10), 1, Complex(-1,10)), - (Complex(0,10), Complex(1), Complex(-1,10)), - (Complex(1), Complex(0,10), Complex(1,-10)), - ], - 'a*b': [ - (1, 10, 10), - (1, Complex(0,10), Complex(0, 10)), - (Complex(0,10), 1, Complex(0,10)), - (Complex(0,10), Complex(1), Complex(0,10)), - (Complex(1), Complex(0,10), Complex(0,10)), - ], - 'a/b': [ - (1., 10, 0.1), - (1, Complex(0,10), Complex(0, -0.1)), - (Complex(0, 10), 1, Complex(0, 10)), - (Complex(0, 10), Complex(1), Complex(0, 10)), - (Complex(1), Complex(0,10), Complex(0, -0.1)), - ], - 'pow(a,b)': [ - (1, 10, 1), - (1, Complex(0,10), 1), - (Complex(0,10), 1, Complex(0,10)), - (Complex(0,10), Complex(1), Complex(0,10)), - (Complex(1), Complex(0,10), 1), - (2, Complex(4,0), 16), - ], - } - for expr in sorted(testsuite): - print(expr + ':') - t = (expr,) - for item in testsuite[expr]: - checkop(*(t+item)) - - -if __name__ == '__main__': - test() diff --git a/Demo/classes/Dates.py b/Demo/classes/Dates.py deleted file mode 100644 index 63488ce..0000000 --- a/Demo/classes/Dates.py +++ /dev/null @@ -1,227 +0,0 @@ -""" -Class Date supplies date objects that support date arithmetic. - -Date(month,day,year) returns a Date object. An instance prints as, -e.g., 'Mon 16 Aug 1993'. - -Addition, subtraction, comparison operators, min, max, and sorting -all work as expected for date objects: int+date or date+int returns -the date `int' days from `date'; date+date raises an exception; -date-int returns the date `int' days before `date'; date2-date1 returns -an integer, the number of days from date1 to date2; int-date raises an -exception; date1 < date2 is true iff date1 occurs before date2 (& -similarly for other comparisons); min(date1,date2) is the earlier of -the two dates and max(date1,date2) the later; and date objects can be -used as dictionary keys. - -Date objects support one visible method, date.weekday(). This returns -the day of the week the date falls on, as a string. - -Date objects also have 4 read-only data attributes: - .month in 1..12 - .day in 1..31 - .year int or long int - .ord the ordinal of the date relative to an arbitrary staring point - -The Dates module also supplies function today(), which returns the -current date as a date object. - -Those entranced by calendar trivia will be disappointed, as no attempt -has been made to accommodate the Julian (etc) system. On the other -hand, at least this package knows that 2000 is a leap year but 2100 -isn't, and works fine for years with a hundred decimal digits <wink>. - -Tim Peters tim@ksr.com -not speaking for Kendall Square Research Corp - -Adapted to Python 1.1 (where some hacks to overcome coercion are unnecessary) -by Guido van Rossum - -Note that as of Python 2.3, a datetime module is included in the stardard -library. -""" - -import functools - -_MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', - 'June', 'July', 'August', 'September', 'October', - 'November', 'December' ] - -_DAY_NAMES = [ 'Friday', 'Saturday', 'Sunday', 'Monday', - 'Tuesday', 'Wednesday', 'Thursday' ] - -_DAYS_IN_MONTH = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] - -_DAYS_BEFORE_MONTH = [] -dbm = 0 -for dim in _DAYS_IN_MONTH: - _DAYS_BEFORE_MONTH.append(dbm) - dbm = dbm + dim -del dbm, dim - -def _is_leap(year): # 1 if leap year, else 0 - if year % 4 != 0: return 0 - if year % 400 == 0: return 1 - return year % 100 != 0 - -def _days_in_year(year): # number of days in year - return 365 + _is_leap(year) - -def _days_before_year(year): # number of days before year - return year*365 + (year+3)//4 - (year+99)//100 + (year+399)//400 - -def _days_in_month(month, year): # number of days in month of year - if month == 2 and _is_leap(year): return 29 - return _DAYS_IN_MONTH[month-1] - -def _days_before_month(month, year): # number of days in year before month - return _DAYS_BEFORE_MONTH[month-1] + (month > 2 and _is_leap(year)) - -def _date2num(date): # compute ordinal of date.month,day,year - return _days_before_year(date.year) + \ - _days_before_month(date.month, date.year) + \ - date.day - -_DI400Y = _days_before_year(400) # number of days in 400 years - -def _num2date(n): # return date with ordinal n - if not isinstance(n, int): - raise TypeError('argument must be integer: %r' % type(n)) - - # Get uninitialized Date object. This is necesary because once - # attributes are set, they cannot be changed. - ans = Date.__new__(Date) - ans.ord = n - - n400 = (n-1)//_DI400Y # # of 400-year blocks preceding - year, n = 400 * n400, n - _DI400Y * n400 - more = n // 365 - dby = _days_before_year(more) - if dby >= n: - more = more - 1 - dby = dby - _days_in_year(more) - year, n = year + more, n - dby - month = min(n//29 + 1, 12) - dbm = _days_before_month(month, year) - if dbm >= n: - month = month - 1 - dbm = dbm - _days_in_month(month, year) - - ans.month, ans.day, ans.year = month, n-dbm, year - return ans - -def _num2day(n): # return weekday name of day with ordinal n - return _DAY_NAMES[n % 7] - -@functools.total_ordering -class Date: - def __init__(self, month, day, year): - if not 1 <= month <= 12: - raise ValueError('month must be in 1..12: %r' % (month,)) - dim = _days_in_month(month, year) - if not 1 <= day <= dim: - raise ValueError('day must be in 1..%r: %r' % (dim, day)) - self.month, self.day, self.year = map(int, (month, day, year)) - self.ord = _date2num(self) - - # don't allow setting existing attributes - def __setattr__(self, name, value): - if name in self.__dict__: - raise AttributeError('read-only attribute ' + name) - self.__dict__[name] = value - - def __eq__(self, other): - return self.ord == other.ord - - def __lt__(self, other): - return self.ord < other.ord - - # define a hash function so dates can be used as dictionary keys - def __hash__(self): - return hash(self.ord) - - # print as, e.g., Mon 16 Aug 1993 - def __repr__(self): - return '%.3s %2d %.3s %r' % ( - self.weekday(), - self.day, - _MONTH_NAMES[self.month-1], - self.year) - - # Python 1.1 coerces neither int+date nor date+int - def __add__(self, n): - if not isinstance(n, int): - raise TypeError('can\'t add %r to date' % type(n)) - return _num2date(self.ord + n) - __radd__ = __add__ # handle int+date - - # Python 1.1 coerces neither date-int nor date-date - def __sub__(self, other): - if isinstance(other, int): # date-int - return _num2date(self.ord - other) - else: - return self.ord - other.ord # date-date - - # complain about int-date - def __rsub__(self, other): - raise TypeError('Can\'t subtract date from integer') - - def weekday(self): - return _num2day(self.ord) - -def today(): - import time - local = time.localtime(time.time()) - return Date(local[1], local[2], local[0]) - -class DateTestError(Exception): - pass - -def test(firstyear, lastyear): - a = Date(9,30,1913) - b = Date(9,30,1914) - if repr(a) != 'Tue 30 Sep 1913': - raise DateTestError('__repr__ failure') - if (not a < b) or a == b or a > b or b != b: - raise DateTestError('__cmp__ failure') - if a+365 != b or 365+a != b: - raise DateTestError('__add__ failure') - if b-a != 365 or b-365 != a: - raise DateTestError('__sub__ failure') - try: - x = 1 - a - raise DateTestError('int-date should have failed') - except TypeError: - pass - try: - x = a + b - raise DateTestError('date+date should have failed') - except TypeError: - pass - if a.weekday() != 'Tuesday': - raise DateTestError('weekday() failure') - if max(a,b) is not b or min(a,b) is not a: - raise DateTestError('min/max failure') - d = {a-1:b, b:a+1} - if d[b-366] != b or d[a+(b-a)] != Date(10,1,1913): - raise DateTestError('dictionary failure') - - # verify date<->number conversions for first and last days for - # all years in firstyear .. lastyear - - lord = _days_before_year(firstyear) - y = firstyear - while y <= lastyear: - ford = lord + 1 - lord = ford + _days_in_year(y) - 1 - fd, ld = Date(1,1,y), Date(12,31,y) - if (fd.ord,ld.ord) != (ford,lord): - raise DateTestError('date->num failed', y) - fd, ld = _num2date(ford), _num2date(lord) - if (1,1,y,12,31,y) != \ - (fd.month,fd.day,fd.year,ld.month,ld.day,ld.year): - raise DateTestError('num->date failed', y) - y = y + 1 - -if __name__ == '__main__': - test(1850, 2150) diff --git a/Demo/classes/README b/Demo/classes/README deleted file mode 100644 index 3c636f0..0000000 --- a/Demo/classes/README +++ /dev/null @@ -1,11 +0,0 @@ -Examples of classes that implement special operators (see reference manual): - -Complex.py Complex numbers -Dates.py Date manipulation package by Tim Peters -Range.py Example of a generator: re-implement built-in range() -Rev.py Yield the reverse of a sequence -Vec.py A simple vector class -bitvec.py A bit-vector class by Jan-Hein B\"uhrman - -(For straightforward examples of basic class features, such as use of -methods and inheritance, see the library code.) diff --git a/Demo/classes/Range.py b/Demo/classes/Range.py deleted file mode 100644 index 4e6f45d..0000000 --- a/Demo/classes/Range.py +++ /dev/null @@ -1,93 +0,0 @@ -"""Example of a generator: re-implement the built-in range function -without actually constructing the list of values. - -OldStyleRange is coded in the way required to work in a 'for' loop before -iterators were introduced into the language; using __getitem__ and __len__ . - -""" -def handleargs(arglist): - """Take list of arguments and extract/create proper start, stop, and step - values and return in a tuple""" - try: - if len(arglist) == 1: - return 0, int(arglist[0]), 1 - elif len(arglist) == 2: - return int(arglist[0]), int(arglist[1]), 1 - elif len(arglist) == 3: - if arglist[2] == 0: - raise ValueError("step argument must not be zero") - return tuple(int(x) for x in arglist) - else: - raise TypeError("range() accepts 1-3 arguments, given", len(arglist)) - except TypeError: - raise TypeError("range() arguments must be numbers or strings " - "representing numbers") - -def genrange(*a): - """Function to implement 'range' as a generator""" - start, stop, step = handleargs(a) - value = start - while value < stop: - yield value - value += step - -class oldrange: - """Class implementing a range object. - To the user the instances feel like immutable sequences - (and you can't concatenate or slice them) - - Done using the old way (pre-iterators; __len__ and __getitem__) to have an - object be used by a 'for' loop. - - """ - - def __init__(self, *a): - """ Initialize start, stop, and step values along with calculating the - nubmer of values (what __len__ will return) in the range""" - self.start, self.stop, self.step = handleargs(a) - self.len = max(0, (self.stop - self.start) // self.step) - - def __repr__(self): - """implement repr(x) which is also used by print""" - return 'range(%r, %r, %r)' % (self.start, self.stop, self.step) - - def __len__(self): - """implement len(x)""" - return self.len - - def __getitem__(self, i): - """implement x[i]""" - if 0 <= i <= self.len: - return self.start + self.step * i - else: - raise IndexError('range[i] index out of range') - - -def test(): - import time, builtins - #Just a quick sanity check - correct_result = list(builtins.range(5, 100, 3)) - oldrange_result = list(oldrange(5, 100, 3)) - genrange_result = list(genrange(5, 100, 3)) - if genrange_result != correct_result or oldrange_result != correct_result: - raise Exception("error in implementation:\ncorrect = %s" - "\nold-style = %s\ngenerator = %s" % - (correct_result, oldrange_result, genrange_result)) - print("Timings for range(1000):") - t1 = time.time() - for i in oldrange(1000): - pass - t2 = time.time() - for i in genrange(1000): - pass - t3 = time.time() - for i in builtins.range(1000): - pass - t4 = time.time() - print(t2-t1, 'sec (old-style class)') - print(t3-t2, 'sec (generator)') - print(t4-t3, 'sec (built-in)') - - -if __name__ == '__main__': - test() diff --git a/Demo/classes/Rev.py b/Demo/classes/Rev.py deleted file mode 100644 index d36ddb9..0000000 --- a/Demo/classes/Rev.py +++ /dev/null @@ -1,93 +0,0 @@ -''' -A class which presents the reverse of a sequence without duplicating it. -From: "Steven D. Majewski" <sdm7g@elvis.med.virginia.edu> - -It works on mutable or inmutable sequences. - ->>> Rev('Hello World!') -!dlroW olleH - -The .forw is so you can use anonymous sequences in __init__, and still -keep a reference the forward sequence. ) -If you give it a non-anonymous mutable sequence, the reverse sequence -will track the updated values. ( but not reassignment! - another -good reason to use anonymous values in creating the sequence to avoid -confusion. Maybe it should be change to copy input sequence to break -the connection completely ? ) - ->>> nnn = list(range(3)) ->>> rnn = Rev(nnn) ->>> for n in rnn: n -... -2 -1 -0 ->>> for n in range(4, 6): nnn.append(n) # update nnn -... ->>> for n in rnn: n # prints reversed updated values -... -5 -4 -2 -1 -0 ->>> nnn = nnn[1:-1] ->>> nnn -[1, 2, 4] ->>> for n in rnn: n # prints reversed values of old nnn -... -5 -4 -2 -1 -0 - -# ->>> WH = Rev('Hello World!') ->>> print(WH.forw, WH.back) -Hello World! !dlroW olleH ->>> nnn = Rev(list(range(1, 10))) ->>> print(nnn.forw) -[1, 2, 3, 4, 5, 6, 7, 8, 9] ->>> print(nnn.back) -[9, 8, 7, 6, 5, 4, 3, 2, 1] - ->>> Rev(nnn) -<1, 2, 3, 4, 5, 6, 7, 8, 9> - -''' - -class Rev: - def __init__(self, seq): - self.forw = seq - self.back = self - - def __len__(self): - return len(self.forw) - - def __getitem__(self, j): - return self.forw[-(j + 1)] - - def __repr__(self): - seq = self.forw - if isinstance(seq, list): - wrap = '[]' - sep = ', ' - elif isinstance(seq, tuple): - wrap = '()' - sep = ', ' - elif isinstance(seq, str): - wrap = '' - sep = '' - else: - wrap = '<>' - sep = ', ' - outstrs = [str(item) for item in self.back] - return wrap[:1] + sep.join(outstrs) + wrap[-1:] - -def _test(): - import doctest, Rev - return doctest.testmod(Rev) - -if __name__ == "__main__": - _test() diff --git a/Demo/classes/Vec.py b/Demo/classes/Vec.py deleted file mode 100644 index 787af69..0000000 --- a/Demo/classes/Vec.py +++ /dev/null @@ -1,68 +0,0 @@ -class Vec: - """ A simple vector class - - Instances of the Vec class can be constructed from numbers - - >>> a = Vec(1, 2, 3) - >>> b = Vec(3, 2, 1) - - added - >>> a + b - Vec(4, 4, 4) - - subtracted - >>> a - b - Vec(-2, 0, 2) - - and multiplied by a scalar on the left - >>> 3.0 * a - Vec(3.0, 6.0, 9.0) - - or on the right - >>> a * 3.0 - Vec(3.0, 6.0, 9.0) - """ - def __init__(self, *v): - self.v = list(v) - - @classmethod - def fromlist(cls, v): - if not isinstance(v, list): - raise TypeError - inst = cls() - inst.v = v - return inst - - def __repr__(self): - args = ', '.join(repr(x) for x in self.v) - return 'Vec({})'.format(args) - - def __len__(self): - return len(self.v) - - def __getitem__(self, i): - return self.v[i] - - def __add__(self, other): - # Element-wise addition - v = [x + y for x, y in zip(self.v, other.v)] - return Vec.fromlist(v) - - def __sub__(self, other): - # Element-wise subtraction - v = [x - y for x, y in zip(self.v, other.v)] - return Vec.fromlist(v) - - def __mul__(self, scalar): - # Multiply by scalar - v = [x * scalar for x in self.v] - return Vec.fromlist(v) - - __rmul__ = __mul__ - - -def test(): - import doctest - doctest.testmod() - -test() diff --git a/Demo/classes/bitvec.py b/Demo/classes/bitvec.py deleted file mode 100644 index 62b26cc..0000000 --- a/Demo/classes/bitvec.py +++ /dev/null @@ -1,322 +0,0 @@ -# -# this is a rather strict implementation of a bit vector class -# it is accessed the same way as an array of python-ints, except -# the value must be 0 or 1 -# - -import sys; rprt = sys.stderr.write #for debugging - -class error(Exception): - pass - - -def _check_value(value): - if type(value) != type(0) or not 0 <= value < 2: - raise error('bitvec() items must have int value 0 or 1') - - -import math - -def _compute_len(param): - mant, l = math.frexp(float(param)) - bitmask = 1 << l - if bitmask <= param: - raise ValueError('(param, l) = %r' % ((param, l),)) - while l: - bitmask = bitmask >> 1 - if param & bitmask: - break - l = l - 1 - return l - - -def _check_key(len, key): - if type(key) != type(0): - raise TypeError('sequence subscript not int') - if key < 0: - key = key + len - if not 0 <= key < len: - raise IndexError('list index out of range') - return key - -def _check_slice(len, i, j): - #the type is ok, Python already checked that - i, j = max(i, 0), min(len, j) - if i > j: - i = j - return i, j - - -class BitVec: - - def __init__(self, *params): - self._data = 0 - self._len = 0 - if not len(params): - pass - elif len(params) == 1: - param, = params - if type(param) == type([]): - value = 0 - bit_mask = 1 - for item in param: - # strict check - #_check_value(item) - if item: - value = value | bit_mask - bit_mask = bit_mask << 1 - self._data = value - self._len = len(param) - elif type(param) == type(0): - if param < 0: - raise error('bitvec() can\'t handle negative longs') - self._data = param - self._len = _compute_len(param) - else: - raise error('bitvec() requires array or long parameter') - elif len(params) == 2: - param, length = params - if type(param) == type(0): - if param < 0: - raise error('can\'t handle negative longs') - self._data = param - if type(length) != type(0): - raise error('bitvec()\'s 2nd parameter must be int') - computed_length = _compute_len(param) - if computed_length > length: - print('warning: bitvec() value is longer than the length indicates, truncating value') - self._data = self._data & \ - ((1 << length) - 1) - self._len = length - else: - raise error('bitvec() requires array or long parameter') - else: - raise error('bitvec() requires 0 -- 2 parameter(s)') - - - def append(self, item): - #_check_value(item) - #self[self._len:self._len] = [item] - self[self._len:self._len] = \ - BitVec(int(not not item), 1) - - - def count(self, value): - #_check_value(value) - if value: - data = self._data - else: - data = (~self)._data - count = 0 - while data: - data, count = data >> 1, count + (data & 1 != 0) - return count - - - def index(self, value): - #_check_value(value): - if value: - data = self._data - else: - data = (~self)._data - index = 0 - if not data: - raise ValueError('list.index(x): x not in list') - while not (data & 1): - data, index = data >> 1, index + 1 - return index - - - def insert(self, index, item): - #_check_value(item) - #self[index:index] = [item] - self[index:index] = BitVec(int(not not item), 1) - - - def remove(self, value): - del self[self.index(value)] - - - def reverse(self): - #ouch, this one is expensive! - #for i in self._len>>1: self[i], self[l-i] = self[l-i], self[i] - data, result = self._data, 0 - for i in range(self._len): - if not data: - result = result << (self._len - i) - break - result, data = (result << 1) | (data & 1), data >> 1 - self._data = result - - - def sort(self): - c = self.count(1) - self._data = ((1 << c) - 1) << (self._len - c) - - - def copy(self): - return BitVec(self._data, self._len) - - - def seq(self): - result = [] - for i in self: - result.append(i) - return result - - - def __repr__(self): - ##rprt('<bitvec class instance object>.' + '__repr__()\n') - return 'bitvec(%r, %r)' % (self._data, self._len) - - def __cmp__(self, other, *rest): - #rprt('%r.__cmp__%r\n' % (self, (other,) + rest)) - if type(other) != type(self): - other = bitvec(other, *rest) - #expensive solution... recursive binary, with slicing - length = self._len - if length == 0 or other._len == 0: - return cmp(length, other._len) - if length != other._len: - min_length = min(length, other._len) - return cmp(self[:min_length], other[:min_length]) or \ - cmp(self[min_length:], other[min_length:]) - #the lengths are the same now... - if self._data == other._data: - return 0 - if length == 1: - return cmp(self[0], other[0]) - else: - length = length >> 1 - return cmp(self[:length], other[:length]) or \ - cmp(self[length:], other[length:]) - - - def __len__(self): - #rprt('%r.__len__()\n' % (self,)) - return self._len - - def __getitem__(self, key): - #rprt('%r.__getitem__(%r)\n' % (self, key)) - key = _check_key(self._len, key) - return self._data & (1 << key) != 0 - - def __setitem__(self, key, value): - #rprt('%r.__setitem__(%r, %r)\n' % (self, key, value)) - key = _check_key(self._len, key) - #_check_value(value) - if value: - self._data = self._data | (1 << key) - else: - self._data = self._data & ~(1 << key) - - def __delitem__(self, key): - #rprt('%r.__delitem__(%r)\n' % (self, key)) - key = _check_key(self._len, key) - #el cheapo solution... - self._data = self[:key]._data | self[key+1:]._data >> key - self._len = self._len - 1 - - def __getslice__(self, i, j): - #rprt('%r.__getslice__(%r, %r)\n' % (self, i, j)) - i, j = _check_slice(self._len, i, j) - if i >= j: - return BitVec(0, 0) - if i: - ndata = self._data >> i - else: - ndata = self._data - nlength = j - i - if j != self._len: - #we'll have to invent faster variants here - #e.g. mod_2exp - ndata = ndata & ((1 << nlength) - 1) - return BitVec(ndata, nlength) - - def __setslice__(self, i, j, sequence, *rest): - #rprt('%s.__setslice__%r\n' % (self, (i, j, sequence) + rest)) - i, j = _check_slice(self._len, i, j) - if type(sequence) != type(self): - sequence = bitvec(sequence, *rest) - #sequence is now of our own type - ls_part = self[:i] - ms_part = self[j:] - self._data = ls_part._data | \ - ((sequence._data | \ - (ms_part._data << sequence._len)) << ls_part._len) - self._len = self._len - j + i + sequence._len - - def __delslice__(self, i, j): - #rprt('%r.__delslice__(%r, %r)\n' % (self, i, j)) - i, j = _check_slice(self._len, i, j) - if i == 0 and j == self._len: - self._data, self._len = 0, 0 - elif i < j: - self._data = self[:i]._data | (self[j:]._data >> i) - self._len = self._len - j + i - - def __add__(self, other): - #rprt('%r.__add__(%r)\n' % (self, other)) - retval = self.copy() - retval[self._len:self._len] = other - return retval - - def __mul__(self, multiplier): - #rprt('%r.__mul__(%r)\n' % (self, multiplier)) - if type(multiplier) != type(0): - raise TypeError('sequence subscript not int') - if multiplier <= 0: - return BitVec(0, 0) - elif multiplier == 1: - return self.copy() - #handle special cases all 0 or all 1... - if self._data == 0: - return BitVec(0, self._len * multiplier) - elif (~self)._data == 0: - return ~BitVec(0, self._len * multiplier) - #otherwise el cheapo again... - retval = BitVec(0, 0) - while multiplier: - retval, multiplier = retval + self, multiplier - 1 - return retval - - def __and__(self, otherseq, *rest): - #rprt('%r.__and__%r\n' % (self, (otherseq,) + rest)) - if type(otherseq) != type(self): - otherseq = bitvec(otherseq, *rest) - #sequence is now of our own type - return BitVec(self._data & otherseq._data, \ - min(self._len, otherseq._len)) - - - def __xor__(self, otherseq, *rest): - #rprt('%r.__xor__%r\n' % (self, (otherseq,) + rest)) - if type(otherseq) != type(self): - otherseq = bitvec(otherseq, *rest) - #sequence is now of our own type - return BitVec(self._data ^ otherseq._data, \ - max(self._len, otherseq._len)) - - - def __or__(self, otherseq, *rest): - #rprt('%r.__or__%r\n' % (self, (otherseq,) + rest)) - if type(otherseq) != type(self): - otherseq = bitvec(otherseq, *rest) - #sequence is now of our own type - return BitVec(self._data | otherseq._data, \ - max(self._len, otherseq._len)) - - - def __invert__(self): - #rprt('%r.__invert__()\n' % (self,)) - return BitVec(~self._data & ((1 << self._len) - 1), \ - self._len) - - def __int__(self): - return int(self._data) - - def __float__(self): - return float(self._data) - - -bitvec = BitVec |