ROSE 0.11.145.147
Combinatorics.h
1#ifndef ROSE_Combinatorics_H
2#define ROSE_Combinatorics_H
3
4#include <rosePublicConfig.h>
5
6#include <Rose/Constants.h>
7#include <ROSE_UNUSED.h>
8
9#include <algorithm>
10#include <cassert>
11#include <istream>
12#include <list>
13#include <memory>
14#include <ostream>
15#include <Rose/Exception.h>
16#include <Sawyer/Assert.h>
17#include <Sawyer/Synchronization.h>
18#include <stdexcept>
19#include <stdint.h>
20#include <string>
21#include <vector>
22
23#ifdef ROSE_HAVE_LIBGCRYPT
24#include <gcrypt.h>
25#endif
26
27namespace Rose {
28
30namespace Combinatorics {
31
33template<typename T>
34static T
35factorial(T n)
36{
37 T retval = 1;
38 while (n>1) {
39 T next = retval * n--;
40 assert(next>retval); // overflow
41 retval = next;
42 }
43 return retval;
44}
45
47ROSE_DLL_API bool flip_coin();
48
54template<typename T>
55static void
56permute(std::vector<T> &values/*in,out*/, uint64_t pn, size_t sz = UNLIMITED)
57{
58 if (UNLIMITED == sz)
59 sz = values.size();
60 assert(sz<=values.size());
61 assert(pn<factorial(sz));
62 for (size_t i=0; i<sz; ++i) {
63 uint64_t radix = sz - i;
64 uint64_t idx = pn % radix;
65 std::swap(values[i+idx], values[i]);
66 pn /= radix;
67 }
68}
69
75template<typename T>
76void
77shuffle(std::vector<T> &vector, size_t nitems = UNLIMITED, size_t limit = UNLIMITED)
78{
79 nitems = std::min(nitems, vector.size());
80 limit = std::min(limit, nitems);
81
82 for (size_t i=0; i<limit; ++i) {
83 size_t j = Sawyer::fastRandomIndex(nitems);
84 std::swap(vector[i], vector[j]);
85 }
86}
87
113template<class T>
114void
115reorder(std::vector<T> &values, const std::vector<size_t> &remap) {
116 assert(values.size() == remap.size());
117
118 // O(n) implementation using a temporary vector. Alternatively, we could use an O(n^2) algorithm that uses constant space.
119 std::vector<T> old = values;
120 for (size_t i = 0; i < old.size(); ++i)
121 values[i] = old[remap[i]];
122}
123
130ROSE_DLL_API std::string toBase62String(uint64_t num);
131
138ROSE_DLL_API uint64_t fromBase62String(const std::string& base62);
139
173class ROSE_DLL_API Hasher {
174public:
179 typedef std::vector<uint8_t> Digest;
180
183 public:
185 Exception(const std::string &mesg): Rose::Exception(mesg) {}
186 ~Exception() throw () {}
187 };
188
189protected:
190 Digest digest_;
191
192public:
193 virtual ~Hasher() {}
194
196 virtual void clear() { digest_ = Digest(); }
197
202 virtual const Digest& digest() { return digest_; }
203
209 void insert(const std::string &x) { append((const uint8_t*)x.c_str(), x.size()); }
210 void insert(uint64_t x) { append((uint8_t*)&x, sizeof x); }
211 void insert(const uint8_t *x, size_t size) { append(x, size); }
212 void insert(const std::vector<uint8_t> &v) { append(v.data(), v.size()); }
213 void insert(std::istream &stream) {
214 char buf[4096]; // multiple of 64
215 while (stream.good()) {
216 stream.read(buf, sizeof buf);
217 append((const uint8_t*)buf, stream.gcount());
218 }
219 if (!stream.eof())
220 throw Hasher::Exception("failed to read data from file");
221 }
228 virtual void append(const uint8_t *message, size_t messageSize) = 0;
229
231 static std::string toString(const Digest&);
232
237 std::string toString();
238
246 uint64_t make64Bits();
247 uint64_t make64Bits(const Digest&);
254 void print(std::ostream&);
255
263 {
264 public:
265 virtual std::shared_ptr<Hasher> create() const = 0;
266 virtual ~IHasherMaker() {}
267 };
268
284 template<typename T>
286 {
287 public:
296 HasherMaker(const std::string& hashType)
297 {
298 HasherFactory::Instance().registerMaker(hashType, this);
299 }
300
305 virtual std::shared_ptr<Hasher> create() const
306 {
307 return std::make_shared<T>();
308 }
309
310 };
311
324 {
325 public:
329
332 void registerMaker(const std::string& hashType, IHasherMaker* createHasherPtr);
333
341 std::shared_ptr<Hasher> createHasher(const std::string& hashType) const;
342
343 private:
344 HasherFactory() {}
345
347 HasherFactory(const HasherFactory& other);
349 HasherFactory& operator=(const HasherFactory& other);
350
352 std::map<std::string, IHasherMaker* > hashMakers;
353 };
354};
355
361template<int hashAlgorithmId>
362class HasherGcrypt: public Hasher {
363#ifdef ROSE_HAVE_LIBGCRYPT
364 gcry_md_hd_t md_;
365#endif
366
367public:
368 HasherGcrypt() {
369 #ifdef ROSE_HAVE_LIBGCRYPT
370 if (gcry_md_open(&md_, hashAlgorithmId, 0) != GPG_ERR_NO_ERROR)
371 throw Exception("cannot initialize libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
372 #else
373 throw Exception("ROSE was not configured with libgcrypt");
374 #endif
375 }
376
377 HasherGcrypt(const HasherGcrypt &other) {
378 #ifdef ROSE_HAVE_LIBGCRYPT
379 if (gcry_md_copy(&md_, other.md_) != GPG_ERR_NO_ERROR)
380 throw Exception("cannot copy libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
381 #else
382 ROSE_UNUSED(other);
383 throw Exception("ROSE was not configured with libgcrypt");
384 #endif
385 }
386
387 ~HasherGcrypt() {
388 #ifdef ROSE_HAVE_LIBGCRYPT
389 gcry_md_close(md_);
390 #endif
391 }
392
393 HasherGcrypt& operator=(const HasherGcrypt &other) {
394 #ifdef ROSE_HAVE_LIBGCRYPT
395 gcry_md_close(md_);
396 if (gcry_md_copy(&md_, other.md_) != GPG_ERR_NO_ERROR)
397 throw Exception("cannot copy libgcrypt hash " + std::string(gcry_md_algo_name(hashAlgorithmId)));
398 #else
399 ROSE_UNUSED(other);
400 throw Exception("ROSE was not configured with libgcrypt");
401 #endif
402 }
403
404 void clear() {
405 #ifdef ROSE_HAVE_LIBGCRYPT
406 gcry_md_reset(md_);
407 #endif
409 }
410
411 const Digest& digest() {
412 if (digest_.empty()) {
413 #ifdef ROSE_HAVE_LIBGCRYPT
414 gcry_md_final(md_);
415 digest_.resize(gcry_md_get_algo_dlen(hashAlgorithmId), 0);
416 uint8_t *d = gcry_md_read(md_, hashAlgorithmId);
417 ASSERT_not_null(d);
418 memcpy(&digest_[0], d, digest_.size());
419 #else
420 ASSERT_not_reachable("ROSE was not configured with libgcrypt");
421 #endif
422 }
423 return Hasher::digest();
424 }
425
426 void append(const uint8_t *message, size_t messageSize) {
427 ASSERT_require(message || 0==messageSize);
428 #ifdef ROSE_HAVE_LIBGCRYPT
429 if (!digest_.empty())
430 throw Exception("cannot append after returning digest");
431 if (messageSize > 0)
432 gcry_md_write(md_, message, messageSize);
433 #else
434 ASSERT_not_reachable("ROSE was not configured with libgcrypt");
435 #endif
436 }
437};
438
439#ifdef ROSE_HAVE_LIBGCRYPT
440typedef HasherGcrypt<GCRY_MD_MD5> HasherMd5;
441typedef HasherGcrypt<GCRY_MD_SHA1> HasherSha1;
442typedef HasherGcrypt<GCRY_MD_SHA256> HasherSha256;
443typedef HasherGcrypt<GCRY_MD_SHA384> HasherSha384;
444typedef HasherGcrypt<GCRY_MD_SHA512> HasherSha512;
445typedef HasherGcrypt<GCRY_MD_CRC32> HasherCrc32;
446#else // the template argument for the following unimplemented hashers is arbitrary and they can all be the same
453#endif
454
455
456
458class ROSE_DLL_API HasherFnv: public Hasher {
459 uint64_t partial_;
460public:
461 HasherFnv(): partial_(0xcbf29ce484222325ull) {}
462 const Digest& digest() override;
463 void append(const uint8_t *message, size_t messageSize) override;
464 uint64_t partial() const { return partial_; }
465};
466
470class ROSE_DLL_API HasherSha256Builtin: public Hasher {
471 static const uint32_t roundConstants_[64]; // statically-generated constants for the algorithm
472 uint32_t state_[8]; // 256 bits of state information
473 size_t processedBytes_; // number of message bytes hashed (excludes padding)
474 std::vector<uint8_t> leftoverBytes_; // message bytes inserted but not yet hashed
475public:
477 void clear() override;
478 const Digest& digest() override;
479 void append(const uint8_t *message, size_t messageSize) override;
480private:
481 uint8_t messageByte(size_t index, const uint8_t *message, size_t messageSize);
482 bool getNextChunk(const uint8_t* &message /*in,out*/, size_t &messageSize /*in,out*/, uint32_t words[16] /*out*/);
483 void accumulateChunk(const uint32_t chunk[16]);
484};
485
489template<class T, class U>
490std::vector<std::pair<T, U> >
491zip(const std::vector<T> &first, const std::vector<U> &second) {
492 size_t retvalSize = std::min(first.size(), second.size());
493 std::vector<std::pair<T, U> > retval;
494 retval.reserve(retvalSize);
495 for (size_t i = 0; i < retvalSize; ++i)
496 retval.push_back(std::pair<T, U>(first[i], second[i]));
497 return retval;
498}
499
501template<class T, class U>
502std::pair<std::vector<T>, std::vector<U> >
503unzip(const std::vector<std::pair<T, U> > &pairs) {
504 std::pair<std::vector<T>, std::vector<U> > retval;
505 retval.first.reserve(pairs.size());
506 retval.second.reserve(pairs.size());
507 for (size_t i = 0; i < pairs.size(); ++i) {
508 retval.first.push_back(pairs[i].first);
509 retval.second.push_back(pairs[i].second);
510 }
511 return retval;
512}
513
514} // namespace
515} // namespace
516
518
520ROSE_DLL_API std::ostream& operator<<(std::ostream&, Rose::Combinatorics::Hasher&);
521
522#endif
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.
std::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.
HasherMaker(const std::string &hashType)
Creates a HasherMaker and registers it with HasherFactory.
virtual std::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.
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.
The ROSE library.
const size_t UNLIMITED
Effectively unlimited size.
Definition Constants.h:19
size_t fastRandomIndex(size_t n, size_t seed=0)
Thread-safe random number generator.