1
0
Fork 0

Initialize ConcatenableQueue name in constructor

This commit is contained in:
Joris van Rantwijk 2024-11-16 23:18:09 +01:00
parent e8020f3e58
commit 5b5c107a5c
2 changed files with 34 additions and 44 deletions

View File

@ -84,9 +84,9 @@ public:
* The node must be contained in a queue. * The node must be contained in a queue.
* This function takes time O(log(n)). * This function takes time O(log(n)).
*/ */
NameType find() NameType find() const
{ {
Node* node = this; const Node* node = this;
while (node->parent_) { while (node->parent_) {
node = node->parent_; node = node->parent_;
} }
@ -150,8 +150,9 @@ public:
}; };
/** Construct an empty queue. */ /** Construct an empty queue. */
ConcatenableQueue() explicit ConcatenableQueue(const NameType& name)
: tree_(nullptr) : name_(name)
, tree_(nullptr)
, first_node_(nullptr) , first_node_(nullptr)
, first_subqueue_(nullptr) , first_subqueue_(nullptr)
, next_subqueue_(nullptr) , next_subqueue_(nullptr)
@ -167,12 +168,6 @@ public:
return name_; return name_;
} }
/** Assign a name to this queue. */
void set_name(const NameType& name)
{
name_ = name;
}
/** /**
* Insert the specified Node into the queue. * Insert the specified Node into the queue.
* *
@ -829,7 +824,7 @@ private:
} }
/** Name of this queue. */ /** Name of this queue. */
NameType name_; const NameType name_;
/** Root node of the queue, or "nullptr" if the queue is empty. */ /** Root node of the queue, or "nullptr" if the queue is empty. */
Node* tree_; Node* tree_;

View File

@ -28,9 +28,7 @@ BOOST_AUTO_TEST_SUITE(test_concatenable_queue)
BOOST_AUTO_TEST_CASE(test_single) BOOST_AUTO_TEST_CASE(test_single)
{ {
using Queue = ConcatenableQueue<int, std::string, std::string>; using Queue = ConcatenableQueue<int, std::string, std::string>;
Queue q; Queue q("Q");
q.set_name("Q");
BOOST_TEST(q.name() == std::string("Q")); BOOST_TEST(q.name() == std::string("Q"));
Queue::Node n; Queue::Node n;
@ -47,9 +45,6 @@ BOOST_AUTO_TEST_CASE(test_single)
BOOST_TEST(n.find() == std::string("Q")); BOOST_TEST(n.find() == std::string("Q"));
BOOST_TEST(q.min_prio() == 8); BOOST_TEST(q.min_prio() == 8);
BOOST_TEST(q.min_elem() == std::string("a")); BOOST_TEST(q.min_elem() == std::string("a"));
q.set_name("R");
BOOST_TEST(n.find() == std::string("R"));
} }
BOOST_AUTO_TEST_CASE(test_simple) BOOST_AUTO_TEST_CASE(test_simple)
@ -57,29 +52,23 @@ BOOST_AUTO_TEST_CASE(test_simple)
using Queue = ConcatenableQueue<int, std::string, char>; using Queue = ConcatenableQueue<int, std::string, char>;
Queue::Node n1, n2, n3, n4, n5; Queue::Node n1, n2, n3, n4, n5;
Queue q1; Queue q1("A");
q1.set_name("A");
q1.insert(&n1, 5, 'a'); q1.insert(&n1, 5, 'a');
Queue q2; Queue q2("B");
q2.set_name("B");
q2.insert(&n2, 6, 'b'); q2.insert(&n2, 6, 'b');
Queue q3; Queue q3("C");
q3.set_name("C");
q3.insert(&n3, 7, 'c'); q3.insert(&n3, 7, 'c');
Queue q4; Queue q4("D");
q4.set_name("D");
q4.insert(&n4, 4, 'd'); q4.insert(&n4, 4, 'd');
Queue q5; Queue q5("E");
q5.set_name("E");
q5.insert(&n5, 3, 'e'); q5.insert(&n5, 3, 'e');
auto m345 = {&q3, &q4, &q5}; auto m345 = {&q3, &q4, &q5};
Queue q345; Queue q345("P");
q345.set_name("P");
q345.merge(m345.begin(), m345.end()); q345.merge(m345.begin(), m345.end());
BOOST_TEST(n1.find() == std::string("A")); BOOST_TEST(n1.find() == std::string("A"));
@ -95,8 +84,7 @@ BOOST_AUTO_TEST_CASE(test_simple)
BOOST_TEST(q345.min_elem() == 'd'); BOOST_TEST(q345.min_elem() == 'd');
auto m12 = {&q1, &q2}; auto m12 = {&q1, &q2};
Queue q12; Queue q12("Q");
q12.set_name("Q");
q12.merge(m12.begin(), m12.end()); q12.merge(m12.begin(), m12.end());
BOOST_TEST(n1.find() == std::string("Q")); BOOST_TEST(n1.find() == std::string("Q"));
@ -105,8 +93,7 @@ BOOST_AUTO_TEST_CASE(test_simple)
BOOST_TEST(q12.min_elem() == 'a'); BOOST_TEST(q12.min_elem() == 'a');
auto m12345 = {&q12, &q345}; auto m12345 = {&q12, &q345};
Queue q12345; Queue q12345("R");
q12345.set_name("R");
q12345.merge(m12345.begin(), m12345.end()); q12345.merge(m12345.begin(), m12345.end());
BOOST_TEST(n1.find() == std::string("R")); BOOST_TEST(n1.find() == std::string("R"));
@ -155,7 +142,17 @@ BOOST_AUTO_TEST_CASE(test_medium)
{ {
using Queue = ConcatenableQueue<int, char, char>; using Queue = ConcatenableQueue<int, char, char>;
Queue queues[19]; std::vector<char> queue_names(19);
for (int i = 0; i < 14; i++) {
queue_names[i] = 'A' + i;
}
queue_names[14] = 'P';
queue_names[15] = 'Q';
queue_names[16] = 'R';
queue_names[17] = 'S';
queue_names[18] = 'Z';
std::vector<Queue> queues(queue_names.begin(), queue_names.end());
Queue::Node nodes[14]; Queue::Node nodes[14];
int prios[14] = {3, 8, 6, 2, 9, 4, 6, 8, 1, 5, 9, 4, 7, 8}; int prios[14] = {3, 8, 6, 2, 9, 4, 6, 8, 1, 5, 9, 4, 7, 8};
@ -172,29 +169,29 @@ BOOST_AUTO_TEST_CASE(test_medium)
}; };
for (int i = 0; i < 14; i++) { for (int i = 0; i < 14; i++) {
queues[i].set_name('A' + i); BOOST_TEST(queues[i].name() == 'A' + i);
queues[i].insert(&nodes[i], prios[i], 'a' + i); queues[i].insert(&nodes[i], prios[i], 'a' + i);
} }
auto m14 = {&queues[0], &queues[1]}; auto m14 = {&queues[0], &queues[1]};
queues[14].set_name('P');
queues[14].merge(m14.begin(), m14.end()); queues[14].merge(m14.begin(), m14.end());
BOOST_TEST(queues[14].name() == 'P');
BOOST_TEST(queues[14].min_prio() == min_prio(0, 2)); BOOST_TEST(queues[14].min_prio() == min_prio(0, 2));
auto m15 = {&queues[2], &queues[3], &queues[4]}; auto m15 = {&queues[2], &queues[3], &queues[4]};
queues[15].set_name('Q');
queues[15].merge(m15.begin(), m15.end()); queues[15].merge(m15.begin(), m15.end());
BOOST_TEST(queues[15].name() == 'Q');
BOOST_TEST(queues[15].min_prio() == min_prio(2, 5)); BOOST_TEST(queues[15].min_prio() == min_prio(2, 5));
auto m16 = {&queues[5], &queues[6], &queues[7], &queues[8]}; auto m16 = {&queues[5], &queues[6], &queues[7], &queues[8]};
queues[16].set_name('R');
queues[16].merge(m16.begin(), m16.end()); queues[16].merge(m16.begin(), m16.end());
BOOST_TEST(queues[16].name() == 'R');
BOOST_TEST(queues[16].min_prio() == min_prio(5, 9)); BOOST_TEST(queues[16].min_prio() == min_prio(5, 9));
auto m17 = {&queues[9], &queues[10], &queues[11], &queues[12], auto m17 = {&queues[9], &queues[10], &queues[11], &queues[12],
&queues[13]}; &queues[13]};
queues[17].set_name('S');
queues[17].merge(m17.begin(), m17.end()); queues[17].merge(m17.begin(), m17.end());
BOOST_TEST(queues[17].name() == 'S');
BOOST_TEST(queues[17].min_prio() == min_prio(9, 14)); BOOST_TEST(queues[17].min_prio() == min_prio(9, 14));
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
@ -211,8 +208,8 @@ BOOST_AUTO_TEST_CASE(test_medium)
} }
auto m18 = {&queues[14], &queues[15], &queues[16], &queues[17]}; auto m18 = {&queues[14], &queues[15], &queues[16], &queues[17]};
queues[18].set_name('Z');
queues[18].merge(m18.begin(), m18.end()); queues[18].merge(m18.begin(), m18.end());
BOOST_TEST(queues[18].name() == 'Z');
BOOST_TEST(queues[18].min_prio() == 1); BOOST_TEST(queues[18].min_prio() == 1);
BOOST_TEST(queues[18].min_elem() == 'i'); BOOST_TEST(queues[18].min_elem() == 'i');
@ -278,8 +275,7 @@ BOOST_AUTO_TEST_CASE(test_random)
for (int i = 0; i < NUM_NODES; i++) { for (int i = 0; i < NUM_NODES; i++) {
int name = 10000 + i; int name = 10000 + i;
prios[i] = prio_distribution(rng); prios[i] = prio_distribution(rng);
queues[name] = std::unique_ptr<Queue>(new Queue); queues[name] = std::unique_ptr<Queue>(new Queue(name));
queues[name]->set_name(name);
queues[name]->insert(&nodes[i], prios[i], i); queues[name]->insert(&nodes[i], prios[i], i);
queue_nodes[name].insert(i); queue_nodes[name].insert(i);
live_queues.insert(name); live_queues.insert(name);
@ -340,8 +336,7 @@ BOOST_AUTO_TEST_CASE(test_random)
// Create new queue by merging selected queues. // Create new queue by merging selected queues.
int name = 20000 + i; int name = 20000 + i;
queues[name] = std::unique_ptr<Queue>(new Queue); queues[name] = std::unique_ptr<Queue>(new Queue(name));
queues[name]->set_name(name);
queues[name]->merge(sub_queues.begin(), sub_queues.end()); queues[name]->merge(sub_queues.begin(), sub_queues.end());
for (int nn : sub_names) { for (int nn : sub_names) {
queue_nodes[name].insert(queue_nodes[nn].begin(), queue_nodes[name].insert(queue_nodes[nn].begin(),