Changed the array2d object so you don't have to say array2d<type>::kernel_1a

anymore to declare it.  Now you just say array2d<type>.
This commit is contained in:
Davis King 2011-05-28 10:26:29 -04:00
parent 35a6408f8e
commit 5e384614be
6 changed files with 106 additions and 457 deletions

View File

@ -5,39 +5,7 @@
#include "array2d/array2d_kernel_1.h"
#include "array2d/array2d_kernel_c.h"
#include "algs.h"
namespace dlib
{
template <
typename T,
typename mem_manager = default_memory_manager
>
class array2d
{
array2d() {}
public:
//----------- kernels ---------------
// kernel_1a
typedef array2d_kernel_1<T,mem_manager>
kernel_1a;
typedef array2d_kernel_c<kernel_1a>
kernel_1a_c;
};
}
#endif // DLIB_ARRAY2d_

View File

@ -15,7 +15,7 @@ namespace dlib
typename T,
typename mem_manager = default_memory_manager
>
class array2d_kernel_1 : public enumerable<T>
class array2d : public enumerable<T>
{
/*!
@ -59,6 +59,10 @@ namespace dlib
class row_helper;
public:
// These typedefs are here for backwards compatibility with older versions of dlib.
typedef array2d kernel_1a;
typedef array2d kernel_1a_c;
typedef T type;
typedef mem_manager mem_manager_type;
@ -74,7 +78,7 @@ namespace dlib
- (*this)[x] == data[x]
!*/
friend class array2d_kernel_1;
friend class array2d;
friend class row_helper;
public:
@ -83,11 +87,35 @@ namespace dlib
const T& operator[] (
long column
) const { return data[column]; }
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(column < nc() && column >= 0,
"\tconst T& array2d::operator[](long column) const"
<< "\n\tThe column index given must be less than the number of columns."
<< "\n\tthis: " << this
<< "\n\tcolumn: " << column
<< "\n\tnc(): " << nc()
);
return data[column];
}
T& operator[] (
long column
) { return data[column]; }
)
{
// make sure requires clause is not broken
DLIB_ASSERT(column < nc() && column >= 0,
"\tT& array2d::operator[](long column)"
<< "\n\tThe column index given must be less than the number of columns."
<< "\n\tthis: " << this
<< "\n\tcolumn: " << column
<< "\n\tnc(): " << nc()
);
return data[column];
}
private:
@ -103,7 +131,7 @@ namespace dlib
// -----------------------------------
array2d_kernel_1 (
array2d (
) :
nc_(0),
nr_(0),
@ -115,7 +143,7 @@ namespace dlib
{
}
virtual ~array2d_kernel_1 (
virtual ~array2d (
) { clear(); }
long nc (
@ -126,14 +154,38 @@ namespace dlib
row& operator[] (
long row
) { return rows[row]; }
)
{
// make sure requires clause is not broken
DLIB_ASSERT(row < nr() && row >= 0,
"\trow& array2d::operator[](long row)"
<< "\n\tThe row index given must be less than the number of rows."
<< "\n\tthis: " << this
<< "\n\trow: " << row
<< "\n\tnr(): " << nr()
);
return rows[row];
}
const row& operator[] (
long row
) const { return rows[row]; }
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(row < nr() && row >= 0,
"\tconst row& array2d::operator[](long row) const"
<< "\n\tThe row index given must be less than the number of rows."
<< "\n\tthis: " << this
<< "\n\trow: " << row
<< "\n\tnr(): " << nr()
);
return rows[row];
}
void swap (
array2d_kernel_1& item
array2d& item
)
{
exchange(data,item.data);
@ -179,10 +231,30 @@ namespace dlib
) const { return (cur != 0); }
const T& element (
) const { return *cur; }
) const
{
// make sure requires clause is not broken
DLIB_ASSERT(current_element_valid() == true,
"\tconst T& array2d::element()()"
<< "\n\tYou can only call element() when you are at a valid one."
<< "\n\tthis: " << this
);
return *cur;
}
T& element (
) { return *cur; }
)
{
// make sure requires clause is not broken
DLIB_ASSERT(current_element_valid() == true,
"\tT& array2d::element()()"
<< "\n\tYou can only call element() when you are at a valid one."
<< "\n\tthis: " << this
);
return *cur;
}
bool move_next (
) const
@ -233,8 +305,8 @@ namespace dlib
mutable bool at_start_;
// restricted functions
array2d_kernel_1(array2d_kernel_1&); // copy constructor
array2d_kernel_1& operator=(array2d_kernel_1&); // assignment operator
array2d(array2d&); // copy constructor
array2d& operator=(array2d&); // assignment operator
};
@ -245,8 +317,8 @@ namespace dlib
typename mem_manager
>
inline void swap (
array2d_kernel_1<T,mem_manager>& a,
array2d_kernel_1<T,mem_manager>& b
array2d<T,mem_manager>& a,
array2d<T,mem_manager>& b
) { a.swap(b); }
@ -255,7 +327,7 @@ namespace dlib
typename mem_manager
>
void serialize (
const array2d_kernel_1<T,mem_manager>& item,
const array2d<T,mem_manager>& item,
std::ostream& out
)
{
@ -271,7 +343,7 @@ namespace dlib
}
catch (serialization_error e)
{
throw serialization_error(e.info + "\n while serializing object of type array2d_kernel_1");
throw serialization_error(e.info + "\n while serializing object of type array2d");
}
}
@ -279,7 +351,7 @@ namespace dlib
typename T
>
void deserialize (
array2d_kernel_1<T>& item,
array2d<T>& item,
std::istream& in
)
{
@ -298,7 +370,7 @@ namespace dlib
catch (serialization_error e)
{
item.clear();
throw serialization_error(e.info + "\n while deserializing object of type array2d_kernel_1");
throw serialization_error(e.info + "\n while deserializing object of type array2d");
}
}
@ -312,12 +384,22 @@ namespace dlib
typename T,
typename mem_manager
>
void array2d_kernel_1<T,mem_manager>::
void array2d<T,mem_manager>::
set_size (
long nr__,
long nc__
)
{
// make sure requires clause is not broken
DLIB_ASSERT((nc__ > 0 && nr__ > 0) ||
(nc__ == 0 && nr__ == 0),
"\tvoid array2d::set_size(long nr__, long nc__)"
<< "\n\tYou have to give a non zero nc and nr or just make both zero."
<< "\n\tthis: " << this
<< "\n\tnc__: " << nc__
<< "\n\tnr__: " << nr__
);
// set the enumerator back at the start
at_start_ = true;
cur = 0;

View File

@ -1,382 +0,0 @@
// Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_ARRAY2D_KERNEl_C_
#define DLIB_ARRAY2D_KERNEl_C_
#include "array2d_kernel_abstract.h"
#include "../algs.h"
#include "../assert.h"
#include "../interfaces/enumerable.h"
namespace dlib
{
template <
typename array2d_base // is an implementation of array2d_kernel_abstract.h
>
class array2d_kernel_c : public enumerable<typename array2d_base::type>
{
/*!
CONVENTION
- if (obj.size() > 0) then
- rows == an array of size obj.nr() row objects and
each row object in the array has a valid pointer to its
associated row in obj.
- else
- rows == 0
!*/
typedef typename array2d_base::type T;
public:
typedef typename array2d_base::type type;
typedef typename array2d_base::mem_manager_type mem_manager_type;
// -----------------------------------
class row
{
friend class array2d_kernel_c;
public:
long nc (
) const { return data->nc(); }
const T& operator[] (
long column
) const;
T& operator[] (
long column
);
private:
typename array2d_base::row* data;
// restricted functions
row(){}
row(row&);
row& operator=(row&);
};
// -----------------------------------
array2d_kernel_c (
) :
rows(0)
{
}
virtual ~array2d_kernel_c (
) { clear(); }
long nc (
) const { return obj.nc(); }
long nr (
) const { return obj.nr(); }
row& operator[] (
long row
);
const row& operator[] (
long row
) const;
void swap (
array2d_kernel_c& item
)
{
exchange(obj,item.obj);
exchange(rows,item.rows);
}
void clear (
)
{
obj.clear();
if (rows != 0)
{
delete [] rows;
rows = 0;
}
}
void set_size (
long nr__,
long nc__
);
bool at_start (
) const { return obj.at_start();; }
void reset (
) const { obj.reset(); }
bool current_element_valid (
) const { return obj.current_element_valid(); }
const T& element (
) const;
T& element (
);
bool move_next (
) const { return obj.move_next(); }
unsigned long size (
) const { return obj.size(); }
private:
array2d_base obj;
row* rows;
};
template <
typename array2d_base
>
inline void swap (
array2d_kernel_c<array2d_base>& a,
array2d_kernel_c<array2d_base>& b
) { a.swap(b); }
template <
typename array2d_base
>
void serialize (
const array2d_kernel_c<array2d_base>& item,
std::ostream& out
)
{
try
{
serialize(item.nc(),out);
serialize(item.nr(),out);
item.reset();
while (item.move_next())
serialize(item.element(),out);
item.reset();
}
catch (serialization_error e)
{
throw serialization_error(e.info + "\n while serializing object of type array2d_kernel_c");
}
}
template <
typename array2d_base
>
void deserialize (
array2d_kernel_c<array2d_base>& item,
std::istream& in
)
{
try
{
long nc, nr;
deserialize(nc,in);
deserialize(nr,in);
item.set_size(nr,nc);
while (item.move_next())
deserialize(item.element(),in);
item.reset();
}
catch (serialization_error e)
{
item.clear();
throw serialization_error(e.info + "\n while deserializing object of type array2d_kernel_c");
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename array2d_base
>
const typename array2d_base::type& array2d_kernel_c<array2d_base>::
element (
) const
{
// make sure requires clause is not broken
DLIB_CASSERT(current_element_valid() == true,
"\tT& array2d::element()()"
<< "\n\tYou can only call element() when you are at a valid one."
<< "\n\tthis: " << this
);
return obj.element();
}
// ----------------------------------------------------------------------------------------
template <
typename array2d_base
>
typename array2d_base::type& array2d_kernel_c<array2d_base>::
element (
)
{
// make sure requires clause is not broken
DLIB_CASSERT(current_element_valid() == true,
"\tT& array2d::element()()"
<< "\n\tYou can only call element() when you are at a valid one."
<< "\n\tthis: " << this
);
return obj.element();
}
// ----------------------------------------------------------------------------------------
template <
typename array2d_base
>
void array2d_kernel_c<array2d_base>::
set_size (
long nr_,
long nc_
)
{
// make sure requires clause is not broken
DLIB_CASSERT((nc_ > 0 && nr_ > 0) ||
(nc_ == 0 && nr_ == 0),
"\tvoid array2d::set_size(long nr_, long nc_)"
<< "\n\tYou have to give a non zero nc and nr or just make both zero."
<< "\n\tthis: " << this
<< "\n\tnc_: " << nc_
<< "\n\tnr_: " << nr_
);
obj.set_size(nr_,nc_);
// set up the rows array
if (rows != 0)
delete [] rows;
try
{
rows = new row[obj.nr()];
}
catch (...)
{
rows = 0;
obj.clear();
throw;
}
for (long i = 0; i < obj.nr(); ++i)
{
rows[i].data = &obj[i];
}
}
// ----------------------------------------------------------------------------------------
template <
typename array2d_base
>
typename array2d_kernel_c<array2d_base>::row& array2d_kernel_c<array2d_base>::
operator[] (
long row
)
{
// make sure requires clause is not broken
DLIB_CASSERT(row < nr() && row >= 0,
"\trow& array2d::operator[](long row)"
<< "\n\tThe row index given must be less than the number of rows."
<< "\n\tthis: " << this
<< "\n\trow: " << row
<< "\n\tnr(): " << nr()
);
return rows[row];
}
// ----------------------------------------------------------------------------------------
template <
typename array2d_base
>
const typename array2d_kernel_c<array2d_base>::row& array2d_kernel_c<array2d_base>::
operator[] (
long row
) const
{
// make sure requires clause is not broken
DLIB_CASSERT(row < nr() && row >= 0,
"\tconst row& array2d::operator[](long row) const"
<< "\n\tThe row index given must be less than the number of rows."
<< "\n\tthis: " << this
<< "\n\trow: " << row
<< "\n\tnr(): " << nr()
);
return rows[row];
}
// ----------------------------------------------------------------------------------------
template <
typename array2d_base
>
const typename array2d_base::type& array2d_kernel_c<array2d_base>::row::
operator[] (
long column
) const
{
// make sure requires clause is not broken
DLIB_CASSERT(column < nc() && column >= 0,
"\tconst T& array2d::operator[](long column) const"
<< "\n\tThe column index given must be less than the number of columns."
<< "\n\tthis: " << this
<< "\n\tcolumn: " << column
<< "\n\tnc(): " << nc()
);
return (*data)[column];
}
// ----------------------------------------------------------------------------------------
template <
typename array2d_base
>
typename array2d_base::type& array2d_kernel_c<array2d_base>::row::
operator[] (
long column
)
{
// make sure requires clause is not broken
DLIB_CASSERT(column < nc() && column >= 0,
"\tT& array2d::operator[](long column)"
<< "\n\tThe column index given must be less than the number of columns."
<< "\n\tthis: " << this
<< "\n\tcolumn: " << column
<< "\n\tnc(): " << nc()
);
return (*data)[column];
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_ARRAY2D_KERNEl_C_

View File

@ -815,25 +815,6 @@
<example>image_ex.cpp.html</example>
</examples>
<implementations>
<implementation>
<name>array2d_kernel_1</name>
<file>dlib/array2d/array2d_kernel_1.h</file>
<description>
This is implemented in the obvious way. See the source for details.
It uses the <a href="other.html#memory_manager">memory_manager</a> for all memory allocations.
</description>
<typedefs>
<typedef>
<name>kernel_1a</name>
<description>is a typedef for array2d_kernel_1</description>
</typedef>
</typedefs>
</implementation>
</implementations>
</component>
<!-- ************************************************************************* -->

View File

@ -37,7 +37,7 @@ int main(int argc, char** argv)
// Here we declare an image object that can store rgb_pixels. Note that in
// dlib there is no explicit image object, just a 2D array and
// various pixel types.
array2d<rgb_pixel>::kernel_1a img;
array2d<rgb_pixel> img;
// Now load the image file into our image. If something is wrong then
// load_image() will throw an exception. Also, if you compiled with libpng
@ -48,8 +48,8 @@ int main(int argc, char** argv)
// Now lets use some image functions. This example is going to perform
// simple edge detection on the image. First lets find the horizontal and
// vertical gradient images.
array2d<short>::kernel_1a horz_gradient, vert_gradient;
array2d<unsigned char>::kernel_1a edge_image;
array2d<short> horz_gradient, vert_gradient;
array2d<unsigned char> edge_image;
sobel_edge_detector(img,horz_gradient, vert_gradient);
// now we do the non-maximum edge suppression step so that our edges are nice and thin

View File

@ -43,7 +43,7 @@ int main(int argc, char** argv)
// Here we declare an image object that can store rgb_pixels. Note that in
// dlib there is no explicit image object, just a 2D array and
// various pixel types.
array2d<rgb_pixel>::kernel_1a img;
array2d<rgb_pixel> img;
// Now load the image file into our image. If something is wrong then
// load_image() will throw an exception. Also, if you compiled with libpng