diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/array.cpp | 57 | ||||
-rw-r--r-- | core/array.h | 5 | ||||
-rw-r--r-- | core/crypto/hashing_context.cpp | 2 | ||||
-rw-r--r-- | core/io/file_access_encrypted.cpp | 16 | ||||
-rw-r--r-- | core/io/file_access_encrypted.h | 3 | ||||
-rw-r--r-- | core/io/file_access_zip.cpp | 2 | ||||
-rw-r--r-- | core/math/disjoint_set.cpp | 31 | ||||
-rw-r--r-- | core/math/disjoint_set.h | 155 | ||||
-rw-r--r-- | core/math/geometry.cpp | 2 | ||||
-rw-r--r-- | core/ustring.cpp | 22 | ||||
-rw-r--r-- | core/variant.cpp | 10 | ||||
-rw-r--r-- | core/variant_call.cpp | 2 | ||||
-rw-r--r-- | core/variant_parser.cpp | 2 |
13 files changed, 297 insertions, 12 deletions
diff --git a/core/array.cpp b/core/array.cpp index 108d9f7386..ac30df08bc 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -222,6 +222,63 @@ Array Array::duplicate(bool p_deep) const { return new_arr; } + +int Array::_fix_slice_index(int p_index, int p_arr_len, int p_top_mod) { + p_index = CLAMP(p_index, -p_arr_len, p_arr_len + p_top_mod); + if (p_index < 0) { + p_index = (p_index % p_arr_len + p_arr_len) % p_arr_len; // positive modulo + } + return p_index; +} + +int Array::_clamp_index(int p_index) const { + return CLAMP(p_index, -size() + 1, size() - 1); +} + +#define ARRAY_GET_DEEP(idx, is_deep) is_deep ? get(idx).duplicate(is_deep) : get(idx) + +Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound + Array new_arr; + + p_begin = Array::_fix_slice_index(p_begin, size(), -1); // can't start out of range + p_end = Array::_fix_slice_index(p_end, size(), 0); + + int x = p_begin; + int new_arr_i = 0; + + ERR_FAIL_COND_V(p_step == 0, new_arr); + if (Array::_clamp_index(p_begin) == Array::_clamp_index(p_end)) { // don't include element twice + new_arr.resize(1); + // new_arr[0] = 1; + new_arr[0] = ARRAY_GET_DEEP(Array::_clamp_index(p_begin), p_deep); + return new_arr; + } else { + int element_count = ceil((int)MAX(0, (p_end - p_begin) / p_step)) + 1; + if (element_count == 1) { // delta going in wrong direction to reach end + new_arr.resize(0); + return new_arr; + } + new_arr.resize(element_count); + } + + // if going backwards, have to have a different terminating condition + if (p_step < 0) { + while (x >= p_end) { + new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep); + x += p_step; + new_arr_i += 1; + } + } else if (p_step > 0) { + while (x <= p_end) { + new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep); + x += p_step; + new_arr_i += 1; + } + } + + return new_arr; +} + struct _ArrayVariantSort { _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const { diff --git a/core/array.h b/core/array.h index d4e937a486..7a754d53ea 100644 --- a/core/array.h +++ b/core/array.h @@ -44,6 +44,9 @@ class Array { void _ref(const Array &p_from) const; void _unref() const; + int _clamp_index(int p_index) const; + static int _fix_slice_index(int p_index, int p_arr_len, int p_top_mod); + public: Variant &operator[](int p_idx); const Variant &operator[](int p_idx) const; @@ -91,6 +94,8 @@ public: Array duplicate(bool p_deep = false) const; + Array slice(int p_begin, int p_end, int p_step = 1, bool p_deep = false) const; + Variant min() const; Variant max() const; diff --git a/core/crypto/hashing_context.cpp b/core/crypto/hashing_context.cpp index bdccb258dd..bd863f546b 100644 --- a/core/crypto/hashing_context.cpp +++ b/core/crypto/hashing_context.cpp @@ -103,7 +103,7 @@ void HashingContext::_create_ctx(HashType p_type) { } void HashingContext::_delete_ctx() { - return; + switch (type) { case HASH_MD5: memdelete((CryptoCore::MD5Context *)ctx); diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index 77decc107d..0eef0ee79f 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -176,6 +176,22 @@ bool FileAccessEncrypted::is_open() const { return file != NULL; } +String FileAccessEncrypted::get_path() const { + + if (file) + return file->get_path(); + else + return ""; +} + +String FileAccessEncrypted::get_path_absolute() const { + + if (file) + return file->get_path_absolute(); + else + return ""; +} + void FileAccessEncrypted::seek(size_t p_position) { if (p_position > (size_t)data.size()) diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h index d779a150ac..c3be0f7de8 100644 --- a/core/io/file_access_encrypted.h +++ b/core/io/file_access_encrypted.h @@ -60,6 +60,9 @@ public: virtual void close(); ///< close a file virtual bool is_open() const; ///< true when file is open + virtual String get_path() const; /// returns the path for the current open file + virtual String get_path_absolute() const; /// returns the absolute path for the current open file + virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file virtual size_t get_position() const; ///< get position in the file diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp index be28c9a877..abc0bd064d 100644 --- a/core/io/file_access_zip.cpp +++ b/core/io/file_access_zip.cpp @@ -194,7 +194,7 @@ bool ZipArchive::try_open_pack(const String &p_path) { packages.push_back(pkg); int pkg_num = packages.size() - 1; - for (unsigned int i = 0; i < gi.number_entry; i++) { + for (uint64_t i = 0; i < gi.number_entry; i++) { char filename_inzip[256]; diff --git a/core/math/disjoint_set.cpp b/core/math/disjoint_set.cpp new file mode 100644 index 0000000000..838939e1ba --- /dev/null +++ b/core/math/disjoint_set.cpp @@ -0,0 +1,31 @@ +/*************************************************************************/ +/* disjoint_set.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "disjoint_set.h" diff --git a/core/math/disjoint_set.h b/core/math/disjoint_set.h new file mode 100644 index 0000000000..c9b3d0b65d --- /dev/null +++ b/core/math/disjoint_set.h @@ -0,0 +1,155 @@ +/*************************************************************************/ +/* disjoint_set.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef DISJOINT_SET_H +#define DISJOINT_SET_H + +#include "core/map.h" +#include "core/vector.h" + +/** + @author Marios Staikopoulos <marios@staik.net> +*/ + +/* This DisjointSet class uses Find with path compression and Union by rank */ +template <typename T, class C = Comparator<T>, class AL = DefaultAllocator> +class DisjointSet { + + struct Element { + T object; + Element *parent = nullptr; + int rank = 0; + }; + + typedef Map<T, Element *, C, AL> MapT; + + MapT elements; + + Element *get_parent(Element *element); + + _FORCE_INLINE_ Element *insert_or_get(T object); + +public: + ~DisjointSet(); + + _FORCE_INLINE_ void insert(T object) { (void)insert_or_get(object); } + + void create_union(T a, T b); + + void get_representatives(Vector<T> &out_roots); + + void get_members(Vector<T> &out_members, T representative); +}; + +/* FUNCTIONS */ + +template <typename T, class C, class AL> +DisjointSet<T, C, AL>::~DisjointSet() { + for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) { + memdelete_allocator<Element, AL>(itr->value()); + } +} + +template <typename T, class C, class AL> +typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::get_parent(Element *element) { + if (element->parent != element) { + element->parent = get_parent(element->parent); + } + + return element->parent; +} + +template <typename T, class C, class AL> +typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::insert_or_get(T object) { + typename MapT::Element *itr = elements.find(object); + if (itr != nullptr) { + return itr->value(); + } + + Element *new_element = memnew_allocator(Element, AL); + new_element->object = object; + new_element->parent = new_element; + elements.insert(object, new_element); + + return new_element; +} + +template <typename T, class C, class AL> +void DisjointSet<T, C, AL>::create_union(T a, T b) { + + Element *x = insert_or_get(a); + Element *y = insert_or_get(b); + + Element *x_root = get_parent(x); + Element *y_root = get_parent(y); + + // Already in the same set + if (x_root == y_root) + return; + + // Not in the same set, merge + if (x_root->rank < y_root->rank) { + SWAP(x_root, y_root); + } + + // Merge y_root into x_root + y_root->parent = x_root; + if (x_root->rank == y_root->rank) { + ++x_root->rank; + } +} + +template <typename T, class C, class AL> +void DisjointSet<T, C, AL>::get_representatives(Vector<T> &out_representatives) { + for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) { + Element *element = itr->value(); + if (element->parent == element) { + out_representatives.push_back(element->object); + } + } +} + +template <typename T, class C, class AL> +void DisjointSet<T, C, AL>::get_members(Vector<T> &out_members, T representative) { + typename MapT::Element *rep_itr = elements.find(representative); + ERR_FAIL_COND(rep_itr == nullptr); + + Element *rep_element = rep_itr->value(); + ERR_FAIL_COND(rep_element->parent != rep_element); + + for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) { + Element *parent = get_parent(itr->value()); + if (parent == rep_element) { + out_members.push_back(itr->key()); + } + } +} + +#endif diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index f37db90929..77383e2839 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -715,7 +715,7 @@ Vector<Vector<Vector2> > Geometry::decompose_polygon_in_convex(Vector<Point2> po decomp.write[idx].resize(tp.GetNumPoints()); - for (int i = 0; i < tp.GetNumPoints(); i++) { + for (int64_t i = 0; i < tp.GetNumPoints(); i++) { decomp.write[idx].write[i] = tp.GetPoint(i); } diff --git a/core/ustring.cpp b/core/ustring.cpp index fb4bd6d802..dee554716f 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2147,13 +2147,13 @@ int64_t String::to_int(const CharType *p_str, int p_len) { if (c >= '0' && c <= '9') { - if (integer > INT32_MAX / 10) { + if (integer > INT64_MAX / 10) { String number(""); str = p_str; while (*str && str != limit) { number += *(str++); } - ERR_FAIL_V_MSG(sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + number + " as integer, provided value is " + (sign == 1 ? "too big." : "too small.")); + ERR_FAIL_V_MSG(sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + number + " as integer, provided value is " + (sign == 1 ? "too big." : "too small.")); } integer *= 10; integer += c - '0'; @@ -3285,18 +3285,26 @@ static int _humanize_digits(int p_num) { String String::humanize_size(size_t p_size) { uint64_t _div = 1; - static const char *prefix[] = { " B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB", "" }; + Vector<String> prefixes; + prefixes.push_back(RTR("B")); + prefixes.push_back(RTR("KiB")); + prefixes.push_back(RTR("MiB")); + prefixes.push_back(RTR("GiB")); + prefixes.push_back(RTR("TiB")); + prefixes.push_back(RTR("PiB")); + prefixes.push_back(RTR("EiB")); + int prefix_idx = 0; - while (p_size > (_div * 1024) && prefix[prefix_idx][0]) { + while (prefix_idx < prefixes.size() && p_size > (_div * 1024)) { _div *= 1024; prefix_idx++; } - int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0; - double divisor = prefix_idx > 0 ? _div : 1; + const int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0; + const double divisor = prefix_idx > 0 ? _div : 1; - return String::num(p_size / divisor).pad_decimals(digits) + RTR(prefix[prefix_idx]); + return String::num(p_size / divisor).pad_decimals(digits) + " " + prefixes[prefix_idx]; } bool String::is_abs_path() const { diff --git a/core/variant.cpp b/core/variant.cpp index e7d0e58367..16bbf94c54 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -910,7 +910,15 @@ bool Variant::is_one() const { void Variant::reference(const Variant &p_variant) { - clear(); + switch (type) { + case NIL: + case BOOL: + case INT: + case REAL: + break; + default: + clear(); + } type = p_variant.type; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 5e3876d6a4..c288c50abf 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -534,6 +534,7 @@ struct _VariantCall { VCALL_LOCALMEM2R(Array, bsearch); VCALL_LOCALMEM4R(Array, bsearch_custom); VCALL_LOCALMEM1R(Array, duplicate); + VCALL_LOCALMEM4R(Array, slice); VCALL_LOCALMEM0(Array, invert); VCALL_LOCALMEM0R(Array, max); VCALL_LOCALMEM0R(Array, min); @@ -1759,6 +1760,7 @@ void register_variant_methods() { ADDFUNC4R(ARRAY, INT, Array, bsearch_custom, NIL, "value", OBJECT, "obj", STRING, "func", BOOL, "before", varray(true)); ADDFUNC0NC(ARRAY, NIL, Array, invert, varray()); ADDFUNC1R(ARRAY, ARRAY, Array, duplicate, BOOL, "deep", varray(false)); + ADDFUNC4R(ARRAY, ARRAY, Array, slice, INT, "begin", INT, "end", INT, "step", BOOL, "deep", varray(1, false)); ADDFUNC0R(ARRAY, NIL, Array, max, varray()); ADDFUNC0R(ARRAY, NIL, Array, min, varray()); diff --git a/core/variant_parser.cpp b/core/variant_parser.cpp index 07212ec669..fe2c981c3c 100644 --- a/core/variant_parser.cpp +++ b/core/variant_parser.cpp @@ -1522,7 +1522,7 @@ Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r return err; if (tk.type != TK_STRING) { r_err_str = "Error reading quoted string"; - return err; + return ERR_INVALID_DATA; } what = tk.value; |