source: git/factory/fac_iterfor.cc @ e4fe2b

spielwiese
Last change on this file since e4fe2b 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
RevLine 
[493c477]1/* emacs edit mode for this file is -*- C++ -*- */
[341696]2/* $Id$ */
[2dd068]3
[e4fe2b]4#include "config.h"
[b973c0]5
[650f2d8]6#include "cf_assert.h"
[a347e8]7
[2dd068]8#include "cf_defs.h"
9#include "fac_iterfor.h"
10
11void
12IteratedFor::fill ( int from, int max )
13{
14    while ( from < N ) {
[806c18]15        imax[from] = max;
16        index[from] = 0;
17        from++;
[2dd068]18    }
19    index[N] = max;
20}
21
[d675c7]22IteratedFor::IteratedFor( int from, int to, int max ) : MAX( max ), FROM( from ), TO( to ), N( TO-FROM ), last( false )
[2dd068]23{
[9c9e2a4]24    ASSERT( N >= 0 && max >= 0, "illegal iterated for" );
[d675c7]25    index = new int[N+1];
26    imax = new int[N+1];
27    fill( 0, max );
[2dd068]28}
29
[d675c7]30IteratedFor::IteratedFor( const IteratedFor & I ) : MAX( I.MAX ), FROM( I.FROM ), TO( I.TO ), N( I.N ), last( I.last )
[2dd068]31{
32    index = new int[N+1];
33    imax = new int[N+1];
[d675c7]34    for ( int i = 0; i <= N; i++ ) {
[806c18]35        index[i] = I.index[i];
36        imax[i] = I.imax[i];
[2dd068]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 ) {
[806c18]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        }
[2dd068]65    }
66    return *this;
67}
68
69void
70IteratedFor::nextiteration()
71{
72    ASSERT( ! last, "no more iterations" );
[d675c7]73    if ( index[0] == MAX )
[806c18]74        last = true;
[2dd068]75    else {
[806c18]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        }
[2dd068]89    }
90}
91
92int
93IteratedFor::operator[] ( int i ) const
94{
[d675c7]95    ASSERT( i >= FROM && i <= TO, "illegal index" );
96    return index[i-FROM];
[2dd068]97}
98
[a347e8]99#ifndef NOSTREAMIO
[181148]100OSTREAM& operator<< ( OSTREAM& os, const IteratedFor & I )
[2dd068]101{
[d675c7]102    os << "( " << I[I.from()];
103    for ( int i = I.from()+1; i <= I.to(); i++ )
[806c18]104        os << ", " << I[i];
[2dd068]105    os << " )";
106    return os;
107}
[a347e8]108#endif /* NOSTREAMIO */
Note: See TracBrowser for help on using the repository browser.