Medusa  1.1
Coordinate Free Mehless Method implementation
Range_fwd.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_TYPES_RANGE_FWD_HPP_
2 #define MEDUSA_BITS_TYPES_RANGE_FWD_HPP_
3 
11 #include <medusa/Config.hpp>
13 #include "traits.hpp"
14 #include <memory>
15 #include <vector>
16 #include <iosfwd>
17 
18 namespace mm {
19 
29 template<class T, class Allocator = std::allocator<T>>
30 class Range : public std::vector<T, Allocator> {
31  public:
33  typedef typename std::vector<T, Allocator>::value_type value_type;
35  typedef typename std::vector<T, Allocator>::reference reference;
37  typedef typename std::vector<T, Allocator>::const_reference const_reference;
39  typedef int size_type;
40 
45  class RangeView {
48 
50  RangeView(Range<T>& receiver_, const indexes_t& modifier_)
51  : receiver(receiver_), modifier(modifier_) {}
52 
53  public:
54  RangeView(RangeView&) = delete;
55  RangeView(RangeView&&) = delete;
56  RangeView& operator=(RangeView&) = delete;
57  RangeView& operator=(RangeView&&) = delete;
58 
60  Range<T> asRange() const {
61  Range<T> ret(size());
62  int sz = modifier.size();
63  for (int i = 0; i < sz; ++i) ret[i] = operator[](i);
64  return ret;
65  }
66 
68  void operator=(const Range<T>& rhs) {
69  assert_msg(rhs.size() == size(),
70  "Container sizes must match in multiindexed assignment, but my size is %d "
71  "and assigned size is %d.", size(), rhs.size());
72  for (size_type i = 0; i < size(); ++i) operator[](i) = rhs[i];
73  }
74 
76  void operator=(const value_type& x) {
77  for (size_type i = 0; i < size(); ++i)
78  operator[](i) = x;
79  }
80 
83 
86 
88  size_type size() const { return static_cast<int>(modifier.size()); }
89 
90  friend class Range;
91 
93  friend std::ostream& operator<<(std::ostream& os, const RangeView& c) {
94  return os << c.asRange();
95  }
96  };
97 
105 
107  ConstRangeView(const Range<T>& receiver_, const indexes_t& modifier_)
108  : receiver(receiver_), modifier(modifier_) {}
109 
110  public:
111  ConstRangeView(ConstRangeView&) = delete;
112  ConstRangeView(ConstRangeView&&) = delete;
115 
117  Range<T> asRange() const {
118  Range<T> ret(size());
119  int sz = modifier.size();
120  for (int i = 0; i < sz; ++i) ret[i] = operator[](i);
121  return ret;
122  }
123 
126 
128  size_type size() const { return static_cast<int>(modifier.size()); }
129 
130  friend class Range;
131 
133  friend std::ostream& operator<<(std::ostream& os, const ConstRangeView& c) {
134  return os << c.asRange();
135  }
136  };
137 
138  public:
139  using std::vector<T, Allocator>::vector;
140  using std::vector<T, Allocator>::operator=;
141  Range(const Range& o) = default;
142  Range() : std::vector<T, Allocator>() {}
143  Range(Range&& o) noexcept = default;
144  Range& operator=(const Range& o) = default;
145  Range& operator=(Range&& o) noexcept = default;
146  Range(const std::vector<T, Allocator>& o) : std::vector<T, Allocator>(o) {}
149  Range(std::vector<T, Allocator>&& o) noexcept : std::vector<T, Allocator>(std::move(o)) {}
151  Range(const Range::RangeView& o) : Range(o.asRange()) {}
153  Range(const Range::ConstRangeView& o) : Range(o.asRange()) {}
154 
156  template <typename V>
157  static Range seq(V n);
159  template <typename V>
160  static Range seq(V start, V stop);
162  template <typename V, typename D>
163  static Range seq(V start, V stop, D step);
164 
166  Range& operator=(std::initializer_list<value_type> lst);
167 
169  Range& operator=(const value_type& x);
170 
173 
176 
178  RangeView operator[](const indexes_t& indexes);
179 
181  ConstRangeView operator[](const indexes_t& indexes) const;
182 
185  int size() const { return static_cast<int>(std::vector<T, Allocator>::size()); }
186 
188  Range& append(const Range& rng);
190  Range& operator+=(const Range& rng) { return append(rng); }
191 
193  Range join(const Range& rng) const;
194 
196  void remove(indexes_t indexes);
197 
208  template<class Predicate>
209  indexes_t filter(const Predicate& predicate) const;
210 
217  template<typename UnaryOp>
218  auto map(UnaryOp op) -> Range<decltype(op(this->operator[](0)))> const;
219 
221  indexes_t operator<(const value_type& v) const;
222 
224  indexes_t operator>(const value_type& v) const;
225 
227  indexes_t operator<=(const value_type& v) const;
228 
230  indexes_t operator>=(const value_type& v) const;
231 
233  indexes_t operator==(const value_type& v) const;
234 
236  indexes_t operator!=(const value_type& v) const;
237 };
238 
240 template <typename T> struct scalar_type<Range<T>> {
241  typedef typename Range<T>::value_type type;
242 };
244 template <typename T> struct vector_type<Range<T>> {
245  typedef typename Range<T>::value_type type;
246 };
248 template <typename T> struct scalar_type<std::vector<T>> {
249  typedef typename std::vector<T>::value_type type;
250 };
252 template <typename T> struct vector_type<std::vector<T>> {
253  typedef typename std::vector<T>::value_type type;
254 };
255 
264 template<class T, class Allocator = std::allocator<T>>
265 std::vector<T, Allocator> operator+(const std::vector<T, Allocator>& v1,
266  const std::vector<T, Allocator>& v2) {
267  std::vector<T, Allocator> ret = v1;
268  ret.insert(ret.end(), v2.begin(), v2.end());
269  return ret;
270 }
271 
272 } // namespace mm
273 
274 #endif // MEDUSA_BITS_TYPES_RANGE_FWD_HPP_
mm::Range::RangeView::size
size_type size() const
Size of the sub-container.
Definition: Range_fwd.hpp:88
mm::Range::operator[]
reference operator[](size_type i)
Overload vector's [] to assert parameter.
Definition: Range.hpp:33
mm::Range::filter
indexes_t filter(const Predicate &predicate) const
Returns list of indexes for which predicate returns true.
Definition: Range.hpp:101
mm::Range::operator>=
indexes_t operator>=(const value_type &v) const
Returns list of indexes of elements that are greater or equal to v.
Definition: Range.hpp:133
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::Range::operator!=
indexes_t operator!=(const value_type &v) const
Returns list of indexes of elements that are not equal to v.
Definition: Range.hpp:143
mm::Range::Range
Range(std::vector< T, Allocator > &&o) noexcept
Move Construct from std::vector.
Definition: Range_fwd.hpp:149
mm::Range::const_reference
std::vector< T, Allocator >::const_reference const_reference
This container's const reference to value type.
Definition: Range_fwd.hpp:37
mm::Range::operator<=
indexes_t operator<=(const value_type &v) const
Returns list of indexes of elements that are less or equal to v.
Definition: Range.hpp:128
mm::Range::reference
std::vector< T, Allocator >::reference reference
This container's reference to value type.
Definition: Range_fwd.hpp:35
mm::Range::ConstRangeView::asRange
Range< T > asRange() const
Cast to underlying container type.
Definition: Range_fwd.hpp:117
mm::Range::map
auto map(UnaryOp op) -> Range< decltype(op(this->operator[](0)))> const
Returns a new range, obtained by applying op to all elements of this Range.
Definition: Range.hpp:110
mm::Range::RangeView::RangeView
RangeView(Range< T > &receiver_, const indexes_t &modifier_)
Constructor.
Definition: Range_fwd.hpp:50
mm::Range::ConstRangeView::ConstRangeView
ConstRangeView(const Range< T > &receiver_, const indexes_t &modifier_)
Constructor.
Definition: Range_fwd.hpp:107
mm::Range::RangeView
This class represents a non contiguous view to a Range, allowing for read and write operations.
Definition: Range_fwd.hpp:45
mm::Range::operator<
indexes_t operator<(const value_type &v) const
Returns list of indexes of elements that are less than v.
Definition: Range.hpp:118
mm::Range::seq
static Range seq(V n)
Returns range {0, ..., n-1}.
mm::Range::RangeView::operator=
void operator=(const Range< T > &rhs)
Multiindex assignment: a[{1, 2, 3}] = Range<int>({1, 2, 3});.
Definition: Range_fwd.hpp:68
mm::scalar_type< std::vector< T > >::type
std::vector< T >::value_type type
Underlying vector type.
Definition: Range_fwd.hpp:249
mm::Range::operator==
indexes_t operator==(const value_type &v) const
Returns list of indexes of elements that are equal to v.
Definition: Range.hpp:138
mm::Range::Range
Range(const Range::ConstRangeView &o)
Construct from ConstRangeView.
Definition: Range_fwd.hpp:153
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
mm::Range::RangeView::asRange
Range< T > asRange() const
Cast to underlying container type.
Definition: Range_fwd.hpp:60
mm::Range::Range
Range(const Range::RangeView &o)
Construct from RangeView.
Definition: Range_fwd.hpp:151
mm::vector_type< std::vector< T > >::type
std::vector< T >::value_type type
Underlying vector type.
Definition: Range_fwd.hpp:253
mm::vector_type< Range< T > >::type
Range< T >::value_type type
Underlying scalar type.
Definition: Range_fwd.hpp:245
Config.hpp
mm::Range::ConstRangeView::operator=
ConstRangeView & operator=(ConstRangeView &)=delete
Disallow copying.
mm::operator+
std::vector< T, Allocator > operator+(const std::vector< T, Allocator > &v1, const std::vector< T, Allocator > &v2)
Concatenate two vectors.
Definition: Range_fwd.hpp:265
mm::indexes_t
std::vector< int > indexes_t
Class representing a collection of indices.
Definition: Config.hpp:36
mm::Range::value_type
std::vector< T, Allocator >::value_type value_type
This container's value type.
Definition: Range_fwd.hpp:33
mm::Range::ConstRangeView::size
size_type size() const
Size of the sub-container.
Definition: Range_fwd.hpp:128
mm::Range::operator=
Range & operator=(const Range &o)=default
Default copy assignment.
mm::Range::RangeView::receiver
Range< T > & receiver
Reference to object we are viewing / modifying.
Definition: Range_fwd.hpp:46
mm::Range::size_type
int size_type
This container's size type.
Definition: Range_fwd.hpp:39
mm::vector_type
Type trait for vector fields to obtain their underlying vector type.
Definition: traits.hpp:28
assert.hpp
mm::Range::RangeView::operator=
void operator=(const value_type &x)
Multiindex value assignment: a = 4;.
Definition: Range_fwd.hpp:76
mm::Range::operator>
indexes_t operator>(const value_type &v) const
Returns list of indexes of elements that are greater than v.
Definition: Range.hpp:123
mm::scalar_type< Range< T > >::type
Range< T >::value_type type
Underlying scalar type.
Definition: Range_fwd.hpp:241
mm::Range::RangeView::operator=
RangeView & operator=(RangeView &)=delete
Disallow copying.
mm::Range::append
Range & append(const Range &rng)
Append all elements of rng to self.
Definition: Range.hpp:66
mm::Range::ConstRangeView::modifier
const indexes_t & modifier
List of indexes of elements to modify.
Definition: Range_fwd.hpp:104
mm::Range::ConstRangeView::receiver
const Range< T > & receiver
Reference to object we are viewing.
Definition: Range_fwd.hpp:103
mm::Range::RangeView::operator[]
const_reference operator[](size_type i) const
Read access to sub-container elements.
Definition: Range_fwd.hpp:85
mm::Range::size
int size() const
Returns number of elements.
Definition: Range_fwd.hpp:185
mm::Range::RangeView::operator<<
friend std::ostream & operator<<(std::ostream &os, const RangeView &c)
Output a RangeView.
Definition: Range_fwd.hpp:93
mm::Range::Range
Range(const Range &o)=default
Default copy constructor.
mm::Range::join
Range join(const Range &rng) const
Return new copy containing this range's elements followed by all elements of rng.
Definition: Range.hpp:72
mm::Range::ConstRangeView::operator<<
friend std::ostream & operator<<(std::ostream &os, const ConstRangeView &c)
Output a ConstRangeView.
Definition: Range_fwd.hpp:133
mm::Range::ConstRangeView::operator[]
const_reference operator[](size_type i) const
Read access to sub-container elements.
Definition: Range_fwd.hpp:125
mm::Range
An extension of std::vector<T> to support additional useful operations.
Definition: Range_fwd.hpp:30
mm::Range::ConstRangeView
This class represents a non contiguous view to a Range, allowing for read-only operations.
Definition: Range_fwd.hpp:102
traits.hpp
mm::scalar_type
Type trait for scalar fields to obtain their underlying scalar type.
Definition: traits.hpp:17
mm::Range::remove
void remove(indexes_t indexes)
Remove elements wih given indexes.
Definition: Range.hpp:79
mm::Range::operator+=
Range & operator+=(const Range &rng)
Operator version of Range::append.
Definition: Range_fwd.hpp:190
mm::Range::RangeView::modifier
const indexes_t & modifier
List of indexes of elements to modify.
Definition: Range_fwd.hpp:47
mm::Range::RangeView::operator[]
reference operator[](size_type i)
Write access to sub-container elements.
Definition: Range_fwd.hpp:82