NOTE: This patch has been committed. The version below is
informational only (whitespace differences have been removed).
ChangeLog addition:
2006-01-10 Didier Verna <didier(a)lrde.epita.fr>
* 2.0/src/oo/c++/Makefile: New.
* 2.0/src/oo/c++/oo_cpp.cc: Avoid using unsigned.
Add checks for dynamic_cast NULL return value.
Provide command-line arguments for size and steps (defaulting to
800x800 and 200).
GSC source patch:
Diff command: svn diff --diff-cmd /usr/bin/diff -x "-u -t -b -B -w"
Files affected: 2.0/src/oo/c++/oo_cpp.cc 2.0/src/oo/c++/Makefile
Index: 2.0/src/oo/c++/Makefile
===================================================================
--- 2.0/src/oo/c++/Makefile (revision 0)
+++ 2.0/src/oo/c++/Makefile (revision 0)
@@ -0,0 +1,5 @@
+CXX = g++
+CPPFLAGS = -DNDEBUG
+CXXFLAGS = -Wall -W -O3
+
+all: oo_cpp
Index: 2.0/src/oo/c++/oo_cpp.cc
===================================================================
--- 2.0/src/oo/c++/oo_cpp.cc (revision 46)
+++ 2.0/src/oo/c++/oo_cpp.cc (working copy)
@@ -24,6 +24,7 @@
virtual value& assign(const value& rhs)
{
const integer* rhs_ = dynamic_cast<const integer*>(&rhs);
+ if (rhs_ != NULL)
value_ = rhs_->value_;
return *this;
}
@@ -47,17 +48,17 @@
class point1d : public point
{
public:
- point1d(unsigned i = 0) :
+ point1d(int i = 0) :
i_(i)
{
}
virtual ~point1d() {}
- unsigned index() const { return i_; }
- unsigned& index() { return i_; }
+ int index() const { return i_; }
+ int& index() { return i_; }
private:
- unsigned i_;
+ int i_;
};
@@ -79,7 +80,7 @@
class piter1d : public piter
{
public:
- piter1d(unsigned n) :
+ piter1d(int n) :
n_(n)
{
}
@@ -100,7 +101,7 @@
return p_;
}
private:
- unsigned n_;
+ int n_;
point1d p_;
};
@@ -122,7 +123,7 @@
class image1d : public image
{
public:
- image1d(unsigned n) :
+ image1d(int n) :
n_(n)
{
data_ = new T[n_];
@@ -130,6 +131,8 @@
value& operator[](const point& p)
{
const point1d* p_ = dynamic_cast<const point1d*>(&p);
+ if (p_ == NULL)
+ return data_[0]; // Silly return, just to perform the dyncast check.
return data_[p_->index()];
}
piter& new_piter()
@@ -137,7 +140,7 @@
return *new piter1d(n_);
}
private:
- unsigned n_;
+ int n_;
T* data_;
};
@@ -160,24 +163,29 @@
void usage(char* argv[])
{
- std::cout << "usage: " << argv[0] << " [nsteps =
1]" << std::endl;
+ std::cout << "usage: " << argv[0] << " [size = 800]
[nsteps = 1]"
+ << std::endl;
exit(1);
}
int main(int argc, char* argv[])
{
- if (argc > 2)
+ if (argc > 3)
usage(argv);
- const unsigned size = 1024 * 1024;
- const unsigned nsteps = argc == 2 ? atoi(argv[1]) : 1;
+ const int size = argc > 1 ? atoi (argv[1]) : 800;
+ const int nsteps = argc > 2 ? atoi (argv[2]) : 200;
- image1d<integer> ima(size);
+ std::cout << size << "x" << size << " / "
<< nsteps << " steps:"
+ << std::endl;
+
+ image1d<integer> ima(size * size);
integer val = 51;
float t0 = (float)clock();
- for (unsigned step = 0; step < nsteps; ++step)
+ for (int step = 0; step < nsteps; ++step)
assign(ima, val);
- std::cout << (clock() - t0) / CLOCKS_PER_SEC << std::endl;
+ float t1 = (float)clock();
+ std::cout << (t1 - t0) / CLOCKS_PER_SEC << std::endl;
}
--
Didier Verna, didier(a)lrde.epita.fr,
http://www.lrde.epita.fr/~didier
EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (1) 44 08 01 85
94276 Le Kremlin-BicĂȘtre, France Fax.+33 (1) 53 14 59 22 didier(a)xemacs.org