URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-09-19 Matthieu Garrigues <garrigues(a)lrde.epita.fr>
add tracked pointer in image1d_b and image3d_b
* image1d_b.hh:
* image1d_b_data.hh:
* image3d_b.hh:
* image3d_b_data.hh: update image1d_b and image3d_b
---
image1d_b.hh | 116 ++++++++++--------------------------------
image1d_b_data.hh | 116 ++++++++++++++++++++++++++++++++++++++++++
image3d_b.hh | 147 +++++++++++-------------------------------------------
image3d_b_data.hh | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 317 insertions(+), 205 deletions(-)
Index: trunk/milena/mln/core/image1d_b_data.hh
===================================================================
--- trunk/milena/mln/core/image1d_b_data.hh (revision 0)
+++ trunk/milena/mln/core/image1d_b_data.hh (revision 1134)
@@ -0,0 +1,116 @@
+// Copyright (C) 2007 EPITA Research and Development Laboratory
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef MLN_IMAGE1D_B_DATA_HH
+# define MLN_IMAGE1D_B_DATA_HH
+
+/*! \file mln/core/image1d_b_data.hh
+ *
+ * \brief Definition of the basic mln::image1d_b_data class.
+ */
+
+# include <mln/core/internal/image_base.hh>
+# include <mln/core/box1d.hh>
+
+# include <mln/fun/i2v/all.hh>
+
+namespace mln
+{
+ template <typename T>
+ struct image1d_b_data
+ {
+ public:
+ image1d_b_data(const box1d& b, unsigned bdr);
+ ~image1d_b_data();
+
+ T* buffer_;
+ T* array_;
+
+ box1d b_; // theoretical box
+ unsigned bdr_;
+ box1d vb_; // virtual box, i.e., box including the virtual border
+
+ void update_vb_();
+ void allocate_();
+ void deallocate_();
+
+ };
+
+
+ template <typename T>
+ image1d_b_data<T>::image1d_b_data(const box1d& b, unsigned bdr)
+ : buffer_(0),
+ array_ (0),
+ b_ (b),
+ bdr_ (bdr)
+ {
+ allocate_();
+ }
+
+ template <typename T>
+ image1d_b_data<T>::~image1d_b_data()
+ {
+ deallocate_();
+ }
+
+
+ // private
+
+ template <typename T>
+ void
+ image1d_b_data<T>::update_vb_()
+ {
+ vb_.pmin() = b_.pmin() - dpoint1d(all(bdr_));
+ vb_.pmax() = b_.pmax() + dpoint1d(all(bdr_));
+ }
+
+ template <typename T>
+ void
+ image1d_b_data<T>::allocate_()
+ {
+ update_vb_();
+ unsigned
+ ni = vb_.len(0);
+ buffer_ = new T[ni];
+ array_ = buffer_ - vb_.pmin().ind();
+ mln_postcondition(vb_.len(0) == b_.len(0) + 2 * bdr_);
+ }
+
+ template <typename T>
+ void
+ image1d_b_data<T>::deallocate_()
+ {
+ if (buffer_)
+ {
+ delete[] buffer_;
+ buffer_ = 0;
+ }
+ }
+
+}
+
+#endif // ! MLN_IMAGE1D_B_DATA_HH
Index: trunk/milena/mln/core/image3d_b_data.hh
===================================================================
--- trunk/milena/mln/core/image3d_b_data.hh (revision 0)
+++ trunk/milena/mln/core/image3d_b_data.hh (revision 1134)
@@ -0,0 +1,143 @@
+// Copyright (C) 2007 EPITA Research and Development Laboratory
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef MLN_IMAGE3D_B_DATA_HH
+# define MLN_IMAGE3D_B_DATA_HH
+
+/*! \file mln/core/image3d_b_data.hh
+ *
+ * \brief Definition of the basic mln::image3d_b_data class.
+ */
+
+# include <mln/core/box3d.hh>
+# include <mln/core/point.hh>
+
+# include <mln/fun/i2v/all.hh>
+
+namespace mln
+{
+ template <typename T>
+ struct image3d_b_data
+ {
+ public:
+ image3d_b_data(const box3d& b, unsigned bdr);
+ ~image3d_b_data();
+
+ T* buffer_;
+ T*** array_;
+
+ box3d b_; // theoretical box
+ unsigned bdr_;
+ box3d vb_; // virtual box, i.e., box including the virtual border
+
+ void update_vb_();
+ void allocate_();
+ void deallocate_();
+
+ };
+
+
+ template <typename T>
+ image3d_b_data<T>::image3d_b_data(const box3d& b, unsigned bdr)
+ : buffer_(0),
+ array_ (0),
+ b_ (b),
+ bdr_ (bdr)
+ {
+ allocate_();
+ }
+
+ template <typename T>
+ image3d_b_data<T>::~image3d_b_data()
+ {
+ deallocate_();
+ }
+
+ template <typename T>
+ void
+ image3d_b_data<T>::update_vb_()
+ {
+ vb_.pmin() = b_.pmin() - dpoint3d(all(bdr_));
+ vb_.pmax() = b_.pmax() + dpoint3d(all(bdr_));
+ }
+
+ template <typename T>
+ void
+ image3d_b_data<T>::allocate_()
+ {
+ update_vb_();
+ unsigned
+ ns = vb_.len(0),
+ nr = vb_.len(1),
+ nc = vb_.len(2);
+ buffer_ = new T[nr * nc * ns];
+ array_ = new T**[ns];
+ T* buf = buffer_ - vb_.pmin().col();
+ for (unsigned i = 0; i < ns; ++i)
+ {
+ T** tmp = new T*[nr];
+ array_[i] = tmp;
+ for (unsigned j = 0; j < nr; ++j)
+ {
+ array_[i][j] = buf;
+ buf += nc;
+ }
+ array_[i] -= vb_.pmin().row();
+ }
+ array_ -= vb_.pmin().sli();
+ mln_postcondition(vb_.len(0) == b_.len(0) + 2 * bdr_);
+ }
+
+ template <typename T>
+ void
+ image3d_b_data<T>::deallocate_()
+ {
+ if (buffer_)
+ {
+ delete[] buffer_;
+ buffer_ = 0;
+ }
+ for (typename point3d::coord i = vb_.pmin().sli(); i <= vb_.pmax().sli(); ++i)
+ {
+ if (array_[i])
+ {
+ array_[i] += vb_.pmin().row();
+ delete[] array_[i];
+ array_[i] = 0;
+ }
+ }
+ if (array_)
+ {
+ array_ += vb_.pmin().sli();
+ delete[] array_;
+ array_ = 0;
+ }
+ }
+
+}
+
+#endif // ! MLN_IMAGE3D_B_DATA_HH
Index: trunk/milena/mln/core/image1d_b.hh
===================================================================
--- trunk/milena/mln/core/image1d_b.hh (revision 1133)
+++ trunk/milena/mln/core/image1d_b.hh (revision 1134)
@@ -42,6 +42,9 @@
# include <mln/core/line_piter.hh>
+# include <mln/core/internal/tracked_ptr.hh>
+# include <mln/core/image1d_b_data.hh>
+
// FIXME:
// # include <mln/core/pixter1d_b.hh>
@@ -190,16 +193,7 @@
private:
- T* buffer_;
- T* array_;
-
- box1d b_; // theoretical box
- unsigned bdr_;
- box1d vb_; // virtual box, i.e., box including the virtual border
-
- void update_vb_();
- void allocate_();
- void deallocate_();
+ tracked_ptr< image1d_b_data<T> > data_;
typedef internal::image_base_< box1d, image1d_b<T> > super;
};
@@ -212,16 +206,13 @@
template <typename T>
image1d_b<T>::image1d_b()
- : buffer_(0),
- array_ (0)
+ : data_(0)
{
- bdr_ = border::thickness; // default value in ctors.
}
template <typename T>
image1d_b<T>::image1d_b(int ninds, unsigned bdr)
- : buffer_(0),
- array_ (0)
+ : data_(0)
{
init_with(ninds, bdr);
}
@@ -231,15 +222,12 @@
image1d_b<T>::init_with(int ninds, unsigned bdr)
{
mln_precondition(! this->has_data());
- b_ = make::box1d(ninds);
- bdr_ = bdr;
- allocate_();
+ data_ = new image1d_b_data<T>(make::box1d(ninds), bdr);
}
template <typename T>
image1d_b<T>::image1d_b(const box1d& b, unsigned bdr)
- : buffer_(0),
- array_ (0)
+ : data_(0)
{
init_with(b, bdr);
}
@@ -249,21 +237,14 @@
image1d_b<T>::init_with(const box1d& b, unsigned bdr)
{
mln_precondition(! this->has_data());
- b_ = b;
- bdr_ = bdr;
- allocate_();
+ data_ = new image1d_b_data<T>(b, bdr);
}
template <typename T>
image1d_b<T>::image1d_b(const image1d_b<T>& rhs)
: super(rhs),
- b_(rhs.domain()),
- bdr_(rhs.border())
+ data_(rhs.data_)
{
- allocate_();
- std::memcpy(this->buffer_,
- rhs.buffer_,
- ncells() * sizeof(T));
}
// assignment
@@ -275,14 +256,8 @@
mln_precondition(rhs.has_data());
if (& rhs == this)
return *this;
- if (this->has_data())
- this->deallocate_();
- this->b_ = rhs.domain();
- this->bdr_ = rhs.border();
- allocate_();
- std::memcpy(this->buffer_,
- rhs.buffer_,
- ncells() * sizeof(T));
+
+ this->data_ = rhs.data_;
return *this;
}
@@ -292,7 +267,7 @@
bool
image1d_b<T>::has_data() const
{
- return buffer_ != 0 && array_ != 0;;
+ return data_ != 0;
}
template <typename T>
@@ -307,7 +282,7 @@
image1d_b<T>::domain() const
{
mln_precondition(this->has_data());
- return b_;
+ return data_->b_;
}
template <typename T>
@@ -315,7 +290,7 @@
image1d_b<T>::border() const
{
mln_precondition(this->has_data());
- return bdr_;
+ return data_->bdr_;
}
template <typename T>
@@ -323,19 +298,19 @@
image1d_b<T>::ncells() const
{
mln_precondition(this->has_data());
- return vb_.npoints();
+ return data_->vb_.npoints();
}
template <typename T>
bool
image1d_b<T>::owns_(const point1d& p) const
{
- if (! vb_.has(p))
+ if (! data_->vb_.has(p))
{
std::cout << " p = " << p << std::endl;
}
mln_precondition(this->has_data());
- return vb_.has(p);
+ return data_->vb_.has(p);
}
template <typename T>
@@ -343,7 +318,7 @@
image1d_b<T>::operator()(const point1d& p) const
{
mln_precondition(this->owns_(p));
- return array_[p.ind()];
+ return data_->array_[p.ind()];
}
template <typename T>
@@ -351,7 +326,7 @@
image1d_b<T>::operator()(const point1d& p)
{
mln_precondition(this->owns_(p));
- return array_[p.ind()];
+ return data_->array_[p.ind()];
}
template <typename T>
@@ -359,7 +334,7 @@
image1d_b<T>::operator[](unsigned o) const
{
mln_precondition(o < ncells());
- return *(buffer_ + o);
+ return *(data_->buffer_ + o);
}
template <typename T>
@@ -367,7 +342,7 @@
image1d_b<T>::operator[](unsigned o)
{
mln_precondition(o < ncells());
- return *(buffer_ + o);
+ return *(data_->buffer_ + o);
}
template <typename T>
@@ -375,7 +350,7 @@
image1d_b<T>::at(int ind) const
{
mln_precondition(this->owns_(make::point1d(ind)));
- return array_[ind];
+ return data_->array_[ind];
}
template <typename T>
@@ -383,13 +358,12 @@
image1d_b<T>::at(int ind)
{
mln_precondition(this->owns_(make::point1d(ind)));
- return array_[ind];
+ return data_->array_[ind];
}
template <typename T>
image1d_b<T>::~image1d_b()
{
- deallocate_();
}
template <typename T>
@@ -397,7 +371,7 @@
image1d_b<T>::buffer() const
{
mln_precondition(this->has_data());
- return buffer_;
+ return data_->buffer_;
}
template <typename T>
@@ -405,7 +379,7 @@
image1d_b<T>::buffer()
{
mln_precondition(this->has_data());
- return buffer_;
+ return data_->buffer_;
}
template <typename T>
@@ -422,45 +396,11 @@
image1d_b<T>::point_at_offset(unsigned o) const
{
mln_precondition(o < ncells());
- point1d p = make::point1d(o + vb_.min_ind());
- mln_postcondition(& this->operator()(p) == this->buffer_ + o);
+ point1d p = make::point1d(o + data_->vb_.min_ind());
+ mln_postcondition(& this->operator()(p) == this->data_->buffer_ + o);
return p;
}
-
- // private
-
- template <typename T>
- void
- image1d_b<T>::update_vb_()
- {
- vb_.pmin() = b_.pmin() - dpoint1d(all(bdr_));
- vb_.pmax() = b_.pmax() + dpoint1d(all(bdr_));
- }
-
- template <typename T>
- void
- image1d_b<T>::allocate_()
- {
- update_vb_();
- unsigned
- ni = vb_.len(0);
- buffer_ = new T[ni];
- array_ = buffer_ - vb_.pmin().ind();
- mln_postcondition(vb_.len(0) == b_.len(0) + 2 * bdr_);
- }
-
- template <typename T>
- void
- image1d_b<T>::deallocate_()
- {
- if (buffer_)
- {
- delete[] buffer_;
- buffer_ = 0;
- }
- }
-
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
Index: trunk/milena/mln/core/image3d_b.hh
===================================================================
--- trunk/milena/mln/core/image3d_b.hh (revision 1133)
+++ trunk/milena/mln/core/image3d_b.hh (revision 1134)
@@ -42,6 +42,9 @@
# include <mln/core/line_piter.hh>
+# include <mln/core/internal/tracked_ptr.hh>
+# include <mln/core/image3d_b_data.hh>
+
// FIXME:
// # include <mln/core/pixter3d_b.hh>
@@ -190,16 +193,7 @@
private:
- T* buffer_;
- T*** array_;
-
- box3d b_; // theoretical box
- unsigned bdr_;
- box3d vb_; // virtual box, i.e., box including the virtual border
-
- void update_vb_();
- void allocate_();
- void deallocate_();
+ tracked_ptr< image3d_b_data<T> > data_;
typedef internal::image_base_< box3d, image3d_b<T> > super;
};
@@ -212,16 +206,14 @@
template <typename T>
image3d_b<T>::image3d_b()
- : buffer_(0),
- array_ (0)
+ : data_(0)
{
- bdr_ = border::thickness; // default value in ctors.
+ data_->bdr_ = border::thickness; // default value in ctors.
}
template <typename T>
image3d_b<T>::image3d_b(int nslis, int nrows, int ncols, unsigned bdr)
- : buffer_(0),
- array_ (0)
+ : data_(0)
{
init_with(nslis, nrows, ncols, bdr);
}
@@ -231,15 +223,12 @@
image3d_b<T>::init_with(int nslis, int nrows, int ncols, unsigned bdr)
{
mln_precondition(! this->has_data());
- b_ = make::box3d(nslis, nrows, ncols);
- bdr_ = bdr;
- allocate_();
+ data_ = new image3d_b_data<T>(make::box3d(nslis, nrows, ncols), bdr);
}
template <typename T>
image3d_b<T>::image3d_b(const box3d& b, unsigned bdr)
- : buffer_(0),
- array_ (0)
+ : data_(0)
{
init_with(b, bdr);
}
@@ -249,21 +238,14 @@
image3d_b<T>::init_with(const box3d& b, unsigned bdr)
{
mln_precondition(! this->has_data());
- b_ = b;
- bdr_ = bdr;
- allocate_();
+ data_ = new image3d_b_data<T>(b, bdr);
}
template <typename T>
image3d_b<T>::image3d_b(const image3d_b<T>& rhs)
: super(rhs),
- b_(rhs.domain()),
- bdr_(rhs.border())
+ data_(rhs.data_)
{
- allocate_();
- std::memcpy(this->buffer_,
- rhs.buffer_,
- ncells() * sizeof(T));
}
// assignment
@@ -275,14 +257,8 @@
mln_precondition(rhs.has_data());
if (& rhs == this)
return *this;
- if (this->has_data())
- this->deallocate_();
- this->b_ = rhs.domain();
- this->bdr_ = rhs.border();
- allocate_();
- std::memcpy(this->buffer_,
- rhs.buffer_,
- ncells() * sizeof(T));
+
+ this->data_ = rhs.data_;
return *this;
}
@@ -292,7 +268,7 @@
bool
image3d_b<T>::has_data() const
{
- return buffer_ != 0 && array_ != 0;;
+ return data_ != 0;
}
template <typename T>
@@ -307,7 +283,7 @@
image3d_b<T>::domain() const
{
mln_precondition(this->has_data());
- return b_;
+ return data_->b_;
}
template <typename T>
@@ -315,7 +291,7 @@
image3d_b<T>::border() const
{
mln_precondition(this->has_data());
- return bdr_;
+ return data_->bdr_;
}
template <typename T>
@@ -323,7 +299,7 @@
image3d_b<T>::ncells() const
{
mln_precondition(this->has_data());
- return vb_.npoints();
+ return data_->vb_.npoints();
}
template <typename T>
@@ -331,7 +307,7 @@
image3d_b<T>::owns_(const point3d& p) const
{
mln_precondition(this->has_data());
- return vb_.has(p);
+ return data_->vb_.has(p);
}
template <typename T>
@@ -339,7 +315,7 @@
image3d_b<T>::operator()(const point3d& p) const
{
mln_precondition(this->owns_(p));
- return array_[p.sli()][p.row()][p.col()];
+ return data_->array_[p.sli()][p.row()][p.col()];
}
template <typename T>
@@ -347,7 +323,7 @@
image3d_b<T>::operator()(const point3d& p)
{
mln_precondition(this->owns_(p));
- return array_[p.sli()][p.row()][p.col()];
+ return data_->array_[p.sli()][p.row()][p.col()];
}
template <typename T>
@@ -355,7 +331,7 @@
image3d_b<T>::operator[](unsigned o) const
{
mln_precondition(o < ncells());
- return *(buffer_ + o);
+ return *(data_->buffer_ + o);
}
template <typename T>
@@ -363,7 +339,7 @@
image3d_b<T>::operator[](unsigned o)
{
mln_precondition(o < ncells());
- return *(buffer_ + o);
+ return *(data_->buffer_ + o);
}
template <typename T>
@@ -371,7 +347,7 @@
image3d_b<T>::at(int sli, int row, int col) const
{
mln_precondition(this->owns_(make::point3d(sli, row, col)));
- return array_[sli][row][col];
+ return data_->array_[sli][row][col];
}
template <typename T>
@@ -379,13 +355,12 @@
image3d_b<T>::at(int sli, int row, int col)
{
mln_precondition(this->owns_(make::point3d(sli, row, col)));
- return array_[sli][row][col];
+ return data_->array_[sli][row][col];
}
template <typename T>
image3d_b<T>::~image3d_b()
{
- deallocate_();
}
template <typename T>
@@ -393,7 +368,7 @@
image3d_b<T>::buffer() const
{
mln_precondition(this->has_data());
- return buffer_;
+ return data_->buffer_;
}
template <typename T>
@@ -401,7 +376,7 @@
image3d_b<T>::buffer()
{
mln_precondition(this->has_data());
- return buffer_;
+ return data_->buffer_;
}
template <typename T>
@@ -418,76 +393,14 @@
image3d_b<T>::point_at_offset(unsigned o) const
{
mln_precondition(o < ncells());
- point3d p = make::point3d(o / (vb_.len(1) * vb_.len(2)) + vb_.min_sli(),
- (o % (vb_.len(1) * vb_.len(2))) / vb_.len(2) + vb_.min_row(),
- o % vb_.len(2) + vb_.min_col());
- mln_postcondition(& this->operator()(p) == this->buffer_ + o);
+ point3d p = make::point3d(o / (data_->vb_.len(1) * data_->vb_.len(2)) +
data_->vb_.min_sli(),
+ (o % (data_->vb_.len(1) * data_->vb_.len(2))) / data_->vb_.len(2) +
data_->vb_.min_row(),
+ o % data_->vb_.len(2) + data_->vb_.min_col());
+ mln_postcondition(& this->operator()(p) == this->data_->buffer_ + o);
return p;
}
- // private
-
- template <typename T>
- void
- image3d_b<T>::update_vb_()
- {
- vb_.pmin() = b_.pmin() - dpoint3d(all(bdr_));
- vb_.pmax() = b_.pmax() + dpoint3d(all(bdr_));
- }
-
- template <typename T>
- void
- image3d_b<T>::allocate_()
- {
- update_vb_();
- unsigned
- ns = vb_.len(0),
- nr = vb_.len(1),
- nc = vb_.len(2);
- buffer_ = new T[nr * nc * ns];
- array_ = new T**[ns];
- T* buf = buffer_ - vb_.pmin().col();
- for (unsigned i = 0; i < ns; ++i)
- {
- T** tmp = new T*[nr];
- array_[i] = tmp;
- for (unsigned j = 0; j < nr; ++j)
- {
- array_[i][j] = buf;
- buf += nc;
- }
- array_[i] -= vb_.pmin().row();
- }
- array_ -= vb_.pmin().sli();
- mln_postcondition(vb_.len(0) == b_.len(0) + 2 * bdr_);
- }
-
- template <typename T>
- void
- image3d_b<T>::deallocate_()
- {
- if (buffer_)
- {
- delete[] buffer_;
- buffer_ = 0;
- }
- for (typename point::coord i = vb_.pmin().sli(); i <= vb_.pmax().sli(); ++i)
- {
- if (array_[i])
- {
- array_[i] += vb_.pmin().row();
- delete[] array_[i];
- array_[i] = 0;
- }
- }
- if (array_)
- {
- array_ += vb_.pmin().sli();
- delete[] array_;
- array_ = 0;
- }
- }
# endif // ! MLN_INCLUDE_ONLY