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 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
31 * \brief Iterator over combinations of items without repetition
32 *//*--------------------------------------------------------------------*/
34 #include "tcuDefs.hpp"
35 #include "deRandom.hpp"
45 class CombinationsIterator
48 CombinationsIterator (deUint32 numItems, deUint32 combinationSize);
49 virtual ~CombinationsIterator (void) {}
50 bool hasNext (void) const;
55 virtual T getCombinationValue (const std::vector<deUint32>& combination) = 0;
58 static deUint32 factorial (deUint32 x);
61 deUint32 m_combinationIndex;
62 deUint32 m_combinationSize;
63 deUint32 m_combinationCount;
65 std::vector<deUint32> m_combination;
68 static deUint32 seriesProduct (deUint32 first, deUint32 last)
72 for (deUint32 i = first; i <= last; i++)
79 CombinationsIterator<T>::CombinationsIterator (deUint32 numItems, deUint32 combinationSize)
80 : m_numItems (numItems)
81 , m_combinationSize (combinationSize)
83 DE_ASSERT(m_combinationSize > 0);
84 DE_ASSERT(m_combinationSize <= m_numItems);
86 m_combinationCount = seriesProduct(numItems - combinationSize + 1, numItems) / seriesProduct(1, combinationSize);
88 m_combination.resize(m_combinationSize);
93 bool CombinationsIterator<T>::hasNext (void) const
95 return m_combinationIndex < m_combinationCount;
99 T CombinationsIterator<T>::next (void)
101 DE_ASSERT(m_combinationIndex < m_combinationCount);
103 if (m_combinationIndex > 0)
105 for (int combinationItemNdx = (int)m_combinationSize - 1; combinationItemNdx >= 0; combinationItemNdx--)
107 if ((m_combination[combinationItemNdx] + 1 < m_numItems) && ((combinationItemNdx == (int)m_combinationSize - 1) || (m_combination[combinationItemNdx + 1] > m_combination[combinationItemNdx] + 1)))
109 m_combination[combinationItemNdx]++;
111 for (deUint32 resetNdx = combinationItemNdx + 1; resetNdx < m_combinationSize; resetNdx++)
112 m_combination[resetNdx] = m_combination[resetNdx - 1] + 1;
119 m_combinationIndex++;
121 return getCombinationValue(m_combination);
124 template <typename T>
125 void CombinationsIterator<T>::reset (void)
127 // Set up first combination
128 for (deUint32 itemNdx = 0; itemNdx < m_combinationSize; itemNdx++)
129 m_combination[itemNdx] = itemNdx;
131 m_combinationIndex = 0;
134 template <typename T>
135 deUint32 CombinationsIterator<T>::factorial (deUint32 x)
139 for (deUint32 value = x; value > 1; value--)
148 #endif // _VKTPIPELINECOMBINATIONSITERATOR_HPP