[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[2dd068] | 3 | |
---|
[e4fe2b] | 4 | #include "config.h" |
---|
[ab4548f] | 5 | |
---|
[650f2d8] | 6 | #include "cf_assert.h" |
---|
[ab4548f] | 7 | |
---|
[2dd068] | 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 | |
---|
[99ffd8] | 31 | CFGenerator * FFGenerator::clone () const |
---|
| 32 | { |
---|
| 33 | return new FFGenerator(); |
---|
| 34 | } |
---|
[2dd068] | 35 | |
---|
| 36 | GFGenerator::GFGenerator() |
---|
| 37 | { |
---|
| 38 | current = gf_zero(); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | bool GFGenerator::hasItems() const |
---|
| 42 | { |
---|
| 43 | return ( current != gf_q + 1 ); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void GFGenerator::reset() |
---|
| 47 | { |
---|
| 48 | current = gf_zero(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | CanonicalForm GFGenerator::item() const |
---|
| 52 | { |
---|
| 53 | ASSERT( current != gf_q + 1, "no more items" ); |
---|
| 54 | return CanonicalForm( int2imm_gf( current ) ); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | void GFGenerator::next() |
---|
| 58 | { |
---|
| 59 | ASSERT( current != gf_q + 1, "no more items" ); |
---|
| 60 | if ( gf_iszero( current ) ) |
---|
[806c18] | 61 | current = 0; |
---|
[2dd068] | 62 | else if ( current == gf_q1 - 1 ) |
---|
[806c18] | 63 | current = gf_q + 1; |
---|
[2dd068] | 64 | else |
---|
[806c18] | 65 | current++; |
---|
[2dd068] | 66 | } |
---|
| 67 | |
---|
[99ffd8] | 68 | CFGenerator * GFGenerator::clone () const |
---|
| 69 | { |
---|
| 70 | return new GFGenerator(); |
---|
| 71 | } |
---|
| 72 | |
---|
[2dd068] | 73 | AlgExtGenerator::AlgExtGenerator() |
---|
| 74 | { |
---|
| 75 | ASSERT( 0, "not a valid generator" ); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | AlgExtGenerator::AlgExtGenerator( const AlgExtGenerator & ) |
---|
| 79 | { |
---|
| 80 | ASSERT( 0, "not a valid generator" ); |
---|
[ab4548f] | 81 | } |
---|
[2dd068] | 82 | |
---|
| 83 | AlgExtGenerator& AlgExtGenerator::operator= ( const AlgExtGenerator & ) |
---|
| 84 | { |
---|
| 85 | ASSERT( 0, "not a valid generator" ); |
---|
| 86 | return *this; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | AlgExtGenerator::AlgExtGenerator( const Variable & a ) |
---|
| 90 | { |
---|
| 91 | ASSERT( a.level() < 0, "not an algebraic extension" ); |
---|
| 92 | ASSERT( getCharacteristic() > 0, "not a finite field" ); |
---|
| 93 | algext = a; |
---|
| 94 | n = degree( getMipo( a ) ); |
---|
[b96e07] | 95 | if ( getGFDegree() > 1 ) |
---|
| 96 | { |
---|
| 97 | gensg = new GFGenerator * [n]; |
---|
| 98 | for ( int i = 0; i < n; i++ ) |
---|
| 99 | gensg[i] = new GFGenerator(); |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | gensf = new FFGenerator * [n]; |
---|
| 104 | for ( int i = 0; i < n; i++ ) |
---|
| 105 | gensf[i] = new FFGenerator(); |
---|
| 106 | } |
---|
[2dd068] | 107 | nomoreitems = false; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | AlgExtGenerator::~AlgExtGenerator() |
---|
| 111 | { |
---|
[b96e07] | 112 | if ( getGFDegree() > 1 ) |
---|
| 113 | { |
---|
| 114 | for ( int i = 0; i < n; i++ ) |
---|
| 115 | delete gensg[i]; |
---|
| 116 | delete [] gensg; |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | { |
---|
| 120 | for ( int i = 0; i < n; i++ ) |
---|
| 121 | delete gensf[i]; |
---|
| 122 | delete [] gensf; |
---|
| 123 | } |
---|
[2dd068] | 124 | } |
---|
| 125 | |
---|
| 126 | void AlgExtGenerator::reset() |
---|
| 127 | { |
---|
[b96e07] | 128 | if ( getGFDegree() > 1 ) |
---|
| 129 | { |
---|
| 130 | for ( int i = 0; i < n; i++ ) |
---|
| 131 | gensg[i]->reset(); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | for ( int i = 0; i < n; i++ ) |
---|
| 136 | gensf[i]->reset(); |
---|
| 137 | } |
---|
[2dd068] | 138 | nomoreitems = false; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | CanonicalForm AlgExtGenerator::item() const |
---|
| 142 | { |
---|
| 143 | ASSERT( ! nomoreitems, "no more items" ); |
---|
| 144 | CanonicalForm result = 0; |
---|
[b96e07] | 145 | if ( getGFDegree() > 1 ) |
---|
| 146 | { |
---|
| 147 | for ( int i = 0; i < n; i++ ) |
---|
[806c18] | 148 | result += power( algext, i ) * gensg[i]->item(); |
---|
[b96e07] | 149 | } |
---|
| 150 | else |
---|
| 151 | { |
---|
| 152 | for ( int i = 0; i < n; i++ ) |
---|
[806c18] | 153 | result += power( algext, i ) * gensf[i]->item(); |
---|
[b96e07] | 154 | } |
---|
[2dd068] | 155 | return result; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void AlgExtGenerator::next() |
---|
| 159 | { |
---|
| 160 | ASSERT( ! nomoreitems, "no more items" ); |
---|
| 161 | int i = 0; |
---|
| 162 | bool stop = false; |
---|
[b96e07] | 163 | if ( getGFDegree() > 1 ) |
---|
| 164 | { |
---|
| 165 | while ( ! stop && i < n ) |
---|
| 166 | { |
---|
[806c18] | 167 | gensg[i]->next(); |
---|
| 168 | if ( ! gensg[i]->hasItems() ) |
---|
[b96e07] | 169 | { |
---|
[806c18] | 170 | gensg[i]->reset(); |
---|
| 171 | i++; |
---|
| 172 | } |
---|
| 173 | else |
---|
| 174 | stop = true; |
---|
[b96e07] | 175 | } |
---|
| 176 | } |
---|
| 177 | else |
---|
| 178 | { |
---|
| 179 | while ( ! stop && i < n ) |
---|
| 180 | { |
---|
[806c18] | 181 | gensf[i]->next(); |
---|
| 182 | if ( ! gensf[i]->hasItems() ) |
---|
[b96e07] | 183 | { |
---|
[806c18] | 184 | gensf[i]->reset(); |
---|
| 185 | i++; |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | stop = true; |
---|
[b96e07] | 189 | } |
---|
[2dd068] | 190 | } |
---|
| 191 | if ( ! stop ) |
---|
[806c18] | 192 | nomoreitems = true; |
---|
[2dd068] | 193 | } |
---|
| 194 | |
---|
[99ffd8] | 195 | CFGenerator * AlgExtGenerator::clone () const |
---|
| 196 | { |
---|
| 197 | return new AlgExtGenerator(algext); |
---|
| 198 | } |
---|
| 199 | |
---|
[2dd068] | 200 | CFGenerator * CFGenFactory::generate() |
---|
| 201 | { |
---|
| 202 | ASSERT( getCharacteristic() > 0, "not a finite field" ); |
---|
| 203 | if ( getGFDegree() > 1 ) |
---|
[806c18] | 204 | return new GFGenerator(); |
---|
[2dd068] | 205 | else |
---|
[806c18] | 206 | return new FFGenerator(); |
---|
[2dd068] | 207 | } |
---|