source: git/factory/fac_iterfor.cc @ 2234726

fieker-DuValspielwiese
Last change on this file since 2234726 was 564dd8, checked in by Hans Schoenemann <hannes@…>, 4 years ago
factorize in Z[x,..] w/o NTL
  • Property mode set to 100644
File size: 2.3 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3
4#include "config.h"
5
6
7#include "cf_assert.h"
8
9#include "cf_defs.h"
10#include "fac_iterfor.h"
11
12#ifndef HAVE_NTL
13
14void
15IteratedFor::fill ( int from, int max )
16{
17    while ( from < N ) {
18        imax[from] = max;
19        index[from] = 0;
20        from++;
21    }
22    index[N] = max;
23}
24
25IteratedFor::IteratedFor( int from, int to, int max ) : MAX( max ), FROM( from ), TO( to ), N( TO-FROM ), last( false )
26{
27    ASSERT( N >= 0 && max >= 0, "illegal iterated for" );
28    index = new int[N+1];
29    imax = new int[N+1];
30    fill( 0, max );
31}
32
33IteratedFor::IteratedFor( const IteratedFor & I ) : MAX( I.MAX ), FROM( I.FROM ), TO( I.TO ), N( I.N ), last( I.last )
34{
35    index = new int[N+1];
36    imax = new int[N+1];
37    for ( int i = 0; i <= N; i++ ) {
38        index[i] = I.index[i];
39        imax[i] = I.imax[i];
40    }
41}
42
43IteratedFor::~IteratedFor()
44{
45    delete [] index;
46    delete [] imax;
47}
48
49IteratedFor&
50IteratedFor::operator= ( const IteratedFor & I )
51{
52    if ( this != &I ) {
53        if ( N != I.N ) {
54            N = I.N;
55            delete [] index;
56            delete [] imax;
57            index = new int[N+1];
58            imax = new int[N+1];
59        }
60        FROM = I.FROM;
61        TO = I.TO;
62        MAX = I.MAX;
63        last = I.last;
64        for ( int i = 0; i<= N; i++ ) {
65            index[i] = I.index[i];
66            imax[i] = I.imax[i];
67        }
68    }
69    return *this;
70}
71
72void
73IteratedFor::nextiteration()
74{
75    ASSERT( ! last, "no more iterations" );
76    if ( index[0] == MAX )
77        last = true;
78    else {
79        if ( index[N-1] != imax[N-1] ) {
80            index[N-1]++;
81            index[N]--;
82        }
83        else {
84            int i = N-1, m = index[N];
85            while ( i > 0 && index[i] == imax[i] ) {
86                m += imax[i];
87                i--;
88            }
89            index[i]++; m--;
90            fill( i+1, m );
91        }
92    }
93}
94
95int
96IteratedFor::operator[] ( int i ) const
97{
98    ASSERT( i >= FROM && i <= TO, "illegal index" );
99    return index[i-FROM];
100}
101
102#ifndef NOSTREAMIO
103OSTREAM& operator<< ( OSTREAM& os, const IteratedFor & I )
104{
105    os << "( " << I[I.from()];
106    for ( int i = I.from()+1; i <= I.to(); i++ )
107        os << ", " << I[i];
108    os << " )";
109    return os;
110}
111#endif /* NOSTREAMIO */
112#endif
Note: See TracBrowser for help on using the repository browser.