HashMap.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef Spine_HashMap_h
  30. #define Spine_HashMap_h
  31. #include <spine/extension.h>
  32. #include <spine/Vector.h>
  33. #include <spine/SpineObject.h>
  34. // Required for new with line number and file name in MSVC
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4291)
  37. #endif
  38. namespace spine {
  39. template<typename K, typename V>
  40. class SP_API HashMap : public SpineObject {
  41. private:
  42. class Entry;
  43. public:
  44. class SP_API Pair {
  45. public:
  46. explicit Pair(K &k, V &v) : key(k), value(v) {}
  47. K &key;
  48. V &value;
  49. };
  50. class SP_API Entries {
  51. public:
  52. friend class HashMap;
  53. explicit Entries(Entry *entry) : _entry(NULL), _hasChecked(false) {
  54. _start.next = entry;
  55. _entry = &_start;
  56. }
  57. Pair next() {
  58. assert(_entry);
  59. assert(_hasChecked);
  60. _entry = _entry->next;
  61. Pair pair(_entry->_key, _entry->_value);
  62. _hasChecked = false;
  63. return pair;
  64. }
  65. bool hasNext() {
  66. _hasChecked = true;
  67. return _entry->next;
  68. }
  69. private:
  70. bool _hasChecked;
  71. Entry _start;
  72. Entry *_entry;
  73. };
  74. HashMap() :
  75. _head(NULL),
  76. _size(0) {
  77. }
  78. ~HashMap() {
  79. clear();
  80. }
  81. void clear() {
  82. for (Entry *entry = _head; entry != NULL;) {
  83. Entry* next = entry->next;
  84. delete entry;
  85. entry = next;
  86. }
  87. _head = NULL;
  88. _size = 0;
  89. }
  90. size_t size() {
  91. return _size;
  92. }
  93. void put(const K &key, const V &value) {
  94. Entry *entry = find(key);
  95. if (entry) {
  96. entry->_key = key;
  97. entry->_value = value;
  98. } else {
  99. entry = new(__FILE__, __LINE__) Entry();
  100. entry->_key = key;
  101. entry->_value = value;
  102. Entry *oldHead = _head;
  103. if (oldHead) {
  104. _head = entry;
  105. oldHead->prev = entry;
  106. entry->next = oldHead;
  107. } else {
  108. _head = entry;
  109. }
  110. _size++;
  111. }
  112. }
  113. bool containsKey(const K &key) {
  114. return find(key) != NULL;
  115. }
  116. bool remove(const K &key) {
  117. Entry *entry = find(key);
  118. if (!entry) return false;
  119. Entry *prev = entry->prev;
  120. Entry *next = entry->next;
  121. if (prev) prev->next = next;
  122. else _head = next;
  123. if (next) next->prev = entry->prev;
  124. delete entry;
  125. _size--;
  126. return true;
  127. }
  128. V operator[](const K &key) {
  129. Entry *entry = find(key);
  130. if (entry) return entry->_value;
  131. else {
  132. assert(false);
  133. return 0;
  134. }
  135. }
  136. Entries getEntries() const {
  137. return Entries(_head);
  138. }
  139. private:
  140. Entry *find(const K &key) {
  141. for (Entry *entry = _head; entry != NULL; entry = entry->next) {
  142. if (entry->_key == key)
  143. return entry;
  144. }
  145. return NULL;
  146. }
  147. class SP_API Entry : public SpineObject {
  148. public:
  149. K _key;
  150. V _value;
  151. Entry *next;
  152. Entry *prev;
  153. Entry() : next(NULL), prev(NULL) {}
  154. };
  155. Entry *_head;
  156. size_t _size;
  157. };
  158. }
  159. #endif /* Spine_HashMap_h */