Skip to content

Commit

Permalink
Operators for GenericGraph; zip(array, array)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Mar 22, 2017
1 parent 82fa45c commit c99133a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
25 changes: 23 additions & 2 deletions array.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,21 @@ GenericArray<T>::operator std::string() const {
return std::string(begin(), end());
}

template<typename T>
using TArray = GenericArray<T>;

} // namespace jngen

template<typename T>
using TArray = jngen::GenericArray<T>;
using jngen::TArray;

using Array = jngen::GenericArray<int>;
using Array2d = jngen::GenericArray<jngen::GenericArray<int>>;
using Array64 = jngen::GenericArray<long long>;
using Arrayf = jngen::GenericArray<double>;
using Arrayp = jngen::GenericArray<std::pair<int, int>>;

namespace jngen {

template<typename T>
jngen::GenericArray<T> makeArray(const std::vector<T>& values) {
return jngen::GenericArray<T>(values);
Expand All @@ -401,3 +405,20 @@ template<typename T>
jngen::GenericArray<T> makeArray(const std::initializer_list<T>& values) {
return jngen::GenericArray<T>(values);
}

template<typename T, typename U>
TArray<std::pair<T, U>> zip(const TArray<T>& lhs, const TArray<U>& rhs) {
ensure(
lhs.size() == rhs.size(),
"In zip(a, b), a and b must have the same size");
TArray<std::pair<T, U>> result;
for (size_t i = 0; i < lhs.size(); ++i) {
result.emplace_back(lhs[i], rhs[i]);
}
return result;
}

} // namespace jngen

using jngen::makeArray;
using jngen::zip;
22 changes: 21 additions & 1 deletion generic_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ class GenericGraph {

// TODO: more operators!
virtual bool operator==(const GenericGraph& other) const;
virtual bool operator!=(const GenericGraph& other) const;
virtual bool operator<(const GenericGraph& other) const;
virtual bool operator>(const GenericGraph& other) const;
virtual bool operator<=(const GenericGraph& other) const;
virtual bool operator>=(const GenericGraph& other) const;

protected:
void doShuffle();
Expand Down Expand Up @@ -175,7 +179,7 @@ void GenericGraph::addEdgeUnsafe(int u, int v) {
edges_.emplace_back(u, v);

adjList_[u].push_back(id);
if (u != v) {
if (!directed_ && u != v) {
adjList_[v].push_back(id);
}
}
Expand Down Expand Up @@ -310,10 +314,26 @@ inline bool GenericGraph::operator==(const GenericGraph& other) const {
return compareTo(other) == 0;
}

inline bool GenericGraph::operator!=(const GenericGraph& other) const {
return compareTo(other) != 0;
}

inline bool GenericGraph::operator<(const GenericGraph& other) const {
return compareTo(other) == -1;
}

inline bool GenericGraph::operator>(const GenericGraph& other) const {
return compareTo(other) == 1;
}

inline bool GenericGraph::operator<=(const GenericGraph& other) const {
return compareTo(other) != 1;
}

inline bool GenericGraph::operator>=(const GenericGraph& other) const {
return compareTo(other) == -1;
}

inline int GenericGraph::compareTo(const GenericGraph& other) const {
if (n() != other.n()) {
return n() < other.n() ? -1 : 1;
Expand Down
47 changes: 44 additions & 3 deletions jngen.h
Original file line number Diff line number Diff line change
Expand Up @@ -1761,17 +1761,21 @@ GenericArray<T>::operator std::string() const {
return std::string(begin(), end());
}

template<typename T>
using TArray = GenericArray<T>;

} // namespace jngen

template<typename T>
using TArray = jngen::GenericArray<T>;
using jngen::TArray;

using Array = jngen::GenericArray<int>;
using Array2d = jngen::GenericArray<jngen::GenericArray<int>>;
using Array64 = jngen::GenericArray<long long>;
using Arrayf = jngen::GenericArray<double>;
using Arrayp = jngen::GenericArray<std::pair<int, int>>;

namespace jngen {

template<typename T>
jngen::GenericArray<T> makeArray(const std::vector<T>& values) {
return jngen::GenericArray<T>(values);
Expand All @@ -1782,6 +1786,23 @@ jngen::GenericArray<T> makeArray(const std::initializer_list<T>& values) {
return jngen::GenericArray<T>(values);
}

template<typename T, typename U>
TArray<std::pair<T, U>> zip(const TArray<T>& lhs, const TArray<U>& rhs) {
ensure(
lhs.size() == rhs.size(),
"In zip(a, b), a and b must have the same size");
TArray<std::pair<T, U>> result;
for (size_t i = 0; i < lhs.size(); ++i) {
result.emplace_back(lhs[i], rhs[i]);
}
return result;
}

} // namespace jngen

using jngen::makeArray;
using jngen::zip;


#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -3074,7 +3095,11 @@ class GenericGraph {

// TODO: more operators!
virtual bool operator==(const GenericGraph& other) const;
virtual bool operator!=(const GenericGraph& other) const;
virtual bool operator<(const GenericGraph& other) const;
virtual bool operator>(const GenericGraph& other) const;
virtual bool operator<=(const GenericGraph& other) const;
virtual bool operator>=(const GenericGraph& other) const;

protected:
void doShuffle();
Expand Down Expand Up @@ -3162,7 +3187,7 @@ void GenericGraph::addEdgeUnsafe(int u, int v) {
edges_.emplace_back(u, v);

adjList_[u].push_back(id);
if (u != v) {
if (!directed_ && u != v) {
adjList_[v].push_back(id);
}
}
Expand Down Expand Up @@ -3297,10 +3322,26 @@ inline bool GenericGraph::operator==(const GenericGraph& other) const {
return compareTo(other) == 0;
}

inline bool GenericGraph::operator!=(const GenericGraph& other) const {
return compareTo(other) != 0;
}

inline bool GenericGraph::operator<(const GenericGraph& other) const {
return compareTo(other) == -1;
}

inline bool GenericGraph::operator>(const GenericGraph& other) const {
return compareTo(other) == 1;
}

inline bool GenericGraph::operator<=(const GenericGraph& other) const {
return compareTo(other) != 1;
}

inline bool GenericGraph::operator>=(const GenericGraph& other) const {
return compareTo(other) == -1;
}

inline int GenericGraph::compareTo(const GenericGraph& other) const {
if (n() != other.n()) {
return n() < other.n() ? -1 : 1;
Expand Down

0 comments on commit c99133a

Please sign in to comment.