Commit 4d216333 authored by Thierry Geraud's avatar Thierry Geraud
Browse files

Add a tutorial example of use of p_array.

	* tests/core/p_array.cc: Clean up (no more stdout).
	* doc/tutorial/examples: New directory.
	* doc/tutorial/examples/p_array.cc: New.
	* mln/core/internal/image_base.hh (mesh, bbox): Remove.
	(point, npoints): Rename as...
	(site, nsites): ...these.
	* mln/core/concept/image.hh: Likewise.
	* mln/core/p_array_piter.hh (index): New method.
	(index_of_in): New overload.
	* mln/core/concept/proxy.hh
	(get_adr): Make it work when no match is found.
	(operator Subject): Change into a more reliable version.
	* mln/core/p_array.hh (index_of_in): New procedures.
	(insert, change): New methods.
	(print): Remove.
	* mln/core/image2d.hh (bbox): New.
	(npoints): Rename as...
	(nsites): ...this.


git-svn-id: https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008@1981 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 1a052ebb
2008-05-29 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Add a tutorial example of use of p_array.
* tests/core/p_array.cc: Clean up (no more stdout).
* doc/tutorial/examples: New directory.
* doc/tutorial/examples/p_array.cc: New.
* mln/core/internal/image_base.hh (mesh, bbox): Remove.
(point, npoints): Rename as...
(site, nsites): ...these.
* mln/core/concept/image.hh: Likewise.
* mln/core/p_array_piter.hh (index): New method.
(index_of_in): New overload.
* mln/core/concept/proxy.hh
(get_adr): Make it work when no match is found.
(operator Subject): Change into a more reliable version.
* mln/core/p_array.hh (index_of_in): New procedures.
(insert, change): New methods.
(print): Remove.
* mln/core/image2d.hh (bbox): New.
(npoints): Rename as...
(nsites): ...this.
2008-05-26 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Handle fwd iterations on p_array.
......
# include <mln/core/image2d.hh>
# include <mln/core/p_array.hh>
# include <mln/debug/println.hh>
# include <mln/level/fill.hh>
# include <mln/value/int_u8.hh>
template <typename A>
void picture(const A& arr)
{
using namespace mln;
image2d<char> ima(5, 5);
level::fill(ima, '-');
unsigned i = 0;
mln_piter(A) p(arr);
for_all(p)
ima(p) = '0' + i++;
debug::println(ima);
}
int main()
{
using namespace mln;
typedef p_array<point2d> Arr1;
Arr1 arr1;
image2d<value::int_u8> ima(5, 5);
{
point2d p(1,1);
dpoint2d dp[] = { right, right, down, down, left, left, up };
arr1.append(p);
for (unsigned i = 0; i < 7; ++i)
p += dp[i], arr1.append(p);
std::cout << "arr1 = " << arr1 << std::endl;
picture(arr1);
}
{
typedef p_array< mln_psite_(Arr1) > Arr2;
Arr2 arr2;
mln_piter_(Arr1) p(arr1);
for_all(p)
if (p.row() % 2 && p.col() % 2)
arr2.append(p);
std::cout << "arr2 = " << arr2 << std::endl;
picture(arr2);
{
mln_piter_(Arr2) p(arr2);
for_all(p)
std::cout << "point " << p << ": #"
<< index_of_in(p, arr2) << " in arr2, #"
<< index_of_in(p, arr1) << " in arr1" << std::endl;
}
}
}
......@@ -106,8 +106,7 @@ namespace mln
typedef bkd_piter;
bool has(const psite& p) const;
const box_<point>& bbox() const;
std::size_t npoints() const;
std::size_t nsites() const;
bool has_data() const;
*/
......@@ -137,14 +136,9 @@ namespace mln
bool (E::*m1)(const psite& p) const = & E::has;
m1 = 0;
const box_<point>& (E::*m2)() const = & E::bbox;
m2 = 0;
// to be provided in concrete image classes:
typedef mln_mesh(E) mesh;
metal::is_a<mesh, Mesh>::check(); // FIXME: Add other checks.
typedef mln_value(E) value;
typedef mln_rvalue(E) rvalue;
typedef mln_lvalue(E) lvalue;
......
......@@ -150,12 +150,15 @@ namespace mln
// Case 3: Fail to found!
template <typename T, typename O>
void get_adr(const T *& ptr, const Object<O>& obj);
void get_adr(const T *& ptr, const Object<O>& obj)
{
ptr = 0;
}
template <typename T, typename O>
void get_adr( T *& ptr, Object<O>& obj)
{
return 0;
ptr = 0;
}
......@@ -183,9 +186,13 @@ namespace mln
{
operator Subject() const
{
const Subject* adr;
get_adr(adr, mln::internal::force_exact<const E>(*this));
return *adr;
return mln::internal::force_exact<const E>(*this).unproxy();
// The code above seems more effective than the one below:
//
// const Subject* adr;
// get_adr(adr, mln::internal::force_exact<const E>(*this));
// mln_postcondition(adr != 0);
// return *adr;
}
};
......
......@@ -168,6 +168,9 @@ namespace mln
/// Give the definition domain.
const box2d& domain() const;
/// Give the bounding box domain.
const box2d& bbox() const;
/// Give the border thickness.
unsigned border() const;
......@@ -389,6 +392,15 @@ namespace mln
return this->data_->b_;
}
template <typename T>
inline
const box2d&
image2d<T>::bbox() const
{
mln_precondition(this->has_data());
return this->data_->b_;
}
template <typename T>
inline
unsigned
......@@ -404,7 +416,7 @@ namespace mln
image2d<T>::ncells() const
{
mln_precondition(this->has_data());
return this->data_->vb_.npoints();
return this->data_->vb_.nsites();
}
template <typename T>
......
......@@ -100,27 +100,23 @@ namespace mln
/// Site_Set associated type.
typedef S pset;
/// Mesh associated type.
typedef mln_mesh(S) mesh;
/// Point_Site associated type.
typedef mln_psite(S) psite;
/// Point associated type.
typedef mln_point(S) point;
typedef mln_site(S) site;
/// Dpoint associated type.
typedef mln_dpoint(point) dpoint;
typedef mln_dpoint(site) dpoint;
/// Coordinate associated type.
typedef mln_coord(point) coord;
typedef mln_coord(site) coord;
/// Forward Point_Iterator associated type.
/// Forward Site_Iterator associated type.
typedef mln_fwd_piter(S) fwd_piter;
/// Backward Point_Iterator associated type.
/// Backward Site_Iterator associated type.
typedef mln_bkd_piter(S) bkd_piter;
......@@ -131,10 +127,10 @@ namespace mln
bool owns_(const psite& p) const;
/// Give a bounding box of the image domain.
const box_<point>& bbox() const;
const box_<site>& bbox() const;
/// Give the number of points of the image domain.
std::size_t npoints() const;
/// Give the number of sites of the image domain.
std::size_t nsites() const;
/// Test if this image has been initialized; default impl.
bool has_data() const;
......@@ -219,22 +215,13 @@ namespace mln
return exact(this)->has(p);
}
template <typename S, typename E>
inline
const box_<mln_point(S)>&
image_base_<S,E>::bbox() const
{
mln_precondition(exact(this)->has_data());
return exact(this)->domain().bbox();
}
template <typename S, typename E>
inline
std::size_t
image_base_<S,E>::npoints() const
image_base_<S,E>::nsites() const
{
mln_precondition(exact(this)->has_data());
return exact(this)->domain().npoints();
return exact(this)->domain().nsites();
}
template <typename S, typename E>
......
......@@ -85,8 +85,6 @@ namespace mln
void change_target(const p_array<P>& arr);
void print() const;
private:
const p_array<P>* arr_;
......@@ -94,6 +92,11 @@ namespace mln
};
template <typename P, typename A>
int index_of_in(const P&, const A&);
template <typename P, typename A>
int index_of_in(const p_array_psite<P>& p, const A& arr);
namespace trait
......@@ -149,6 +152,9 @@ namespace mln
// FIXME: Add an overload "has(index)".
/// Change site \p p into \p new_p.
void change(const psite& p, const P& new_p);
/// Give the number of sites.
std::size_t nsites() const;
......@@ -158,6 +164,9 @@ namespace mln
/// Append an array \p other of points.
p_array<P>& append(const p_array<P>& other);
/// Insert a point \p p (equivalent as 'append').
void insert(const P& p);
/// Clear this set.
void clear();
......@@ -233,6 +242,14 @@ namespace mln
return *this;
}
template <typename P>
inline
void
p_array<P>::insert(const P& p)
{
vect_.push_back(p);
}
template <typename P>
inline
p_array<P>&
......@@ -277,6 +294,14 @@ namespace mln
return vect_[i];
}
template <typename P>
inline
void
p_array<P>::change(const psite& p, const P& new_p)
{
mln_precondition(has(p));
vect_[p.index()] = new_p;
}
// p_array_psite<P>
......@@ -338,14 +363,6 @@ namespace mln
arr_ = & arr;
}
template <typename P>
inline
void
p_array_psite<P>::print() const
{
std::cout << i_ << "-th site of " << arr_ << " => site " << to_site() << std::endl;
}
template <typename P>
inline
const P&
......@@ -355,6 +372,24 @@ namespace mln
return (*arr_)[i_];
}
// Procedures
template <typename P, typename A>
int index_of_in(const P&, const A&)
{
return -1;
}
template <typename P, typename A>
int index_of_in(const p_array_psite<P>& p, const A& arr)
{
if ((void*)(p.target()) == (void*)(&arr))
return p.index();
else
return index_of_in(p.unproxy(), arr);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
......
......@@ -75,15 +75,25 @@ namespace mln
const p_array_psite<P>& unproxy() const;
// As a Site_Proxy:
typedef typename super::site site;
const site& to_site() const;
/// Return the current index.
int index() const;
protected:
p_array_psite<P> p_;
};
template <typename P, typename A>
int index_of_in(const p_array_fwd_piter_<P>& p, const A& arr)
{
return index_of_in(p.unproxy(), arr);
}
// /// \brief Backward iterator on points of a p_array<P>.
// template <typename P>
......@@ -222,6 +232,15 @@ namespace mln
return p_.to_site();
}
template <typename P>
inline
int
p_array_fwd_piter_<P>::index() const
{
mln_precondition(p_.target() != 0);
return p_.index();
}
/*------------------------.
| p_array_bkd_piter_<P>. |
`------------------------*/
......
......@@ -30,44 +30,26 @@
* \brief Tests on mln::p_array.
*/
#include <iterator>
#include <mln/core/point2d.hh>
#include <mln/core/p_array.hh>
int main()
{
using namespace mln;
typedef p_array<point2d> Arr;
Arr pa;
pa
.append(make::point2d(6, 9))
.append(make::point2d(4, 2))
.append(make::point2d(4, 2))
.append(make::point2d(5, 1));
mln_assertion(pa.nsites() == 4);
mlc_equal( mln_psite_(Arr)::site, point2d )::check();
mln_psite_(Arr) p(pa, 0);
std::cout << p.to_site() << ' '
<< p.row() << ' '
<< point2d(p) << ' '
<< std::endl;
point2d x(5, 1);
Arr arr;
arr.append(x).append(x);
mln_assertion(arr.nsites() == 2);
std::copy(pa.vect().begin(), pa.vect().end(),
std::ostream_iterator<point2d>(std::cout, " "));
std::cout << std::endl;
mlc_equal( mln_psite_(Arr)::site, point2d )::check();
{
mln_piter_(Arr) p(pa);
for_all(p)
std::cout << p << ' ';
std::cout << std::endl;
}
mln_psite_(Arr) p(arr, 0);
mln_assertion(p.to_site() == x);
mln_assertion(p.row() == 5);
mln_assertion(point2d(p) == x);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment