Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:03

0001 //===-- llvm/ADT/CombinationGenerator.h ------------------------*- C++ -*--===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// Combination generator.
0011 ///
0012 /// Example: given input {{0, 1}, {2}, {3, 4}} it will produce the following
0013 /// combinations: {0, 2, 3}, {0, 2, 4}, {1, 2, 3}, {1, 2, 4}.
0014 ///
0015 /// It is useful to think of input as vector-of-vectors, where the
0016 /// outer vector is the variable space, and inner vector is choice space.
0017 /// The number of choices for each variable can be different.
0018 ///
0019 /// As for implementation, it is useful to think of this as a weird number,
0020 /// where each digit (==variable) may have different base (==number of choices).
0021 /// Thus modelling of 'produce next combination' is exactly analogous to the
0022 /// incrementing of an number - increment lowest digit (pick next choice for the
0023 /// variable), and if it wrapped to the beginning then increment next digit.
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     // Rewind the tape, placing the position to again point at the beginning.
0048     void rewind() { Position = Range.begin(); }
0049 
0050     // Advance position forward, possibly wrapping to the beginning.
0051     // Returns whether the wrap happened.
0052     bool advance() {
0053       ++Position;
0054       bool Wrapped = Position == Range.end();
0055       if (Wrapped)
0056         rewind();
0057       return Wrapped;
0058     }
0059 
0060     // Get the value at which we are currently pointing.
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     // 'increment' of the whole VariablesState is defined identically to the
0077     // increment of a number: starting from the least significant element,
0078     // increment it, and if it wrapped, then propagate that carry by also
0079     // incrementing next (more significant) element.
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; // There you go, next combination is ready.
0088         // We have carry - increment more significant variable next..
0089       }
0090       return true; // MSB variable wrapped, no more unique combinations.
0091     };
0092 
0093     // Initialize the per-variable state to refer to the possible choices for
0094     // that variable.
0095     VariablesState.reserve(VariablesChoices.size());
0096     for (ArrayRef<choice_type> VC : VariablesChoices)
0097       VariablesState.emplace_back(VC);
0098 
0099     // Temporary buffer to store each combination before performing Callback.
0100     SmallVector<choice_type, variable_smallsize> CurrentCombination;
0101     CurrentCombination.resize(VariablesState.size());
0102 
0103     while (true) {
0104       // Gather the currently-selected variable choices into a vector.
0105       for (auto I : llvm::zip(VariablesState, CurrentCombination))
0106         std::get<1>(I) = *std::get<0>(I);
0107       // And pass the new combination into callback, as intended.
0108       if (/*Abort=*/Callback(CurrentCombination))
0109         return;
0110       // And tick the state to next combination, which will be unique.
0111       if (IncrementState(VariablesState))
0112         return; // All combinations produced.
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   // How many combinations can we produce, max?
0129   // This is at most how many times the callback will be called.
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   // Actually perform exhaustive combination generation.
0140   // Each result will be passed into the callback.
0141   void generate(const function_ref<bool(ArrayRef<choice_type>)> Callback) {
0142     performGeneration(Callback);
0143   }
0144 };
0145 
0146 } // namespace llvm
0147 
0148 #endif