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

Improving code efficiency by errors from cppcheck #1369

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
16 changes: 8 additions & 8 deletions avogadro/core/angleiterator.cpp
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also used clang-format on the 2 files.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ using namespace std;

AngleIterator::AngleIterator(const Molecule* mol)
: m_current(0, 0, 0), m_mol(mol)
{}
{
}

Angle AngleIterator::begin()
{
Expand Down Expand Up @@ -44,15 +45,14 @@ Angle AngleIterator::operator++()
if (valid) {
// we have a valid current angle, try to find a new edge
for (const auto maybeC : graph.neighbors(b)) {
if (maybeC != a
&& (!valid || maybeC > c)) {
if (maybeC != a && maybeC > c) {
m_current = make_tuple(a, b, maybeC);
return m_current;
}

} // end "c" loop
} // end "c" loop
valid = false; // we couldn't find a "c", so find a new "a"
} // end if()
} // end if()

// can we find a new edge?
for (const auto maybeA : graph.neighbors(b)) {
Expand All @@ -67,13 +67,13 @@ Angle AngleIterator::operator++()
// if we don't have a valid "a", move out to find a new "b"
} while (valid);

while(!valid && b + 1 < count) {
while (!valid && b + 1 < count) {
++b; // try going to the next atom

const auto neighbors = graph.neighbors(b);
if (neighbors.size() < 2)
continue;

a = neighbors[0];
c = neighbors[0]; // we'll move to the next one in the loop
valid = true;
Expand All @@ -84,4 +84,4 @@ Angle AngleIterator::operator++()
return make_tuple(MaxIndex, MaxIndex, MaxIndex);
} // end ++ operator

} // namespace Avogadro
} // namespace Avogadro::Core
17 changes: 7 additions & 10 deletions avogadro/core/angleiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include "avogadrocore.h"

#include <vector>
#include <tuple>
#include <vector>

namespace Avogadro {
namespace Core {
Expand All @@ -26,28 +26,25 @@ class AVOGADROCORE_EXPORT AngleIterator
/**
* Constructor.
*/
AngleIterator(const Molecule *mol);
explicit AngleIterator(const Molecule* mol);

~AngleIterator() {}

Angle* operator*() {
return &m_current;
}
Angle* operator*() { return &m_current; }

Angle begin();

Angle end() const {
return std::make_tuple(MaxIndex, MaxIndex, MaxIndex);
}
Angle end() const { return std::make_tuple(MaxIndex, MaxIndex, MaxIndex); }

Angle operator++();

bool operator!=(const AngleIterator& other ) {
bool operator!=(const AngleIterator& other)
{
return m_current != other.m_current;
}

private:
Angle m_current;
Angle m_current;
const Molecule* m_mol;
};

Expand Down
22 changes: 12 additions & 10 deletions avogadro/core/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ class ArrayRefContainer
explicit ArrayRefContainer(const size_t n,
const ValueType& value = ValueType())
: m_ref(1), data(n, value)
{}
{
}

ArrayRefContainer(const ArrayRefContainer& other) : m_ref(1), data(other.data)
{}
{
}

template <typename InputIterator>
ArrayRefContainer(InputIterator first, InputIterator last)
: m_ref(1), data(first, last)
{}
{
}

// Increment the reference count.
void reref() { ++m_ref; }
Expand Down Expand Up @@ -118,19 +121,18 @@ class Array

explicit Array(const size_t n, const ValueType& value = ValueType())
: d(new Container(n, value))
{}
{
}

template <typename InputIterator>
Array(InputIterator first, InputIterator last) : d(new Container(first, last))
{}

/** Copy constructor, note the copy made of the internal data of other. */
Array(const Array& other)
{
other.d->reref();
d = other.d;
}

/** Copy constructor, note the copy made of the internal data of other. */
Array(const Array& other) : d(new Container(*other.d)) {}
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we don't want to do this. The Array class is copy-on-write. Please remove and/or tell cppcheck to ignore this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

//'d' is allocated new memory and a deep copy is one

~Array();

/**
Expand Down
9 changes: 4 additions & 5 deletions avogadro/core/variant.h
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this commit somehow has message from the previous commit, but changes in it are for the just next commit about removing unused private function

Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ class AVOGADROCORE_EXPORT Variant

private:
template <typename T>
static T lexical_cast(const std::string& string);
// static T lexical_cast(const std::string& string);

private:
Type m_type;
private : Type m_type;
union
{
bool _bool;
Expand All @@ -146,8 +145,8 @@ class AVOGADROCORE_EXPORT Variant
} m_value;
};

} // end Core namespace
} // end Avogadro namespace
} // namespace Core
} // namespace Avogadro

#include "variant-inline.h"

Expand Down