Skip to content

Commit 87ee607

Browse files
committed
remove difference between strides and slices.
1 parent a43ac47 commit 87ee607

File tree

6 files changed

+256
-369
lines changed

6 files changed

+256
-369
lines changed

include/boost/numeric/ublas/tensor/span.hpp

Lines changed: 28 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@
2121

2222
#include "concepts.hpp"
2323

24-
namespace boost::numeric::ublas::tag{
25-
26-
struct sliced {};
27-
struct strided {};
28-
29-
} // namespace boost::numeric::ublas::tag
30-
3124

3225
namespace boost::numeric::ublas {
3326

@@ -43,21 +36,17 @@ namespace boost::numeric::ublas {
4336
*/
4437

4538

39+
//template<class unsigned_type>
40+
//class span;
4641

47-
//using offsets = std::vector<std::ptrdiff_t>;
48-
49-
template<class span_tag, class unsigned_type>
50-
class span;
51-
52-
53-
static constexpr inline std::size_t max = std::numeric_limits<std::size_t>::max();
54-
55-
template<>
56-
class span<tag::strided, std::size_t>
42+
template<integral unsigned_type>
43+
class span
5744
{
5845
public:
59-
using span_tag = tag::strided;
60-
using value_type = std::size_t;
46+
using value_type = unsigned_type;
47+
48+
static constexpr inline value_type max = std::numeric_limits<value_type>::max();
49+
6150

6251
// covers the complete range of one dimension
6352
// e.g. a(:)
@@ -92,6 +81,13 @@ class span<tag::strided, std::size_t>
9281
}
9382
}
9483

84+
// covers a linear range of one dimension
85+
// e.g. a(1:n)
86+
span(value_type f, value_type l)
87+
: span(f,1,l)
88+
{
89+
}
90+
9591
// covers only one index of one dimension
9692
// e.g. a(1) or a(end)
9793
span(value_type n)
@@ -142,105 +138,34 @@ class span<tag::strided, std::size_t>
142138
value_type first_, last_ , step_, size_;
143139
};
144140

145-
using strided_span = span<tag::strided, std::size_t>;
146-
147-
} // namespace
148-
149-
150-
/////////////
151-
152-
namespace boost::numeric::ublas {
153-
154-
template<>
155-
class span<tag::sliced, std::size_t> :
156-
private span<tag::strided, std::size_t>
157-
{
158-
using super_type = span<tag::strided,std::size_t>;
159-
public:
160-
using span_tag = tag::sliced;
161-
using value_type = typename super_type::value_type;
162-
constexpr explicit span()
163-
: super_type()
164-
{
165-
}
166-
167-
span(value_type f, value_type l)
168-
: super_type(f, value_type(1), l )
169-
{
170-
}
171-
172-
span(value_type n)
173-
: super_type(n)
174-
{
175-
}
176-
177-
span(span const& other)
178-
: super_type(other)
179-
{
180-
}
181-
182-
inline span& operator=(const span &other)
183-
{
184-
super_type::operator=(other);
185-
return *this;
186-
}
187-
188-
~span() = default;
189-
190-
inline value_type operator[] (std::size_t idx) const
191-
{
192-
return super_type::operator [](idx);
193-
}
194-
195-
inline auto first() const {return super_type::first(); }
196-
inline auto last () const {return super_type::last (); }
197-
inline auto step () const {return super_type::step (); }
198-
inline auto size () const {return super_type::size (); }
199-
200-
inline span operator()(const span &rhs) const
201-
{
202-
auto const& lhs = *this;
203-
return span( rhs.first_ + lhs.first_, rhs.last_ + lhs.first_ );
204-
}
205-
};
206-
207-
using sliced_span = span<tag::sliced, std::size_t>;
208-
209-
210-
template<integral unsigned_type_left, integral unsigned_type_right>
211-
inline auto ran(unsigned_type_left f, unsigned_type_right l)
212-
{
213-
return sliced_span(f,l);
214-
}
215-
216-
template<integral unsigned_type_left, integral unsigned_type_middle, integral unsigned_type_right>
217-
inline auto ran(unsigned_type_left f, unsigned_type_middle s, unsigned_type_right l)
218-
{
219-
return strided_span(f,s,l);
220-
}
141+
using sspan = span<std::size_t>;
221142

222143
} // namespace
223144

224145

225-
template <class span_tag, class unsigned_type>
226-
std::ostream& operator<< (std::ostream& out, boost::numeric::ublas::span<span_tag,unsigned_type> const& s)
146+
template <boost::numeric::ublas::integral unsigned_type>
147+
std::ostream& operator<< (std::ostream& out, boost::numeric::ublas::span<unsigned_type> const& s)
227148
{
228149
return out << "[" << s.first() << ":" << s.step() << ":" << s.last() << "]" << std::endl;
229150
}
230151

231-
template<class span_tag_lhs, class span_tag_rhs, class unsigned_type>
152+
template<
153+
boost::numeric::ublas::integral unsigned_type_lhs,
154+
boost::numeric::ublas::integral unsigned_type_rhs>
232155
inline bool operator==(
233-
boost::numeric::ublas::span<span_tag_lhs,unsigned_type> const& lhs,
234-
boost::numeric::ublas::span<span_tag_rhs,unsigned_type> const& rhs)
156+
boost::numeric::ublas::span<unsigned_type_lhs> const& lhs,
157+
boost::numeric::ublas::span<unsigned_type_rhs> const& rhs)
235158
{
236159
return lhs.first() == rhs.first() && lhs.last() == rhs.last() && lhs.step() == rhs.step();
237160
}
238161

239162

240-
template<class span_tag_lhs, class span_tag_rhs, class unsigned_type>
163+
template<
164+
boost::numeric::ublas::integral unsigned_type_lhs,
165+
boost::numeric::ublas::integral unsigned_type_rhs>
241166
inline bool operator!=(
242-
boost::numeric::ublas::span<span_tag_lhs,unsigned_type> const& lhs,
243-
boost::numeric::ublas::span<span_tag_rhs,unsigned_type> const& rhs)
167+
boost::numeric::ublas::span<unsigned_type_lhs> const& lhs,
168+
boost::numeric::ublas::span<unsigned_type_rhs> const& rhs)
244169
{
245170
return lhs.first() != rhs.first() || lhs.last() != rhs.last() || lhs.step() != rhs.step();
246171
}

include/boost/numeric/ublas/tensor/subtensor.hpp

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ namespace boost::numeric::ublas {
2929

3030
/** @brief A view of a dense tensor of values of type \c T.
3131
*
32-
* @tparam T type of the objects stored in the tensor (like int, double, complex,...)
33-
* @tparam F
34-
* @tparam A The type of the storage array of the tensor. Default is \c unbounded_array<T>. \c <bounded_array<T> and \c std::vector<T> can also be used
32+
* @tparam T tensor type
3533
*/
36-
template<class S, class T>
34+
template<class T>
3735
class subtensor;
3836

3937

@@ -48,22 +46,20 @@ class subtensor;
4846
* @tparam A The type of the storage array of the tensor. Default is \c unbounded_array<T>. \c <bounded_array<T> and \c std::vector<T> can also be used
4947
*/
5048
template<class T, class F>
51-
class subtensor <tag::sliced, tensor_dynamic<T,F>>
49+
class subtensor <tensor_dynamic<T,F>>
5250
: public detail::tensor_expression<
53-
subtensor<tag::sliced,tensor_dynamic<T,F>> ,
54-
subtensor<tag::sliced,tensor_dynamic<T,F>> >
51+
subtensor<tensor_dynamic<T,F>> ,
52+
subtensor<tensor_dynamic<T,F>> >
5553
{
5654

5755
static_assert( std::is_same<F,layout::first_order>::value || std::is_same<F,layout::last_order >::value,
5856
"boost::numeric::tensor template class only supports first- or last-order storage formats.");
5957

6058
using tensor_type = tensor_dynamic<T,F>;
61-
using self_type = subtensor<tag::sliced, tensor_type>;
59+
using self_type = subtensor<tensor_type>;
6260
public:
6361

64-
using domain_tag = tag::sliced;
65-
66-
using span_type = span<domain_tag,std::size_t>;
62+
using span_type = sspan;
6763

6864
template<class derived_type>
6965
using tensor_expression_type = detail::tensor_expression<self_type,derived_type>;
@@ -116,23 +112,23 @@ class subtensor <tag::sliced, tensor_dynamic<T,F>>
116112
*/
117113
BOOST_UBLAS_INLINE
118114
subtensor (tensor_type& t)
119-
: super_type ()
120-
, spans_ ()
121-
, extents_ (t.extents())
122-
, strides_ (t.strides())
123-
, span_strides_ (t.strides())
124-
, data_ (t.data())
115+
: super_type ()
116+
, spans_ ()
117+
, extents_ (t.extents())
118+
, strides_ (t.strides())
119+
, span_strides_ (t.strides())
120+
, data_ (t.data())
125121
{
126122
}
127123

128124
template<typename ... span_types>
129-
subtensor(tensor_type& t, span_types&& ... spans)
130-
: super_type ()
131-
, spans_ (detail::generate_span_vector<span_type>(t.extents(),std::forward<span_types>(spans)...))
132-
, extents_ (detail::to_extents(spans_))
133-
, strides_ (ublas::to_strides(extents_,layout_type{}))
134-
, span_strides_ (detail::to_span_strides(t.strides(),spans_))
135-
, data_ {t.data() + detail::to_offset(t.strides(), spans_)}
125+
subtensor(tensor_type& t, span_types&& ... spans)
126+
: super_type ()
127+
, spans_ (detail::generate_vector<span_type>(t.extents(),std::forward<span_types>(spans)...))
128+
, extents_ (detail::to_extents(spans_))
129+
, strides_ (ublas::to_strides(extents_,layout_type{}))
130+
, span_strides_ (detail::to_span_strides(t.strides(),spans_))
131+
, data_ {t.data() + detail::to_offset(t.strides(), spans_)}
136132
{
137133
// if( m == nullptr)
138134
// throw std::length_error("Error in tensor_view<T>::tensor_view : multi_array_type is nullptr.");
@@ -145,16 +141,16 @@ class subtensor <tag::sliced, tensor_dynamic<T,F>>
145141
*
146142
* @note is similar to a handle to a tensor
147143
*/
148-
explicit
149-
subtensor (tensor_type const& t)
150-
: super_type ()
151-
, spans_ ()
152-
, extents_ (t.extents())
153-
, strides_ (t.strides())
154-
, span_strides_ (t.strides())
155-
, data_ (t.data())
156-
{
157-
}
144+
explicit
145+
subtensor (tensor_type const& t)
146+
: super_type ()
147+
, spans_ ()
148+
, extents_ (t.extents())
149+
, strides_ (t.strides())
150+
, span_strides_ (t.strides())
151+
, data_ (t.data())
152+
{
153+
}
158154

159155

160156

0 commit comments

Comments
 (0)