update
This commit is contained in:
Binary file not shown.
BIN
build_linux64/objstore/permutation_basic.o
Normal file
BIN
build_linux64/objstore/permutation_basic.o
Normal file
Binary file not shown.
@ -11,7 +11,25 @@ namespace ams
|
||||
namespace perm
|
||||
{
|
||||
|
||||
//Buffer operations
|
||||
//Basic operations on preallocated buffers
|
||||
//for later C library export
|
||||
|
||||
int factorial(int n);
|
||||
|
||||
int index_to_mindex(int index, int *mindex, int ndim);
|
||||
|
||||
int mindex_to_index(int *mindex, int ndim);
|
||||
void mindex_to_perm(int *mindex, int *perm, int ndim);
|
||||
void perm_to_mindex(int *perm, int *mindex, int ndim);
|
||||
|
||||
bool perm_valid(int *perm, int ndim);
|
||||
|
||||
bool mindex_valid(int *mindex, int ndim);
|
||||
|
||||
void index_to_perm(int index, int *perm, int ndim);
|
||||
int perm_to_index(int *perm, int ndim);
|
||||
|
||||
|
||||
|
||||
|
||||
//Permutation class
|
||||
@ -22,6 +40,7 @@ public:
|
||||
int *data;
|
||||
|
||||
permutation();
|
||||
permutation(int _dim);
|
||||
~permutation();
|
||||
};
|
||||
|
||||
|
106
src/amscppperm1/permutation_basic.cpp
Normal file
106
src/amscppperm1/permutation_basic.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
#include <amscppperm1/amscppperm1.hpp>
|
||||
|
||||
namespace ams
|
||||
{
|
||||
namespace perm
|
||||
{
|
||||
|
||||
//basic factorial found with integer loop
|
||||
int factorial(int n)
|
||||
{
|
||||
int I;
|
||||
int ret = 0;
|
||||
if(n>=0)
|
||||
{
|
||||
ret = 1;
|
||||
for(I=0;I<n;I++)
|
||||
{
|
||||
ret = ret*(I+1);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
//returns a factoriadic multi-index for a given index.
|
||||
//Each factoriadic multi-index corresponds to a permutation according to a simple algorithm
|
||||
// factoriadic indices range from (ex 4 dim case) {0,0,0,0} to {3,2,1,0}
|
||||
// parameters:
|
||||
// index - [0 to factorial(ndim)) index of permutation
|
||||
// mindex - factoriadic multi-index output (size=ndim)
|
||||
// ndim - dimension of the permutation (number of elements to permute)
|
||||
// return value:
|
||||
// 0: success
|
||||
// -1 : privided index was out of the valid range
|
||||
int index_to_mindex(int index, int *mindex, int ndim)
|
||||
{
|
||||
int ret = -1;
|
||||
int I,J,K;
|
||||
if(index>=0 && index<factorial(ndim))
|
||||
{
|
||||
ret = 0; //success
|
||||
K = ndim;
|
||||
J = index;
|
||||
for(I=0;I<ndim;I++)
|
||||
{
|
||||
mindex[I] = J%K;
|
||||
J = (J - mindex[I])/K;
|
||||
K = K-1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1; //error
|
||||
//invalid index
|
||||
for(I=0;I<ndim;I++)
|
||||
mindex[I] = -1;
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//Is the multi-index in the appropriate factoriadic range?
|
||||
// position 0 can range from [0 to ndim)
|
||||
// position 1 can range from [0 to (ndim-1))
|
||||
bool mindex_valid(int *mindex, int ndim)
|
||||
{
|
||||
int I;
|
||||
bool ret = 1;
|
||||
for(I=0;I<ndim;I++)
|
||||
{
|
||||
if(mindex[I]<0 || mindex[I]>=(ndim-I))
|
||||
{
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mindex_to_index(int *mindex, int ndim)
|
||||
{
|
||||
int ret = -1;
|
||||
int I;
|
||||
int K;
|
||||
if(mindex_valid(mindex,ndim))
|
||||
{
|
||||
K = 1;
|
||||
ret = 0;
|
||||
for(I=0;I<ndim;I++)
|
||||
{
|
||||
ret = ret + mindex[I]*K;
|
||||
K = K*(ndim-I);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}; //end namespace perm
|
||||
}; //end namespace ams
|
Reference in New Issue
Block a user