From 9cbbe7b75b9251d2e6fe7c8b999d6635e69207bf Mon Sep 17 00:00:00 2001 From: Davis King Date: Mon, 9 Nov 2015 17:56:25 -0500 Subject: [PATCH] Made a tensor iterable --- dlib/dnn/tensor.h | 7 +++++++ dlib/dnn/tensor_abstract.h | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/dlib/dnn/tensor.h b/dlib/dnn/tensor.h index a52b1dcd0..d6ee89f2b 100644 --- a/dlib/dnn/tensor.h +++ b/dlib/dnn/tensor.h @@ -35,6 +35,13 @@ namespace dlib long nc() const { return m_nc; } size_t size() const { return data.size(); } + typedef float* iterator; + typedef const float* const_iterator; + iterator begin() { return host(); } + const_iterator begin() const { return host(); } + iterator end() { return host()+size(); } + const_iterator end() const { return host()+size(); } + void async_copy_to_device() const { data.async_copy_to_device(); diff --git a/dlib/dnn/tensor_abstract.h b/dlib/dnn/tensor_abstract.h index 9a1169765..ff16bfb15 100644 --- a/dlib/dnn/tensor_abstract.h +++ b/dlib/dnn/tensor_abstract.h @@ -98,6 +98,17 @@ namespace dlib async_copy_to_device() and then immediately call device(). !*/ + typedef float* iterator; + typedef const float* const_iterator; + iterator begin() { return host(); } + const_iterator begin() const { return host(); } + iterator end() { return host()+size(); } + const_iterator end() const { return host()+size(); } + /*! + ensures + - makes a tensor iterable just like the STL containers. + !*/ + const float* host( ) const; /*!