source: git/factory/fac_iterfor.cc @ 2dd068

spielwiese
Last change on this file since 2dd068 was 2dd068, checked in by Rüdiger Stobbe <stobbe@…>, 28 years ago
Initial revision git-svn-id: file:///usr/local/Singular/svn/trunk@6 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 1.9 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id: fac_iterfor.cc,v 1.0 1996-05-17 10:59:45 stobbe Exp $
3
4/*
5$Log: not supported by cvs2svn $
6*/
7
8#include "assert.h"
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 n, int max ) : MAX( max ), N( n ), last( false )
24{
25    ASSERT( n > 1 && max >= 0, "illegal iterated for" );
26    index = new int[n+1];
27    imax = new int[n+1];
28    fill( 2, max );
29}
30
31IteratedFor::IteratedFor( const IteratedFor & I ) : MAX( I.MAX ), N( I.N ), last( I.last )
32{
33    index = new int[N+1];
34    imax = new int[N+1];
35    for ( int i = 2; 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        MAX = I.MAX;
59        last = I.last;
60        for ( int i = 2; 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[2] == 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 > 1 && 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 >= 2 && i <= N, "illegal index" );
95    return index[i];
96}
97
98ostream& operator<< ( ostream& os, const IteratedFor & I )
99{
100    os << "( " << I[2];
101    for ( int i = 3; i <= I.n(); i++ )
102        os << ", " << I[i];
103    os << " )";
104    return os;
105}
Note: See TracBrowser for help on using the repository browser.