38 #ifndef SHARK_DATA_DATAVIEW_H 39 #define SHARK_DATA_DATAVIEW_H 43 #include <boost/type_traits/is_const.hpp> 44 #include <boost/range/adaptor/transformed.hpp> 45 #include <boost/bind.hpp> 46 #include <boost/range/algorithm/copy.hpp> 62 template <
class DatasetType>
66 typedef typename boost::remove_const<DatasetType>::type dataset_type;
67 typedef typename dataset_type::element_type value_type;
69 typedef typename dataset_type::batch_type batch_type;
72 typedef typename boost::mpl::if_<
73 boost::is_const<DatasetType>,
74 typename dataset_type::const_element_reference,
75 typename dataset_type::element_reference
79 typedef typename boost::mpl::if_<
80 boost::is_const<DatasetType>,
81 typename dataset_type::const_batch_range,
82 typename dataset_type::batch_range
84 template<
class Reference,
class View>
85 class IteratorBase:
public SHARK_ITERATOR_FACADE<
86 IteratorBase<Reference,View>,
88 std::random_access_iterator_tag,
94 IteratorBase(View& view, std::size_t position)
95 : mpe_view(&view),m_position(position) {}
97 template<
class R,
class V>
98 IteratorBase(IteratorBase<R,V>
const& other)
99 : mpe_view(other.mpe_view),m_position(other.position){}
104 std::size_t
index()
const{
105 return mpe_view->index(m_position);
109 friend class SHARK_ITERATOR_CORE_ACCESS;
110 template <
class,
class>
friend class IteratorBase;
119 void advance(std::ptrdiff_t n){
123 template<
class R,
class V>
124 std::ptrdiff_t distance_to(IteratorBase<R,V>
const& other)
const{
125 return (std::ptrdiff_t)other.m_position - (std::ptrdiff_t)m_position;
128 template<
class R,
class V>
129 bool equal(IteratorBase<R,V>
const& other)
const{
130 return m_position == other.m_position;
132 Reference dereference()
const {
133 return (*mpe_view)[m_position];
137 std::size_t m_position;
140 typedef IteratorBase<reference,DataView<DatasetType> >
iterator;
141 typedef IteratorBase<const_reference, DataView<DatasetType>
const >
const_iterator;
145 :m_dataset(dataset),m_indices(dataset.numberOfElements())
147 std::size_t
index = 0;
148 for(std::size_t i = 0; i != dataset.numberOfBatches(); ++i){
149 std::size_t batchSize =
shark::size(dataset.batch(i));
150 for(std::size_t j = 0; j != batchSize; ++j,++
index){
151 m_indices[
index].batch = i;
152 m_indices[
index].positionInBatch = j;
159 template<
class IndexRange>
161 :m_dataset(view.m_dataset),m_indices(
shark::
size(indices))
163 for(std::size_t i = 0; i != m_indices.size(); ++i)
164 m_indices[i] = view.m_indices[indices[i]];
169 Index
const&
index = m_indices[position];
170 return get(m_dataset.batch(index.batch),index.positionInBatch);
174 Index
const&
index = m_indices[position];
175 return get(m_dataset.batch(index.batch),index.positionInBatch);
181 std::size_t
index(std::size_t position)
const{
182 return m_indices[position].datasetIndex;
186 return m_indices.size();
198 const_iterator
end()
const{
206 dataset_type m_dataset;
211 std::size_t positionInBatch;
212 std::size_t datasetIndex;
214 std::vector<Index> m_indices;
221 template<
class DatasetType,
class IndexRange>
231 template<
class DatasetType>
233 std::vector<std::size_t> indices(view.
size());
234 boost::iota(indices,0);
236 return subset(view,boost::make_iterator_range(indices.begin(),indices.begin()+
size));
243 template<
class DatasetType,
class IndexRange>
244 typename DataView<DatasetType>::batch_type
subBatch(
246 IndexRange
const& indizes
259 template<
class DatasetType>
264 std::vector<std::size_t> indices(view.
size());
265 boost::iota(indices,0);
267 return subBatch(view,boost::make_iterator_range(indices.begin(),indices.begin()+
size));
275 template<
class DatasetType>
288 typename DataView<T>::dataset_type
291 return typename DataView<T>::dataset_type();
293 std::size_t elements = view.
size();
294 typename DataView<T>::dataset_type
dataset(elements,view[0],batchSize);
295 std::size_t batches = dataset.numberOfBatches();
297 std::size_t element = 0;
298 for(std::size_t i = 0; i != batches; ++i){
299 std::size_t batchSize =
shark::size(dataset.batch(i));
300 for(std::size_t j = 0; j != batchSize; ++j, ++element){
301 get(dataset.batch(i),j) = view[element];
309 template <
class DatasetType>
315 template <
class DatasetType>
320 template <
class DatasetType>
326 template <
class DatasetType>