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

View File

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