-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utils.inl
110 lines (97 loc) · 3.67 KB
/
Utils.inl
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
template <typename T>
inline void GeneratePermutations(std::vector<std::vector<T>>& vResults, const std::vector<T>& vStartingCombination)
{
std::vector<T> vCurrentResult(vStartingCombination);
do
{
vResults.push_back(vCurrentResult);
} while (std::next_permutation(vCurrentResult.begin(), vCurrentResult.end()));
}
template <typename T>
inline void GenerateCombinationsAndPermutationsHelper(std::vector<std::vector<T>>& vResults, const std::vector<T>& vCurrentResult, const std::vector<T>& vObjects, int nResultSlots, int nStartSlot)
{
if (nStartSlot >= static_cast<int>(vObjects.size()))
return;
for (int nSlot = nStartSlot ; nSlot < static_cast<int>(vObjects.size()) ; ++nSlot)
{
std::vector<T> vMyResult = vCurrentResult;
vMyResult.push_back(vObjects[nSlot]);
if (static_cast<int>(vMyResult.size()) >= nResultSlots)
{
GeneratePermutations(vResults);
}
else
{
GenerateCombinationsAndPermutationsHelper(vResults, vMyResult, vObjects, nResultSlots, nSlot + 1);
}
}
}
template <typename T>
inline std::vector<std::vector<T>> GenerateCombinationsAndPermutations(const std::vector<T>& vObjects, int nResultSlots)
{
std::vector<std::vector<T>> vResults;
if (vObjects.empty() || nResultSlots <= 0)
return vResults;
std::vector<T> vCurrentResult;
GenerateCombinationsAndPermutationsHelper(vResults, vCurrentResult, vObjects, nResultSlots, 0);
return vResults;
}
template <typename T>
inline void GeneratePermutationsStaticMemory(std::vector<T*>& vResults, T* pStartingCombination, int nResultSlots)
{
std::vector<T> vCurrentResult(nResultSlots);
for (int nSlot = 0 ; nSlot < nResultSlots ; ++nSlot)
{
vCurrentResult[nSlot] = pStartingCombination[nSlot];
}
do
{
T* pNewResult = CMemory<T>::Alloc(nResultSlots);
memcpy(pNewResult, vCurrentResult.data(), nResultSlots * sizeof(T));
vResults.push_back(pNewResult);
} while (std::next_permutation(vCurrentResult.begin(), vCurrentResult.end()));
}
template <typename T, bool t_bPermutations>
inline void GenerateCombinationsAndPermutationsStaticMemoryHelper(std::vector<T*>& vResults, T* pCurrentResult, int nCurrentResultSize, const std::vector<T>& vObjects, int nResultSlots, int nStartSlot)
{
if (nStartSlot >= static_cast<int>(vObjects.size()))
return;
for (int nSlot = nStartSlot ; nSlot < static_cast<int>(vObjects.size()) ; ++nSlot)
{
const int nSlotsLeft = static_cast<int>(vObjects.size()) - nSlot;
if (nSlotsLeft + nCurrentResultSize < nResultSlots)
{
return;
}
pCurrentResult[nCurrentResultSize] = vObjects[nSlot];
if (nCurrentResultSize + 1 >= nResultSlots)
{
if (t_bPermutations)
{
GeneratePermutationsStaticMemory(vResults, pCurrentResult, nResultSlots);
}
else
{
T* pNewResult = CMemory<T>::Alloc(nResultSlots);
memcpy(pNewResult, pCurrentResult, nResultSlots * sizeof(T));
vResults.push_back(pNewResult);
}
}
else
{
GenerateCombinationsAndPermutationsStaticMemoryHelper<T, t_bPermutations>(vResults, pCurrentResult, nCurrentResultSize + 1, vObjects, nResultSlots, nSlot + 1);
}
}
}
template <typename T, bool t_bPermutations>
inline std::vector<T*> GenerateCombinationsAndPermutationsStaticMemory(const std::vector<T>& vObjects, int nResultSlots)
{
std::vector<T*> vResults;
if (vObjects.empty() || nResultSlots <= 0)
return vResults;
std::vector<T> vSortedObjects = vObjects;
std::sort(vSortedObjects.begin(), vSortedObjects.end());
std::vector<T> vCurrentResult(nResultSlots);
GenerateCombinationsAndPermutationsStaticMemoryHelper<T, t_bPermutations>(vResults, vCurrentResult.data(), 0, vSortedObjects, nResultSlots, 0);
return vResults;
}