Blaze 3.9
Indices.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_UTIL_INDICES_H_
36#define _BLAZE_UTIL_INDICES_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <algorithm>
44#include <numeric>
45#include <stdexcept>
46#include <vector>
48#include <blaze/util/Random.h>
49#include <blaze/util/Types.h>
50
51
52namespace blaze {
53
54//=================================================================================================
55//
56// CLASS DEFINITION
57//
58//=================================================================================================
59
60//*************************************************************************************************
65template< typename T > // Type of the indices
67{
68 public:
69 //**Type definitions****************************************************************************
71 using ConstIterator = typename std::vector<T>::const_iterator;
72 //**********************************************************************************************
73
74 //**Constructors********************************************************************************
77 inline Indices( T min, T max, T number );
79 //**********************************************************************************************
80
81 //**Utility functions***************************************************************************
84 inline size_t size () const noexcept;
85 inline ConstIterator begin() const noexcept;
86 inline ConstIterator end () const noexcept;
88 //**********************************************************************************************
89
90 private:
91 //**Member variables****************************************************************************
94 std::vector<T> indices_;
96 //**********************************************************************************************
97};
98//*************************************************************************************************
99
100
101
102
103//=================================================================================================
104//
105// CONSTRUCTORS
106//
107//=================================================================================================
108
109//*************************************************************************************************
122template< typename T > // Type of the indices
123inline Indices<T>::Indices( T min, T max, T number )
124 : indices_() // The generated indices
125{
126 if( max < min ) {
127 BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range" );
128 }
129
130 const T maxNumber( max + T(1) - min );
131
132 if( number > maxNumber ) {
133 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of indices" );
134 }
135
136 if( number == 0UL ) {
137 return;
138 }
139
140 if( number <= T( maxNumber * 0.5 ) )
141 {
142 indices_.reserve( number );
143
144 while( indices_.size() < number )
145 {
146 const T value = rand<T>(min,max);
147 BLAZE_INTERNAL_ASSERT( min <= value && value <= max, "Invalid index detected" );
148 const auto pos = std::lower_bound( indices_.begin(), indices_.end(), value );
149
150 if( pos == indices_.end() || *pos != value ) {
151 indices_.insert( pos, value );
152 }
153 }
154 }
155 else
156 {
157 indices_.resize( maxNumber );
158 std::iota( indices_.begin(), indices_.end(), min );
159
160 while( indices_.size() > number )
161 {
162 const T value = rand<T>(min,max);
163 BLAZE_INTERNAL_ASSERT( min <= value && value <= max, "Invalid index detected" );
164 const auto pos = std::lower_bound( indices_.begin(), indices_.end(), value );
165
166 if( pos != indices_.end() && *pos == value ) {
167 indices_.erase( pos );
168 }
169 }
170 }
171}
172//*************************************************************************************************
173
174
175
176
177//=================================================================================================
178//
179// UTILITY FUNCTIONS
180//
181//=================================================================================================
182
183//*************************************************************************************************
188template< typename T > // Type of the indices
189inline size_t Indices<T>::size() const noexcept
190{
191 return indices_.size();
192}
193//*************************************************************************************************
194
195
196//*************************************************************************************************
201template< typename T > // Type of the indices
202inline typename Indices<T>::ConstIterator Indices<T>::begin() const noexcept
203{
204 return indices_.begin();
205}
206//*************************************************************************************************
207
208
209//*************************************************************************************************
214template< typename T > // Type of the indices
215inline typename Indices<T>::ConstIterator Indices<T>::end() const noexcept
216{
217 return indices_.end();
218}
219//*************************************************************************************************
220
221} // namespace blaze
222
223#endif
Auxiliary class for the generation of random indices.
Definition: Indices.h:67
typename std::vector< T >::const_iterator ConstIterator
Iterator over the generated indices.
Definition: Indices.h:71
ConstIterator begin() const noexcept
Returns an iterator to the beginning of the vector.
Definition: Indices.h:202
size_t size() const noexcept
Returns the total number of random indices.
Definition: Indices.h:189
std::vector< T > indices_
The generated indices.
Definition: Indices.h:94
Indices(T min, T max, T number)
The constructor for the Indices class.
Definition: Indices.h:123
ConstIterator end() const noexcept
Returns an iterator just past the last element of the vector.
Definition: Indices.h:215
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1339
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1375
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
Header file for exception macros.
Implementation of a random number generator.
Header file for basic type definitions.