Add search methods for packed arrays

* count()
* find()
* rfind()
This commit is contained in:
Haoyu Qiu 2022-05-07 20:16:11 +08:00
parent 563690347a
commit 380a53f02f
12 changed files with 268 additions and 0 deletions

View file

@ -183,6 +183,8 @@ public:
}
int find(const T &p_val, int p_from = 0) const;
int rfind(const T &p_val, int p_from = -1) const;
int count(const T &p_val) const;
_FORCE_INLINE_ CowData() {}
_FORCE_INLINE_ ~CowData();
@ -349,6 +351,36 @@ int CowData<T>::find(const T &p_val, int p_from) const {
return ret;
}
template <class T>
int CowData<T>::rfind(const T &p_val, int p_from) const {
const int s = size();
if (p_from < 0) {
p_from = s + p_from;
}
if (p_from < 0 || p_from >= s) {
p_from = s - 1;
}
for (int i = p_from; i >= 0; i--) {
if (get(i) == p_val) {
return i;
}
}
return -1;
}
template <class T>
int CowData<T>::count(const T &p_val) const {
int amount = 0;
for (int i = 0; i < size(); i++) {
if (get(i) == p_val) {
amount++;
}
}
return amount;
}
template <class T>
void CowData<T>::_ref(const CowData *p_from) {
_ref(*p_from);

View file

@ -92,6 +92,8 @@ public:
_FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); }
Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }
int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); }
int rfind(const T &p_val, int p_from = -1) const { return _cowdata.rfind(p_val, p_from); }
int count(const T &p_val) const { return _cowdata.count(p_val); }
void append_array(Vector<T> p_other);