2 # ifndef TPL_ARRAYQUEUE_H
3 # define TPL_ARRAYQUEUE_H
7 # include <tpl_memArray.H>
45 void increase_index(
size_t & i,
const size_t inc = 1)
const
51 T & rear_item(
const size_t i = 0)
53 return this->access((
size_t) (rear_index - i - 1) % this->dim);
56 const T & rear_item(
const size_t i = 0)
const
58 return this->access((
size_t) (rear_index - i - 1) % this->dim);
66 std::swap(front_index, q.front_index);
67 std::swap(rear_index, q.rear_index);
79 :
MemArray<T>(sz), front_index(0), rear_index(0)
85 :
MemArray<T>(q), front_index(q.front_index), rear_index(q.rear_index)
92 front_index(q.front_index), rear_index(q.rear_index)
104 front_index = q.front_index;
105 rear_index = q.rear_index;
112 std::swap(front_index, q.front_index);
113 std::swap(rear_index, q.rear_index);
122 T & ret_val = this->access(rear_index);
123 increase_index(rear_index);
138 T &
put(
const T & item)
throw(std::exception, std::bad_alloc)
140 if (this->expand(front_index))
143 rear_index = this->n;
145 this->access(rear_index) = item;
146 return complete_put();
149 T &
put(T && item) throw(std::exception, std::bad_alloc)
151 if (this->expand(front_index))
154 rear_index = this->n;
156 this->access(rear_index) = std::move(item);
157 return complete_put();
176 T &
putn(
size_t sz)
throw(std::exception, std::bad_alloc)
178 const int avail_n = this->dim - this->n;
181 sz -= this->dim - this->n;
183 this->expand(front_index);
186 increase_index(rear_index, sz);
199 T
get()
throw(std::exception, std::underflow_error, std::bad_alloc)
202 throw std::underflow_error (
"queue is empty");
204 T ret_val = std::move(this->access(front_index));
206 increase_index(front_index);
208 if (this->contract(front_index))
211 rear_index = this->n;
227 T &
getn(
const size_t i)
throw(std::exception, std::underflow_error)
230 throw std::underflow_error (
"queue is empty");
233 increase_index(front_index, i);
235 if (this->contract(front_index))
238 rear_index = this->n;
241 return this->access(front_index);
254 T &
front(
const size_t i = 0) throw(std::exception, std::range_error)
257 throw std::range_error (
"index of front out of range");
259 return this->access((front_index + i) % this->dim);
262 const T &
front(
const size_t i = 0)
const
263 throw(std::exception, std::range_error)
266 throw std::range_error (
"index of front out of range");
268 return this->access((front_index + i) % this->dim);
281 T &
rear(
const size_t i = 0) throw(std::exception, std::range_error)
284 throw std::range_error (
"index of rear out of range");
289 const T &
rear(
const size_t i = 0)
const
290 throw(std::exception, std::range_error)
293 throw std::range_error (
"index of rear out of range");
298 template <
class Operation>
299 bool traverse(Operation & operation)
304 for (
size_t i = front_index; i != rear_index; ++i)
305 if (not operation(this->access(i)))
310 template <
class Operation>
311 bool traverse(Operation & operation)
const
313 return const_cast<ArrayQueue*
>(
this)->traverse(operation);
316 template <
class Operation>
317 bool traverse(Operation && operation = Operation())
const
319 return traverse<Operation>(operation);
322 template <
class Operation>
323 bool traverse(Operation && operation = Operation())
325 return traverse<Operation>(operation);
328 Functional_Methods(T);
355 template <
typename T>
368 void increase_index(
size_t & i,
const size_t inc = 1)
const
370 I( ((i + inc)%dim) == ((i+ inc)&mask) );
376 T & rear_item(
const size_t i = 0)
378 I(static_cast<size_t>((rear_index - i - 1) & mask) ==
379 (rear_index - i - 1)%dim);
381 return array[
static_cast<size_t> ((rear_index - i - 1) & mask)];
384 const T & rear_item(
const size_t i = 0)
const
386 I(static_cast<size_t>((rear_index - i - 1) & mask) ==
387 (rear_index - i - 1)%dim);
389 return array[
static_cast<size_t> ((rear_index - i - 1) & mask)];
396 std::swap(two_pow, q.two_pow);
397 std::swap(dim, q.dim);
398 std::swap(array, q.array);
399 std::swap(front_index, q.front_index);
400 std::swap(rear_index, q.rear_index);
401 std::swap(mask, q.mask);
402 std::swap(num_items, q.num_items);
413 : two_pow(tp), dim(1<<two_pow), array(NULL), front_index(0),
414 rear_index(0), mask(dim - 1), num_items(0)
427 : two_pow(q.two_pow), dim(q.dim), array(new T [dim]),
428 front_index(q.front_index), rear_index(q.rear_index), mask(q.mask),
429 num_items(q.num_items)
431 for (
size_t i = front_index; i != rear_index; ++i)
432 array[i] = q.array[i];
465 T &
put(
const T& item)
throw(std::exception, std::overflow_error)
468 array[rear_index] = item;
469 T & ret_val = array[rear_index];
470 increase_index(rear_index);
475 T &
put(T && item) throw(std::exception, std::overflow_error)
478 array[rear_index] = std::move(item);
479 T & ret_val = array[rear_index];
480 increase_index(rear_index);
499 T &
putn(
const size_t n)
throw(std::exception, std::overflow_error)
501 I(num_items + n < dim);
502 increase_index(rear_index, n);
514 T
get()
throw(std::exception, std::underflow_error)
518 T ret_val = std::move(array[front_index]);
519 increase_index(front_index);
532 T &
getn(
const size_t n)
throw(std::exception, std::underflow_error)
536 increase_index(front_index, n);
537 return array[front_index];
550 T &
front(
const size_t i = 0) throw(std::exception, std::range_error)
553 return array[front_index + i];
556 const T &
front(
const size_t i = 0)
const
557 throw(std::exception, std::range_error)
560 return array[front_index + i];
572 T &
rear(
const size_t i = 0) throw(std::exception, std::range_error)
578 const T &
rear(
const size_t i = 0)
const
579 throw(std::exception, std::range_error)
586 const size_t &
size()
const {
return num_items; }
595 template <
class Operation>
596 bool traverse(Operation & operation)
600 for (
size_t i = front_index; i != rear_index; ++i)
601 if (not operation(array[i]))
606 template <
class Operation>
607 bool traverse(Operation & operation)
const
609 return const_cast<FixedQueue*
>(
this)->traverse(operation);
612 template <
class Operation>
613 bool traverse(Operation && operation = Operation())
const
615 return traverse<Operation>(operation);
618 template <
class Operation>
619 bool traverse(Operation && operation = Operation())
621 return traverse<Operation>(operation);
624 Functional_Methods(T);
Definition: tpl_memArray.H:17
T & putn(size_t sz)
Definition: tpl_arrayQueue.H:176
bool is_empty() const
Retorna true si la cola está vacía.
Definition: tpl_arrayQueue.H:589
const size_t & size() const
Retorna el número de elementos que contiene la cola.
Definition: tpl_arrayQueue.H:586
T & front(const size_t i=0)
Definition: tpl_arrayQueue.H:550
Definition: tpl_arrayQueue.H:356
T & rear(const size_t i=0)
Definition: tpl_arrayQueue.H:281
T & rear(const size_t i=0)
Definition: tpl_arrayQueue.H:572
Definition: tpl_arrayQueue.H:38
T & front(const size_t i=0)
Definition: tpl_arrayQueue.H:254
T & put(const T &item)
Definition: tpl_arrayQueue.H:138
T & getn(const size_t i)
Definition: tpl_arrayQueue.H:227
const size_t & capacity() const
Definition: tpl_arrayQueue.H:593
T & putn(const size_t n)
Definition: tpl_arrayQueue.H:499
ArrayQueue(const size_t sz=8)
Definition: tpl_arrayQueue.H:78
T & getn(const size_t n)
Definition: tpl_arrayQueue.H:532
~FixedQueue()
Destructor.
Definition: tpl_arrayQueue.H:420
T & put(const T &item)
Definition: tpl_arrayQueue.H:465
FixedQueue(const size_t &tp=8)
Definition: tpl_arrayQueue.H:412