Skip to content

Commit 7f2ec4e

Browse files
committed
Move CowData find, rfind and count to new header span_algorithms.h in namespace Spans.
1 parent 30bb49e commit 7f2ec4e

File tree

4 files changed

+91
-57
lines changed

4 files changed

+91
-57
lines changed

core/string/ustring.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "core/string/string_name.h"
3939
#include "core/string/translation_server.h"
4040
#include "core/string/ucaps.h"
41+
#include "core/templates/span_algorithms.h"
4142
#include "core/variant/variant.h"
4243
#include "core/version_generated.gen.h"
4344

@@ -3347,7 +3348,7 @@ int String::find(const char *p_str, int p_from) const {
33473348
}
33483349

33493350
int String::find_char(char32_t p_char, int p_from) const {
3350-
return _cowdata.find(p_char, p_from);
3351+
return Spans::find<char32_t>(_cowdata, p_char, p_from);
33513352
}
33523353

33533354
int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
@@ -3584,7 +3585,7 @@ int String::rfind(const char *p_str, int p_from) const {
35843585
}
35853586

35863587
int String::rfind_char(char32_t p_char, int p_from) const {
3587-
return _cowdata.rfind(p_char, p_from);
3588+
return Spans::rfind<char32_t>(_cowdata, p_char, p_from);
35883589
}
35893590

35903591
int String::rfindn(const String &p_str, int p_from) const {

core/templates/cowdata.h

-52
Original file line numberDiff line numberDiff 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);
@@ -437,54 +433,6 @@ Error CowData<T>::_realloc(Size p_alloc_size) {
437433
return OK;
438434
}
439435

440-
template <typename T>
441-
typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const {
442-
Size ret = -1;
443-
444-
if (p_from < 0 || size() == 0) {
445-
return ret;
446-
}
447-
448-
for (Size i = p_from; i < size(); i++) {
449-
if (get(i) == p_val) {
450-
ret = i;
451-
break;
452-
}
453-
}
454-
455-
return ret;
456-
}
457-
458-
template <typename T>
459-
typename CowData<T>::Size CowData<T>::rfind(const T &p_val, Size p_from) const {
460-
const Size s = size();
461-
462-
if (p_from < 0) {
463-
p_from = s + p_from;
464-
}
465-
if (p_from < 0 || p_from >= s) {
466-
p_from = s - 1;
467-
}
468-
469-
for (Size i = p_from; i >= 0; i--) {
470-
if (get(i) == p_val) {
471-
return i;
472-
}
473-
}
474-
return -1;
475-
}
476-
477-
template <typename T>
478-
typename CowData<T>::Size CowData<T>::count(const T &p_val) const {
479-
Size amount = 0;
480-
for (Size i = 0; i < size(); i++) {
481-
if (get(i) == p_val) {
482-
amount++;
483-
}
484-
}
485-
return amount;
486-
}
487-
488436
template <typename T>
489437
void CowData<T>::_ref(const CowData *p_from) {
490438
_ref(*p_from);

core/templates/span_algorithms.h

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**************************************************************************/
2+
/* span_algorithms.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+
31+
#pragma once
32+
33+
#include "core/typedefs.h"
34+
35+
namespace Spans {
36+
template <typename T, typename Size = int64_t>
37+
Size find(const Span<T> span, const T &p_val, Size p_from = 0) {
38+
Size ret = -1;
39+
40+
if (p_from < 0 || span.size() == 0) {
41+
return ret;
42+
}
43+
44+
for (uint64_t i = p_from; i < span.size(); i++) {
45+
if (span.ptr()[i] == p_val) {
46+
ret = i;
47+
break;
48+
}
49+
}
50+
51+
return ret;
52+
}
53+
54+
template <typename T, typename Size = int64_t>
55+
Size rfind(const Span<T> span, const T &p_val, Size p_from = -1) {
56+
const Size s = span.size();
57+
58+
if (p_from < 0) {
59+
p_from = s + p_from;
60+
}
61+
if (p_from < 0 || p_from >= s) {
62+
p_from = s - 1;
63+
}
64+
65+
for (Size i = p_from; i >= 0; i--) {
66+
if (span.ptr()[i] == p_val) {
67+
return i;
68+
}
69+
}
70+
return -1;
71+
}
72+
73+
template <typename T>
74+
uint64_t count(const Span<T> span, const T &p_val) {
75+
uint64_t amount = 0;
76+
for (uint64_t i = 0; i < span.size(); i++) {
77+
if (span.ptr()[i] == p_val) {
78+
amount++;
79+
}
80+
}
81+
return amount;
82+
}
83+
84+
} //namespace spans

core/templates/vector.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "core/templates/cowdata.h"
4444
#include "core/templates/search_array.h"
4545
#include "core/templates/sort_array.h"
46+
#include "core/templates/span_algorithms.h"
4647

4748
#include <climits>
4849
#include <initializer_list>
@@ -105,9 +106,9 @@ class Vector {
105106
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
106107
// Must take a copy instead of a reference (see GH-31736).
107108
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); }
109+
Size find(const T &p_val, Size p_from = 0) const { return Spans::find<T>(_cowdata, p_val, p_from); }
110+
Size rfind(const T &p_val, Size p_from = -1) const { return Spans::rfind<T>(_cowdata, p_val, p_from); }
111+
Size count(const T &p_val) const { return Spans::count<T>(_cowdata, p_val); }
111112

112113
// Must take a copy instead of a reference (see GH-31736).
113114
void append_array(Vector<T> p_other);

0 commit comments

Comments
 (0)