Blaze  3.6
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>
47 #include <blaze/util/Exception.h>
48 #include <blaze/util/Random.h>
49 #include <blaze/util/Types.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // CLASS DEFINITION
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
65 class Indices
66 {
67  public:
68  //**Type definitions****************************************************************************
69  using ConstIterator = std::vector<size_t>::const_iterator;
70  //**********************************************************************************************
71 
72  //**Constructors********************************************************************************
75  inline Indices( size_t min, size_t max, size_t number );
77  //**********************************************************************************************
78 
79  //**Utility functions***************************************************************************
82  inline size_t size () const;
83  inline ConstIterator begin() const;
84  inline ConstIterator end () const;
86  //**********************************************************************************************
87 
88  private:
89  //**Member variables****************************************************************************
92  std::vector<size_t> indices_;
93 
94  //**********************************************************************************************
95 };
96 //*************************************************************************************************
97 
98 
99 
100 
101 //=================================================================================================
102 //
103 // CONSTRUCTORS
104 //
105 //=================================================================================================
106 
107 //*************************************************************************************************
120 inline Indices::Indices( size_t min, size_t max, size_t number )
121  : indices_() // The generated indices
122 {
123  if( max < min ) {
124  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range" );
125  }
126 
127  const size_t maxNumber( max + 1UL - min );
128 
129  if( number > maxNumber ) {
130  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of indices" );
131  }
132 
133  if( number == 0UL ) {
134  return;
135  }
136 
137  if( number <= size_t( maxNumber * 0.5 ) )
138  {
139  indices_.reserve( number );
140 
141  while( indices_.size() < number )
142  {
143  const size_t value = rand<size_t>(min,max);
144  BLAZE_INTERNAL_ASSERT( min <= value && value <= max, "Invalid index detected" );
145  const auto pos = std::lower_bound( indices_.begin(), indices_.end(), value );
146 
147  if( pos == indices_.end() || *pos != value ) {
148  indices_.insert( pos, value );
149  }
150  }
151  }
152  else
153  {
154  indices_.resize( maxNumber );
155  std::iota( indices_.begin(), indices_.end(), min );
156 
157  while( indices_.size() > number )
158  {
159  const size_t value = rand<size_t>(min,max);
160  BLAZE_INTERNAL_ASSERT( min <= value && value <= max, "Invalid index detected" );
161  const auto pos = std::lower_bound( indices_.begin(), indices_.end(), value );
162 
163  if( pos != indices_.end() && *pos == value ) {
164  indices_.erase( pos );
165  }
166  }
167  }
168 }
169 //*************************************************************************************************
170 
171 
172 
173 
174 //=================================================================================================
175 //
176 // UTILITY FUNCTIONS
177 //
178 //=================================================================================================
179 
180 //*************************************************************************************************
185 inline size_t Indices::size() const
186 {
187  return indices_.size();
188 }
189 //*************************************************************************************************
190 
191 
192 //*************************************************************************************************
198 {
199  return indices_.begin();
200 }
201 //*************************************************************************************************
202 
203 
204 //*************************************************************************************************
210 {
211  return indices_.end();
212 }
213 //*************************************************************************************************
214 
215 } // namespace blaze
216 
217 #endif
ConstIterator end() const
Returns an iterator just past the last element of the vector.
Definition: Indices.h:209
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Auxiliary class for the generation of random indices.
Definition: Indices.h:65
Header file for basic type definitions.
Header file for exception macros.
Indices(size_t min, size_t max, size_t number)
The constructor for the Indices class.
Definition: Indices.h:120
Implementation of a random number generator.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
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:1162
std::vector< size_t > indices_
The generated indices.
Definition: Indices.h:92
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:1198
std::vector< size_t >::const_iterator ConstIterator
Iterator over the generated indices.
Definition: Indices.h:69
ConstIterator begin() const
Returns an iterator to the beginning of the vector.
Definition: Indices.h:197
size_t size() const
Returns the total number of random indices.
Definition: Indices.h:185
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101