Ignore:
Timestamp:
May 14, 2017, 6:30:20 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
a32cfc90
Parents:
4c8f86b3
Message:

first attempt converting rational numbers to generic type

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/libcfa/rational

    r4c8f86b3 r561f730  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Mon May  1 08:25:06 2017
    15 // Update Count     : 33
     14// Last Modified On : Sun May 14 16:49:13 2017
     15// Update Count     : 78
    1616//
    1717
     
    2121#include "iostream"
    2222
     23trait scalar( otype T ) {
     24};
     25
     26trait arithmetic( otype T | scalar( T ) ) {
     27        int !?( T );
     28        int ?==?( T, T );
     29        int ?!=?( T, T );
     30        int ?<?( T, T );
     31        int ?<=?( T, T );
     32        int ?>?( T, T );
     33        int ?>=?( T, T );
     34        void ?{}( T *, zero_t );
     35        void ?{}( T *, one_t );
     36        T +?( T );
     37        T -?( T );
     38        T ?+?( T, T );
     39        T ?-?( T, T );
     40        T ?*?( T, T );
     41        T ?/?( T, T );
     42        T ?%?( T, T );
     43        T ?/=?( T *, T );
     44        T abs( T );
     45};
     46
    2347// implementation
    24 typedef long int RationalImpl;
     48
     49forall ( otype RationalImpl | arithmetic( RationalImpl ) )
    2550struct Rational {
    26         RationalImpl numerator, denominator;                                    // invariant: denominator > 0
     51        RationalImpl numerator, denominator;                            // invariant: denominator > 0
    2752}; // Rational
    2853
    29 // constants
    30 extern struct Rational 0;
    31 extern struct Rational 1;
     54// constructors
    3255
    33 // constructors
    34 void ?{}( Rational * r );
    35 void ?{}( Rational * r, RationalImpl n );
    36 void ?{}( Rational * r, RationalImpl n, RationalImpl d );
     56forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     57void ?{}( Rational(RationalImpl) * r );
     58
     59forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     60void ?{}( Rational(RationalImpl) * r, RationalImpl n );
     61
     62forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     63void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d );
     64
     65forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     66void ?{}( Rational(RationalImpl) * r, zero_t );
     67
     68forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     69void ?{}( Rational(RationalImpl) * r, one_t );
    3770
    3871// getter for numerator/denominator
    39 RationalImpl numerator( Rational r );
    40 RationalImpl denominator( Rational r );
    41 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src );
     72
     73forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     74RationalImpl numerator( Rational(RationalImpl) r );
     75
     76forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     77RationalImpl denominator( Rational(RationalImpl) r );
     78forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     79[ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );
     80
    4281// setter for numerator/denominator
    43 RationalImpl numerator( Rational r, RationalImpl n );
    44 RationalImpl denominator( Rational r, RationalImpl d );
     82
     83forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     84RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n );
     85
     86forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     87RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d );
    4588
    4689// comparison
    47 int ?==?( Rational l, Rational r );
    48 int ?!=?( Rational l, Rational r );
    49 int ?<?( Rational l, Rational r );
    50 int ?<=?( Rational l, Rational r );
    51 int ?>?( Rational l, Rational r );
    52 int ?>=?( Rational l, Rational r );
     90
     91forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     92int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     93
     94forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     95int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     96
     97forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     98int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     99
     100forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     101int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     102
     103forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     104int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     105
     106forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     107int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    53108
    54109// arithmetic
    55 Rational -?( Rational r );
    56 Rational ?+?( Rational l, Rational r );
    57 Rational ?-?( Rational l, Rational r );
    58 Rational ?*?( Rational l, Rational r );
    59 Rational ?/?( Rational l, Rational r );
     110
     111forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     112Rational(RationalImpl) +?( Rational(RationalImpl) r );
     113
     114forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     115Rational(RationalImpl) -?( Rational(RationalImpl) r );
     116
     117forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     118Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     119
     120forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     121Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     122
     123forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     124Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r );
     125
     126forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     127Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r );
    60128
    61129// conversion
    62 double widen( Rational r );
    63 Rational narrow( double f, RationalImpl md );
     130// forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     131// double widen( Rational(RationalImpl) r );
     132// forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     133// Rational(RationalImpl) narrow( double f, RationalImpl md );
    64134
    65135// I/O
    66 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
    67 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
     136forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     137forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } )
     138istype * ?|?( istype *, Rational(RationalImpl) * );
     139
     140forall ( otype RationalImpl | arithmetic( RationalImpl ) )
     141forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } )
     142ostype * ?|?( ostype *, Rational(RationalImpl ) );
    68143
    69144#endif // RATIONAL_H
Note: See TracChangeset for help on using the changeset viewer.