array.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* array.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef ARRAY_H
  31. #define ARRAY_H
  32. #include "core/typedefs.h"
  33. #include <climits>
  34. class Variant;
  35. class ArrayPrivate;
  36. class Object;
  37. class StringName;
  38. class Callable;
  39. class Array {
  40. mutable ArrayPrivate *_p;
  41. void _unref() const;
  42. public:
  43. struct ConstIterator {
  44. _FORCE_INLINE_ const Variant &operator*() const;
  45. _FORCE_INLINE_ const Variant *operator->() const;
  46. _FORCE_INLINE_ ConstIterator &operator++();
  47. _FORCE_INLINE_ ConstIterator &operator--();
  48. _FORCE_INLINE_ bool operator==(const ConstIterator &p_other) const { return element_ptr == p_other.element_ptr; }
  49. _FORCE_INLINE_ bool operator!=(const ConstIterator &p_other) const { return element_ptr != p_other.element_ptr; }
  50. _FORCE_INLINE_ ConstIterator(const Variant *p_element_ptr, Variant *p_read_only = nullptr) :
  51. element_ptr(p_element_ptr), read_only(p_read_only) {}
  52. _FORCE_INLINE_ ConstIterator() {}
  53. _FORCE_INLINE_ ConstIterator(const ConstIterator &p_other) :
  54. element_ptr(p_other.element_ptr), read_only(p_other.read_only) {}
  55. _FORCE_INLINE_ ConstIterator &operator=(const ConstIterator &p_other) {
  56. element_ptr = p_other.element_ptr;
  57. read_only = p_other.read_only;
  58. return *this;
  59. }
  60. private:
  61. const Variant *element_ptr = nullptr;
  62. Variant *read_only = nullptr;
  63. };
  64. struct Iterator {
  65. _FORCE_INLINE_ Variant &operator*() const;
  66. _FORCE_INLINE_ Variant *operator->() const;
  67. _FORCE_INLINE_ Iterator &operator++();
  68. _FORCE_INLINE_ Iterator &operator--();
  69. _FORCE_INLINE_ bool operator==(const Iterator &p_other) const { return element_ptr == p_other.element_ptr; }
  70. _FORCE_INLINE_ bool operator!=(const Iterator &p_other) const { return element_ptr != p_other.element_ptr; }
  71. _FORCE_INLINE_ Iterator(Variant *p_element_ptr, Variant *p_read_only = nullptr) :
  72. element_ptr(p_element_ptr), read_only(p_read_only) {}
  73. _FORCE_INLINE_ Iterator() {}
  74. _FORCE_INLINE_ Iterator(const Iterator &p_other) :
  75. element_ptr(p_other.element_ptr), read_only(p_other.read_only) {}
  76. _FORCE_INLINE_ Iterator &operator=(const Iterator &p_other) {
  77. element_ptr = p_other.element_ptr;
  78. read_only = p_other.read_only;
  79. return *this;
  80. }
  81. operator ConstIterator() const {
  82. return ConstIterator(element_ptr, read_only);
  83. }
  84. private:
  85. Variant *element_ptr = nullptr;
  86. Variant *read_only = nullptr;
  87. };
  88. Iterator begin();
  89. Iterator end();
  90. ConstIterator begin() const;
  91. ConstIterator end() const;
  92. void _ref(const Array &p_from) const;
  93. Variant &operator[](int p_idx);
  94. const Variant &operator[](int p_idx) const;
  95. void set(int p_idx, const Variant &p_value);
  96. const Variant &get(int p_idx) const;
  97. int size() const;
  98. bool is_empty() const;
  99. void clear();
  100. bool operator==(const Array &p_array) const;
  101. bool operator!=(const Array &p_array) const;
  102. bool recursive_equal(const Array &p_array, int recursion_count) const;
  103. uint32_t hash() const;
  104. uint32_t recursive_hash(int recursion_count) const;
  105. void operator=(const Array &p_array);
  106. void assign(const Array &p_array);
  107. void push_back(const Variant &p_value);
  108. _FORCE_INLINE_ void append(const Variant &p_value) { push_back(p_value); } //for python compatibility
  109. void append_array(const Array &p_array);
  110. Error resize(int p_new_size);
  111. Error insert(int p_pos, const Variant &p_value);
  112. void remove_at(int p_pos);
  113. void fill(const Variant &p_value);
  114. Variant front() const;
  115. Variant back() const;
  116. Variant pick_random() const;
  117. void sort();
  118. void sort_custom(const Callable &p_callable);
  119. void shuffle();
  120. int bsearch(const Variant &p_value, bool p_before = true) const;
  121. int bsearch_custom(const Variant &p_value, const Callable &p_callable, bool p_before = true) const;
  122. void reverse();
  123. int find(const Variant &p_value, int p_from = 0) const;
  124. int find_custom(const Callable &p_callable, int p_from = 0) const;
  125. int rfind(const Variant &p_value, int p_from = -1) const;
  126. int rfind_custom(const Callable &p_callable, int p_from = -1) const;
  127. int count(const Variant &p_value) const;
  128. bool has(const Variant &p_value) const;
  129. void erase(const Variant &p_value);
  130. void push_front(const Variant &p_value);
  131. Variant pop_back();
  132. Variant pop_front();
  133. Variant pop_at(int p_pos);
  134. Array duplicate(bool p_deep = false) const;
  135. Array recursive_duplicate(bool p_deep, int recursion_count) const;
  136. Array slice(int p_begin, int p_end = INT_MAX, int p_step = 1, bool p_deep = false) const;
  137. Array filter(const Callable &p_callable) const;
  138. Array map(const Callable &p_callable) const;
  139. Variant reduce(const Callable &p_callable, const Variant &p_accum) const;
  140. bool any(const Callable &p_callable) const;
  141. bool all(const Callable &p_callable) const;
  142. bool operator<(const Array &p_array) const;
  143. bool operator<=(const Array &p_array) const;
  144. bool operator>(const Array &p_array) const;
  145. bool operator>=(const Array &p_array) const;
  146. Variant min() const;
  147. Variant max() const;
  148. const void *id() const;
  149. void set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
  150. bool is_typed() const;
  151. bool is_same_typed(const Array &p_other) const;
  152. bool is_same_instance(const Array &p_other) const;
  153. uint32_t get_typed_builtin() const;
  154. StringName get_typed_class_name() const;
  155. Variant get_typed_script() const;
  156. void make_read_only();
  157. bool is_read_only() const;
  158. static Array create_read_only();
  159. Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
  160. Array(const Array &p_from);
  161. Array();
  162. ~Array();
  163. };
  164. #endif // ARRAY_H