Commit f0dff5fc authored by Davis King's avatar Davis King

Added alias_tensor_const_instance

parent 7c6a800b
...@@ -478,6 +478,7 @@ namespace dlib ...@@ -478,6 +478,7 @@ namespace dlib
return sum; return sum;
} }
// ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
class alias_tensor_instance : public tensor class alias_tensor_instance : public tensor
...@@ -487,6 +488,7 @@ namespace dlib ...@@ -487,6 +488,7 @@ namespace dlib
public: public:
friend class alias_tensor; friend class alias_tensor;
friend class alias_tensor_const_instance;
alias_tensor_instance& operator= (float val) alias_tensor_instance& operator= (float val)
{ {
...@@ -529,6 +531,23 @@ namespace dlib ...@@ -529,6 +531,23 @@ namespace dlib
virtual const gpu_data& data() const { return *data_instance; } virtual const gpu_data& data() const { return *data_instance; }
}; };
// ----------------------------------------------------------------------------------------
class alias_tensor_const_instance
{
public:
const tensor& get() const { return inst; }
operator const tensor& () { return inst; }
private:
alias_tensor_instance inst;
friend class alias_tensor;
alias_tensor_const_instance() {}
};
// ----------------------------------------------------------------------------------------
class alias_tensor class alias_tensor
{ {
public: public:
...@@ -586,6 +605,16 @@ namespace dlib ...@@ -586,6 +605,16 @@ namespace dlib
return inst; return inst;
} }
alias_tensor_const_instance operator() (
const tensor& t,
size_t offset
)
{
alias_tensor_const_instance temp;
temp.inst = (*this)(const_cast<tensor&>(t),offset);
return temp;
}
private: private:
alias_tensor_instance inst; alias_tensor_instance inst;
}; };
......
...@@ -575,6 +575,28 @@ namespace dlib ...@@ -575,6 +575,28 @@ namespace dlib
); );
}; };
class alias_tensor_const_instance
{
/*!
WHAT THIS OBJECT REPRESENTS
This is essentially a const version of alias_tensor_instance and therefore
represents a tensor. However, due to the mechanics of C++, this object
can't inherit from tensor. So instead it provides a get() and an implicit
conversion to const tensor.
!*/
public:
// Methods that cast the alias to a tensor.
const tensor& get() const;
operator const tensor& ();
private:
// You can't default initialize this object. You can only get instances of it from
// alias_tensor::operator().
alias_tensor_const_instance();
};
class alias_tensor class alias_tensor
{ {
/*! /*!
...@@ -629,7 +651,7 @@ namespace dlib ...@@ -629,7 +651,7 @@ namespace dlib
requires requires
- offset+size() <= t.size() - offset+size() <= t.size()
ensures ensures
- Return a tensor that simply aliases the elements of t beginning with t's - Returns a tensor that simply aliases the elements of t beginning with t's
offset'th element. Specifically, this function returns an aliasing offset'th element. Specifically, this function returns an aliasing
tensor T such that: tensor T such that:
- T.size() == size() - T.size() == size()
...@@ -641,6 +663,18 @@ namespace dlib ...@@ -641,6 +663,18 @@ namespace dlib
- T.device() == t.device()+offset - T.device() == t.device()+offset
- &T.annotation() == &t.annotation() - &T.annotation() == &t.annotation()
!*/ !*/
alias_tensor_const_instance operator() (
const tensor& t,
size_t offset
);
/*!
requires
- offset+size() <= t.size()
ensures
- This function is identical to the above version of operator() except that
it takes and returns const tensors instead of non-const tensors.
!*/
}; };
void serialize(const alias_tensor& item, std::ostream& out); void serialize(const alias_tensor& item, std::ostream& out);
......
...@@ -387,7 +387,7 @@ namespace ...@@ -387,7 +387,7 @@ namespace
alias_tensor at(2,2); alias_tensor at(2,2);
auto A0 = at(A,0); auto A0 = at(A,0);
auto A4 = at(A,4); auto A4 = at(A,4);
auto A8 = at(A,8); auto A8 = at(const_cast<const resizable_tensor&>(A),8);
DLIB_TEST(mat(A0) == truth1); DLIB_TEST(mat(A0) == truth1);
DLIB_TEST(mat(at(A,4)) == truth2); DLIB_TEST(mat(at(A,4)) == truth2);
DLIB_TEST(mat(A8) == truth3); DLIB_TEST(mat(A8) == truth3);
......
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