Skip to content

Commit

Permalink
Big reformat.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Mar 24, 2016
1 parent e929b0e commit 14491cf
Show file tree
Hide file tree
Showing 66 changed files with 121,059 additions and 119,160 deletions.
398 changes: 201 additions & 197 deletions advancedbenchmarking/include/budgetedpostingcollector.h

Large diffs are not rendered by default.

243 changes: 120 additions & 123 deletions advancedbenchmarking/include/maropuparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,140 +29,137 @@ using namespace std;
*/
class MaropuGapReader {
public:
MaropuGapReader(const string &filename) :
mFilename(filename), fd(NULL) {
MaropuGapReader(const string &filename) : mFilename(filename), fd(NULL) {}

/**
* The copy constructor will assign the same file name,
* but the newly constructed object won't be opened.
*/
MaropuGapReader(const MaropuGapReader &mgr)
: mFilename(mgr.mFilename), fd(NULL) {}

/**
* Assignment will close the current reader, and change
* the file name. You need to reopen the reader after the assignment.
*/
MaropuGapReader &operator=(const MaropuGapReader &mgr) {
close();
mFilename = mgr.mFilename;
return *this;
}

~MaropuGapReader() { close(); }

// @daniel: should we worry about our code being compilable on 32-bit
// machines?
// if so, we need to add -D_FILE_OFFSET_BITS=64 to the makefile
// Daniel: it would seem odd to consider 32-bit machines when we assume AVX
// support!
off_t getPos() {
errno = 0;
off_t res = ftello(fd);
if (res < 0) {
stringstream err;
err << "Error getting file position, IO status: " << strerror(errno);
throw runtime_error(err.str());
}


/**
* The copy constructor will assign the same file name,
* but the newly constructed object won't be opened.
*/
MaropuGapReader(const MaropuGapReader &mgr) :
mFilename(mgr.mFilename), fd(NULL) {
}

/**
* Assignment will close the current reader, and change
* the file name. You need to reopen the reader after the assignment.
*/
MaropuGapReader &operator=(const MaropuGapReader &mgr) {
close();
mFilename = mgr.mFilename;
return *this;
return res;
}

void setPos(off_t pos) {
errno = 0;
off_t res = fseeko(fd, pos, SEEK_SET);
if (res < 0) {
stringstream err;
err << "Error setting file position, IO status: " << strerror(errno);
throw runtime_error(err.str());
}

~MaropuGapReader() {
close();
}

// @daniel: should we worry about our code being compilable on 32-bit machines?
// if so, we need to add -D_FILE_OFFSET_BITS=64 to the makefile
// Daniel: it would seem odd to consider 32-bit machines when we assume AVX support!
off_t getPos() {
errno = 0;
off_t res = ftello(fd);
if (res < 0) {
stringstream err;
err << "Error getting file position, IO status: " << strerror(errno);
throw runtime_error(err.str());
}
return res;
}

/*
* Return false if no more data can be loaded.
* Throw an exception in the case of IO error.
*/
template <class container> bool loadIntegers(container &buffer) {
uint32_t qty = 0;
if (!ReadQty(qty))
return false; // EOF
buffer.resize(qty);
errno = 0;
size_t result = fread(buffer.data(), sizeof(uint32_t), buffer.size(), fd);
if (result != buffer.size()) {
if (!errno) {
// If we can't read, the file maybe truncated, i.e., corrupt
throw runtime_error("The file appears to be truncated/corrupt!");
}
stringstream err;
err << "Error reading from file, IO status: " << strerror(errno);
throw runtime_error(err.str());
}

void setPos(off_t pos) {
errno = 0;
off_t res = fseeko(fd, pos, SEEK_SET);
if (res < 0) {
stringstream err;
err << "Error setting file position, IO status: " << strerror(errno);
throw runtime_error(err.str());
}
return true;
}

/*
* Return false if no more data can be loaded.
* Throw an exception in the case of IO error.
*/
bool readNextPosAndQty(off_t &pos, uint32_t &qty) {
pos = getPos();
if (!ReadQty(qty))
return false; // EOF
setPos(getPos() + qty * sizeof(uint32_t));
return true;
}

/**
* We must call open before we can use this class meaningfully.
*/
bool open() {
close();
fd = ::fopen(mFilename.c_str(), "rb");
if (fd == NULL) {
return false;
}

/*
* Return false if no more data can be loaded.
* Throw an exception in the case of IO error.
*/
template <class container>
bool loadIntegers(container &buffer) {
uint32_t qty = 0;
if (!ReadQty(qty)) return false; // EOF
buffer.resize(qty);
errno = 0;
size_t result = fread(buffer.data(), sizeof(uint32_t), buffer.size(), fd);
if (result != buffer.size()) {
if (!errno) {
// If we can't read, the file maybe truncated, i.e., corrupt
throw runtime_error("The file appears to be truncated/corrupt!");
}
stringstream err;
err << "Error reading from file, IO status: " << strerror(errno);
throw runtime_error(err.str());
}
return true;
setvbuf(fd, NULL, _IOFBF, 1024 * 4); // large buffer
return true;
}

void close() {
if (fd != NULL) {
::fclose(fd);
fd = NULL;
}
}

/*
* Return false if no more data can be loaded.
* Throw an exception in the case of IO error.
*/
bool readNextPosAndQty(off_t &pos, uint32_t &qty) {
pos = getPos();
if (!ReadQty(qty)) return false; // EOF
setPos(getPos() + qty * sizeof(uint32_t));
return true;
private:
/*
* Returns false on EOF.
* Throws an exception in the case of IO error.
*/
bool ReadQty(uint32_t &qty) {
qty = 0;
if (fd == NULL) {
throw runtime_error("You forgot to open the file.");
}

/**
* We must call open before we can use this class meaningfully.
*/
bool open() {
close();
fd = ::fopen(mFilename.c_str(), "rb");
if (fd == NULL) {
return false;
}
setvbuf(fd , NULL , _IOFBF , 1024 * 4); // large buffer
return true;
errno = 0;
size_t result = fread(&qty, sizeof(qty), 1, fd);
if (errno) {
stringstream err;
err << "Error opening file, IO status: " << strerror(errno);
throw runtime_error(err.str());
}

void close() {
if (fd != NULL) {
::fclose(fd);
fd = NULL;
}
if (result != 1) {
return false;
}
private:
/*
* Returns false on EOF.
* Throws an exception in the case of IO error.
*/
bool ReadQty(uint32_t &qty) {
qty = 0;
if (fd == NULL) {
throw runtime_error("You forgot to open the file.");
}
errno = 0;
size_t result = fread(&qty, sizeof(qty), 1, fd);
if (errno) {
stringstream err;
err << "Error opening file, IO status: " << strerror(errno);
throw runtime_error(err.str());
}
if (result != 1) {
return false;
}
if (qty > 1 << 29) {
cout << "warning: reading a very large array (" << qty << " integers) : is your input file in the right format?" <<
endl;
}
return true;
if (qty > 1 << 29) {
cout << "warning: reading a very large array (" << qty
<< " integers) : is your input file in the right format?" << endl;
}
return true;
}

string mFilename;
FILE *fd;

string mFilename;
FILE *fd;
};

#endif /* SIMDCompressionAndIntersection_MAROPUPARSER_H_ */
Loading

0 comments on commit 14491cf

Please sign in to comment.