1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
#include <iostream>
#include <string>
#include <vector>
#include <map>
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
#define __COMBI_WITH_EXCLUSION_OF_PREVIOUS_CARDINALITY__ 1
//#define __DEBUG__ 1
using namespace std;
enum STATES {E1 = 1, E2 = 2, LAST};
static ushort nbStates = 2;
//enum STATES {E1 = 1, E2 = 2, E3 = 3, LAST};
//static ushort nbStates = 3;
ulong pow(ushort n, ushort k){
ulong res = n;
for (ushort i = 1; i < k; ++i)
res *= n;
return res;
}
class MyObject{
friend ostream& operator<< (ostream& stream, const MyObject& obj){cout << obj.name /*<< obj.type*/; return stream;}
public:
MyObject():name() {}
MyObject(const string &str):name(str){}
bool operator==(const MyObject &other) { return name == other.name;}
private:
string name;
};
template<typename T> struct Resultats{
map<ushort, vector<vector<pair<T*, ushort>> > > var_KO;
map<ushort, vector<vector<pair<T*, ushort>> > > var_OK;
};
static Resultats<MyObject> resultats;
template<typename T> bool combinationIsOK(pair<T*, ushort> *combiToTest, ushort K)
{
// TODO: if resultats.var_KO[K].size() < resultats.var_OK[K].size()
// else we should loop on resultats.var_OK[K] ;)
for (auto & variantKO : resultats.var_KO[K])
{
bool isMatching = true;
for (ushort i = 0 ; i < K ; ++i)
{
if (variantKO[i] != combiToTest[i])
{
isMatching = false;
break;
}
}
if (isMatching)
return false;
}
return true;
}
template<typename T> void printCombiVariant(const char* title, pair<T*, ushort> *newVar, ushort K)
{
cout << title << " ";
for (int i = 0 ; i < K ; ++i)
cout << *(newVar[i].first) << "_" << newVar[i].second << ", ";
cout << "\n";
}
template<typename T> vector<pair<T*, ushort>*> *getStateVariationsKminus1(
const vector<T*> &combi,
ushort K)
{
// 1.: Initialize the output (and reserve memory)
vector<pair<T*, ushort>*> *variants = new vector<pair<T*, ushort>*>();
variants->reserve(pow(nbStates, K));
// 2.: Initialize the list with all elements in the first state
variants->push_back(new pair<T*, ushort>[K]);
for (ushort i = 0 ; i < K ; ++i){
(*variants)[0][i] = {combi[i], E1};
}
// 3.: Do the job!
for (ushort indexElem = 0 ; indexElem < K ; ++indexElem)
{// for each element
T *elem = combi[indexElem];
ushort nbExistingVariants = variants->size();
vector<pair<T*, ushort>*> elemVariants;
for (ushort state = E1 + 1 ; state != LAST ; ++state)
{ // for all other state
for (int i = 0 ; i < nbExistingVariants ; ++i)
{ // for all the variants
pair<T*, ushort> *exitingVar = (*variants)[i];
ushort index = 0;
bool containElem = false;
for (; index < K; ++index)
{ // find the current element in the combination
if (exitingVar[index].first == elem)
{
containElem = true;
break;
}
}
if (containElem)
{ // we found the element so we double the combination with the other state
pair<T*, ushort> *newVar = new pair<T*, ushort>[K];
for (int k = 0 ; k < K ; ++k)
newVar[k] = exitingVar[k];
newVar[index].second = state;
elemVariants.push_back(newVar);
}
}
}
for (pair<T*, ushort> *newVar : elemVariants)
{
if (combinationIsOK(newVar, K))
{
#ifdef __DEBUG__
printCombiVariant("Adding combi: ", newVar, K);
#endif
variants->push_back(newVar);
}
}
}
// 4.: check if we should remove the first variantion
if (!combinationIsOK((*variants)[0], K))
{
auto it = variants->begin();
delete *it;
variants->erase(it);
#ifdef __DEBUG__
cout << "First combi is not OK...\n";
#endif
}
// 5.: if the list is empty, free the memory
if (variants->size() == 0)
{
delete variants;
variants = nullptr;
}
return variants;
}
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
K ---> Size of a combination to be printed */
template<typename T> void combinationWithStateAndSomeExclusions(
const vector<T*> &elements,
vector<T*> &data,
int start,
int end,
int index,
int K,
vector<pair<T*, ushort>*> *variantsKminus1)
{
if (index == K - 1)
{
#ifdef __DEBUG__
cout << "[DEBUG] index = K-1 >>>>>>>>>\n";
#endif
variantsKminus1 = getStateVariationsKminus1(data, index);
}
// Current combination is ready to be printed, print it
else if (index == K)
{
if (variantsKminus1)
{
MyObject *lastElem = data[K-1];
for (uint i = 0 ; i < variantsKminus1->size() ; ++i)
{
pair<T*, ushort> *variant = (*variantsKminus1)[i];
// we need to add all the variant of the last element missing
// if the combination including it from the end is OK
for (ushort state = E1 ; state != LAST ; ++state)
{
pair<T*, ushort> *lastVar = new pair<T*, ushort>[K-1];
for (ushort i = 1 ; i < K-1 ; ++i)
lastVar[i-1] = variant[i];
lastVar[K-2] = {lastElem, state};
if (combinationIsOK(lastVar, K-1))
{
for (ushort i = 0 ; i < K-1 ; ++i)
{
cout << *(variant[i].first) << "_" << variant[i].second << " ";
}
cout << " " << *lastElem << "_" << state << "\n";
}
}
}
}
return;
}
// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= K-index; i++)
{
data[index] = elements[i];
combinationWithStateAndSomeExclusions(elements, data, i+1, end, index+1, K, variantsKminus1);
}
if (variantsKminus1 && index == K - 1)
{
for (uint i = 0 ; i < variantsKminus1->size() ; ++i)
delete[] (*variantsKminus1)[i];
variantsKminus1->clear();
delete variantsKminus1;
variantsKminus1 = nullptr;
#ifdef __DEBUG__
cout << "[DEBUG] index = K-1 <<<<<<<<\n";
#endif
}
}
template<typename T> void combinationWithState(
const vector<T*> &elements,
vector<T*> &data,
int start,
int end,
int index,
int K,
vector<pair<T*, ushort>*> *variantsKminus1);
template<typename T> void printCombination(const vector<T*> &elements, int N, int K)
{
// A temporary array to store all combination one by one
vector<T*> data;
data.reserve(K);
// Print all combination using temprary array 'data[]'
vector<pair<T*, ushort>*> *variantsKminus1 = nullptr;
#ifdef __COMBI_WITH_EXCLUSION_OF_PREVIOUS_CARDINALITY__
combinationWithStateAndSomeExclusions(elements, data, 0, N-1, 0, K, variantsKminus1);
#else
combinationWithState(elements, data, 0, N-1, 0, K, variantsKminus1);
#endif
}
int main()
{
vector<MyObject*> elements = {
new MyObject("A")
, new MyObject("B")
, new MyObject("C")
, new MyObject("D")
};
int K = 2;
resultats.var_KO[1].push_back({ {elements[0], E1} }); // exlude A_1
resultats.var_KO[1].push_back({ {elements[1], E2} }); // exclude B_2
resultats.var_KO[1].push_back({ {elements[2], E1} }); // exclude C_1
printCombination(elements, elements.size(), K);
for (uint k = 0 ; k < elements.size(); ++k)
delete elements[k];
return 0;
}
template<typename T> vector<pair<T*, ushort>*> *getStateVariations(const vector<T*> &combi, int K)
{
uint nbVariants = pow(nbStates, K);
// Initialize the output (reserve memory)
vector<pair<T*, ushort>*> *variants = new vector<pair<T*, ushort>*>();
variants->reserve(nbVariants);
for (uint i = 0 ; i < nbVariants ; ++i)
(*variants)[i] = new pair<T*, ushort>[K];
// Initialize the list with all elements in the first state
ushort indexNewVar = 0;
for (ushort i = 0 ; i < K ; ++i){
(*variants)[indexNewVar][i] = {combi[i], E1};
}
++indexNewVar;
for (ushort indexElem = 0 ; indexElem < K ; ++indexElem)
{// for each element
T *elem = combi[indexElem];
vector<pair<T*, ushort>*> elemVariants;
for (ushort state = E1 + 1 ; state != LAST ; ++state)
{ // for all other state
for (int i = 0 ; i < indexNewVar ; ++i)
{ // for all the variants
pair<T*, ushort> *exitingVar = (*variants)[i];
ushort index = 0;
bool containElem = false;
for (; index < K; ++index)
{ // find the current element in the combination
if (exitingVar[index].first == elem)
{
containElem = true;
break;
}
}
if (containElem)
{ // we found the element so we double the combination with the other state
pair<T*, ushort> *newVar = new pair<T*, ushort>[K];
for (int k = 0 ; k < K ; ++k)
newVar[k] = exitingVar[k];
newVar[index].second = state;
elemVariants.push_back(newVar);
}
}
}
for (pair<T*, ushort> *newVar : elemVariants)
(*variants)[indexNewVar++] = newVar;
}
return variants;
}
template<typename T> void combinationWithState(
const vector<T*> &elements,
vector<T*> &data,
int start,
int end,
int index,
int K,
vector<pair<T*, ushort>*> *variantsKminus1)
{
// Current combination is ready to be printed, print it
if (index == K)
{
variantsKminus1 = getStateVariations(data, index);
uint nbVariants = pow(nbStates, index);
for (uint i = 0 ; i < nbVariants ; ++i)
{
pair<T*, ushort> *variant = (*variantsKminus1)[i];
for (ushort i = 0 ; i < index ; i++)
{
cout << *(variant[i].first) << "_" << variant[i].second << " ";
}
delete[] variant;
cout << "\n";
}
variantsKminus1->clear();
delete variantsKminus1;
variantsKminus1 = nullptr;
return;
}
// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= K-index; i++)
{
data[index] = elements[i];
combinationWithState(elements, data, i+1, end, index+1, K, variantsKminus1);
}
} |
Partager