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  //**Destructor**********************************************************************************
80  // No explicitly declared destructor.
81  //**********************************************************************************************
82 
83  //**Utility functions***************************************************************************
86  inline size_t size () const;
87  inline ConstIterator begin() const;
88  inline ConstIterator end () const;
90  //**********************************************************************************************
91 
92  private:
93  //**Member variables****************************************************************************
96  std::vector<size_t> indices_;
97 
98  //**********************************************************************************************
99 };
100 //*************************************************************************************************
101 
102 
103 
104 
105 //=================================================================================================
106 //
107 // CONSTRUCTORS
108 //
109 //=================================================================================================
110 
111 //*************************************************************************************************
124 inline Indices::Indices( size_t min, size_t max, size_t number )
125  : indices_() // The generated indices
126 {
127  if( max < min ) {
128  BLAZE_THROW_INVALID_ARGUMENT( "Invalid index range" );
129  }
130 
131  const size_t maxNumber( max + 1UL - min );
132 
133  if( number > maxNumber ) {
134  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of indices" );
135  }
136 
137  if( number == 0UL ) {
138  return;
139  }
140 
141  if( number <= size_t( maxNumber * 0.5 ) )
142  {
143  indices_.reserve( number );
144 
145  while( indices_.size() < number )
146  {
147  const size_t value = rand<size_t>(min,max);
148  BLAZE_INTERNAL_ASSERT( min <= value && value <= max, "Invalid index detected" );
149  const auto pos = std::lower_bound( indices_.begin(), indices_.end(), value );
150 
151  if( pos == indices_.end() || *pos != value ) {
152  indices_.insert( pos, value );
153  }
154  }
155  }
156  else
157  {
158  indices_.resize( maxNumber );
159  std::iota( indices_.begin(), indices_.end(), min );
160 
161  while( indices_.size() > number )
162  {
163  const size_t value = rand<size_t>(min,max);
164  BLAZE_INTERNAL_ASSERT( min <= value && value <= max, "Invalid index detected" );
165  const auto pos = std::lower_bound( indices_.begin(), indices_.end(), value );
166 
167  if( pos != indices_.end() && *pos == value ) {
168  indices_.erase( pos );
169  }
170  }
171  }
172 }
173 //*************************************************************************************************
174 
175 
176 
177 
178 //=================================================================================================
179 //
180 // UTILITY FUNCTIONS
181 //
182 //=================================================================================================
183 
184 //*************************************************************************************************
189 inline size_t Indices::size() const
190 {
191  return indices_.size();
192 }
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
202 {
203  return indices_.begin();
204 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
214 {
215  return indices_.end();
216 }
217 //*************************************************************************************************
218 
219 } // namespace blaze
220 
221 #endif
ConstIterator end() const
Returns an iterator just past the last element of the vector.
Definition: Indices.h:213
#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:124
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1762
Implementation of a random number generator.
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1809
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
std::vector< size_t > indices_
The generated indices.
Definition: Indices.h:96
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:201
size_t size() const
Returns the total number of random indices.
Definition: Indices.h:189
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101