File indexing completed on 2026-05-10 08:43:03
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #ifndef LLVM_ADT_COMBINATIONGENERATOR_H
0028 #define LLVM_ADT_COMBINATIONGENERATOR_H
0029
0030 #include "llvm/ADT/ArrayRef.h"
0031 #include "llvm/ADT/STLFunctionalExtras.h"
0032 #include "llvm/ADT/SmallVector.h"
0033 #include <cassert>
0034 #include <cstring>
0035
0036 namespace llvm {
0037
0038 template <typename choice_type, typename choices_storage_type,
0039 int variable_smallsize>
0040 class CombinationGenerator {
0041 template <typename T> struct WrappingIterator {
0042 using value_type = T;
0043
0044 const ArrayRef<value_type> Range;
0045 typename decltype(Range)::const_iterator Position;
0046
0047
0048 void rewind() { Position = Range.begin(); }
0049
0050
0051
0052 bool advance() {
0053 ++Position;
0054 bool Wrapped = Position == Range.end();
0055 if (Wrapped)
0056 rewind();
0057 return Wrapped;
0058 }
0059
0060
0061 const value_type &operator*() const { return *Position; }
0062
0063 WrappingIterator(ArrayRef<value_type> Range_) : Range(Range_) {
0064 assert(!Range.empty() && "The range must not be empty.");
0065 rewind();
0066 }
0067 };
0068
0069 const ArrayRef<choices_storage_type> VariablesChoices;
0070
0071 void performGeneration(
0072 const function_ref<bool(ArrayRef<choice_type>)> Callback) const {
0073 SmallVector<WrappingIterator<choice_type>, variable_smallsize>
0074 VariablesState;
0075
0076
0077
0078
0079
0080 auto IncrementState =
0081 [](MutableArrayRef<WrappingIterator<choice_type>> VariablesState)
0082 -> bool {
0083 for (WrappingIterator<choice_type> &Variable :
0084 llvm::reverse(VariablesState)) {
0085 bool Wrapped = Variable.advance();
0086 if (!Wrapped)
0087 return false;
0088
0089 }
0090 return true;
0091 };
0092
0093
0094
0095 VariablesState.reserve(VariablesChoices.size());
0096 for (ArrayRef<choice_type> VC : VariablesChoices)
0097 VariablesState.emplace_back(VC);
0098
0099
0100 SmallVector<choice_type, variable_smallsize> CurrentCombination;
0101 CurrentCombination.resize(VariablesState.size());
0102
0103 while (true) {
0104
0105 for (auto I : llvm::zip(VariablesState, CurrentCombination))
0106 std::get<1>(I) = *std::get<0>(I);
0107
0108 if (Callback(CurrentCombination))
0109 return;
0110
0111 if (IncrementState(VariablesState))
0112 return;
0113 }
0114 };
0115
0116 public:
0117 CombinationGenerator(ArrayRef<choices_storage_type> VariablesChoices_)
0118 : VariablesChoices(VariablesChoices_) {
0119 #ifndef NDEBUG
0120 assert(!VariablesChoices.empty() && "There should be some variables.");
0121 llvm::for_each(VariablesChoices, [](ArrayRef<choice_type> VariableChoices) {
0122 assert(!VariableChoices.empty() &&
0123 "There must always be some choice, at least a placeholder one.");
0124 });
0125 #endif
0126 }
0127
0128
0129
0130 size_t numCombinations() const {
0131 size_t NumVariants = 1;
0132 for (ArrayRef<choice_type> VariableChoices : VariablesChoices)
0133 NumVariants *= VariableChoices.size();
0134 assert(NumVariants >= 1 &&
0135 "We should always end up producing at least one combination");
0136 return NumVariants;
0137 }
0138
0139
0140
0141 void generate(const function_ref<bool(ArrayRef<choice_type>)> Callback) {
0142 performGeneration(Callback);
0143 }
0144 };
0145
0146 }
0147
0148 #endif