dataQueueObj.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This file is part of the PikaPython project.
  3. * http://github.com/pikastech/pikapython
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "dataQueueObj.h"
  28. #include "BaseObj.h"
  29. #include "dataQueue.h"
  30. QueueObj* New_queueObj(void) {
  31. PikaObj* self = New_PikaObj(NULL);
  32. queueObj_init(self);
  33. return self;
  34. }
  35. int32_t queueObj_init(QueueObj* self) {
  36. obj_setInt(self, "top", 0);
  37. obj_setInt(self, "bottom", 0);
  38. return 0;
  39. }
  40. int32_t queueObj_pushObj(QueueObj* self, char* className) {
  41. uint64_t top = obj_getInt(self, "top");
  42. char buff[11];
  43. char* topStr = fast_itoa(buff, top);
  44. /* add top */
  45. obj_setInt(self, "top", top + 1);
  46. return obj_newObj(self, topStr, className, New_TinyObj);
  47. }
  48. PikaObj* queueObj_getCurrentObj(QueueObj* self) {
  49. uint64_t current = obj_getInt(self, "top") - 1;
  50. char buff[11];
  51. char* currentStr = fast_itoa(buff, current);
  52. return obj_getObj(self, currentStr);
  53. }
  54. PikaObj* queueObj_popObj(QueueObj* self) {
  55. uint64_t bottom = obj_getInt(self, "bottom");
  56. char buff[11];
  57. char* bottomStr = fast_itoa(buff, bottom);
  58. /* add bottom */
  59. obj_setInt(self, "bottom", bottom + 1);
  60. PikaObj* res = obj_getObj(self, bottomStr);
  61. return res;
  62. }
  63. int32_t queueObj_pushInt(QueueObj* self, int val) {
  64. uint64_t top = obj_getInt(self, "top");
  65. char buff[11];
  66. char* topStr = fast_itoa(buff, top);
  67. /* add top */
  68. obj_setInt(self, "top", top + 1);
  69. return obj_setInt(self, topStr, val);
  70. }
  71. int64_t queueObj_popInt(QueueObj* self) {
  72. uint64_t bottom = obj_getInt(self, "bottom");
  73. char buff[11];
  74. char* bottomStr = fast_itoa(buff, bottom);
  75. /* add bottom */
  76. obj_setInt(self, "bottom", bottom + 1);
  77. int64_t res = obj_getInt(self, bottomStr);
  78. obj_removeArg(self, bottomStr);
  79. return res;
  80. }
  81. int32_t queueObj_pushFloat(QueueObj* self, pika_float val) {
  82. uint64_t top = obj_getInt(self, "top");
  83. char buff[11];
  84. char* topStr = fast_itoa(buff, top);
  85. /* add top */
  86. obj_setInt(self, "top", top + 1);
  87. return obj_setFloat(self, topStr, val);
  88. }
  89. pika_float queueObj_popFloat(QueueObj* self) {
  90. uint64_t bottom = obj_getInt(self, "bottom");
  91. char buff[11];
  92. char* bottomStr = fast_itoa(buff, bottom);
  93. /* add bottom */
  94. obj_setInt(self, "bottom", bottom + 1);
  95. pika_float res = obj_getFloat(self, bottomStr);
  96. obj_removeArg(self, bottomStr);
  97. return res;
  98. }
  99. int32_t queueObj_pushStr(QueueObj* self, char* str) {
  100. uint64_t top = obj_getInt(self, "top");
  101. char buff[11];
  102. char* topStr = fast_itoa(buff, top);
  103. /* add top */
  104. obj_setInt(self, "top", top + 1);
  105. return obj_setStr(self, topStr, str);
  106. }
  107. char* queueObj_popStr(QueueObj* self) {
  108. uint64_t bottom = obj_getInt(self, "bottom");
  109. char buff[11];
  110. char* bottomStr = fast_itoa(buff, bottom);
  111. /* add bottom */
  112. obj_setInt(self, "bottom", bottom + 1);
  113. return obj_getStr(self, bottomStr);
  114. }