Medusa  1.1
Coordinate Free Mehless Method implementation
Range.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_TYPES_RANGE_HPP_
2 #define MEDUSA_BITS_TYPES_RANGE_HPP_
3 
9 #include "Range_fwd.hpp"
10 #include <algorithm>
11 
12 namespace mm {
13 
14 template<class T, class Allocator>
15 Range<T, Allocator>& Range<T, Allocator>::operator=(std::initializer_list<value_type> lst) {
16  this->resize(lst.size());
17  int i = 0;
18  for (const_reference x : lst) {
19  operator[](i++) = x;
20  }
21  return *this;
22 }
23 
24 template<class T, class Allocator>
26  for (size_type i = 0; i < size(); ++i) {
27  operator[](i) = x;
28  }
29  return *this;
30 }
31 
32 template<class T, class Allocator>
34  assert_msg(0 <= i && i < size(), "Index %d out of range [%d, %d) when accessing Range "
35  "for write.", i, 0, size());
36  return std::vector<T>::operator[](i);
37 }
38 
39 template<class T, class Allocator>
41  assert_msg(0 <= i && i < size(), "Index %d out of range [%d, %d) when accessing Range "
42  "for read/write.", i, 0, size());
43  return std::vector<T>::operator[](i);
44 }
45 
46 template<class T, class Allocator>
48  for (size_type idx : indexes) {
49  assert_msg(0 <= idx && idx < size(), "Index %d out of range [%d, %d) when using "
50  "multiindexed read-write access.", idx, 0, size());
51  }
52  return {*this, indexes};
53 }
54 
55 template<class T, class Allocator>
58  for (size_type idx : indexes) {
59  assert_msg(0 <= idx && idx < size(), "Index %d out of range [%d, %d) when using "
60  "multiindexed read access.", idx, 0, size());
61  }
62  return {*this, indexes};
63 }
64 
65 template<class T, class Allocator>
67  this->insert(this->end(), rng.begin(), rng.end());
68  return *this;
69 }
70 
71 template<class T, class Allocator>
73  Range ret = *this;
74  ret.insert(ret.end(), rng.begin(), rng.end());
75  return ret;
76 }
77 
78 template<class T, class Allocator>
80  std::sort(indexes.begin(), indexes.end());
81  indexes.resize(std::unique(indexes.begin(), indexes.end()) - indexes.begin());
82  int sz = indexes.size();
83  auto it = std::vector<T, Allocator>::begin();
84  auto cur = std::vector<T, Allocator>::begin();
85  int to_remove = 0;
86  int c = 0;
87  while (cur != std::vector<T, Allocator>::end()) {
88  if (to_remove < sz && c == indexes[to_remove]) {
89  ++to_remove;
90  } else {
91  *it++ = std::move(*cur);
92  }
93  ++c;
94  ++cur;
95  }
96  std::vector<T, Allocator>::resize(it - std::vector<T, Allocator>::begin());
97 }
98 
99 template<class T, class Allocator>
100 template<class Predicate>
101 indexes_t Range<T, Allocator>::filter(const Predicate& predicate) const {
102  indexes_t ret;
103  for (size_type i = 0; i < size(); ++i)
104  if (predicate(operator[](i))) ret.push_back(i);
105  return ret;
106 }
107 
108 template<class T, class Allocator>
109 template<typename UnaryOp>
110 auto Range<T, Allocator>::map(UnaryOp op) -> Range<decltype(op(this->operator[](0)))> const {
111  Range<decltype(op(this->operator[](0)))> r;
112  r.reserve(size());
113  std::transform(Range<T>::begin(), Range<T>::end(), std::back_inserter(r), op);
114  return r;
115 }
116 
117 template<class T, class Allocator>
119  return filter([&](const value_type& t) { return t < v; });
120 }
121 
122 template<class T, class Allocator>
124  return filter([&](const value_type& t) { return t > v; });
125 }
126 
127 template<class T, class Allocator>
129  return filter([&](const value_type& t) { return t <= v; });
130 }
131 
132 template<class T, class Allocator>
134  return filter([&](const value_type& t) { return t >= v; });
135 }
136 
137 template<class T, class Allocator>
139  return filter([&](const value_type& t) { return t == v; });
140 }
141 
142 template<class T, class Allocator>
144  return filter([&](const value_type& t) { return t != v; });
145 }
146 
148 template <class T, class Allocator>
149 template <typename V>
151  return Range<T, Allocator>::seq(static_cast<V>(0), n);
152 }
153 
154 template <class T, class Allocator>
155 template <typename V>
156 Range<T, Allocator> Range<T, Allocator>::seq(V start, V stop) {
157  return Range<T, Allocator>::seq(start, stop, static_cast<V>(1));
158 }
159 
160 template <class T, class Allocator>
161 template <typename V, typename D>
162 Range<T, Allocator> Range<T, Allocator>::seq(V start, V stop, D step) {
163  assert_msg(step != 0, "Step must be nonzero.");
164  assert_msg((stop - start) / step >= 0, "Step %d must be in the direction from start %d to "
165  "stop %d.", step, start, stop);
166  Range<T> ret; ret.reserve((stop - start) / step);
167  for (V i = start; (step > 0) ? i < stop : i > stop; i += step) {
168  ret.push_back(i);
169  }
170  return ret;
171 }
173 
174 } // namespace mm
175 
176 #endif // MEDUSA_BITS_TYPES_RANGE_HPP_
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< value_t >::const_reference
std::vector< value_t, std::allocator< value_t > >::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::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
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::operator==
indexes_t operator==(const value_type &v) const
Returns list of indexes of elements that are equal to v.
Definition: Range.hpp:138
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
filter
mm::indexes_t filter(const Pred &pred) const
Returns list of indexes for which predicate returns true.
Definition: MatrixBaseAddons.hpp:46
mm::indexes_t
std::vector< int > indexes_t
Class representing a collection of indices.
Definition: Config.hpp:36
mm::Range< value_t >::value_type
std::vector< value_t, std::allocator< value_t > >::value_type value_type
This container's value type.
Definition: Range_fwd.hpp:33
Range_fwd.hpp
mm::Range::operator=
Range & operator=(const Range &o)=default
Default copy assignment.
mm::Range< value_t >::size_type
int size_type
This container's size type.
Definition: Range_fwd.hpp:39
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::Range::append
Range & append(const Range &rng)
Append all elements of rng to self.
Definition: Range.hpp:66
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::sort
container_t & sort(container_t &v)
Sorts a container inplace.
Definition: stdtypesutils.hpp:43
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
mm::Range::remove
void remove(indexes_t indexes)
Remove elements wih given indexes.
Definition: Range.hpp:79