diff --git a/dlib/array2d.h b/dlib/array2d.h index 55489f1e8..b4512dbca 100644 --- a/dlib/array2d.h +++ b/dlib/array2d.h @@ -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 - kernel_1a; - typedef array2d_kernel_c - kernel_1a_c; - - - - }; -} - #endif // DLIB_ARRAY2d_ diff --git a/dlib/array2d/array2d_kernel_1.h b/dlib/array2d/array2d_kernel_1.h index 6b7a665d4..bd6182a69 100644 --- a/dlib/array2d/array2d_kernel_1.h +++ b/dlib/array2d/array2d_kernel_1.h @@ -15,7 +15,7 @@ namespace dlib typename T, typename mem_manager = default_memory_manager > - class array2d_kernel_1 : public enumerable + class array2d : public enumerable { /*! @@ -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& a, - array2d_kernel_1& b + array2d& a, + array2d& b ) { a.swap(b); } @@ -255,7 +327,7 @@ namespace dlib typename mem_manager > void serialize ( - const array2d_kernel_1& item, + const array2d& 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& item, + array2d& 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:: + void array2d:: 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; diff --git a/dlib/array2d/array2d_kernel_c.h b/dlib/array2d/array2d_kernel_c.h deleted file mode 100644 index 599f3c71b..000000000 --- a/dlib/array2d/array2d_kernel_c.h +++ /dev/null @@ -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 - { - - /*! - 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& a, - array2d_kernel_c& b - ) { a.swap(b); } - - - template < - typename array2d_base - > - void serialize ( - const array2d_kernel_c& 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& 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:: - 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:: - 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:: - 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::row& array2d_kernel_c:: - 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::row& array2d_kernel_c:: - 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::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::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_ - diff --git a/docs/docs/containers.xml b/docs/docs/containers.xml index 87db4aeb4..61eddeb75 100644 --- a/docs/docs/containers.xml +++ b/docs/docs/containers.xml @@ -815,25 +815,6 @@ image_ex.cpp.html - - - array2d_kernel_1 - dlib/array2d/array2d_kernel_1.h - - This is implemented in the obvious way. See the source for details. - It uses the memory_manager for all memory allocations. - - - - - kernel_1a - is a typedef for array2d_kernel_1 - - - - - - diff --git a/examples/image_ex.cpp b/examples/image_ex.cpp index dff359bc6..932ca3755 100644 --- a/examples/image_ex.cpp +++ b/examples/image_ex.cpp @@ -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::kernel_1a img; + array2d 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::kernel_1a horz_gradient, vert_gradient; - array2d::kernel_1a edge_image; + array2d horz_gradient, vert_gradient; + array2d 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 diff --git a/examples/surf_ex.cpp b/examples/surf_ex.cpp index f79bf71d8..be88ecef7 100644 --- a/examples/surf_ex.cpp +++ b/examples/surf_ex.cpp @@ -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::kernel_1a img; + array2d 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