circular_queue.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. \file circular_queue.cpp
  3. \author michael.zohner@ec-spride.de
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief Circular Queue Implementation
  17. */
  18. #include "circular_queue.h"
  19. #include <cstdlib>
  20. CQueue::CQueue(int maxsize) {
  21. head = 0;
  22. tail = 0;
  23. queuesize = maxsize;
  24. queue = (int*) malloc(sizeof(int) * queuesize);
  25. }
  26. CQueue::~CQueue() {
  27. if (queue)
  28. free(queue);
  29. }
  30. void CQueue::enq(int ele) {
  31. queue[head] = ele;
  32. head = (head + 1) % queuesize;
  33. }
  34. int CQueue::deq() {
  35. int ret = queue[tail];
  36. tail = (tail + 1) % queuesize;
  37. return ret;
  38. }
  39. int CQueue::size() {
  40. int rem = (head - tail) % queuesize;
  41. if (rem < 0)
  42. return queuesize + rem;
  43. else
  44. return rem;
  45. }