File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3329,7 +3329,7 @@ int String::find(const char *p_str, int p_from) const {
33293329}
33303330
33313331int String::find_char (char32_t p_char, int p_from) const {
3332- return _cowdata .find (p_char, p_from);
3332+ return span () .find (p_char, p_from);
33333333}
33343334
33353335int String::findmk (const Vector<String> &p_keys, int p_from, int *r_key) const {
@@ -3566,7 +3566,7 @@ int String::rfind(const char *p_str, int p_from) const {
35663566}
35673567
35683568int String::rfind_char (char32_t p_char, int p_from) const {
3569- return _cowdata .rfind (p_char, p_from);
3569+ return span () .rfind (p_char, p_from);
35703570}
35713571
35723572int String::rfindn (const String &p_str, int p_from) const {
Original file line number Diff line number Diff line change @@ -252,10 +252,6 @@ class CowData {
252252 _FORCE_INLINE_ operator Span<T>() const { return Span<T>(ptr (), size ()); }
253253 _FORCE_INLINE_ Span<T> span () const { return operator Span<T>(); }
254254
255- Size find (const T &p_val, Size p_from = 0 ) const ;
256- Size rfind (const T &p_val, Size p_from = -1 ) const ;
257- Size count (const T &p_val) const ;
258-
259255 _FORCE_INLINE_ CowData () {}
260256 _FORCE_INLINE_ ~CowData () { _unref (); }
261257 _FORCE_INLINE_ CowData (std::initializer_list<T> p_init);
@@ -430,54 +426,6 @@ Error CowData<T>::_realloc(Size p_alloc_size) {
430426 return OK;
431427}
432428
433- template <typename T>
434- typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const {
435- Size ret = -1 ;
436-
437- if (p_from < 0 || size () == 0 ) {
438- return ret;
439- }
440-
441- for (Size i = p_from; i < size (); i++) {
442- if (get (i) == p_val) {
443- ret = i;
444- break ;
445- }
446- }
447-
448- return ret;
449- }
450-
451- template <typename T>
452- typename CowData<T>::Size CowData<T>::rfind(const T &p_val, Size p_from) const {
453- const Size s = size ();
454-
455- if (p_from < 0 ) {
456- p_from = s + p_from;
457- }
458- if (p_from < 0 || p_from >= s) {
459- p_from = s - 1 ;
460- }
461-
462- for (Size i = p_from; i >= 0 ; i--) {
463- if (get (i) == p_val) {
464- return i;
465- }
466- }
467- return -1 ;
468- }
469-
470- template <typename T>
471- typename CowData<T>::Size CowData<T>::count(const T &p_val) const {
472- Size amount = 0 ;
473- for (Size i = 0 ; i < size (); i++) {
474- if (get (i) == p_val) {
475- amount++;
476- }
477- }
478- return amount;
479- }
480-
481429template <typename T>
482430void CowData<T>::_ref(const CowData *p_from) {
483431 _ref (*p_from);
Original file line number Diff line number Diff line change @@ -62,4 +62,54 @@ class Span {
6262
6363 _FORCE_INLINE_ constexpr const T *begin () const { return _ptr; }
6464 _FORCE_INLINE_ constexpr const T *end () const { return _ptr + _len; }
65+
66+ // Algorithms.
67+ constexpr int64_t find (const T &p_val, int64_t p_from = 0 ) const ;
68+ constexpr int64_t rfind (const T &p_val, int64_t p_from = 0 ) const ;
69+ constexpr uint64_t count (const T &p_val) const ;
6570};
71+
72+ template <typename T>
73+ constexpr int64_t Span<T>::find(const T &p_val, int64_t p_from) const {
74+ if (p_from < 0 || size () == 0 ) {
75+ return -1 ;
76+ }
77+
78+ for (uint64_t i = p_from; i < size (); i++) {
79+ if (ptr ()[i] == p_val) {
80+ return i;
81+ }
82+ }
83+
84+ return -1 ;
85+ }
86+
87+ template <typename T>
88+ constexpr int64_t Span<T>::rfind(const T &p_val, int64_t p_from) const {
89+ const int64_t s = size ();
90+
91+ if (p_from < 0 ) {
92+ p_from = s + p_from;
93+ }
94+ if (p_from < 0 || p_from >= s) {
95+ p_from = s - 1 ;
96+ }
97+
98+ for (int64_t i = p_from; i >= 0 ; i--) {
99+ if (ptr ()[i] == p_val) {
100+ return i;
101+ }
102+ }
103+ return -1 ;
104+ }
105+
106+ template <typename T>
107+ constexpr uint64_t Span<T>::count(const T &p_val) const {
108+ uint64_t amount = 0 ;
109+ for (uint64_t i = 0 ; i < size (); i++) {
110+ if (ptr ()[i] == p_val) {
111+ amount++;
112+ }
113+ }
114+ return amount;
115+ }
Original file line number Diff line number Diff line change @@ -105,9 +105,9 @@ class Vector {
105105 _FORCE_INLINE_ const T &operator [](Size p_index) const { return _cowdata.get (p_index); }
106106 // Must take a copy instead of a reference (see GH-31736).
107107 Error insert (Size p_pos, T p_val) { return _cowdata.insert (p_pos, p_val); }
108- Size find (const T &p_val, Size p_from = 0 ) const { return _cowdata .find (p_val, p_from); }
109- Size rfind (const T &p_val, Size p_from = -1 ) const { return _cowdata .rfind (p_val, p_from); }
110- Size count (const T &p_val) const { return _cowdata .count (p_val); }
108+ Size find (const T &p_val, Size p_from = 0 ) const { return span () .find (p_val, p_from); }
109+ Size rfind (const T &p_val, Size p_from = -1 ) const { return span () .rfind (p_val, p_from); }
110+ Size count (const T &p_val) const { return span () .count (p_val); }
111111
112112 // Must take a copy instead of a reference (see GH-31736).
113113 void append_array (Vector<T> p_other);
You can’t perform that action at this time.
0 commit comments