34 template <
typename T, nat_t CAP = 100>
46 std::swap(array, q.array);
47 std::swap(num_items, q.num_items);
60 : num_items(0), f(0), r(CAP - 1)
66 : num_items(q.num_items)
82 num_items = q.num_items;
98 return num_items == 0;
103 return num_items == CAP;
126 throw std::underflow_error(
"Queue is empty");
134 throw std::underflow_error(
"Queue is empty");
142 throw std::underflow_error(
"Queue is empty");
150 throw std::underflow_error(
"Queue is empty");
158 throw std::overflow_error(
"Queue is full");
169 throw std::overflow_error(
"Queue is full");
172 array[r] = std::move(item);
180 throw std::underflow_error(
"Queue is empty");
182 T ret_val = std::move(array[f]);
189 template <
typename T, nat_t CAP>
194 for (
nat_t i = 0; i < num_items; ++i)
196 array[i] = q.array[ii];
204 template <
typename T>
209 static constexpr
nat_t MIN_SIZE = 32;
210 static constexpr
real_t RESIZE_FACTOR = 0.4;
221 std::swap(num_items, q.num_items);
230 if (num_items < BaseArray::get_capacity())
233 assert(BaseArray::get_capacity() * (1 + RESIZE_FACTOR) > num_items);
235 resize(BaseArray::get_capacity() * (1 + RESIZE_FACTOR));
240 if (num_items > BaseArray::get_capacity() * RESIZE_FACTOR or
241 BaseArray::get_capacity() == MIN_SIZE)
244 assert(BaseArray::get_capacity() * (1 - RESIZE_FACTOR) > num_items);
246 resize(BaseArray::get_capacity() * (1 - RESIZE_FACTOR));
257 :
BaseArray(MIN_SIZE), num_items(0), f(0), r(MIN_SIZE - 1)
280 BaseArray::swap(arr);
281 num_items = q.num_items;
295 return num_items == 0;
307 if (BaseArray::get_capacity() != MIN_SIZE)
310 BaseArray::swap(new_array);
320 throw std::underflow_error(
"Queue is empty");
322 return BaseArray::at(f);
328 throw std::underflow_error(
"Queue is empty");
330 return BaseArray::at(f);
336 throw std::underflow_error(
"Queue is empty");
338 return BaseArray::at(r);
344 throw std::underflow_error(
"Queue is empty");
346 return BaseArray::at(r);
351 r = (r + 1) % BaseArray::get_capacity();
352 BaseArray::at(r) = item;
355 return BaseArray::at(r);
360 r = (r + 1) % BaseArray::get_capacity();
361 BaseArray::at(r) = std::move(item);
364 return BaseArray::at(r);
370 throw std::underflow_error(
"Queue is empty");
372 T ret_val = std::move(BaseArray::at(f));
373 f = (f + 1) % BaseArray::get_capacity();
380 template <
typename T>
385 for (
nat_t i = 0; i < num_items; ++i)
387 BaseArray::at(i) = q.
at(ii);
388 ii = (ii + 1) % BaseArray::get_capacity();
395 template <
typename T>
405 for (
nat_t i = 0; i < num_items; ++i)
407 new_array.
at(i) = BaseArray::at(ii);
408 ii = (ii + 1) % BaseArray::get_capacity();
411 BaseArray::swap(new_array);
417 template <
typename T>
464 return BaseList::is_empty();
469 return BaseList::size();
480 throw std::underflow_error(
"Queue is empty");
482 return BaseList::get_first();
488 throw std::underflow_error(
"Queue is empty");
490 return BaseList::get_first();
496 throw std::underflow_error(
"Queue is empty");
498 return BaseList::get_last();
504 throw std::underflow_error(
"Queue is empty");
506 return BaseList::get_last();
511 return BaseList::append(item);
516 return BaseList::append(std::forward<T>(item));
522 throw std::underflow_error(
"Queue is empty");
524 return BaseList::remove_first();
528 template <
typename T,
class Queue = ListQueue<T>>
532 std::condition_variable cond_var;
538 std::lock_guard<std::mutex> lck(mtx);
539 T & ret = queue.put(item);
540 cond_var.notify_one();
546 std::lock_guard<std::mutex> lck(mtx);
547 T & ret = queue.put(std::move(item));
548 cond_var.notify_one();
554 std::unique_lock<std::mutex> lck(mtx);
555 cond_var.wait(lck, [
this] {
return not queue.is_empty(); });
561 std::lock_guard<std::mutex> lck(const_cast<std::mutex &>(mtx));
567 std::lock_guard<std::mutex> lck(const_cast<std::mutex &>(mtx));
568 return queue.is_empty();
574 # endif // DSGQUEUE_H nat_t size() const
Definition: queue.H:298
T & front()
Definition: queue.H:123
T DataType
Definition: array.H:194
bool is_empty() const
Definition: queue.H:565
FixedQueue(const FixedQueue &q)
Definition: queue.H:65
T & rear()
Definition: queue.H:139
const T & front() const
Definition: queue.H:485
bool is_empty() const
Definition: queue.H:96
T & put(T &&item)
Definition: queue.H:358
DynQueue(const DynQueue &q)
Definition: queue.H:262
double real_t
Definition: types.H:51
T ValueType
Definition: list.H:211
T & put(const T &item)
Definition: queue.H:536
T ItemType
Definition: queue.H:53
bool is_full() const
Definition: queue.H:101
T DataType
Definition: queue.H:55
T & rear()
Definition: queue.H:333
const T & rear() const
Definition: queue.H:341
T & put(T &&item)
Definition: queue.H:544
const T & rear() const
Definition: queue.H:501
const T & rear() const
Definition: queue.H:147
void clear()
Definition: queue.H:472
T KeyType
Definition: list.H:209
T & put(T &&item)
Definition: queue.H:514
FixedQueue()
Definition: queue.H:59
FixedQueue(FixedQueue &&q)
Definition: queue.H:71
bool is_empty() const
Definition: queue.H:293
nat_t size() const
Definition: queue.H:106
nat_t size() const
Definition: queue.H:559
bool is_empty() const
Definition: queue.H:462
T ItemType
Definition: array.H:192
nat_t size() const
Definition: queue.H:467
T & put(const T &item)
Definition: queue.H:155
void clear()
Definition: queue.H:116
T KeyType
Definition: queue.H:54
nat_t SizeType
Definition: queue.H:57
T & front()
Definition: queue.H:477
Definition: italgorithms.H:33
ListQueue()
Definition: queue.H:429
T & put(T &&item)
Definition: queue.H:166
T & rear()
Definition: queue.H:493
void clear()
Definition: queue.H:303
T DataType
Definition: list.H:210
nat_t SizeType
Definition: array.H:196
nat_t SizeType
Definition: list.H:212
nat_t get_capacity() const
Definition: array.H:266
T ValueType
Definition: array.H:195
unsigned long int nat_t
Definition: types.H:50
ListQueue(const ListQueue &q)
Definition: queue.H:435
DynQueue()
Definition: queue.H:256
nat_t get_capacity() const
Definition: queue.H:111
T ValueType
Definition: queue.H:56
FixedQueue & operator=(const FixedQueue &q)
Definition: queue.H:77
T & put(const T &item)
Definition: queue.H:349
DynQueue(DynQueue &&q)
Definition: queue.H:268
const T & front() const
Definition: queue.H:325
T & front()
Definition: queue.H:317
T ItemType
Definition: list.H:208
const T & front() const
Definition: queue.H:131
T KeyType
Definition: array.H:193
T & at(nat_t i)
Definition: array.H:276
ListQueue(ListQueue &&q)
Definition: queue.H:441
T & put(const T &item)
Definition: queue.H:509