1#ifndef ROSE_Combinatorics_H
2#define ROSE_Combinatorics_H
4#include <rosePublicConfig.h>
6#include <Rose/Constants.h>
7#include <ROSE_DEPRECATED.h>
8#include <ROSE_UNUSED.h>
16#include <Rose/Exception.h>
17#include <Sawyer/Assert.h>
18#include <Sawyer/Synchronization.h>
24#ifdef ROSE_HAVE_LIBGCRYPT
31namespace Combinatorics {
39 const T next = retval * n--;
40 assert(next > retval);
58permute(std::vector<T> &values, uint64_t pn,
const size_t sz =
UNLIMITED) {
61 assert(sz <= values.size());
62 assert(pn < factorial(sz));
63 for (
size_t i = 0; i < sz; ++i) {
64 uint64_t radix = sz - i;
65 uint64_t idx = pn % radix;
66 std::swap(values[i + idx], values[i]);
79 nitems = std::min(nitems, vector.size());
80 limit = std::min(limit, nitems);
82 for (
size_t i=0; i<limit; ++i) {
84 std::swap(vector[i], vector[j]);
115reorder(std::vector<T> &values,
const std::vector<size_t> &remap) {
116 assert(values.size() == remap.size());
119 std::vector<T> old = values;
120 for (
size_t i = 0; i < old.size(); ++i)
121 values[i] = old[remap[i]];
115reorder(std::vector<T> &values,
const std::vector<size_t> &remap) {
…}
219 void insert(
const uint8_t *bytes,
size_t nBytes);
220 void insert(
const std::vector<uint8_t>&);
228 virtual void append(
const uint8_t *message,
size_t messageSize) = 0;
250 uint64_t make64Bits() ROSE_DEPRECATED("use toU64");
251 uint64_t make64Bits(const
Digest&) ROSE_DEPRECATED("use toU64");
257 void print(std::ostream&);
266 virtual std::shared_ptr<Hasher> create()
const = 0;
291 HasherFactory::instance().registerMaker(hashType,
this);
297 virtual std::shared_ptr<Hasher>
create()
const {
298 return std::make_shared<T>();
297 virtual std::shared_ptr<Hasher>
create()
const {
…}
316 static HasherFactory& Instance() ROSE_DEPRECATED("use instance");
321 void registerMaker(const std::
string& hashType,
IHasherMaker* createHasherPtr);
328 std::shared_ptr<
Hasher> createHasher(const std::
string& hashType) const;
338 std::map<std::string, IHasherMaker* > hashMakers;
347template<
int hashAlgorithmId>
349#ifdef ROSE_HAVE_LIBGCRYPT
355 #ifdef ROSE_HAVE_LIBGCRYPT
356 if (gcry_md_open(&md_, hashAlgorithmId, 0) != GPG_ERR_NO_ERROR)
357 throw Exception(
"cannot initialize libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
359 throw Exception(
"ROSE was not configured with libgcrypt");
364 #ifdef ROSE_HAVE_LIBGCRYPT
365 if (gcry_md_copy(&md_, other.md_) != GPG_ERR_NO_ERROR)
366 throw Exception(
"cannot copy libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
369 throw Exception(
"ROSE was not configured with libgcrypt");
374 #ifdef ROSE_HAVE_LIBGCRYPT
380 #ifdef ROSE_HAVE_LIBGCRYPT
382 if (gcry_md_copy(&md_, other.md_) != GPG_ERR_NO_ERROR)
383 throw Exception(
"cannot copy libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
386 throw Exception(
"ROSE was not configured with libgcrypt");
391 #ifdef ROSE_HAVE_LIBGCRYPT
398 if (digest_.empty()) {
399 #ifdef ROSE_HAVE_LIBGCRYPT
401 digest_.resize(gcry_md_get_algo_dlen(hashAlgorithmId), 0);
402 uint8_t *d = gcry_md_read(md_, hashAlgorithmId);
404 memcpy(&digest_[0], d, digest_.size());
406 ASSERT_not_reachable(
"ROSE was not configured with libgcrypt");
412 void append(
const uint8_t *message,
size_t messageSize) {
413 ASSERT_require(message || 0==messageSize);
414 #ifdef ROSE_HAVE_LIBGCRYPT
415 if (!digest_.empty())
416 throw Exception(
"cannot append after returning digest");
418 gcry_md_write(md_, message, messageSize);
420 ASSERT_not_reachable(
"ROSE was not configured with libgcrypt");
412 void append(
const uint8_t *message,
size_t messageSize) {
…}
425#ifdef ROSE_HAVE_LIBGCRYPT
426typedef HasherGcrypt<GCRY_MD_MD5>
HasherMd5;
447 HasherFnv(): partial_(0xcbf29ce484222325ull) {}
449 void append(
const uint8_t *message,
size_t messageSize)
override;
450 uint64_t partial()
const {
return partial_; }
457 static const uint32_t roundConstants_[64];
459 size_t processedBytes_;
460 std::vector<uint8_t> leftoverBytes_;
465 void append(
const uint8_t *message,
size_t messageSize)
override;
467 uint8_t messageByte(
size_t index,
const uint8_t *message,
size_t messageSize);
468 bool getNextChunk(
const uint8_t* &message ,
size_t &messageSize , uint32_t words[16] );
469 void accumulateChunk(
const uint32_t chunk[16]);
475template<
class T,
class U>
476std::vector<std::pair<T, U> >
477zip(
const std::vector<T> &first,
const std::vector<U> &second) {
478 size_t retvalSize = std::min(first.size(), second.size());
479 std::vector<std::pair<T, U> > retval;
480 retval.reserve(retvalSize);
481 for (
size_t i = 0; i < retvalSize; ++i)
482 retval.push_back(std::pair<T, U>(first[i], second[i]));
477zip(
const std::vector<T> &first,
const std::vector<U> &second) {
…}
487template<
class T,
class U>
488std::pair<std::vector<T>, std::vector<U> >
489unzip(
const std::vector<std::pair<T, U> > &pairs) {
490 std::pair<std::vector<T>, std::vector<U> > retval;
491 retval.first.reserve(pairs.size());
492 retval.second.reserve(pairs.size());
493 for (
size_t i = 0; i < pairs.size(); ++i) {
494 retval.first.push_back(pairs[i].first);
495 retval.second.push_back(pairs[i].second);
489unzip(
const std::vector<std::pair<T, U> > &pairs) {
…}
31namespace Combinatorics {
…}
Fowler-Noll-Vo hashing using the Hasher interface.
void append(const uint8_t *message, size_t messageSize) override
Insert data into the digest.
const Digest & digest() override
Return the digest.
Hasher for any libgcrypt hash algorithm.
void append(const uint8_t *message, size_t messageSize)
Insert data into the digest.
void clear()
Reset the hasher to its initial state.
const Digest & digest()
Return the digest.
void append(const uint8_t *message, size_t messageSize) override
Insert data into the digest.
void clear() override
Reset the hasher to its initial state.
const Digest & digest() override
Return the digest.
Exception(const std::string &mesg)
Constructor.
A singleton that creates and returns Hashers by name.
static HasherFactory & instance()
Returns a reference to the HasherFactory singleton.
Templated to create any Hasher and register it with HasherFactory.
HasherMaker(const std::string &hashType)
Creates a HasherMaker and registers it with HasherFactory.
virtual std::shared_ptr< Hasher > create() const
Creates a Hasher.
Common subclass all the classes that construct Hashers.
void insert(const std::string &)
Insert data into the digest.
std::string toString()
String representation of the digest.
std::vector< uint8_t > Digest
The digest of the input message.
void insert(uint64_t)
Insert data into the digest.
static std::string toString(const Digest &)
Convert a digest to a hexadecimal string.
uint64_t toU64(const Digest &)
Returns the hash as a 64 bit int.
void insert(std::istream &)
Insert data into the digest.
virtual void clear()
Reset the hasher to its initial state.
virtual const Digest & digest()
Return the digest.
void insert(const uint8_t *bytes, size_t nBytes)
Insert data into the digest.
void insert(const std::vector< uint8_t > &)
Insert data into the digest.
virtual void append(const uint8_t *message, size_t messageSize)=0
Insert data into the digest.
uint64_t toU64()
Returns the hash as a 64 bit int.
Base class for all ROSE exceptions.
ROSE_DLL_API bool flip_coin()
Simulate flipping a coin.
HasherGcrypt< 0 > HasherSha1
SHA1 hasher.
std::vector< std::pair< T, U > > zip(const std::vector< T > &first, const std::vector< U > &second)
Convert two vectors to a vector of pairs.
HasherGcrypt< 0 > HasherSha384
SHA-384 hasher.
HasherGcrypt< 0 > HasherCrc32
ISO 3309 hasher.
void reorder(std::vector< T > &values, const std::vector< size_t > &remap)
Reorder the values of one vector according to another.
HasherGcrypt< 0 > HasherMd5
MD5 hasher.
void shuffle(std::vector< T > &vector, size_t nitems=UNLIMITED, size_t limit=UNLIMITED)
Shuffle the values of a vector.
std::pair< std::vector< T >, std::vector< U > > unzip(const std::vector< std::pair< T, U > > &pairs)
Convert a vector of pairs to a pair of vectors.
ROSE_DLL_API uint64_t fromBase62String(const std::string &base62)
Converts a base 62 string to a 64 bit int.
HasherGcrypt< 0 > HasherSha256
SHA-256 hasher.
ROSE_DLL_API std::string toBase62String(uint64_t)
Converts a 64 bit int to base 62.
HasherGcrypt< 0 > HasherSha512
SHA-512 hasher.
const size_t UNLIMITED
Effectively unlimited size.
size_t fastRandomIndex(size_t n, size_t seed=0)
Thread-safe random number generator.