Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

basics/Zm.hh
Go to the documentation of this file.
00001 /* modular arithmetic
00002  */
00003 
00004 #ifndef Zm_h
00005 #define Zm_h
00006 
00007 #include "typedefs.hh"
00008 
00009 namespace concepts {
00010   // ******************************************************************** Z2 **
00011 
00014   class Z2 {
00016     uchar i_;
00017   public:
00019     Z2() : i_(0) {}
00021     Z2(uint i) : i_(i&1) {}
00022 
00024     operator int() const { return i_; }
00025 
00027     int operator=(int i) { return i_ = i & 1; }  
00029     int operator++() { return i_ ^= 1; }
00031     Z2 succ() const { return i_ ^ 1; }
00032   };
00033 
00034   // ******************************************************************** Z3 **
00035 
00038   class Z3 {
00040     uchar i_;
00041   public:
00043     Z3() : i_(0) {}
00045     Z3(uint i) : i_(i - 3 * (i / 3)) {}
00046 
00048     operator int() const { return i_; }
00049 
00051     int operator=(int i) { return i_ = i - 3 * (i / 3); }  
00053     int operator++() { return i_ = i_ == 2 ? 0 : i_ + 1; }
00055     int operator--() { return i_ = i_ ? i_ - 1 : 2; }
00057     Z3 succ() const { return i_ == 2 ? 0 : i_ + 1; }
00059     Z3 pred() const { return i_ ? i_ - 1 : 2; }
00060   };
00061 
00062   // ******************************************************************** Z4 **
00063 
00066   class Z4 {
00068     uchar i_;
00069   public:
00071     Z4() : i_(0) {}
00073     Z4(uint i) : i_(i & 3) {}
00074 
00076     operator int() const { return i_; }
00077 
00079     int operator=(int i) { return i_ = i & 3; }  
00081     int operator++() { ++i_; return i_ &= 3; }
00083     int operator--() { --i_; return i_ &= 3; }
00085     Z4 succ() const { return (i_ + 1) & 3; }
00087     Z4 pred() const { return (i_ - 1) & 3; }
00088   };
00089 
00090 } // namespace concepts
00091 
00092 #endif // Zm_h

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)