Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
PrimeNumbers.hpp
Go to the documentation of this file.
1
12#include "../../src/interfaces/AutonomyThread.hpp"
13#include "../../src/util/ExampleChecker.h"
14
16#include <iostream>
17#include <vector>
18
20
21
30{
31 private:
32 // Declare and define private methods and variables.
33 std::vector<int> m_vThreadPrimes;
34 int m_nCount = 10;
35 int m_nCurrentCount = 2;
36
37
47 bool IsPrime(int& nNum)
48 {
49 if (nNum <= 1)
50 {
51 return false;
52 }
53 for (int i = 2; i * i <= nNum; ++i)
54 {
55 if (nNum % i == 0)
56 {
57 return false;
58 }
59 }
60
61 return true;
62 }
63
64
72 void CalculatePrimes(int& nCount)
73 {
74 // Loop until we have the required amount of primes.
75 if (int(m_vThreadPrimes.size()) < nCount)
76 {
77 // Check if our current number is a prime.
78 if (IsPrime(m_nCurrentCount))
79 {
80 m_vThreadPrimes.push_back(m_nCurrentCount);
81 }
82
83 // Increment counter.
84 ++m_nCurrentCount;
85 }
86 }
87
88
95 void ThreadedContinuousCode() override
96 {
97 // Change this to calculate a different number of prime numbers.
98 CalculatePrimes(m_nCount);
99
100 // Check if we have reached the desired number of primes.
101 if (int(this->GetPrimes().size()) >= this->GetDesiredPrimeAmount())
102 {
103 // Call thread stop.
104 this->RequestStop();
105 }
106 }
107
108
116 void PooledLinearCode() override {}
117
118 public:
119 // Declare and define public methods and variables.
120 PrimeCalculatorThread() = default;
121
122
130 void SetPrimeCount(int nNum) { m_nCount = nNum; }
131
132
140 int GetDesiredPrimeAmount() { return m_nCount; }
141
142
150 std::vector<int> GetPrimes() { return m_vThreadPrimes; }
151
152
159 void ClearPrimes() { m_vThreadPrimes.clear(); }
160};
161
162
171{
172 // Declare prime calculator threads.
173 PrimeCalculatorThread ThreadedPrimeCalculator1;
174 PrimeCalculatorThread ThreadedPrimeCalculator2;
175 PrimeCalculatorThread ThreadedPrimeCalculator3;
176 PrimeCalculatorThread ThreadedPrimeCalculator4;
177 PrimeCalculatorThread ThreadedPrimeCalculator5;
178
179
182 // Calc 1.
183 ThreadedPrimeCalculator1.SetPrimeCount(5);
184 ThreadedPrimeCalculator1.Start();
185 // Calc 2.
186 ThreadedPrimeCalculator2.SetPrimeCount(50);
187 ThreadedPrimeCalculator2.Start();
188 // Calc 3.
189 ThreadedPrimeCalculator3.SetPrimeCount(10000);
190 ThreadedPrimeCalculator3.Start();
191 // Calc 4.
192 ThreadedPrimeCalculator4.SetPrimeCount(500);
193 ThreadedPrimeCalculator4.Start();
194 // Calc 5. Example of a thread that takes to long.
195 ThreadedPrimeCalculator5.SetPrimeCount(9999999);
196 ThreadedPrimeCalculator5.Start();
197
198 // Wait for threads to finish.
199 ThreadedPrimeCalculator1.Join();
200 ThreadedPrimeCalculator2.Join();
201 ThreadedPrimeCalculator3.Join();
202 ThreadedPrimeCalculator4.Join();
203 // This thread will take took long, stop it prematurely.
204 ThreadedPrimeCalculator5.RequestStop();
205 ThreadedPrimeCalculator5.Join();
206
207 // Print length of calculated primes vectors.
208 std::cout << "Creating separate threads:" << std::endl;
209 std::vector<int> vPrimes = ThreadedPrimeCalculator1.GetPrimes();
210 std::cout << "Calculator1 Primes Length: " << vPrimes.size() << std::endl;
211 vPrimes = ThreadedPrimeCalculator2.GetPrimes();
212 std::cout << "Calculator2 Primes Length: " << vPrimes.size() << std::endl;
213 vPrimes = ThreadedPrimeCalculator3.GetPrimes();
214 std::cout << "Calculator3 Primes Length: " << vPrimes.size() << std::endl;
215 vPrimes = ThreadedPrimeCalculator4.GetPrimes();
216 std::cout << "Calculator4 Primes Length: " << vPrimes.size() << std::endl;
217 std::cout << "\n\nThis thread was stopped prematurely before reaching 9999999." << std::endl;
218 vPrimes = ThreadedPrimeCalculator5.GetPrimes();
219 std::cout << "Calculator5 Primes Length: " << vPrimes.size() << std::endl;
220
221 // Clear Calculator5 and restart the thread.
222 // Don't join before program exits to demonstrate
223 // graceful shutdowns when user doesn't do what they're supposed to.
224 ThreadedPrimeCalculator5.ClearPrimes();
225 ThreadedPrimeCalculator5.Start();
226 // Print info to user.
227 std::cout << "Calculator5 was restarted and then main program exited without joining. (graceful shutdown demo)" << std::endl;
228 vPrimes = ThreadedPrimeCalculator5.GetPrimes();
229 std::cout << "Calculator5 Primes Length: " << vPrimes.size() << std::endl;
230}
void RunExample()
Main example method.
Definition PrimeNumbers.hpp:170
Interface class used to easily multithread a child class.
Definition AutonomyThread.hpp:38
void Join()
Waits for thread to finish executing and then closes thread. This method will block the calling code ...
Definition AutonomyThread.hpp:180
void RequestStop()
Signals threads to stop executing user code, terminate. DOES NOT JOIN. This method will not force the...
Definition AutonomyThread.hpp:164
void Start()
When this method is called, it starts a new thread that runs the code within the ThreadedContinuousCo...
Definition AutonomyThread.hpp:117
This class creates a thread for calculating N prime numbers. This is an example class demonstrating t...
Definition PrimeNumbers.hpp:30
void ThreadedContinuousCode() override
This code will run in a separate thread. Main code goes here.
Definition PrimeNumbers.hpp:95
void ClearPrimes()
Clears the prime results vector.
Definition PrimeNumbers.hpp:159
void SetPrimeCount(int nNum)
Mutator for the Prime Count private member.
Definition PrimeNumbers.hpp:130
bool IsPrime(int &nNum)
Check if a number if prime.
Definition PrimeNumbers.hpp:47
void CalculatePrimes(int &nCount)
Calculate 'count' number of primes.
Definition PrimeNumbers.hpp:72
int GetDesiredPrimeAmount()
Accessor for the Prime Counter private member.
Definition PrimeNumbers.hpp:140
std::vector< int > GetPrimes()
Accessor for the Primes private member.
Definition PrimeNumbers.hpp:150
void PooledLinearCode() override
Any highly parallelizable code that can be used in the main thread goes here.
Definition PrimeNumbers.hpp:116
GOpaque< Size > size(const GMat &src)