Skip to content

Commit

Permalink
Use std::less for default sorting alg; added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
klingaard committed Oct 26, 2023
1 parent 215bd1c commit efc8f59
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
13 changes: 2 additions & 11 deletions sparta/sparta/resources/PriorityQueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,14 @@

#include <list>
#include <algorithm>
#include <functional>

#include "sparta/utils/SpartaAssert.hpp"
#include "sparta/utils/FastList.hpp"

namespace sparta
{

// A default sorting algorithm
template<class DataT>
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
Expand Down Expand Up @@ -59,7 +51,7 @@ namespace sparta
*
*/
template <class DataT,
class SortingAlgorithmT = DefaultSortingAlgorithm<DataT>,
class SortingAlgorithmT = std::less<DataT>,
size_t bounded_cnt=0>
class PriorityQueue
{
Expand Down Expand Up @@ -186,7 +178,6 @@ namespace sparta
private:

//! The internal queue
//PQueueType priority_items_{bounded_cnt, DataT()};
PQueueType priority_items_;

//! Copy of the sorting algorithm
Expand Down
60 changes: 59 additions & 1 deletion sparta/test/PriorityQueue/PriorityQueue_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

#include "sparta/resources/PriorityQueue.hpp"

#include <cinttypes>
#include <iostream>
#include <chrono>

#include "sparta/resources/PriorityQueue.hpp"
#include "sparta/utils/SpartaTester.hpp"

constexpr bool TESTPERF = false;

void test_defafult_pq()
{
Expand Down Expand Up @@ -142,11 +145,66 @@ void test_custom_order_pq()
// }
}

#define PERF_TEST 100000000
template<class ListType>
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<int, std::less<int>, 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<sparta::PriorityQueue<int, std::less<int>, 10>>();
auto end = std::chrono::system_clock::system_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "Raw time (seconds) fast list : " << dur / 1000000.0 << std::endl;

start = std::chrono::system_clock::system_clock::now();
testListPerf<sparta::PriorityQueue<int>>();
end = std::chrono::system_clock::system_clock::now();
dur = std::chrono::duration_cast<std::chrono::microseconds>(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;
}

0 comments on commit efc8f59

Please sign in to comment.