Commit ab38059d authored by Davis King's avatar Davis King

Simplified the implementation of the array2d object.

parent 8516ced3
...@@ -23,7 +23,6 @@ namespace dlib ...@@ -23,7 +23,6 @@ namespace dlib
- nc_ == 0 - nc_ == 0
- nr_ == 0 - nr_ == 0
- data == 0 - data == 0
- rows == 0
- at_start_ == true - at_start_ == true
- cur == 0 - cur == 0
- last == 0 - last == 0
...@@ -34,15 +33,10 @@ namespace dlib ...@@ -34,15 +33,10 @@ namespace dlib
- if (data != 0) then - if (data != 0) then
- last == a pointer to the last element in the data array - last == a pointer to the last element in the data array
- data == pointer to an array of nc_*nr_ T objects - data == pointer to an array of nc_*nr_ T objects
- rows == pointer to an array of nr_ row objects
- for all x < nr_:
- rows[x].data == data + x*nc_
- rows[x].nc_ == nc_
- else - else
- nc_ == 0 - nc_ == 0
- nr_ == 0 - nr_ == 0
- data == 0 - data == 0
- rows == 0
- last == 0 - last == 0
...@@ -119,13 +113,14 @@ namespace dlib ...@@ -119,13 +113,14 @@ namespace dlib
private: private:
long nc_; row(T* data_, long nc__) : data(data_), nc_(nc__) {}
T* data; T* data;
long nc_;
// restricted functions // restricted functions
row(){} row(){}
row(row&);
row& operator=(row&); row& operator=(row&);
}; };
...@@ -133,10 +128,9 @@ namespace dlib ...@@ -133,10 +128,9 @@ namespace dlib
array2d ( array2d (
) : ) :
data(0),
nc_(0), nc_(0),
nr_(0), nr_(0),
rows(0),
data(0),
cur(0), cur(0),
last(0), last(0),
at_start_(true) at_start_(true)
...@@ -147,10 +141,9 @@ namespace dlib ...@@ -147,10 +141,9 @@ namespace dlib
long nr__, long nr__,
long nc__ long nc__
) : ) :
data(0),
nc_(0), nc_(0),
nr_(0), nr_(0),
rows(0),
data(0),
cur(0), cur(0),
last(0), last(0),
at_start_(true) at_start_(true)
...@@ -177,36 +170,36 @@ namespace dlib ...@@ -177,36 +170,36 @@ namespace dlib
long nr ( long nr (
) const { return nr_; } ) const { return nr_; }
row& operator[] ( row operator[] (
long row long row_
) )
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(row < nr() && row >= 0, DLIB_ASSERT(row_ < nr() && row_ >= 0,
"\trow& array2d::operator[](long row)" "\trow array2d::operator[](long row_)"
<< "\n\tThe row index given must be less than the number of rows." << "\n\tThe row index given must be less than the number of rows."
<< "\n\tthis: " << this << "\n\tthis: " << this
<< "\n\trow: " << row << "\n\trow_: " << row_
<< "\n\tnr(): " << nr() << "\n\tnr(): " << nr()
); );
return rows[row]; return row(data+row_*nc_, nc_);
} }
const row& operator[] ( const row operator[] (
long row long row_
) const ) const
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(row < nr() && row >= 0, DLIB_ASSERT(row_ < nr() && row_ >= 0,
"\tconst row& array2d::operator[](long row) const" "\tconst row array2d::operator[](long row_) const"
<< "\n\tThe row index given must be less than the number of rows." << "\n\tThe row index given must be less than the number of rows."
<< "\n\tthis: " << this << "\n\tthis: " << this
<< "\n\trow: " << row << "\n\trow_: " << row_
<< "\n\tnr(): " << nr() << "\n\tnr(): " << nr()
); );
return rows[row]; return row(data+row_*nc_, nc_);
} }
void swap ( void swap (
...@@ -214,14 +207,12 @@ namespace dlib ...@@ -214,14 +207,12 @@ namespace dlib
) )
{ {
exchange(data,item.data); exchange(data,item.data);
exchange(rows,item.rows);
exchange(nr_,item.nr_); exchange(nr_,item.nr_);
exchange(nc_,item.nc_); exchange(nc_,item.nc_);
exchange(at_start_,item.at_start_); exchange(at_start_,item.at_start_);
exchange(cur,item.cur); exchange(cur,item.cur);
exchange(last,item.last); exchange(last,item.last);
pool.swap(item.pool); pool.swap(item.pool);
rpool.swap(item.rpool);
} }
void clear ( void clear (
...@@ -229,11 +220,9 @@ namespace dlib ...@@ -229,11 +220,9 @@ namespace dlib
{ {
if (data != 0) if (data != 0)
{ {
rpool.deallocate_array(reinterpret_cast<row_helper*>(rows));
pool.deallocate_array(data); pool.deallocate_array(data);
nc_ = 0; nc_ = 0;
nr_ = 0; nr_ = 0;
rows = 0;
data = 0; data = 0;
at_start_ = true; at_start_ = true;
cur = 0; cur = 0;
...@@ -317,20 +306,12 @@ namespace dlib ...@@ -317,20 +306,12 @@ namespace dlib
private: private:
// this object exists just so we can have a row type object that
// has a public default constructor so the memory_manager can construct it.
// I would have made rpool a friend of row but some compilers can't handle
// that without crapping out.
class row_helper : public row {};
typename mem_manager::template rebind<T>::other pool;
typename mem_manager::template rebind<row_helper>::other rpool;
T* data;
long nc_; long nc_;
long nr_; long nr_;
row* rows;
T* data;
typename mem_manager::template rebind<T>::other pool;
mutable T* cur; mutable T* cur;
T* last; T* last;
mutable bool at_start_; mutable bool at_start_;
...@@ -464,9 +445,7 @@ namespace dlib ...@@ -464,9 +445,7 @@ namespace dlib
if (data != 0) if (data != 0)
{ {
pool.deallocate_array(data); pool.deallocate_array(data);
rpool.deallocate_array(reinterpret_cast<row_helper*>(rows));
data = 0; data = 0;
rows = 0;
} }
// now setup this object to have the new size // now setup this object to have the new size
...@@ -474,32 +453,21 @@ namespace dlib ...@@ -474,32 +453,21 @@ namespace dlib
{ {
if (nr_ > 0) if (nr_ > 0)
{ {
rows = rpool.allocate_array(nr_);
data = pool.allocate_array(nr_*nc_); data = pool.allocate_array(nr_*nc_);
last = data + nr_*nc_ - 1; last = data + nr_*nc_ - 1;
} }
} }
catch (...) catch (...)
{ {
if (rows)
rpool.deallocate_array(reinterpret_cast<row_helper*>(rows));
if (data) if (data)
pool.deallocate_array(data); pool.deallocate_array(data);
rows = 0;
data = 0; data = 0;
nc_ = 0; nc_ = 0;
nr_ = 0; nr_ = 0;
last = 0; last = 0;
throw; throw;
} }
// now set up all the rows
for (long i = 0; i < nr_; ++i)
{
rows[i].nc_ = nc_;
rows[i].data = data + i*nc_;
}
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -108,7 +108,6 @@ namespace dlib ...@@ -108,7 +108,6 @@ namespace dlib
private: private:
// restricted functions // restricted functions
row(); row();
row(row&);
row& operator=(row&); row& operator=(row&);
}; };
...@@ -191,7 +190,7 @@ namespace dlib ...@@ -191,7 +190,7 @@ namespace dlib
value for its type. value for its type.
!*/ !*/
row& operator[] ( row operator[] (
long row_index long row_index
); );
/*! /*!
...@@ -202,7 +201,7 @@ namespace dlib ...@@ -202,7 +201,7 @@ namespace dlib
given row_index'th row in *this. given row_index'th row in *this.
!*/ !*/
const row& operator[] ( const row operator[] (
long row_index long row_index
) const; ) const;
/*! /*!
......
Markdown is supported
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