Skip to content

Commit

Permalink
Replace asserts with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Mar 24, 2017
1 parent 24136de commit 00d27c6
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 122 deletions.
8 changes: 7 additions & 1 deletion array.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ GenericArray<T> GenericArray<T>::inverse() const {
"Can only take inverse permutation of integral array");
int n = size();

if (n == 0) {
return *this;
}

// sanity check
ensure(*max_element(begin(), end()) == n-1 &&
*min_element(begin(), end()) == 0,
Expand Down Expand Up @@ -358,7 +362,9 @@ T GenericArray<T>::choice() const {

template<typename T>
GenericArray<T> GenericArray<T>::choice(size_t count) const {
ensure(count <= size());
ensure(
count <= size(),
"Use Array::choiceWithRepetition to select more than size() elements");

size_t n = size();

Expand Down
43 changes: 36 additions & 7 deletions common.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
#pragma once

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <string>

#define JNGEN_ENSURE1(cond)\
assert(cond)
namespace jngen {

class Exception : public std::runtime_error {
public:
explicit Exception(const std::string& s) :
std::runtime_error("Assertion `" + s + "' failed.")
{ }

Exception(const std::string& assertMsg, const std::string& expl) :
std::runtime_error(expl + " (assertion `" + assertMsg + "' failed).")
{ }
};

class InternalException : public Exception {
public:
explicit InternalException(const std::string& s) : Exception(s) {}

InternalException(const std::string& assertMsg, const std::string& expl) :
Exception(assertMsg, expl)
{ }
};

} // namespace jngen

#define JNGEN_ENSURE1(exType, cond)\
do\
if (!(cond)) {\
throw exType(#cond);\
}\
while (false)

#define JNGEN_ENSURE2(cond, msg)\
#define JNGEN_ENSURE2(exType, cond, msg)\
do\
if (!(cond)) {\
std::cerr << "Error: " << msg << std::endl;\
assert(cond);\
throw exType(#cond, msg);\
}\
while (false)

#define JNGEN_GET_MACRO(_1, _2, NAME, ...) NAME

#define ensure(...) JNGEN_GET_MACRO(__VA_ARGS__, JNGEN_ENSURE2, JNGEN_ENSURE1)\
(__VA_ARGS__)
(jngen::Exception, __VA_ARGS__)
#define ENSURE(...) JNGEN_GET_MACRO(__VA_ARGS__, JNGEN_ENSURE2, JNGEN_ENSURE1)\
(jngen::InternalException, __VA_ARGS__)

namespace jngen {

Expand Down
32 changes: 23 additions & 9 deletions generic_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class GenericGraph {
// order: by labels
// TODO: think about ordering here
virtual void setVertexWeights(const WeightArray& weights) {
ensure(static_cast<int>(weights.size()) == n());
ensure(
static_cast<int>(weights.size()) == n(),
"The argument of setVertexWeights must have exactly n elements");
vertexWeights_.resize(n());
for (int i = 0; i < n(); ++i) {
vertexWeights_[i] = weights[vertexByLabel(i)];
Expand All @@ -47,26 +49,29 @@ class GenericGraph {

// v: label
virtual void setVertexWeight(int v, const Weight& weight) {
ensure(v < n());
ensure(v < n(), "setVertexWeight");
v = vertexByLabel(v);

vertexWeights_.extend(v + 1);
vertexWeights_[v] = weight;
}

virtual void setEdgeWeights(const WeightArray& weights) {
ensure(static_cast<int>(weights.size()) == m());
ensure(
static_cast<int>(weights.size()) == m(),
"The argument of setEdgeWeights must have exactly m elements");
edgeWeights_ = weights;
}

virtual void setEdgeWeight(size_t index, const Weight& weight) {
ensure(static_cast<int>(index) < m());
ensure(static_cast<int>(index) < m(), "setEdgeWeight");
edgeWeights_.extend(index + 1);
edgeWeights_[index] = weight;
}

// v: label
virtual Weight vertexWeight(int v) const {
ensure(v < n(), "vertexWeight");
size_t index = vertexByLabel(v);
if (index < vertexWeights_.size()) {
return Weight{};
Expand All @@ -75,6 +80,7 @@ class GenericGraph {
}

virtual Weight edgeWeight(size_t index) const {
ensure(static_cast<int>(index) < m(), "edgeWeight");
if (index < edgeWeights_.size()) {
return Weight{};
}
Expand Down Expand Up @@ -126,6 +132,7 @@ class GenericGraph {
};

Array GenericGraph::edges(int v) const {
ensure(v < n(), "Graph::edges(v)");
v = vertexByLabel(v);

Array result;
Expand All @@ -148,9 +155,12 @@ Arrayp GenericGraph::edges() const {
}

inline void GenericGraph::doShuffle() {
// this if is to be removed after all checks pass
if (vertexLabel_.size() < static_cast<size_t>(n())) {
ENSURE(false, "GenericGraph::doShuffle");
vertexLabel_ = Array::id(n());
}

vertexLabel_.shuffle();
vertexByLabel_ = vertexLabel_.inverse();

Expand Down Expand Up @@ -178,24 +188,28 @@ void GenericGraph::addEdgeUnsafe(int u, int v) {
int id = numEdges_++;
edges_.emplace_back(u, v);

ENSURE(u < n() && v < n(), "GenericGraph::addEdgeUnsafe");

adjList_[u].push_back(id);
if (!directed_ && u != v) {
adjList_[v].push_back(id);
}
}

int GenericGraph::edgeOtherEnd(int v, int edgeId) {
ensure(edgeId < numEdges_);
ENSURE(edgeId < numEdges_);
const auto& edge = edges_[edgeId];
if (edge.first == v) {
return edge.second;
}
ensure(!directed_);
ensure(edge.second == v);
ENSURE(!directed_);
ENSURE(edge.second == v);
return edge.first;
}

void GenericGraph::permuteEdges(const Array& order) {
ENSURE(static_cast<int>(order.size()) == m(), "GenericGraph::permuteEdges");

edges_ = edges_.subseq(order);

auto newByOld = order.inverse();
Expand All @@ -212,7 +226,7 @@ void GenericGraph::permuteEdges(const Array& order) {
}

void GenericGraph::normalizeEdges() {
ensure(
ENSURE(
vertexLabel_ == Array::id(n()),
"Can call normalizeEdges() only on newly created graph");

Expand Down Expand Up @@ -249,7 +263,7 @@ inline void GenericGraph::addEdge(int u, int v, const Weight& w) {
namespace {

WeightArray prepareWeightArray(WeightArray a, int requiredSize) {
ensure(a.hasNonEmpty(), "INTERNAL ASSERT");
ENSURE(a.hasNonEmpty(), "Attempt to print empty weight array");

a.extend(requiredSize);
int type = a.anyType();
Expand Down
Loading

0 comments on commit 00d27c6

Please sign in to comment.