source: git/factory/fac_iterfor.cc @ 9f7665

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