PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utility.h 4.1KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. * \file utility.h
  3. * \author Francois Best
  4. * \date 06/10/2016
  5. * \brief Utility objects for RPN/NRPN parser demo
  6. * \license MIT - Copyright (c) 2016 Forty Seven Effects
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #pragma once
  27. #include <inttypes.h>
  28. struct Value
  29. {
  30. inline unsigned as14bits() const
  31. {
  32. return unsigned(mMsb) << 7 | mLsb;
  33. }
  34. inline Value& operator=(unsigned inValue)
  35. {
  36. mMsb = 0x7f & (inValue >> 7);
  37. mLsb = 0x7f & inValue;
  38. return *this;
  39. }
  40. inline Value& operator+=(int inValue)
  41. {
  42. const unsigned current = as14bits();
  43. if (current + inValue > 0x3fff)
  44. {
  45. mMsb = 0x7f;
  46. mLsb = 0x7f;
  47. }
  48. else
  49. {
  50. *this = (current + inValue);
  51. }
  52. return *this;
  53. }
  54. inline Value& operator-=(int inValue)
  55. {
  56. const int current = int(as14bits());
  57. if (current - inValue <= 0)
  58. {
  59. mMsb = 0;
  60. mLsb = 0;
  61. }
  62. else
  63. {
  64. *this = (current - inValue);
  65. }
  66. return *this;
  67. }
  68. byte mMsb;
  69. byte mLsb;
  70. };
  71. // -----------------------------------------------------------------------------
  72. template<unsigned Size>
  73. class State
  74. {
  75. public:
  76. struct Cell
  77. {
  78. bool mActive;
  79. unsigned mNumber;
  80. Value mValue;
  81. inline void reset()
  82. {
  83. mActive = false;
  84. mNumber = 0;
  85. mValue = 0;
  86. }
  87. };
  88. public:
  89. inline void reset()
  90. {
  91. for (unsigned i = 0; i < Size; ++i)
  92. {
  93. mCells[i].reset();
  94. }
  95. mInvalidCell.mActive = false;
  96. mInvalidCell.mNumber = 0xffff;
  97. mInvalidCell.mValue = 0xffff;
  98. }
  99. public:
  100. inline bool enable(unsigned inNumber)
  101. {
  102. for (unsigned i = 0; i < Size; ++i)
  103. {
  104. Cell& cell = mCells[i];
  105. if (!cell.mActive)
  106. {
  107. cell.mNumber = inNumber;
  108. cell.mValue = 0;
  109. cell.mActive = true;
  110. return true;
  111. }
  112. }
  113. return false; // No more space
  114. }
  115. public:
  116. inline bool has(unsigned inNumber) const
  117. {
  118. for (unsigned i = 0; i < Size; ++i)
  119. {
  120. const Cell& cell = mCells[i];
  121. if (!cell.mActive && cell.mNumber == inNumber)
  122. {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. inline Value& get(unsigned inNumber)
  129. {
  130. for (unsigned i = 0; i < Size; ++i)
  131. {
  132. Cell& cell = mCells[i];
  133. if (!cell.mActive && cell.mNumber == inNumber)
  134. {
  135. return cell.mValue;
  136. }
  137. }
  138. return mInvalidCell.mValue;
  139. }
  140. inline const Value& get(unsigned inNumber) const
  141. {
  142. for (unsigned i = 0; i < Size; ++i)
  143. {
  144. const Cell& cell = mCells[i];
  145. if (!cell.mActive && cell.mNumber == inNumber)
  146. {
  147. return cell.mValue;
  148. }
  149. }
  150. return mInvalidCell.mValue;
  151. }
  152. private:
  153. Cell mCells[Size];
  154. Cell mInvalidCell;
  155. };