TPQxxx.xxhxxx 970 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef TPQ_H
  2. #define TPQ_H
  3. #include <Arduino.h>
  4. template<class TP>
  5. class TPQ {
  6. private:
  7. int _front, _back, _count;
  8. TP *_data;
  9. int _maxitems;
  10. public:
  11. TPQ(int maxitems = 256) {
  12. _front = 0;
  13. _back = 0;
  14. _count = 0;
  15. _maxitems = maxitems;
  16. _data = new TP[maxitems + 1];
  17. }
  18. ~TPQ() {
  19. delete[] _data;
  20. }
  21. void push(const TP &item);
  22. TP peek_item(const TP &item);
  23. };
  24. template<class TP>
  25. void TPQ<TP>::push(const TP &item)
  26. {
  27. if(_count < _maxitems) { // Drops out when full
  28. _data[_back++]=item;
  29. ++_count;
  30. // Check wrap around
  31. if (_back > _maxitems)
  32. _back -= (_maxitems + 1);
  33. }
  34. }
  35. template<class TP>
  36. TP TPQ<TP>::peek_item(const TP &item) {
  37. if(_count <= 0) return TP(); // Returns empty
  38. else {
  39. TP result = item;// T result = _data[_front];
  40. //_front++;
  41. //--_count;
  42. if (_front > _maxitems)
  43. _front -= (_maxitems + 1);
  44. return result;
  45. }
  46. }
  47. #endif