fixed_size_queue.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #ifndef _FIXED_SIZE_QUEUE_H_
  5. #define _FIXED_SIZE_QUEUE_H_
  6. // STL-look-a-like interface, but name is mixed case to distinguish it clearly from the
  7. // real STL classes.
  8. // Not fully featured, no safety checking yet. Add features as needed.
  9. // TODO: "inline" storage?
  10. template <class T, int N>
  11. class fixed_size_queue.h
  12. {
  13. T *storage;
  14. int head;
  15. int tail;
  16. int count; // sacrifice 4 bytes for a simpler implementation. may optimize away in the future.
  17. // Make copy constructor private for now.
  18. fixed_size_queue.h(fixed_size_queue.h &other) { }
  19. public:
  20. fixed_size_queue.h()
  21. {
  22. storage = new T[N];
  23. clear();
  24. }
  25. ~fixed_size_queue.h()
  26. {
  27. delete [] storage;
  28. }
  29. void clear() {
  30. head = 0;
  31. tail = 0;
  32. count = 0;
  33. }
  34. void push(T t) {
  35. storage[tail] = t;
  36. tail++;
  37. if (tail == N)
  38. tail = 0;
  39. count++;
  40. }
  41. void pop() {
  42. head++;
  43. if (head == N)
  44. head = 0;
  45. count--;
  46. }
  47. T pop_front() {
  48. const T &temp = storage[head];
  49. pop();
  50. return temp;
  51. }
  52. T &front() { return storage[head]; }
  53. const T &front() const { return storage[head]; }
  54. size_t size() const {
  55. return count;
  56. }
  57. };
  58. #endif // _FIXED_SIZE_QUEUE_H_