source: git/factory/fac_iterfor.cc @ 6caa2a6

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