1#ifndef ROSE_Combinatorics_H
2#define ROSE_Combinatorics_H
4#include <rosePublicConfig.h>
6#include <Rose/Constants.h>
7#include <ROSE_UNUSED.h>
9#include <boost/shared_ptr.hpp>
16#include <Rose/Exception.h>
17#include <Sawyer/Assert.h>
18#include <Sawyer/Synchronization.h>
24#ifdef ROSE_HAVE_LIBGCRYPT
31namespace Combinatorics {
40 T next = retval * n--;
57permute(std::vector<T> &values, uint64_t pn,
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]);
80 nitems = std::min(nitems, vector.size());
81 limit = std::min(limit, nitems);
83 for (
size_t i=0; i<limit; ++i) {
85 std::swap(vector[i], vector[j]);
116reorder(std::vector<T> &values,
const std::vector<size_t> &remap) {
117 assert(values.size() == remap.size());
120 std::vector<T> old = values;
121 for (
size_t i = 0; i < old.size(); ++i)
122 values[i] = old[remap[i]];
210 void insert(
const std::string &x) { append((
const uint8_t*)x.c_str(), x.size()); }
211 void insert(uint64_t x) { append((uint8_t*)&x,
sizeof x); }
212 void insert(
const uint8_t *x,
size_t size) { append(x, size); }
213 void insert(
const std::vector<uint8_t> &v) { append(v.data(), v.size()); }
216 while (stream.good()) {
217 stream.read(buf,
sizeof buf);
218 append((
const uint8_t*)buf, stream.gcount());
229 virtual void append(
const uint8_t *message,
size_t messageSize) = 0;
266 virtual boost::shared_ptr<Hasher> create()
const = 0;
299 HasherFactory::Instance().registerMaker(hashType,
this);
306 virtual boost::shared_ptr<Hasher>
create()
const
309 boost::shared_ptr<Hasher> hashPtr(hasher);
344 boost::shared_ptr<Hasher>
createHasher(
const std::string& hashType)
const;
355 std::map<std::string, IHasherMaker* > hashMakers;
364template<
int hashAlgorithmId>
366#ifdef ROSE_HAVE_LIBGCRYPT
372 #ifdef ROSE_HAVE_LIBGCRYPT
373 if (gcry_md_open(&md_, hashAlgorithmId, 0) != GPG_ERR_NO_ERROR)
374 throw Exception(
"cannot initialize libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
376 throw Exception(
"ROSE was not configured with libgcrypt");
381 #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
397 #ifdef ROSE_HAVE_LIBGCRYPT
399 if (gcry_md_copy(&md_, other.md_) != GPG_ERR_NO_ERROR)
400 throw Exception(
"cannot copy libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
403 throw Exception(
"ROSE was not configured with libgcrypt");
408 #ifdef ROSE_HAVE_LIBGCRYPT
415 if (digest_.empty()) {
416 #ifdef ROSE_HAVE_LIBGCRYPT
418 digest_.resize(gcry_md_get_algo_dlen(hashAlgorithmId), 0);
419 uint8_t *d = gcry_md_read(md_, hashAlgorithmId);
421 memcpy(&digest_[0], d, digest_.size());
423 ASSERT_not_reachable(
"ROSE was not configured with libgcrypt");
429 void append(
const uint8_t *message,
size_t messageSize) {
430 ASSERT_require(message || 0==messageSize);
431 #ifdef ROSE_HAVE_LIBGCRYPT
432 if (!digest_.empty())
433 throw Exception(
"cannot append after returning digest");
435 gcry_md_write(md_, message, messageSize);
437 ASSERT_not_reachable(
"ROSE was not configured with libgcrypt");
442#ifdef ROSE_HAVE_LIBGCRYPT
443typedef HasherGcrypt<GCRY_MD_MD5>
HasherMd5;
464 HasherFnv(): partial_(0xcbf29ce484222325ull) {}
466 void append(
const uint8_t *message,
size_t messageSize)
override;
467 uint64_t partial()
const {
return partial_; }
474 static const uint32_t roundConstants_[64];
476 size_t processedBytes_;
477 std::vector<uint8_t> leftoverBytes_;
482 void append(
const uint8_t *message,
size_t messageSize)
override;
484 uint8_t messageByte(
size_t index,
const uint8_t *message,
size_t messageSize);
485 bool getNextChunk(
const uint8_t* &message ,
size_t &messageSize , uint32_t words[16] );
486 void accumulateChunk(
const uint32_t chunk[16]);
492template<
class T,
class U>
493std::vector<std::pair<T, U> >
494zip(
const std::vector<T> &first,
const std::vector<U> &second) {
495 size_t retvalSize = std::min(first.size(), second.size());
496 std::vector<std::pair<T, U> > retval;
497 retval.reserve(retvalSize);
498 for (
size_t i = 0; i < retvalSize; ++i)
499 retval.push_back(std::pair<T, U>(first[i], second[i]));
504template<
class T,
class U>
505std::pair<std::vector<T>, std::vector<U> >
506unzip(
const std::vector<std::pair<T, U> > &pairs) {
507 std::pair<std::vector<T>, std::vector<U> > retval;
508 retval.first.reserve(pairs.size());
509 retval.second.reserve(pairs.size());
510 for (
size_t i = 0; i < pairs.size(); ++i) {
511 retval.first.push_back(pairs[i].first);
512 retval.second.push_back(pairs[i].second);
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.
HasherFactory is a singleton that creates and returns Hashers by name.
void registerMaker(const std::string &hashType, IHasherMaker *createHasherPtr)
Adds a new @HasherMaker to the HasherFactory.
static HasherFactory & Instance()
Returns a reference to the HasherFactory singleton.
boost::shared_ptr< Hasher > createHasher(const std::string &hashType) const
Creates a registered Hasher by type from the map.
Templated to create any Hasher and register it with HasherFactory.
virtual boost::shared_ptr< Hasher > create() const
Creates a Hasher (the type of Hasher is determined by the template T) and returns it as a shared_ptr.
HasherMaker(const std::string &hashType)
Creates a HasherMaker and registers it with HasherFactory.
Common subclass all the classes that construct Hashers (for the HasherFactory)
void insert(const std::string &x)
Insert data into the digest.
void insert(uint64_t x)
Insert data into the digest.
uint64_t make64Bits(const Digest &)
Returns the hash as a 64 bit int.
std::string toString()
String representation of the digest.
std::vector< uint8_t > Digest
The digest of the input message.
void insert(const std::vector< uint8_t > &v)
Insert data into the digest.
static std::string toString(const Digest &)
Convert a digest to a hexadecimal string.
virtual void clear()
Reset the hasher to its initial state.
virtual const Digest & digest()
Return the digest.
void print(std::ostream &)
Print a hash to a stream.
uint64_t make64Bits()
Returns the hash as a 64 bit int.
void insert(std::istream &stream)
Insert data into the digest.
void insert(const uint8_t *x, size_t size)
Insert data into the digest.
virtual void append(const uint8_t *message, size_t messageSize)=0
Insert data into the digest.
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.
ROSE_DLL_API std::string toBase62String(uint64_t num)
Converts a 64 bit in to base 62 (All letters and numbers).
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 (All letters and numbers) number to a 64 bit int.
HasherGcrypt< 0 > HasherSha256
SHA-256 hasher.
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.