aboutsummaryrefslogtreecommitdiffstats
path: root/dim.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dim.cpp')
-rw-r--r--dim.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/dim.cpp b/dim.cpp
new file mode 100644
index 0000000..5e2bedf
--- /dev/null
+++ b/dim.cpp
@@ -0,0 +1,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