Commit eeb74355 authored by Thierry Geraud's avatar Thierry Geraud
Browse files

2007-01-30 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr>

	Add mini-std.
	
	* samples/mini-std: New.
	* samples/mini-oln/design.hh: New.



git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@725 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 39b80536
2007-01-30 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr>
Add mini-std.
* samples/mini-std: New.
* samples/mini-oln/design.hh: New.
2007-01-26 Roland Levillain <roland@lrde.epita.fr>
Polish stc/scoop2.hh.
......
// Point
Point : class =
{
}
point2d : class < Point =
{
row : int;
col : int;
}
point3d : class < Point =
{
row : int;
col : int;
sli : int;
}
// Iterator
Iterator : class =
{
point_t : typedef < Point;
start : () -> void;
next : () -> void;
is_valid : const () -> bool;
auto : -> point_t;
}
// Image
Image : class =
{
{
point_t < Point;
iter_t < Iterator;
value_t;
}
: typedef;
"()" : const (p : point_t const&) -> value_t;
has : const (p : point_t const&) -> bool;
}
// Image = Image_2D | Image_3D
Image_2D : class < Image =
{
"()" : const (row : int, col : int) -> value_t;
nrows : const () -> int;
ncols : const () -> int;
}
Image_3D : class < Image =
{
"()" : const (row : int, col : int, sli : int) -> value_t;
nrows : const () -> int;
ncols : const () -> int;
nslis : const () -> int;
}
// Image = Image_with_Nbh | not
Image_with_Nbh : class < Image
{
{
niter_t < Iterator;
nbh_t < Neighborhood;
}
: typedef;
nbh : const () -> nbh_t;
}
// plugs !
image_dim_plug : [I : type] -> type = {
switch (I.point_t) {
case point2d: return Image_2D;
case point3d: return Image_3D;
}
}
image_nbh_plug : [I : type] -> type = {
if (I.nbh_t)
return Image_with_Nbh;
else
return Image;
}
// image_entry
image_base : class < image_dim_plug[@],
image_nbh_plug[@] =
{
{
point_t;
iter_t;
value_t;
}
: typedef;
"()" : const (p : point_t const&) -> value_t;
has : const (p : point_t const&) -> bool;
}
// iterator2d
iterator2d : class < Iterator =
{
point_t : typedef := point2d;
make : (ima : Image_2D) = {
nrows := ima.nrows;
ncols := ima.ncols;
}
start : () -> void = {
p.row := 0;
p.col := 0;
}
next : () -> void = {
p.col := p.col + 1;
if (p.col = ncols) {
p.col := 0;
p.row := p.row + 1;
}
}
is_valid : const () -> bool = {
return p.row < p.nrows;
}
auto : -> point_t =
{
return p;
}
protected {
nrows : int;
ncols : int;
p : point_t;
}
}
// image2d[T]
image2d : [T : class] -> class < image_base =
{
{
point_t := point2d;
iter_t := iterator2d;
value_t := T;
}
: typedef;
make : (nrows : int, ncols : int) =
{
data.make(nrows * ncols);
}
"()" : const (p : point_t const&) -> value_t =
{
precondition { has(p) = true }
return data.element(p.row * ncols + p.col);
}
"()" : const (row : int, col : int) -> value_t =
{
p : point2d;
p.make(row, col);
return @[p];
}
has : const (p : point_t const&) -> bool =
{
precondition { data != 0 }
return p.row >= 0 and p.row < nrows and p.col >= 0 and p.col < ncols;
}
data : vector[T];
nrows : int;
ncols : int;
}
// image_morpher
image_morpher : class < image_base =
{
delegatee_t : typedef;
}
// fun
fun_ : [I : class < Image, F : type] where (F.arg_t = I.value_t) -> class =
{
{
delegatee_t := I;
value_t := F.res_t;
}
: typedef;
ima : I;
f : F;
"()" : const (p : point_t const&) -> value_t =
{
precondition { has(p) = true }
return f(ima(p));
}
}
fun1 : class =
{
{
arg_t;
res_t;
}
: typedef;
"()" : (ima : I < Image) -> fun_[I,@] =
{
fun_[I,@] tmp; // '@' is exact type
tmp.make(ima, @); // here '@' is *this
return tmp;
}
}
pow : class < fun1 =
{
n : unsigned;
{
arg_t := double;
res_t := double;
}
: typedef;
"()" : (x : double) -> double =
{
return std::pow(x, n);
}
}
// stack
vec : [n : unsigned, T : type] -> class =
{
data : T[n];
"[]" : (i : unsigned) -> T =
{
precondition { i < n }
return data[i];
}
}
stack_ : [n : unsigned, I : class < Image] -> class =
{
{
delegatee_t := I;
value_t := vec[n, I.value_t];
}
: typedef;
ima : vec[n,I];
"()" : const (p : point_t const&) -> value_t =
{
precondition { has(p) = true }
value_t tmp;
for (unsigned i := 0; i < n; ++i)
tmp[i] := ima[i](p);
return tmp;
}
}
stack : [I : class < Image] (ima_0 : I const&, ima_1 : I const&) -> stack_[2,I] =
{
stack_[2,I] tmp;
tmp.ima[0] := ima_0;
tmp.ima[1] := ima_1;
return tmp;
}
// "image + neighborhood"
plus : [I : class < Image, N : type] where N < Neighborhood
-> class < image_morpher =
{
{
delegatee_t := I;
}
: typedef;
ima : I;
nbh : N;
}
// operator plus
"+" : [I : class < Image, N : class < Neighborhood] (ima : I, nbh : N) -> plus[I, N] =
{
plus[I, N] tmp;
return tmp.make(ima, nbh);
}
// ----------------------------------------------------------------------
// "image + neighborhood"
plus : [I : class < Image, N : type] where N < Neighborhood
-> class < image_morpher =
{
{
delegatee_t := I;
}
: typedef;
ima : I;
nbh : N;
}
// operator plus
"+" : [I : class < Image,
T : type] (lhs : I, rhs : T) -> plus[I, T] =
{
plus[I, T] ima;
return ima.make(lhs, rhs);
}
// unsub
unsubset : (ima : Image_with_Subset) = { FIXME }
// predicate
predicate : [I : class < Image] -> type = {
return (I.point_t const &) -> I.value_t;
}
// sub[I,P]
sub : [I, P] where I < Iterator and P = predicate[I]
-> class < Iterator =
{
point_t : typedef := I.point_t;
iter : I;
pred : P;
p : point_t const&;
make : (ima : Image) {
iter.make(unsubset(ima));
pred := ima.pred();
p := iter.p;
}
invariant {
iter.is_valid() => pred(p);
}
start : () -> void = {
iter.start();
while (iter.is_valid() and not pred(p))
iter.next();
}
next : () -> void = {
iter.next();
while (iter.is_valid() and not pred(p))
iter.next();
}
is_valid : const () -> bool = {
return iter.is_valid();
}
auto : -> point_t =
{
return p;
}
}
// 'pipe' is "image | predicate -> sub-image"
pipe : [I : class < Image, P : type] where P = predicate[I]
-> class < image_morpher =
{
{
delegatee_t := I;
iter_t : sub[I.iter_t, P];
}
: typedef;
pred : P;
unsubset : () -> I =
{
return ref_ima;
}
has : const (p : point_t const&) -> bool =
{
return ref_ima.has(p) and pred(p);
}
}
// operator pipe
"|" : [I : class < Image,
T : type] (lhs : I, rhs : T) -> pipe[I, T] =
{
pipe[I, T] ima;
return ima.make(lhs, rhs);
}
-*- outline -*-
* compilation
cd cpp
g++ -I../../.. -I../../../../metalic -Wall -ansi -pedantic main.cc
* contents
** utility files
concepts.hh
equipment.hh
fwddecls.hh
identity.hh
impl.hh
** based on existing std tools
deque.hh
list.hh
stack.hh
vector.hh
algorithm.hh
** morphers
value_cast.hh
#ifndef MINI_STD_ALGORITHM_HH
# define MINI_STD_ALGORITHM_HH
# include <algorithm>
# include "concepts.hh"
namespace abc
{
/// \{
/// Facades.
template <typename C>
void sort(Container<C>& c);
template <typename C, typename I>
void copy(const Container<C>& c, I iter);
/// \}
/// \{
/// sort
namespace impl
{
template <typename C>
void sort(Random_Access_Container<C>& c)
{
std::sort(c.begin(), c.end());
}
} // impl
template <typename C>
void sort(Container<C>& c)
{
impl::sort(c.exact());
}
/// \}
/// \{
/// copy
template <typename C, typename I>
void copy(const Container<C>& c, I iter)
{
std::copy(c.begin(), c.end(), iter);
}
/// \}
} // end of namespace abc
#endif // ! MINI_STD_ALGORITHM_HH
#ifndef MINI_STD_CONCEPTS_HH
# define MINI_STD_CONCEPTS_HH
# include "equipment.hh"
namespace abc
{
// Container
template <typename E>
class Container : public virtual stc::any__simple<E>,
public automatic::impl<Container, E>
{
public:
// Typedefs.
abc_typename(value_type);
abc_typename(iterator);
abc_typename(const_iterator);
abc_typename(reference);
abc_typename(const_reference);
abc_typename(size_type);
// Methods.
iterator begin() { return this->exact().impl_begin(); }
const_iterator begin() const { return this->exact().impl_begin(); }
iterator end() { return this->exact().impl_end(); }
const_iterator end() const { return this->exact().impl_end(); }
size_type size() const { return this->exact().impl_size(); }
bool empty() const { return this->exact().impl_empty(); }
protected:
Container() {}
};
// Forward_Container
template <typename E>
class Forward_Container : public virtual Container<E>,
public automatic::impl<Forward_Container, E>
{
public:
typedef Forward_Container<E> self_type;
// Methods.
bool operator==(const self_type& rhs) const { return this->exact().impl_equal(rhs.exact()); }
bool operator< (const self_type& rhs) const { return this->exact().impl_less(rhs.exact()); }
// Concrete. // FIXME: Use the default impl mechanism so that the user can override.