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

Artemis: Feature: Code Refactoring for Improved Performance and Efficiency #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions src/control/single.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ SingleForLoop::SumRange(int n) {
* which will make the unit tests fail, so we need a way
* to make sure that doesn't happen
*/
int
SingleForLoop::MaxVector(std::vector<int> &arr) {
int SingleForLoop::MaxVector(std::vector<int> &arr) {
int max = 0;
for (int i = 0; i < (int) arr.size(); i += 1) {
for (int i = 0, size = (int) arr.size(); i < size; i += 1) {
if (arr[i] > max) {
max = arr[i];
}
Expand Down Expand Up @@ -61,4 +60,4 @@ SingleForLoop::SumModulus(int n, int m) {
}
}
return sum;
}
}
31 changes: 9 additions & 22 deletions src/datastructures/vector.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include "vector.h"

void
Expand All @@ -10,7 +11,7 @@ OpsVector::PrintVector(std::vector<int> &v) {

void
OpsVector::PrintVector(std::vector<double> &v) {
for (int i = 0; i < (int) v.size(); i += 1) {
for (size_t i = 0; i < v.size(); i += 1) {
std::cout << v[i] << " ";
}
std::cout << std::endl;
Expand Down Expand Up @@ -55,20 +56,9 @@ OpsVector::SearchVector(std::vector<int> &v, int n) {
* @param v Vector to sort.
* @return The sorted vector.
*/
std::vector<int>
OpsVector::SortVector(std::vector<int> &v) {
std::vector<int> ret(v);

for (int i = 0; i < (int) ret.size(); i += 1) {
for (int j = 0; j < (int) ret.size() - 1; j += 1) {
if (ret[j] > ret[j + 1]) {
int temp = ret[j];
ret[j] = ret[j + 1];
ret[j + 1] = temp;
}
}
}
return ret;
std::vector<int> OpsVector::SortVector(std::vector<int> &v) {
std::sort(v.begin(), v.end());
return v;
}

/**
Expand Down Expand Up @@ -118,12 +108,9 @@ std::vector<int>
OpsVector::MergeVectors(std::vector<int> &v1, std::vector<int> &v2) {
std::vector<int> ret;

for (int i = 0; i < (int) v1.size(); i += 1) {
ret.push_back(v1[i]);
}
for (int i = 0; i < (int) v2.size(); i += 1) {
ret.push_back(v2[i]);
}
ret.reserve(v1.size() + v2.size());
ret.insert(ret.end(), v1.begin(), v1.end());
ret.insert(ret.end(), v2.begin(), v2.end());

return ret;
}
}
13 changes: 6 additions & 7 deletions src/math/vectoralgebra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@ MathVectorAlgebra::Cubic(int a, int b, int c, int d, std::vector<double> &x) {
*
* @return Pointer to array of y values (output)
*/
std::vector<double>
MathVectorAlgebra::Convolve(int offset, std::vector<double> &h, std::vector<double> &x) {
std::vector<double> MathVectorAlgebra::Convolve(int offset, std::vector<double> &h, std::vector<double> &x) {
int m = h.size();
int n = x.size();
int sum_h = 0;
std::vector<double> y = std::vector<double>(n);
double sum_h = 0;
std::vector<double> y(n, 0);
for (int i = 0; i < m; i += 1) {
sum_h += std::abs(h[i]);
}
if (sum_h == 0) {
sum_h = 1;
}
for (int i = 0; i < n; i += 1) {
y[i] = 0;
for (int j = 0; j < m; j += 1) {
if (i - offset + j >= 0 && i - offset + j < n) {
y[i] += h[j] * x[i - offset + j] / sum_h;
int idx = i - offset + j;
if (idx >= 0 && idx < n) {
y[i] += h[j] * x[idx] / sum_h;
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/strings/strops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

std::string
StrOps::ReverseString(std::string &s) {
std::string ret;

for (std::string::reverse_iterator rit = s.rbegin(); rit != s.rend(); ++rit) {
ret.insert(ret.end(), *rit);
}
return ret;
return {s.crbegin(), s.crend()};
}

bool
Expand All @@ -18,4 +13,4 @@ StrOps::IsPalindrome(const std::string &s) {
}

return false;
}
}