aboutsummaryrefslogtreecommitdiffstats
path: root/dim.cpp
blob: 5e2bedf26d7af6cea7f0aed5188fe911ffffd7ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// dim.cpp
// Revision 14-oct-2003

#include "dim.h"
#include "error.h"

#include <algorithm>

class dim_calc {
public:
	dim_calc () : r (1) { }
	void operator () (const size_t n)
	{ r*= n + 1; }
	size_t value () { return r; }
private:
	size_t r;
};

size_t Dimension::elements () const
{
	dim_calc r= std::for_each (dim.begin (), dim.end (), dim_calc () );
	return r.value ();
}

size_t Dimension::evalpos (const Dimension & d) const
{
	size_t n= size ();
	if (d.size () != n)
		throw ErrBadSubscript;
	size_t pos= d [0];
	if (pos > dim [0])
		throw ErrBadSubscript;
	for (size_t i= 1; i < n; ++i)
	{
		if (d [i] > dim [i] )
			throw ErrBadSubscript;
		pos*= dim [i] + 1;
		pos+= d [i];
	}
	return pos;
}

// Only for debug.

std::ostream & operator << (std::ostream & os, const Dimension & d)
{
	size_t s= d.size ();
	if (s == 0)
		os << "(empty)";
	else
	{
		os << '(';
		for (size_t i= 0; i < s; ++i)
		{
			os << d [i];
			if (i < s -1)
				os << ", ";
		}
		os << ')';
	}
	return os;
}

// Fin de dim.cpp
Un proyecto texto-plano.xyz