Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .clang-format and apply to dimod/include/* #742

Merged
merged 3 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
Language: Cpp
BasedOnStyle: Google

ColumnLimit: 80
NamespaceIndentation: Inner

# Scaled by a factor of 2 such that the base indent is 4
AccessModifierOffset: -3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not -2?

Copy link
Member Author

@arcondello arcondello Oct 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IndentWidth: 4
ContinuationIndentWidth: 8
ConstructorInitializerIndentWidth: 8
...
78 changes: 37 additions & 41 deletions dimod/include/dimod/adjarraybqm.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace dimod {

template<class V, class B, class N = std::size_t>
template <class V, class B, class N = std::size_t>
class AdjArrayBQM {
public:
using bias_type = B;
Expand All @@ -32,7 +32,8 @@ class AdjArrayBQM {
using size_type = std::size_t;

using outvars_iterator = typename std::vector<std::pair<V, B>>::iterator;
using const_outvars_iterator = typename std::vector<std::pair<V, B>>::const_iterator;
using const_outvars_iterator =
typename std::vector<std::pair<V, B>>::const_iterator;

// in the future we'd probably like to make this protected
std::vector<std::pair<N, B>> invars;
Expand All @@ -41,10 +42,10 @@ class AdjArrayBQM {
AdjArrayBQM() {}

// can we specify this slightly better?
template<class BQM>
explicit AdjArrayBQM(const BQM &bqm) {
template <class BQM>
explicit AdjArrayBQM(const BQM& bqm) {
invars.reserve(bqm.num_variables());
outvars.reserve(2*bqm.num_interactions());
outvars.reserve(2 * bqm.num_interactions());

for (auto v = 0; v < bqm.num_variables(); ++v) {
invars.emplace_back(outvars.size(), bqm.linear(v));
Expand All @@ -59,9 +60,9 @@ class AdjArrayBQM {
*
* @param dense An array containing the biases. Assumed to contain
* `num_variables`^2 elements. The upper and lower triangle are summed.
* @param num_variables The number of variables.
* @param num_variables The number of variables.
*/
template<class B2>
template <class B2>
AdjArrayBQM(const B2 dense[], size_type num_variables,
bool ignore_diagonal = false) {
// we know how big our linear is going to be. We'd also like to
Expand All @@ -75,35 +76,30 @@ class AdjArrayBQM {
if (ignore_diagonal) {
invars.emplace_back(outvars.size(), 0);
} else {
invars.emplace_back(outvars.size(), dense[u*(num_variables+1)]);
invars.emplace_back(outvars.size(),
dense[u * (num_variables + 1)]);
}

for (size_type v = 0; v < num_variables; ++v) {
if (u == v) continue; // already did linear

qbias = dense[u*num_variables+v] + dense[v*num_variables+u];
qbias = dense[u * num_variables + v] +
dense[v * num_variables + u];

if (qbias != 0)
outvars.emplace_back(v, qbias);
if (qbias != 0) outvars.emplace_back(v, qbias);
}
}
}

size_type num_interactions() const {
return outvars.size() / 2;
}
size_type num_interactions() const { return outvars.size() / 2; }

size_type num_variables() const {
return invars.size();
}
size_type num_variables() const { return invars.size(); }

[[deprecated("Use AdjArrayBQM::linear(v)")]]
bias_type get_linear(variable_type v) const {
return linear(v);
}
[[deprecated("Use AdjArrayBQM::linear(v)")]] bias_type get_linear(
variable_type v) const { return linear(v); }

std::pair<bias_type, bool>
get_quadratic(variable_type u, variable_type v) const {
std::pair<bias_type, bool> get_quadratic(variable_type u,
variable_type v) const {
assert(u >= 0 && u < invars.size());
assert(v >= 0 && v < invars.size());
assert(u != v);
Expand All @@ -121,10 +117,10 @@ class AdjArrayBQM {
assert(v >= 0 && v < invars.size());

// need to check the case that v is the last variable
if ((unsigned) v == invars.size() - 1)
if ((unsigned)v == invars.size() - 1)
return outvars.size() - invars[v].first;

return invars[v+1].first - invars[v].first;
return invars[v + 1].first - invars[v].first;
}

bias_type& linear(variable_type v) {
Expand All @@ -137,54 +133,54 @@ class AdjArrayBQM {
return invars[v].second;
}

std::pair<outvars_iterator, outvars_iterator>
neighborhood(variable_type u) {
std::pair<outvars_iterator, outvars_iterator> neighborhood(
variable_type u) {
assert(u >= 0 && u < invars.size());

outvars_iterator end;
if ((unsigned) u == invars.size() - 1) {
if ((unsigned)u == invars.size() - 1) {
end = outvars.end();
} else {
end = outvars.begin() + invars[u+1].first;
end = outvars.begin() + invars[u + 1].first;
}

return std::make_pair(outvars.begin() + invars[u].first, end);
}

std::pair<const_outvars_iterator, const_outvars_iterator>
neighborhood(variable_type u) const {
std::pair<const_outvars_iterator, const_outvars_iterator> neighborhood(
variable_type u) const {
assert(u >= 0 && u < invars.size());

const_outvars_iterator end;
if ((unsigned) u == invars.size() - 1) {
if ((unsigned)u == invars.size() - 1) {
end = outvars.cend();
} else {
end = outvars.cbegin() + invars[u+1].first;
end = outvars.cbegin() + invars[u + 1].first;
}

return std::make_pair(outvars.cbegin() + invars[u].first, end);
}

/**
* The neighborhood of variable `v`.
*
*
* @param A variable `v`.
* @param The neighborhood will start with the first out variable that
* does not compare less than `start`.
*
* @returns A pair of iterators pointing to the start and end of the
* neighborhood.
*/
std::pair<const_outvars_iterator, const_outvars_iterator>
neighborhood(variable_type v, variable_type start) const {
std::pair<const_outvars_iterator, const_outvars_iterator> neighborhood(
variable_type v, variable_type start) const {
auto span = neighborhood(v);
auto low = std::lower_bound(span.first, span.second,
start, utils::comp_v<V, B>);
auto low = std::lower_bound(span.first, span.second, start,
utils::comp_v<V, B>);
return std::make_pair(low, span.second);
}
}

[[deprecated("Use AdjArrayBQM::linear(v)")]]
void set_linear(variable_type v, bias_type b) {
[[deprecated("Use AdjArrayBQM::linear(v)")]] void set_linear(
variable_type v, bias_type b) {
assert(v >= 0 && v < invars.size());
linear(v) = b;
}
Expand Down
Loading