1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id: cf_generator.cc,v 1.2 1997-06-19 12:27:05 schmidt Exp $ */ |
---|
3 | |
---|
4 | #include <config.h> |
---|
5 | |
---|
6 | #include "assert.h" |
---|
7 | |
---|
8 | #include "cf_defs.h" |
---|
9 | #include "cf_generator.h" |
---|
10 | #include "imm.h" |
---|
11 | #include "gfops.h" |
---|
12 | #include "ffops.h" |
---|
13 | |
---|
14 | bool FFGenerator::hasItems() const |
---|
15 | { |
---|
16 | return current < ff_prime; |
---|
17 | } |
---|
18 | |
---|
19 | CanonicalForm FFGenerator::item() const |
---|
20 | { |
---|
21 | ASSERT( current < ff_prime, "no more items" ); |
---|
22 | return CanonicalForm( int2imm_p( current ) ); |
---|
23 | } |
---|
24 | |
---|
25 | void FFGenerator::next() |
---|
26 | { |
---|
27 | ASSERT( current < ff_prime, "no more items" ); |
---|
28 | current++; |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | GFGenerator::GFGenerator() |
---|
33 | { |
---|
34 | current = gf_zero(); |
---|
35 | } |
---|
36 | |
---|
37 | bool GFGenerator::hasItems() const |
---|
38 | { |
---|
39 | return ( current != gf_q + 1 ); |
---|
40 | } |
---|
41 | |
---|
42 | void GFGenerator::reset() |
---|
43 | { |
---|
44 | current = gf_zero(); |
---|
45 | } |
---|
46 | |
---|
47 | CanonicalForm GFGenerator::item() const |
---|
48 | { |
---|
49 | ASSERT( current != gf_q + 1, "no more items" ); |
---|
50 | return CanonicalForm( int2imm_gf( current ) ); |
---|
51 | } |
---|
52 | |
---|
53 | void GFGenerator::next() |
---|
54 | { |
---|
55 | ASSERT( current != gf_q + 1, "no more items" ); |
---|
56 | if ( gf_iszero( current ) ) |
---|
57 | current = 0; |
---|
58 | else if ( current == gf_q1 - 1 ) |
---|
59 | current = gf_q + 1; |
---|
60 | else |
---|
61 | current++; |
---|
62 | } |
---|
63 | |
---|
64 | AlgExtGenerator::AlgExtGenerator() |
---|
65 | { |
---|
66 | ASSERT( 0, "not a valid generator" ); |
---|
67 | } |
---|
68 | |
---|
69 | AlgExtGenerator::AlgExtGenerator( const AlgExtGenerator & ) |
---|
70 | { |
---|
71 | ASSERT( 0, "not a valid generator" ); |
---|
72 | } |
---|
73 | |
---|
74 | AlgExtGenerator& AlgExtGenerator::operator= ( const AlgExtGenerator & ) |
---|
75 | { |
---|
76 | ASSERT( 0, "not a valid generator" ); |
---|
77 | return *this; |
---|
78 | } |
---|
79 | |
---|
80 | AlgExtGenerator::AlgExtGenerator( const Variable & a ) |
---|
81 | { |
---|
82 | ASSERT( a.level() < 0, "not an algebraic extension" ); |
---|
83 | ASSERT( getCharacteristic() > 0, "not a finite field" ); |
---|
84 | algext = a; |
---|
85 | n = degree( getMipo( a ) ); |
---|
86 | gens = new CFGenerator * [n]; |
---|
87 | nomoreitems = false; |
---|
88 | for ( int i = 0; i < n; i++ ) |
---|
89 | gens[i] = CFGenFactory::generate(); |
---|
90 | } |
---|
91 | |
---|
92 | AlgExtGenerator::~AlgExtGenerator() |
---|
93 | { |
---|
94 | for ( int i = 0; i < n; i++ ) |
---|
95 | delete gens[i]; |
---|
96 | delete [] gens; |
---|
97 | } |
---|
98 | |
---|
99 | void AlgExtGenerator::reset() |
---|
100 | { |
---|
101 | for ( int i = 0; i < n; i++ ) |
---|
102 | gens[i]->reset(); |
---|
103 | nomoreitems = false; |
---|
104 | } |
---|
105 | |
---|
106 | CanonicalForm AlgExtGenerator::item() const |
---|
107 | { |
---|
108 | ASSERT( ! nomoreitems, "no more items" ); |
---|
109 | CanonicalForm result = 0; |
---|
110 | for ( int i = 0; i < n; i++ ) |
---|
111 | result += power( algext, i ) * gens[i]->item(); |
---|
112 | return result; |
---|
113 | } |
---|
114 | |
---|
115 | void AlgExtGenerator::next() |
---|
116 | { |
---|
117 | ASSERT( ! nomoreitems, "no more items" ); |
---|
118 | int i = 0; |
---|
119 | bool stop = false; |
---|
120 | while ( ! stop && i < n ) { |
---|
121 | gens[i]->next(); |
---|
122 | if ( ! gens[i]->hasItems() ) { |
---|
123 | gens[i]->reset(); |
---|
124 | i++; |
---|
125 | } |
---|
126 | else |
---|
127 | stop = true; |
---|
128 | } |
---|
129 | if ( ! stop ) |
---|
130 | nomoreitems = true; |
---|
131 | } |
---|
132 | |
---|
133 | CFGenerator * CFGenFactory::generate() |
---|
134 | { |
---|
135 | ASSERT( getCharacteristic() > 0, "not a finite field" ); |
---|
136 | if ( getGFDegree() > 1 ) |
---|
137 | return new GFGenerator(); |
---|
138 | else |
---|
139 | return new FFGenerator(); |
---|
140 | } |
---|