1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef small_finite_cyclic_group_h
26 #define small_finite_cyclic_group_h
29 // Class Z<n> is used to simulate module arithmetic
30 // of a small finite cyclic group.
33 #include <AD/generic/generic.h> // Generic definitions
35 template<unsigned int n
>
40 Z(unsigned long y
) : x(y
) {}
42 operator unsigned long () const { return x
; }
48 friend Z
operator - (Z a
) { return Z
<n
>(n
- a
); }
49 friend Z
operator + (Z a
, Z b
)
50 { unsigned long sum
= a
.x
+ b
.x
;
51 if (sum
>= n
) sum
-= n
;
54 friend Z
operator * (Z a
, Z b
) { return Z
<n
>((a
.x
* b
.x
) % n
); }
55 friend Z
operator - (Z a
, Z b
) { return Z
<n
>(a
+ (-b
)); }
57 void operator += (Z a
) { *this = *this + a
; }
58 void operator -= (Z a
) { *this = *this - a
; }
59 void operator *= (Z a
) { *this = *this * a
; }
62 // Of course, division cannot be defined
68 friend Bool
operator == (const Z a
, const Z b
) { return a
.x
== b
.x
; }
69 friend Bool
operator != (const Z a
, const Z b
) { return a
.x
!= b
.x
; }
70 friend Bool
operator > (const Z a
, const Z b
) { return a
.x
> b
.x
; }
71 friend Bool
operator < (const Z a
, const Z b
) { return a
.x
< b
.x
; }
72 friend Bool
operator >= (const Z a
, const Z b
) { return a
.x
>= b
.x
; }
73 friend Bool
operator <= (const Z a
, const Z b
) { return a
.x
<= b
.x
; }