mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Add RandomPairSelection and PairMosaicSelection classes
This commit is contained in:
@@ -16,3 +16,50 @@
|
||||
*/
|
||||
|
||||
#include <tools/yulPhaser/PairSelections.h>
|
||||
|
||||
#include <tools/yulPhaser/SimulationRNG.h>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
vector<tuple<size_t, size_t>> RandomPairSelection::materialise(size_t _poolSize) const
|
||||
{
|
||||
if (_poolSize < 2)
|
||||
return {};
|
||||
|
||||
size_t count = static_cast<size_t>(round(_poolSize * m_selectionSize));
|
||||
|
||||
vector<tuple<size_t, size_t>> selection;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
size_t index1 = SimulationRNG::uniformInt(0, _poolSize - 1);
|
||||
size_t index2;
|
||||
do
|
||||
{
|
||||
index2 = SimulationRNG::uniformInt(0, _poolSize - 1);
|
||||
} while (index1 == index2);
|
||||
|
||||
selection.push_back({index1, index2});
|
||||
}
|
||||
|
||||
return selection;
|
||||
}
|
||||
|
||||
vector<tuple<size_t, size_t>> PairMosaicSelection::materialise(size_t _poolSize) const
|
||||
{
|
||||
if (_poolSize < 2)
|
||||
return {};
|
||||
|
||||
size_t count = static_cast<size_t>(round(_poolSize * m_selectionSize));
|
||||
|
||||
vector<tuple<size_t, size_t>> selection;
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
tuple<size_t, size_t> pair = m_pattern[i % m_pattern.size()];
|
||||
selection.push_back({min(get<0>(pair), _poolSize - 1), min(get<1>(pair), _poolSize - 1)});
|
||||
}
|
||||
|
||||
return selection;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
@@ -50,4 +51,49 @@ public:
|
||||
virtual std::vector<std::tuple<size_t, size_t>> materialise(size_t _poolSize) const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* A selection that selects pairs of random elements from a container. The resulting set of pairs
|
||||
* may contain the same pair more than once but does not contain pairs of duplicates. Always
|
||||
* selects as many pairs as the size of the container multiplied by @a _selectionSize (unless the
|
||||
* container is empty).
|
||||
*/
|
||||
class RandomPairSelection: public PairSelection
|
||||
{
|
||||
public:
|
||||
explicit RandomPairSelection(double _selectionSize):
|
||||
m_selectionSize(_selectionSize) {}
|
||||
|
||||
std::vector<std::tuple<size_t, size_t>> materialise(size_t _poolSize) const override;
|
||||
|
||||
private:
|
||||
double m_selectionSize;
|
||||
};
|
||||
|
||||
/**
|
||||
* A selection that selects pairs of elements at specific, fixed positions indicated by a repeating
|
||||
* "pattern". If the positions in the pattern exceed the size of the container, they are capped at
|
||||
* the maximum available position. Always selects as many pairs as the size of the container
|
||||
* multiplied by @a _selectionSize (unless the container is empty).
|
||||
*
|
||||
* E.g. if the pattern is {{0, 1}, {3, 9}} and collection size is 5, the selection will materialise
|
||||
* into {{0, 1}, {3, 4}, {0, 1}, {3, 4}, {0, 1}}. If the size is 3, it will be
|
||||
* {{0, 1}, {2, 2}, {0, 1}}.
|
||||
*/
|
||||
class PairMosaicSelection: public PairSelection
|
||||
{
|
||||
public:
|
||||
explicit PairMosaicSelection(std::vector<std::tuple<size_t, size_t>> _pattern, double _selectionSize = 1.0):
|
||||
m_pattern(move(_pattern)),
|
||||
m_selectionSize(_selectionSize)
|
||||
{
|
||||
assert(m_pattern.size() > 0 || _selectionSize == 0.0);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<size_t, size_t>> materialise(size_t _poolSize) const override;
|
||||
|
||||
private:
|
||||
std::vector<std::tuple<size_t, size_t>> m_pattern;
|
||||
double m_selectionSize;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user