2022-02-07 21:38:35 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "tinyengine/tinyengine.hpp"
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class QuickList final {
|
|
|
|
public:
|
2022-02-15 22:50:31 +08:00
|
|
|
using condition_func = std::function<bool(const T&)>;
|
|
|
|
using destroy_func = std::function<void(const T&)>;
|
|
|
|
|
2022-02-07 21:38:35 +08:00
|
|
|
void Add(const T& elem) {
|
|
|
|
datas_.push_back(elem);
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:12:24 +08:00
|
|
|
bool Empty() const { return datas_.empty(); }
|
|
|
|
|
2022-02-15 22:50:31 +08:00
|
|
|
void RemoveAll(condition_func findFunc,
|
|
|
|
destroy_func destroyFunc = nullptr) {
|
2022-02-08 21:02:52 +08:00
|
|
|
std::size_t idx = 0;
|
2022-02-07 21:38:35 +08:00
|
|
|
while (idx < datas_.size()) {
|
2022-02-08 21:02:52 +08:00
|
|
|
if (datas_.size() > idx && findFunc(datas_[idx])) {
|
2022-02-07 21:38:35 +08:00
|
|
|
std::swap(datas_[idx], datas_[datas_.size() - 1]);
|
2022-02-08 21:02:52 +08:00
|
|
|
if (destroyFunc) {
|
|
|
|
destroyFunc(datas_.back());
|
|
|
|
}
|
2022-02-07 21:38:35 +08:00
|
|
|
datas_.pop_back();
|
|
|
|
} else {
|
|
|
|
idx ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 22:50:31 +08:00
|
|
|
|
|
|
|
void Clear(condition_func condition = nullptr, destroy_func destroy = nullptr) {
|
|
|
|
if (destroy && condition) {
|
|
|
|
RemoveAll(condition, destroy);
|
|
|
|
}
|
|
|
|
datas_.clear();
|
|
|
|
}
|
2022-02-07 21:38:35 +08:00
|
|
|
|
|
|
|
// for debug
|
|
|
|
void PrintAll(std::function<void(const T&)> func = nullptr) {
|
|
|
|
printf("QuickList: ");
|
|
|
|
for (auto& elem : datas_) {
|
|
|
|
if (func == nullptr) {
|
|
|
|
std::cout << elem << ", ";
|
|
|
|
} else {
|
|
|
|
func(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:12:24 +08:00
|
|
|
|
|
|
|
size_t Size() const { return datas_.size(); }
|
|
|
|
T& Get(size_t idx) { return datas_[idx]; }
|
|
|
|
|
|
|
|
|
2022-02-07 21:38:35 +08:00
|
|
|
using const_iterator = typename std::vector<T>::const_iterator;
|
|
|
|
|
|
|
|
const_iterator begin() const { return datas_.begin(); }
|
|
|
|
const_iterator end() const { return datas_.end(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<T> datas_;
|
|
|
|
};
|