URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-09-20 Matthieu Garrigues <garrigues(a)lrde.epita.fr>
Add track pointer for stack & pw images.
* mln/pw/image.hh: Add some tests.
* mln/value/stack.hh: Update pw::image class for memory managment.
* tests/stack.cc: Update stack_image class for memory managment.
---
mln/pw/image.hh | 41 +++++++++++++++++++++++++++------
mln/value/stack.hh | 64 +++++++++++++++++++++++++++++++++++------------------
tests/stack.cc | 12 +++++++++
3 files changed, 87 insertions(+), 30 deletions(-)
Index: trunk/milena/tests/stack.cc
===================================================================
--- trunk/milena/tests/stack.cc (revision 1144)
+++ trunk/milena/tests/stack.cc (revision 1145)
@@ -42,7 +42,7 @@
typedef image2d_b<int> I;
- box2d b = make::box2d(1, 1);
+ box2d b = make::box2d(2, 2);
image2d_b<int> ima5(b), ima1(b);
point2d p = make::point2d(0, 0);
@@ -51,4 +51,14 @@
value::stack(ima5, ima1)(p) = v;
mln_assertion(value::stack(ima5, ima1)(p) == v);
mln_assertion(ima5(p) == 5 && ima1(p) == 1);
+
+ value::stack_image<2, image2d_b<int> > s1(value::stack(ima5, ima1));
+
+ value::stack_image<2, image2d_b<int> > s2;
+
+ p = make::point2d(1, 1);
+ s2 = s1;
+ s2(p) = v;
+ mln_assertion(s1(p) == v);
+ mln_assertion(ima5(p) == 5 && ima1(p) == 1);
}
Index: trunk/milena/mln/pw/image.hh
===================================================================
--- trunk/milena/mln/pw/image.hh (revision 1144)
+++ trunk/milena/mln/pw/image.hh (revision 1145)
@@ -35,6 +35,7 @@
# include <mln/core/internal/image_base.hh>
# include <mln/core/concept/function.hh>
+# include <mln/core/internal/tracked_ptr.hh>
# include <mln/value/set.hh>
@@ -51,6 +52,7 @@
*/
template <typename F, typename S>
pw::image<F,S>
+
operator | (const Function_p2v<F>& f, const Point_Set<S>& ps);
@@ -58,6 +60,25 @@
namespace pw
{
+ /*! \brief data structure for pw::image
+ *
+ */
+ template <typename F, typename S>
+ struct image_data
+ {
+ public:
+ image_data(const Function_p2v<F>& f, const Point_Set<S>& ps);
+ F f_;
+ S pset_;
+ };
+
+ template <typename F, typename S>
+ image_data<F, S>::image_data(const Function_p2v<F>& f, const
Point_Set<S>& ps)
+ : f_(exact(f)),
+ pset_(exact(ps))
+ {
+ }
+
/*! \brief FIXME
*
*/
@@ -91,6 +112,7 @@
/// Constructor.
image(const Function_p2v<F>& f, const Point_Set<S>& ps);
+ image();
/// Test if this image has been initialized.
@@ -112,8 +134,7 @@
const vset& values() const;
protected:
- F f_;
- S pset_;
+ tracked_ptr< image_data<F, S> > data_;
};
} // end of namespace mln::pw
@@ -134,10 +155,14 @@
{
template <typename F, typename S>
+ image<F,S>::image()
+ {
+ }
+
+ template <typename F, typename S>
image<F,S>::image(const Function_p2v<F>& f, const
Point_Set<S>& ps)
- : f_(exact(f)),
- pset_(exact(ps))
{
+ data_ = new image_data<F, S>(f, ps);
}
template <typename F, typename S>
@@ -149,22 +174,22 @@
template <typename F, typename S>
bool image<F,S>::owns_(const psite& p) const
{
- return pset_.has(p);
+ return data_->pset_.has(p);
}
template <typename F, typename S>
const S&
image<F,S>::domain() const
{
- return pset_;
+ return data_->pset_;
}
template <typename F, typename S>
mln_result(F)
image<F,S>::operator()(const psite& p) const
{
- mln_precondition(pset_.has(p));
- return f_(p);
+ mln_precondition(data_->pset_.has(p));
+ return data_->f_(p);
}
template <typename F, typename S>
Index: trunk/milena/mln/value/stack.hh
===================================================================
--- trunk/milena/mln/value/stack.hh (revision 1144)
+++ trunk/milena/mln/value/stack.hh (revision 1145)
@@ -34,6 +34,8 @@
*/
# include <mln/core/internal/image_base.hh>
+# include <mln/core/internal/tracked_ptr.hh>
+
# include <mln/metal/vec.hh>
# include <mln/value/set.hh>
# include <mln/value/proxy.hh>
@@ -76,6 +78,23 @@
} // end of namespace mln::value::internal
+ /*! \brief data structure for stack_image
+ *
+ */
+ template <unsigned n, typename I>
+ struct stack_image_data
+ {
+ public:
+ stack_image_data(const metal::vec<n,I>& imas);
+ metal::vec<n,I> imas_;
+ };
+
+ template <unsigned n, typename I>
+ stack_image_data<n,I>::stack_image_data(const metal::vec<n,I>& imas)
+ : imas_(imas)
+ {
+ }
+
/*! \brief FIXME
*
*/
@@ -106,7 +125,8 @@
/// Constructor.
- stack_image(const metal::vec<n,I*>& imas);
+ stack_image(const metal::vec<n,I>& imas);
+ stack_image();
/// Test if this image has been initialized.
@@ -130,7 +150,7 @@
const vset& values() const;
protected:
- metal::vec<n,I*> imas_;
+ tracked_ptr< stack_image_data<n, I> > data_;
};
@@ -151,13 +171,17 @@
// stack_image<n, I>
template <unsigned n, typename I>
- stack_image<n,I>::stack_image(const metal::vec<n,I*>& imas)
- : imas_(imas)
+ stack_image<n,I>::stack_image()
{
+ }
+
+ template <unsigned n, typename I>
+ stack_image<n,I>::stack_image(const metal::vec<n,I>& imas)
+ {
+ data_ = new stack_image_data<n,I>(imas);
for (unsigned i = 0; i < n; ++i)
{
- mln_precondition(imas[i] != 0);
- mln_precondition(imas[i]->has_data());
+ mln_precondition(imas[i].has_data());
}
}
@@ -165,7 +189,7 @@
bool stack_image<n,I>::has_data() const
{
for (unsigned i = 0; i < n; ++i)
- mln_invariant(imas_[i]->has_data());
+ mln_invariant(data_->imas_[i].has_data());
return true;
}
@@ -173,7 +197,7 @@
bool stack_image<n,I>::owns_(const psite& p) const
{
for (unsigned i = 0; i < n; ++i)
- if (! imas_[i]->owns_(p))
+ if (! data_->imas_[i].owns_(p))
return false;
return true;
}
@@ -182,7 +206,7 @@
const mln_pset(I)&
stack_image<n,I>::domain() const
{
- return imas_[0]->domain();
+ return data_->imas_[0].domain();
}
template <unsigned n, typename I>
@@ -192,7 +216,7 @@
mln_precondition(this->owns_(p));
metal::vec<n, mln_value(I)> tmp;
for (unsigned i = 0; i < n; ++i)
- tmp[i] = imas_[i]->operator()(p);
+ tmp[i] = data_->imas_[i].operator()(p);
return tmp;
}
@@ -210,7 +234,7 @@
mln_precondition(this->owns_(p));
// FIXME!!!
for (unsigned i = 0; i < n; ++i)
- imas_[i]->operator()(p) = v[i];
+ data_->imas_[i].operator()(p) = v[i];
}
template <unsigned n, typename I>
@@ -234,11 +258,10 @@
stack(const Image<I>& ima1, const Image<I>& ima2)
{
mln_precondition(exact(ima1).domain() == exact(ima2).domain());
- metal::vec<2, const I*> imas;
- imas[0] = & exact(ima1);
- imas[1] = & exact(ima2);
- stack_image<2, const I> tmp(imas);
- return tmp;
+ metal::vec<2, const I> imas;
+ imas[0] = exact(ima1);
+ imas[1] = exact(ima2);
+ return imas;
}
template <typename I>
@@ -246,11 +269,10 @@
stack(Image<I>& ima1, Image<I>& ima2)
{
mln_precondition(exact(ima1).domain() == exact(ima2).domain());
- metal::vec<2, I*> imas;
- imas[0] = & exact(ima1);
- imas[1] = & exact(ima2);
- stack_image<2, I> tmp(imas);
- return tmp;
+ metal::vec<2, I> imas;
+ imas[0] = exact(ima1);
+ imas[1] = exact(ima2);
+ return imas;
}
# endif // ! MLN_INCLUDE_ONLY