1 #ifndef _VKTPIPELINECOMBINATIONSITERATOR_HPP
2 #define _VKTPIPELINECOMBINATIONSITERATOR_HPP
3 /*------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
7 * Copyright (c) 2015 The Khronos Group Inc.
8 * Copyright (c) 2015 Imagination Technologies Ltd.
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and/or associated documentation files (the
12 * "Materials"), to deal in the Materials without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sublicense, and/or sell copies of the Materials, and to
15 * permit persons to whom the Materials are furnished to do so, subject to
16 * the following conditions:
18 * The above copyright notice(s) and this permission notice shall be included
19 * in all copies or substantial portions of the Materials.
21 * The Materials are Confidential Information as defined by the
22 * Khronos Membership Agreement until designated non-confidential by Khronos,
23 * at which point this condition clause shall be removed.
25 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
29 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
30 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
35 * \brief Iterator over combinations of items without repetition
36 *//*--------------------------------------------------------------------*/
38 #include "tcuDefs.hpp"
39 #include "deRandom.hpp"
49 class CombinationsIterator
52 CombinationsIterator (deUint32 numItems, deUint32 combinationSize);
53 virtual ~CombinationsIterator (void) {}
54 bool hasNext (void) const;
59 virtual T getCombinationValue (const std::vector<deUint32>& combination) = 0;
62 static deUint32 factorial (deUint32 x);
65 deUint32 m_combinationIndex;
66 deUint32 m_combinationSize;
67 deUint32 m_combinationCount;
69 std::vector<deUint32> m_combination;
72 static deUint32 seriesProduct (deUint32 first, deUint32 last)
76 for (deUint32 i = first; i <= last; i++)
83 CombinationsIterator<T>::CombinationsIterator (deUint32 numItems, deUint32 combinationSize)
84 : m_numItems (numItems)
85 , m_combinationSize (combinationSize)
87 DE_ASSERT(m_combinationSize > 0);
88 DE_ASSERT(m_combinationSize <= m_numItems);
90 m_combinationCount = seriesProduct(numItems - combinationSize + 1, numItems) / seriesProduct(1, combinationSize);
92 m_combination.resize(m_combinationSize);
97 bool CombinationsIterator<T>::hasNext (void) const
99 return m_combinationIndex < m_combinationCount;
102 template <typename T>
103 T CombinationsIterator<T>::next (void)
105 DE_ASSERT(m_combinationIndex < m_combinationCount);
107 if (m_combinationIndex > 0)
109 for (int combinationItemNdx = (int)m_combinationSize - 1; combinationItemNdx >= 0; combinationItemNdx--)
111 if ((m_combination[combinationItemNdx] + 1 < m_numItems) && ((combinationItemNdx == (int)m_combinationSize - 1) || (m_combination[combinationItemNdx + 1] > m_combination[combinationItemNdx] + 1)))
113 m_combination[combinationItemNdx]++;
115 for (deUint32 resetNdx = combinationItemNdx + 1; resetNdx < m_combinationSize; resetNdx++)
116 m_combination[resetNdx] = m_combination[resetNdx - 1] + 1;
123 m_combinationIndex++;
125 return getCombinationValue(m_combination);
128 template <typename T>
129 void CombinationsIterator<T>::reset (void)
131 // Set up first combination
132 for (deUint32 itemNdx = 0; itemNdx < m_combinationSize; itemNdx++)
133 m_combination[itemNdx] = itemNdx;
135 m_combinationIndex = 0;
138 template <typename T>
139 deUint32 CombinationsIterator<T>::factorial (deUint32 x)
143 for (deUint32 value = x; value > 1; value--)
152 #endif // _VKTPIPELINECOMBINATIONSITERATOR_HPP