Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
randomqueue.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <tpl_random_queue.H>
41
42using namespace Aleph;
43using namespace testing;
44
45namespace {
46
47Random_Set<int> build_queue(const std::initializer_list<int> values)
48{
50 for (int v : values)
51 q.put(v);
52 return q;
53}
54
55} // namespace
56
58{
60 auto &ref = q.append(42);
61 ref = 13; // mutate via reference
62
64 bool found = false;
65 q.traverse([&found] (const int v) { found |= (v == 13); return true; });
67}
68
70{
72 for (int i = 0; i < 10; ++i)
73 q.append(i);
74
75 // insert sentinel twice: once to mutate reference, once to ensure random placement
76 auto &first = q.append(100);
77 first = 100;
78 q.append(100);
79
80 size_t count = 0;
81 q.traverse([&count] (const int v)
82 {
83 if (v == 100)
84 ++count;
85 return true;
86 });
87 EXPECT_EQ(2u, count);
88}
89
91{
92 Random_Set<int> q = build_queue({1, 2, 3, 4});
93 size_t seen = 0;
94 while (not q.is_empty())
95 {
96 int val = q.get();
97 EXPECT_GE(val, 1);
98 EXPECT_LE(val, 4);
99 ++seen;
100 }
101 EXPECT_EQ(4u, seen);
102}
103
105{
106 struct Payload
107 {
108 std::string data;
109 Payload() = default;
110 explicit Payload(std::string d) : data(std::move(d)) {}
111 };
112
114 const auto &stored = q.append(Payload("beta"));
115 EXPECT_EQ("beta", stored.data);
116
117 auto &stored2 = q.append(Payload("gamma"));
118 stored2.data = "delta";
119
120 size_t hits = 0;
121 q.traverse([&hits] (const Payload &p)
122 {
123 if (p.data == "delta" || p.data == "beta")
124 ++hits;
125 return true;
126 });
127 EXPECT_EQ(2u, hits);
128}
bool traverse(Operation &operation)
Conditional traversing of the random queue.
bool is_empty() const
Return true if the queue is empty.
T get()
Extract randomly an item.
T & append(const T &item)
Insert randomly an item by copy.
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Random access queue (bag) with O(1) random pop.