diff --git a/sparta/sparta/resources/PriorityQueue.hpp b/sparta/sparta/resources/PriorityQueue.hpp index 650e9f1544..659db4c8a0 100644 --- a/sparta/sparta/resources/PriorityQueue.hpp +++ b/sparta/sparta/resources/PriorityQueue.hpp @@ -10,6 +10,7 @@ #include #include +#include #include "sparta/utils/SpartaAssert.hpp" #include "sparta/utils/FastList.hpp" @@ -17,15 +18,6 @@ namespace sparta { - // A default sorting algorithm - template - struct DefaultSortingAlgorithm - { - bool operator()(const DataT & existing, const DataT & to_be_inserted) const { - return existing < to_be_inserted; - } - }; - /** * \class PriorityQueue * \brief A data structure that allows pushing/emplacing into it @@ -59,7 +51,7 @@ namespace sparta * */ template , + class SortingAlgorithmT = std::less, size_t bounded_cnt=0> class PriorityQueue { @@ -186,7 +178,6 @@ namespace sparta private: //! The internal queue - //PQueueType priority_items_{bounded_cnt, DataT()}; PQueueType priority_items_; //! Copy of the sorting algorithm diff --git a/sparta/test/PriorityQueue/PriorityQueue_test.cpp b/sparta/test/PriorityQueue/PriorityQueue_test.cpp index 46337d789f..6e6d94e441 100644 --- a/sparta/test/PriorityQueue/PriorityQueue_test.cpp +++ b/sparta/test/PriorityQueue/PriorityQueue_test.cpp @@ -1,10 +1,13 @@ +#include "sparta/resources/PriorityQueue.hpp" + #include #include +#include -#include "sparta/resources/PriorityQueue.hpp" #include "sparta/utils/SpartaTester.hpp" +constexpr bool TESTPERF = false; void test_defafult_pq() { @@ -142,11 +145,66 @@ void test_custom_order_pq() // } } +#define PERF_TEST 100000000 +template +void testListPerf() +{ + ListType fl; + const int num_elems = 10; + for(int i = 0; i < PERF_TEST; ++i) { + for(size_t i = 0; i < num_elems; ++i) { + fl.insert(i); + } + + const auto end = fl.end(); + for(auto it = fl.begin(); it != end;) { + fl.erase(it++); + } + } +} + +void test_fastlist_vs_list() +{ + // Uses sparta::FastList + sparta::PriorityQueue, 10> bounded_pq; + for(auto i : {1,3,2,-7,6,4,-8,7,-3,8}) { + bounded_pq.insert(i); + } + + EXPECT_EQUAL(bounded_pq.top(), -8); + bounded_pq.pop(); + EXPECT_EQUAL(bounded_pq.top(), -7); + + bounded_pq.insert(10); + EXPECT_EQUAL(bounded_pq.top(), -7); + + // out of room + EXPECT_THROW(bounded_pq.insert(11)); + + if constexpr(TESTPERF) + { + auto start = std::chrono::system_clock::system_clock::now(); + testListPerf, 10>>(); + auto end = std::chrono::system_clock::system_clock::now(); + auto dur = std::chrono::duration_cast(end - start).count(); + std::cout << "Raw time (seconds) fast list : " << dur / 1000000.0 << std::endl; + + start = std::chrono::system_clock::system_clock::now(); + testListPerf>(); + end = std::chrono::system_clock::system_clock::now(); + dur = std::chrono::duration_cast(end - start).count(); + std::cout << "Raw time (seconds) old list : " << dur / 1000000.0 << std::endl; + } +} + + int main() { test_defafult_pq(); test_custom_order_pq(); + test_fastlist_vs_list(); + REPORT_ERROR; return ERROR_CODE; }