[72713e5] | 1 | #pragma once |
---|
| 2 | |
---|
[85855b0] | 3 | #include "iostream.hfa" |
---|
| 4 | |
---|
[d5efcb7] | 5 | forall( E ) trait Bounded { |
---|
[26d40a1] | 6 | E lowerBound(void); |
---|
| 7 | E upperBound(void); |
---|
[85855b0] | 8 | }; |
---|
| 9 | |
---|
[64eeb06] | 10 | forall( E | Bounded( E ) ) trait Serial { |
---|
[5f210c0] | 11 | int fromInstance( E e ); |
---|
| 12 | E fromInt_unsafe( int i ); |
---|
[6804f38] | 13 | E succ_unsafe( E e ); |
---|
| 14 | E pred_unsafe( E e ); |
---|
[0c327ce] | 15 | }; |
---|
| 16 | |
---|
| 17 | forall( E | Serial( E ) ) { |
---|
[5f210c0] | 18 | E fromInt( int i ); |
---|
[6804f38] | 19 | E succ( E e ); |
---|
| 20 | E pred( E e ); |
---|
[e49c308] | 21 | int Countof( E ); |
---|
[0c327ce] | 22 | } |
---|
| 23 | |
---|
| 24 | // forall( E | Bounded(E) ) trait SafeSerial { |
---|
[6804f38] | 25 | // // unsigned fromInstance( E e ); |
---|
| 26 | // E fromInt_unsafe( unsigned i ); |
---|
| 27 | // // E succ_unsafe( E e ); |
---|
| 28 | // //E pred_unsafe( E e ); |
---|
[0c327ce] | 29 | |
---|
[6804f38] | 30 | // unsigned fromInstance( E e ); |
---|
| 31 | // E fromInt( unsigned i ); |
---|
| 32 | // E succ( E e ); |
---|
| 33 | // E pred( E e ); |
---|
[0c327ce] | 34 | // }; |
---|
[85855b0] | 35 | |
---|
[68ea8d2] | 36 | forall( E ) trait CfaEnum { |
---|
[6804f38] | 37 | const char * label( E e ); |
---|
[5f210c0] | 38 | int posn( E e ); |
---|
[236f133] | 39 | }; |
---|
| 40 | |
---|
| 41 | forall( E, V | CfaEnum( E ) ) trait TypedEnum { |
---|
[6804f38] | 42 | V value( E e ); |
---|
[85855b0] | 43 | }; |
---|
| 44 | |
---|
[d5efcb7] | 45 | // I/O |
---|
[85855b0] | 46 | |
---|
[68ea8d2] | 47 | forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial(E) ) |
---|
[bc48c0d] | 48 | istype & ?|?( istype &, E & ); |
---|
[64eeb06] | 49 | |
---|
[236f133] | 50 | forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { |
---|
[d5efcb7] | 51 | ostype & ?|?( ostype &, E ); |
---|
| 52 | OSTYPE_VOID( E ); |
---|
| 53 | } |
---|
| 54 | |
---|
[062467b] | 55 | static inline |
---|
[26d40a1] | 56 | forall( E | CfaEnum(E) | Serial(E) ) { |
---|
[6804f38] | 57 | int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators |
---|
| 58 | int ?!=?( E l, E r ) { return posn( l ) != posn( r ); } |
---|
| 59 | int ?<?( E l, E r ) { return posn( l ) < posn( r ); } |
---|
| 60 | int ?<=?( E l, E r ) { return posn( l ) <= posn( r ); } |
---|
| 61 | int ?>?( E l, E r ) { return posn( l ) > posn( r ); } |
---|
| 62 | int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); } |
---|
[062467b] | 63 | |
---|
[6804f38] | 64 | E ++?( E & l ) { // increment operators |
---|
[5f210c0] | 65 | int pos = posn(l); |
---|
| 66 | l = fromInt_unsafe(pos+1); |
---|
| 67 | return l; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | E --?( E & l ) { |
---|
| 71 | int pos = posn(l); |
---|
| 72 | l = fromInt_unsafe(pos-1); |
---|
[6804f38] | 73 | return l; |
---|
| 74 | } |
---|
[1571e4d] | 75 | |
---|
| 76 | E ?+=? ( E & l, one_t ) { |
---|
[5f210c0] | 77 | int pos = posn(l); |
---|
| 78 | l = fromInt_unsafe(pos+1); |
---|
[1571e4d] | 79 | return l; |
---|
| 80 | } |
---|
[5f210c0] | 81 | |
---|
[2dd5c6d] | 82 | E ?-=? ( E & l, one_t ) { |
---|
[5f210c0] | 83 | int pos = posn(l); |
---|
| 84 | l = fromInt_unsafe(pos-1); |
---|
[2dd5c6d] | 85 | return l; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | E ?+=? ( E & l, int i ) { |
---|
[5f210c0] | 89 | int pos = posn(l); |
---|
| 90 | l = fromInt_unsafe(pos+i); |
---|
[7db4fcd4] | 91 | return l; |
---|
[2dd5c6d] | 92 | } |
---|
| 93 | |
---|
| 94 | E ?-=? ( E & l, int i ) { |
---|
[5f210c0] | 95 | int pos = posn(l); |
---|
| 96 | l = fromInt_unsafe(pos-i); |
---|
| 97 | return l; |
---|
[6804f38] | 98 | } |
---|
[062467b] | 99 | |
---|
[5f210c0] | 100 | E ?++( E & l ) { |
---|
| 101 | int pos = posn(l); |
---|
| 102 | l = fromInt_unsafe(pos+1); |
---|
| 103 | return fromInt_unsafe(pos); |
---|
[6804f38] | 104 | } |
---|
[062467b] | 105 | |
---|
[6804f38] | 106 | E ?--( E & l ) { |
---|
[5f210c0] | 107 | int pos = posn(l); |
---|
| 108 | l = fromInt_unsafe(pos-1); |
---|
| 109 | return fromInt_unsafe(pos); |
---|
[6804f38] | 110 | } |
---|
[64eeb06] | 111 | } |
---|