| 1 |
/* |
| 2 |
Copyright (c) 2016, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#ifndef PARAMETER_PACK_INCLUDED |
| 30 |
#define PARAMETER_PACK_INCLUDED |
| 31 |
|
| 32 |
///////////////////// |
| 33 |
// parameter packs // |
| 34 |
///////////////////// |
| 35 |
|
| 36 |
namespace ParameterPack |
| 37 |
{ |
| 38 |
//////////////////////////// |
| 39 |
//////////////////////////// |
| 40 |
//// Short declarations //// |
| 41 |
//////////////////////////// |
| 42 |
//////////////////////////// |
| 43 |
|
| 44 |
// A wrapper class for passing unsigned integer parameter packs |
| 45 |
template< typename IntegralType , IntegralType ... Values > struct Pack; |
| 46 |
|
| 47 |
// A class that identifies a single entry and its complement |
| 48 |
template< unsigned int I , typename Pack > struct Selection; |
| 49 |
|
| 50 |
// A class that splits a Pack into two sub-packs |
| 51 |
template< unsigned int I , typename Pack > struct Partition; |
| 52 |
|
| 53 |
// A class for comparing two Packs |
| 54 |
template< typename Pack1 , typename Pack2 > struct Comparison; |
| 55 |
|
| 56 |
// A class for adding/subtracting two Packs |
| 57 |
template< typename Pack1 , typename Pack2 > struct Arithmetic; |
| 58 |
|
| 59 |
// A helper class for defining a concatenation of multiple Packs |
| 60 |
template< typename ... Packs > struct _Concatenation; |
| 61 |
|
| 62 |
// A helper class for defining the permutation of a Pack |
| 63 |
template< typename Pack , typename PermutationPack > struct _Permutation; |
| 64 |
|
| 65 |
// A helper class for defining a Pack with the same value repeated Dim times |
| 66 |
template< typename IntegralType , unsigned int Dim , IntegralType Value > struct _IsotropicPack; |
| 67 |
|
| 68 |
// A helper class for defining a Pack with sequentially increasing values |
| 69 |
template< typename IntegralType , unsigned int Dim , IntegralType StartValue > struct _SequentialPack; |
| 70 |
|
| 71 |
// A Pack that is the concatenation of multiple Packs |
| 72 |
template< typename ... Packs > using Concatenation = typename _Concatenation< Packs ... >::type; |
| 73 |
|
| 74 |
// A Pack that is the permtuation of a Pack |
| 75 |
// [NOTE] The entry in the i-th position of PermutationPack indicates where the i-th position comes from (not goes to) |
| 76 |
template< typename Pack , typename PermutationPack > using Permutation = typename _Permutation< Pack , PermutationPack >::type; |
| 77 |
|
| 78 |
// A Pack that has the same value repeated Dim times |
| 79 |
template< typename IntegralType , unsigned int Dim , IntegralType Value=0 > using IsotropicPack = typename _IsotropicPack< IntegralType , Dim , Value >::type; |
| 80 |
|
| 81 |
// A Pack with sequentially increasing values |
| 82 |
template< typename IntegralType , unsigned int Dim , IntegralType StartValue=0 > using SequentialPack = typename _SequentialPack< IntegralType , Dim , StartValue >::type; |
| 83 |
|
| 84 |
///////////////////////// |
| 85 |
///////////////////////// |
| 86 |
//// Specializations //// |
| 87 |
///////////////////////// |
| 88 |
///////////////////////// |
| 89 |
|
| 90 |
// A pack with int values |
| 91 |
template< int ... Values > using IntPack = Pack< int , Values... >; |
| 92 |
|
| 93 |
// A pack with unsigned int values |
| 94 |
template< unsigned int ... Values > using UIntPack = Pack< unsigned int , Values... >; |
| 95 |
|
| 96 |
// An isotropic pack with int values |
| 97 |
template< unsigned int Dim , int Value=0 > using IsotropicIntPack = IsotropicPack< int , Dim , Value >; |
| 98 |
|
| 99 |
// An isotropic pack with unsigned int values |
| 100 |
template< unsigned int Dim , unsigned int Value=0 > using IsotropicUIntPack = IsotropicPack< unsigned int , Dim , Value >; |
| 101 |
|
| 102 |
|
| 103 |
///////////////////// |
| 104 |
///////////////////// |
| 105 |
//// Definitions //// |
| 106 |
///////////////////// |
| 107 |
///////////////////// |
| 108 |
|
| 109 |
|
| 110 |
////////// |
| 111 |
// Pack // |
| 112 |
////////// |
| 113 |
|
| 114 |
// The general case |
| 115 |
template< typename IntegralType , IntegralType _Value , IntegralType ... _Values > struct Pack< IntegralType , _Value , _Values ... > |
| 116 |
{ |
| 117 |
static const IntegralType First = _Value; |
| 118 |
typedef Pack< IntegralType , _Values ... > Rest; |
| 119 |
typedef typename Rest::Transpose::template Append< First > Transpose; |
| 120 |
|
| 121 |
static const unsigned int Size = 1 + sizeof ... ( _Values ); |
| 122 |
|
| 123 |
template< IntegralType ... __Values > using Append = Pack< IntegralType , _Value , _Values ... , __Values ... >; |
| 124 |
template< IntegralType ... __Values > using Prepend = Pack< IntegralType , __Values ... , _Value , _Values ... >; |
| 125 |
|
| 126 |
static const IntegralType Values[]; |
| 127 |
|
| 128 |
template< unsigned int I > constexpr static IntegralType Get( void ) |
| 129 |
{ |
| 130 |
if constexpr( I==0 ) return _Value; |
| 131 |
else return Rest::template Get< I-1 >(); |
| 132 |
} |
| 133 |
|
| 134 |
static constexpr IntegralType Min( void ){ return _Value < Rest::Min() ? _Value : Rest::Min(); } |
| 135 |
static constexpr IntegralType Max( void ){ return _Value > Rest::Max() ? _Value : Rest::Max(); } |
| 136 |
|
| 137 |
friend std::ostream &operator << ( std::ostream &os , Pack ) |
| 138 |
{ |
| 139 |
os << "< "; |
| 140 |
for( unsigned int i=0 ; i<Size ; i++ ) |
| 141 |
{ |
| 142 |
if( i ) os << " , "; |
| 143 |
os << Values[i]; |
| 144 |
} |
| 145 |
return os << " >"; |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
// The specialized case with one entry |
| 150 |
template< typename IntegralType , IntegralType _Value > struct Pack< IntegralType , _Value > |
| 151 |
{ |
| 152 |
static const IntegralType First = _Value; |
| 153 |
typedef Pack< IntegralType > Rest; |
| 154 |
typedef Pack< IntegralType , _Value > Transpose; |
| 155 |
|
| 156 |
static const unsigned int Size = 1; |
| 157 |
|
| 158 |
template< IntegralType ... __Values > using Append = Pack< IntegralType , _Value , __Values ... >; |
| 159 |
template< IntegralType ... __Values > using Prepend = Pack< IntegralType , __Values ... , _Value >; |
| 160 |
|
| 161 |
static const IntegralType Values[]; |
| 162 |
|
| 163 |
template< unsigned int I > constexpr static IntegralType Get( void ) |
| 164 |
{ |
| 165 |
static_assert( I==0 , "[ERROR] Pack< IntegralType , Value >::Get called with non-zero index" ); |
| 166 |
return _Value; |
| 167 |
} |
| 168 |
|
| 169 |
static constexpr IntegralType Min( void ){ return _Value; } |
| 170 |
static constexpr IntegralType Max( void ){ return _Value; } |
| 171 |
|
| 172 |
friend std::ostream &operator << ( std::ostream &os , Pack ) |
| 173 |
{ |
| 174 |
return os << "< " << First << " >"; |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
// The specialized case with no entries |
| 179 |
template< typename IntegralType > struct Pack< IntegralType > |
| 180 |
{ |
| 181 |
typedef Pack< IntegralType > Rest; |
| 182 |
static const unsigned int Size = 0; |
| 183 |
static constexpr IntegralType Values[] = { 0 }; |
| 184 |
typedef Pack< IntegralType > Transpose; |
| 185 |
template< IntegralType ... __Values > using Append = Pack< IntegralType , __Values ... >; |
| 186 |
template< IntegralType ... __Values > using Prepend = Pack< IntegralType , __Values ... >; |
| 187 |
friend std::ostream &operator << ( std::ostream &os , Pack ){ return os << "< >"; } |
| 188 |
}; |
| 189 |
|
| 190 |
template< typename IntegralType , IntegralType _Value , IntegralType ... _Values > const IntegralType Pack< IntegralType , _Value , _Values ... >::Values[] = { _Value , _Values ... }; |
| 191 |
template< typename IntegralType , IntegralType _Value > const IntegralType Pack< IntegralType , _Value >::Values[] = { _Value }; |
| 192 |
|
| 193 |
/////////////// |
| 194 |
// Selection // |
| 195 |
/////////////// |
| 196 |
template< unsigned int I , typename IntegralType , IntegralType _Value , IntegralType ... _Values > |
| 197 |
struct Selection< I , Pack< IntegralType , _Value , _Values ... > > |
| 198 |
{ |
| 199 |
static const IntegralType Value = Selection< I-1 , Pack< IntegralType , _Values ... > >::Value; |
| 200 |
typedef typename Selection< I-1 , Pack< IntegralType , _Values ... > >::Complement::template Prepend< _Value > Complement; |
| 201 |
}; |
| 202 |
|
| 203 |
template< typename IntegralType , IntegralType _Value , IntegralType ... _Values > |
| 204 |
struct Selection< 0 , Pack< IntegralType , _Value , _Values ... > > |
| 205 |
{ |
| 206 |
static const IntegralType Value = _Value; |
| 207 |
typedef Pack< IntegralType , _Values ... > Complement; |
| 208 |
}; |
| 209 |
|
| 210 |
/////////////// |
| 211 |
// Partition // |
| 212 |
/////////////// |
| 213 |
template< typename IntegralType , IntegralType ... Values > |
| 214 |
struct Partition< 0 , Pack< IntegralType , Values ... > > |
| 215 |
{ |
| 216 |
typedef Pack< IntegralType > First; |
| 217 |
typedef Pack< IntegralType , Values ... > Second; |
| 218 |
}; |
| 219 |
|
| 220 |
template< unsigned int I , typename IntegralType , IntegralType ... Values > |
| 221 |
struct Partition< I , Pack< IntegralType , Values ... > > |
| 222 |
{ |
| 223 |
typedef Concatenation< Pack< IntegralType , Pack< IntegralType , Values ... >::First > , typename Partition< I-1 , typename Pack< IntegralType , Values ... >::Rest >::First > First; |
| 224 |
typedef typename Partition< I-1 , typename Pack< IntegralType , Values ... >::Rest >::Second Second; |
| 225 |
}; |
| 226 |
|
| 227 |
//////////////// |
| 228 |
// Arithmetic // |
| 229 |
//////////////// |
| 230 |
template< typename IntegralType > |
| 231 |
struct Arithmetic< Pack< IntegralType > , Pack< IntegralType > > |
| 232 |
{ |
| 233 |
using Sum = Pack< IntegralType >; |
| 234 |
using Difference = Pack< IntegralType >; |
| 235 |
}; |
| 236 |
|
| 237 |
template< typename IntegralType , IntegralType Value1 , IntegralType ... Values1 , IntegralType Value2 , IntegralType ... Values2 > |
| 238 |
struct Arithmetic< Pack< IntegralType , Value1 , Values1 ... > , Pack< IntegralType , Value2 , Values2 ... > > |
| 239 |
{ |
| 240 |
using Sum = Concatenation< Pack< IntegralType , Value1+Value2 > , typename Arithmetic< Pack< IntegralType , Values1 ... > , Pack< IntegralType , Values2... > >::Sum >; |
| 241 |
using Difference = Concatenation< Pack< IntegralType , Value1-Value2 > , typename Arithmetic< Pack< IntegralType , Values1 ... > , Pack< IntegralType , Values2... > >::Difference >; |
| 242 |
}; |
| 243 |
|
| 244 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 245 |
typename Arithmetic< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::Sum operator + ( Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > ){ return typename Arithmetic< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::Sum(); } |
| 246 |
|
| 247 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 248 |
typename Arithmetic< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::Difference operator - ( Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > ){ return typename Arithmetic< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::Difference(); } |
| 249 |
|
| 250 |
|
| 251 |
//////////////// |
| 252 |
// Comparison // |
| 253 |
//////////////// |
| 254 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 255 |
struct Comparison< Pack< IntegralType , Values1 ... > , Pack< IntegralType , Values2 ... > > |
| 256 |
{ |
| 257 |
typedef Pack< IntegralType , Values1 ... > Pack1; |
| 258 |
typedef Pack< IntegralType , Values2 ... > Pack2; |
| 259 |
static const bool Equal = Pack1::First==Pack2::First && Comparison< typename Pack1::Rest , typename Pack2::Rest >::Equal; |
| 260 |
static const bool NotEqual = Pack1::First!=Pack2::First || Comparison< typename Pack1::Rest , typename Pack2::Rest >::NotEqual; |
| 261 |
static const bool LessThan = Pack1::First< Pack2::First && Comparison< typename Pack1::Rest , typename Pack2::Rest >::LessThan; |
| 262 |
static const bool LessThanOrEqual = Pack1::First<=Pack2::First && Comparison< typename Pack1::Rest , typename Pack2::Rest >::LessThanOrEqual; |
| 263 |
static const bool GreaterThan = Pack1::First> Pack2::First && Comparison< typename Pack1::Rest , typename Pack2::Rest >::GreaterThan; |
| 264 |
static const bool GreaterThanOrEqual = Pack1::First>=Pack2::First && Comparison< typename Pack1::Rest , typename Pack2::Rest >::GreaterThanOrEqual; |
| 265 |
}; |
| 266 |
|
| 267 |
template< typename IntegralType , IntegralType Value1 , IntegralType Value2 > |
| 268 |
struct Comparison< Pack< IntegralType , Value1 > , Pack< IntegralType , Value2 > > |
| 269 |
{ |
| 270 |
static const bool Equal = Value1==Value2; |
| 271 |
static const bool NotEqual = Value1!=Value2; |
| 272 |
static const bool LessThan = Value1<Value2; |
| 273 |
static const bool LessThanOrEqual = Value1<=Value2; |
| 274 |
static const bool GreaterThan = Value1>Value2; |
| 275 |
static const bool GreaterThanOrEqual = Value1>=Value2; |
| 276 |
}; |
| 277 |
|
| 278 |
template< typename IntegralType > |
| 279 |
struct Comparison< Pack< IntegralType > , Pack< IntegralType > > |
| 280 |
{ |
| 281 |
static const bool Equal = true; |
| 282 |
static const bool NotEqual = false; |
| 283 |
static const bool LessThan = false; |
| 284 |
static const bool LessThanOrEqual = true; |
| 285 |
static const bool GreaterThan = false; |
| 286 |
static const bool GreaterThanOrEqual = true; |
| 287 |
}; |
| 288 |
|
| 289 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 290 |
bool operator==( const Pack< IntegralType , Values1... > , const Pack< IntegralType , Values2... > ){ return Comparison< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::Equal; } |
| 291 |
|
| 292 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 293 |
bool operator!=( const Pack< IntegralType , Values1... > , const Pack< IntegralType , Values2... > ){ return Comparison< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::NotEqual; } |
| 294 |
|
| 295 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 296 |
bool operator<( const Pack< IntegralType , Values1... > , const Pack< IntegralType , Values2... > ){ return Comparison< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::LessThan; } |
| 297 |
|
| 298 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 299 |
bool operator<=( const Pack< IntegralType , Values1... > , const Pack< IntegralType , Values2... > ){ return Comparison< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::LessThanOrEqual; } |
| 300 |
|
| 301 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 302 |
bool operator>( const Pack< IntegralType , Values1... > , const Pack< IntegralType , Values2... > ){ return Comparison< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::GreaterThan; } |
| 303 |
|
| 304 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 > |
| 305 |
bool operator>=( const Pack< IntegralType , Values1... > , const Pack< IntegralType , Values2... > ){ return Comparison< Pack< IntegralType , Values1... > , Pack< IntegralType , Values2... > >::GreaterThanOrEqual; } |
| 306 |
|
| 307 |
//////////////////// |
| 308 |
// _Concatenation // |
| 309 |
//////////////////// |
| 310 |
template< typename IntegralType , IntegralType ... Values1 , IntegralType ... Values2 , typename ... Packs > |
| 311 |
struct _Concatenation< Pack< IntegralType , Values1 ... > , Pack< IntegralType , Values2 ... > , Packs ... > |
| 312 |
{ |
| 313 |
typedef typename _Concatenation< typename Pack< IntegralType , Values1 ... >::template Append< Values2 ... > , Packs ... >::type type; |
| 314 |
}; |
| 315 |
template< typename IntegralType , IntegralType ... Values > |
| 316 |
struct _Concatenation< Pack< IntegralType , Values ... > > |
| 317 |
{ |
| 318 |
typedef Pack< IntegralType , Values ... > type; |
| 319 |
}; |
| 320 |
|
| 321 |
////////////////// |
| 322 |
// _Permutation // |
| 323 |
////////////////// |
| 324 |
template< typename IntegralType , IntegralType ... Values , unsigned int ... PermutationValues > |
| 325 |
struct _Permutation< Pack< IntegralType , Values ... > , Pack< unsigned int , PermutationValues ... > > |
| 326 |
{ |
| 327 |
typedef Pack< IntegralType , PermutationValues ... > PPack; |
| 328 |
typedef Concatenation< Pack< IntegralType , Selection< PPack::First , Pack< IntegralType , Values ... > >::Value > , typename _Permutation< Pack< IntegralType , Values ... > , typename PPack::Rest >::type > type; |
| 329 |
}; |
| 330 |
template< typename IntegralType , IntegralType ... Values > |
| 331 |
struct _Permutation< Pack< IntegralType , Values ... > , Pack< unsigned int > > |
| 332 |
{ |
| 333 |
typedef Pack< IntegralType > type; |
| 334 |
}; |
| 335 |
|
| 336 |
//////////////////// |
| 337 |
// _IsotropicPack // |
| 338 |
//////////////////// |
| 339 |
template< typename IntegralType , unsigned int Dim , IntegralType Value > struct _IsotropicPack { typedef typename _IsotropicPack< IntegralType , Dim-1 , Value >::type::template Append< Value > type; }; |
| 340 |
template< typename IntegralType , IntegralType Value > struct _IsotropicPack< IntegralType , 1 , Value >{ typedef Pack< IntegralType , Value > type; }; |
| 341 |
template< typename IntegralType , IntegralType Value > struct _IsotropicPack< IntegralType , 0 , Value >{ typedef Pack< IntegralType > type; }; |
| 342 |
|
| 343 |
///////////////////// |
| 344 |
// _SequentialPack // |
| 345 |
///////////////////// |
| 346 |
template< typename IntegralType , unsigned int Dim , IntegralType Value > struct _SequentialPack { typedef Concatenation< Pack< IntegralType , Value > , typename _SequentialPack< IntegralType , Dim-1 , Value+1 >::type > type; }; |
| 347 |
template< typename IntegralType , IntegralType Value > struct _SequentialPack< IntegralType , 0 , Value >{ typedef Pack< IntegralType > type; }; |
| 348 |
} |
| 349 |
#endif // PARAMETER_PACK_INCLUDED |