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: Proposed changes for increased performance #30

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
16 changes: 11 additions & 5 deletions src/algorithms/primes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ Primes::IsPrime(int n) {
* @param n Upper bound (non-inclusive) of the sum
* @return The sum of all prime numbers from 0 to n
*/
int
Primes::SumPrimes(int n) {
int Primes::SumPrimes(int n) {
int sum = 0;

for (int i = 0; i < n; i += 1) {
if (IsPrime(i)) {
for (int i = 2; i < n; i++) {
bool prime = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
if (prime) {
sum += i;
}
}
Expand All @@ -51,4 +57,4 @@ Primes::PrimeFactors(int n) {
}
}
return factors;
}
}
31 changes: 13 additions & 18 deletions src/algorithms/sort.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include "sort.h"

#include <algorithm>
Expand All @@ -20,19 +21,15 @@ Sort::SortVector(std::vector<int> &v) {
*/
void
Sort::DutchFlagPartition(std::vector<int> &v, int pivot_value) {
int next_value = 0;

for (int i = 0; i < (int) v.size(); i += 1) {
if (v[i] < pivot_value) {
std::swap(v[i], v[next_value]);
next_value += 1;
}
}

for (int i = next_value; i < (int) v.size(); i += 1) {
if (v[i] == pivot_value) {
std::swap(v[i], v[next_value]);
next_value += 1;
int smaller = 0, equal = 0, larger = v.size();

while (equal < larger) {
if (v[equal] < pivot_value) {
std::swap(v[smaller++], v[equal++]);
} else if (v[equal] == pivot_value) {
++equal;
} else {
std::swap(v[equal], v[--larger]);
}
}
}
Expand All @@ -50,11 +47,9 @@ Sort::MaxN(std::vector<int> &v, int n) {
// So that we don't modify the original vector
std::vector<int> temp(v);

std::sort(temp.begin(), temp.end());
std::partial_sort(temp.begin(), temp.begin() + n, temp.end(), std::greater<int>());

for (int i = (int) temp.size() - 1; i >= (int) temp.size() - n; i -= 1) {
ret.push_back(temp[i]);
}
ret.assign(temp.begin(), temp.begin() + n);

return ret;
}
}
24 changes: 11 additions & 13 deletions src/control/double.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <unordered_map>
#include "double.h"

/**
Expand Down Expand Up @@ -45,21 +46,18 @@ DoubleForLoop::SumTriangle(int n) {
* @param v
* @return the number of pairs in an vay
*/
int
DoubleForLoop::CountPairs(std::vector<int> v) {
int DoubleForLoop::CountPairs(std::vector<int> v) {
std::unordered_map<int, int> numCount;
for (const auto& num : v) {
numCount[num]++;
}
int count = 0;
for (int i = 0; i < (int) v.size(); i += 1) {
int nDuplicates = 0;
for (int j = 0; j < (int) v.size(); j += 1) {
if (v[i] == v[j]) {
nDuplicates += 1;
}
}
if (nDuplicates == 2) {
count += 1;
for (const auto& pair : numCount) {
if (pair.second == 2) {
count++;
}
}
return count / 2;
return count;
}

/**
Expand Down Expand Up @@ -98,4 +96,4 @@ DoubleForLoop::SumMatrix(std::vector<std::vector<int>> matrix) {
}
}
return sum;
}
}
26 changes: 5 additions & 21 deletions src/control/single.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@
* @param n the upper bound (non-inclusive)
* @return the sum of all integer values from 0 to n
*/
int
SingleForLoop::SumRange(int n) {
int array[n];
int sum = 0;
for (int i = 0; i < n; i += 1) {
array[i] = i;
}
for (int i = 0; i < n; i += 1) {
sum += array[i];
}
return sum;
}
int SingleForLoop::SumRange(int n) { int sum = 0; for (int i = 0; i < n; i += 1) { sum += i; } return sum; }

/**
* @brief Returns the maximum value in an array of size n
Expand Down Expand Up @@ -48,17 +37,12 @@ SingleForLoop::MaxVector(std::vector<int> &arr) {
* @param m the modulus
* @return the sum of all values from 0 to n that are divisible by m
*/
int
SingleForLoop::SumModulus(int n, int m) {
int array[n];
int SingleForLoop::SumModulus(int n, int m) {
int sum = 0;
for (int i = 0; i < n; i += 1) {
array[i] = i;
}
for (int i = 0; i < n; i += 1) {
if (array[i] % m == 0) {
sum += array[i];
if (i % m == 0) {
sum += i;
}
}
return sum;
}
}
24 changes: 7 additions & 17 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 @@ -24,8 +25,8 @@ OpsVector::PrintVector(std::vector<double> &v) {
*/
std::vector<int>
OpsVector::ModifyVector(std::vector<int> &v) {
for (int i = 0; i < (int) v.size(); i += 1) {
v.at(i) += 1;
for (auto &elem : v) {
elem += 1;
}
return v;
}
Expand Down Expand Up @@ -59,15 +60,8 @@ 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;
}
}
}
std::sort(ret.begin(), ret.end());

return ret;
}

Expand All @@ -79,11 +73,7 @@ OpsVector::SortVector(std::vector<int> &v) {
*/
std::vector<int>
OpsVector::ReverseVector(std::vector<int> &v) {
std::vector<int> ret;

for (int i = (int) v.size() - 1; i >= 0; i -= 1) {
ret.push_back(v[i]);
}
std::vector<int> ret(v.rbegin(), v.rend());
return ret;
}

Expand Down Expand Up @@ -126,4 +116,4 @@ OpsVector::MergeVectors(std::vector<int> &v1, std::vector<int> &v2) {
}

return ret;
}
}
27 changes: 15 additions & 12 deletions src/generator/genvector.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <random>
#include "genvector.h"
#include <random>
#include <vector>
Expand All @@ -10,11 +11,14 @@
*/
std::vector<int>
GenVector::RandomVector(int n, int m) {
std::vector<int> ret = std::vector<int>(n);
std::vector<int> ret(n);

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, m-1);

srand(0);
for (int i = 0; i < n; i += 1) {
ret[i] = rand() % m;
ret[i] = dis(gen);
}

return ret;
Expand Down Expand Up @@ -47,17 +51,16 @@ GenVector::RandomVectorDouble(int n, int l, int u) {
* @param m Upper bound (non-inclusive) of the values in the matrix
* @return A random square matrix of size n
*/
std::vector<std::vector<int>>
GenVector::RandomSquareMatrix(int n, int m) {
std::vector<std::vector<int>> ret = std::vector<std::vector<int>>(n);
std::vector<std::vector<int>> GenVector::RandomSquareMatrix(int n, int m) {
std::vector<std::vector<int>> ret(n, std::vector<int>(n));

srand(0);
for (int i = 0; i < n; i += 1) {
ret[i] = std::vector<int>(n);
for (int j = 0; j < n; j += 1) {
ret[i][j] = rand() % m;
std::mt19937 gen(0);
std::uniform_int_distribution<> dis(0, m-1);
for (auto &row : ret) {
for (auto &elem : row) {
elem = dis(gen);
}
}

return ret;
}
}
12 changes: 2 additions & 10 deletions src/strings/strops.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include "strops.h"

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;
}
std::string StrOps::ReverseString(std::string &s) { return std::string(s.rbegin(), s.rend()); }

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

return false;
}
}