Created
June 20, 2015 15:42
-
-
Save ynonp/9def66ea3d59ee063998 to your computer and use it in GitHub Desktop.
Range iterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include "range.h" | |
#include <QDebug> | |
#include <QtCore> | |
#include <QtConcurrent> | |
static const char ab[] = "0123456789abcdefghijklmnopqrstuvwxyz"; | |
size_t len = 4; | |
size_t sz = sizeof(ab) - 1; | |
size_t total = qPow(sz, len); | |
size_t trace = 100000; | |
typedef QPair<QString, QString> StringPair; | |
typedef QHash<QString,QString> StringHash; | |
char char_at(const char ab[], size_t i, size_t d, size_t sz) | |
{ | |
return ab[int(i / qPow(sz, d)) % sz]; | |
} | |
StringPair nextWord(unsigned long i) | |
{ | |
QString next; | |
for ( size_t d=0; d < len; d++ ) { | |
next += ab[int(i / qPow(sz, d)) % sz]; | |
} | |
if ( i % trace == 0 ) { | |
qDebug() << int(i / trace) << " / " << int(total / trace); | |
} | |
QCryptographicHash hasher(QCryptographicHash::Md5); | |
hasher.addData(next.toLocal8Bit()); | |
QString md5 = QString(hasher.result().toHex()); | |
QString word = next; | |
return StringPair(md5, word); | |
} | |
void mergeResult(StringHash &acc, StringPair val) | |
{ | |
Q_UNUSED(acc); | |
if ( val.first == "657f8b8da628ef83cf69101b6817150a" ) | |
{ | |
qDebug() << "Found: " << val.second; | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
Range r(0, total); | |
QtConcurrent::blockingMappedReduced<StringHash>( | |
r.begin(), | |
r.end(), | |
nextWord, | |
mergeResult | |
); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "range.h" | |
Range::Range(unsigned long start, unsigned long end): | |
_start(start), | |
_end(end) | |
{ | |
} | |
Range::~Range() | |
{ | |
} | |
range_iter Range::begin() const | |
{ | |
return range_iter(_start); | |
} | |
range_iter Range::end() const | |
{ | |
return range_iter(_end); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef RANGE_H | |
#define RANGE_H | |
#include <iterator> | |
struct range_iter: std::iterator<std::random_access_iterator_tag, unsigned long> { | |
unsigned long current; | |
range_iter(unsigned long current): current(current) {} | |
const range_iter& operator++() { | |
current++; return *this; | |
} | |
range_iter operator++(int) { | |
range_iter result = *this; ++(*this); return result; | |
} | |
bool operator!=(const range_iter &other) { | |
return this->current != other.current; | |
} | |
unsigned long operator*() { return current; } | |
range_iter& operator--() { current--; return *this; } | |
range_iter operator--(int) { | |
range_iter result = *this; --(*this); return result; | |
} | |
friend bool operator<(const range_iter&self, const range_iter&other) { | |
return self.current < other.current; | |
} | |
friend bool operator>(const range_iter&self, const range_iter&other) | |
{ | |
return self.current > other.current; | |
} | |
friend bool operator<=(const range_iter&self, const range_iter&other) | |
{ | |
return self.current <= other.current; | |
} | |
friend bool operator>=(const range_iter&self, const range_iter&other) | |
{ | |
return self.current >= other.current; | |
} | |
range_iter& operator+=(unsigned long sz) { this->current += sz; return (*this); } | |
friend range_iter operator+(const range_iter&self, unsigned long sz) | |
{ | |
return range_iter(self.current + sz); | |
} | |
friend range_iter operator+(unsigned long sz, const range_iter&self) | |
{ | |
return range_iter(sz + self.current); | |
} | |
range_iter& operator-=(unsigned long sz) | |
{ | |
this->current -= sz; return (*this); | |
} | |
friend range_iter operator-(const range_iter&self, unsigned long sz) | |
{ | |
return range_iter(self.current - sz); | |
} | |
friend difference_type operator-(range_iter self, range_iter other) | |
{ | |
return self.current - other.current; | |
} | |
unsigned long operator[](unsigned long sz) const | |
{ | |
return this->current + sz; | |
} | |
}; | |
class Range | |
{ | |
public: | |
Range(unsigned long start, unsigned long end); | |
~Range(); | |
range_iter begin() const; | |
range_iter end() const; | |
private: | |
unsigned long _start; | |
unsigned long _end; | |
}; | |
#endif // RANGE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment