source: git/factory/fac_iterfor.cc @ 0dff6bc

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