mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-22 19:54:06 +01:00
Codechange: [Script] Reduce code duplication in ScriptList filtering (#14931)
This commit is contained in:
@@ -667,42 +667,22 @@ void ScriptList::SwapList(ScriptList *list)
|
|||||||
|
|
||||||
void ScriptList::RemoveAboveValue(SQInteger value)
|
void ScriptList::RemoveAboveValue(SQInteger value)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v > value; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second > value) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::RemoveBelowValue(SQInteger value)
|
void ScriptList::RemoveBelowValue(SQInteger value)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v < value; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second < value) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::RemoveBetweenValue(SQInteger start, SQInteger end)
|
void ScriptList::RemoveBetweenValue(SQInteger start, SQInteger end)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v > start && v < end; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second > start && iter->second < end) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::RemoveValue(SQInteger value)
|
void ScriptList::RemoveValue(SQInteger value)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v == value; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second == value) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::RemoveTop(SQInteger count)
|
void ScriptList::RemoveTop(SQInteger count)
|
||||||
@@ -781,55 +761,31 @@ void ScriptList::RemoveBottom(SQInteger count)
|
|||||||
|
|
||||||
void ScriptList::RemoveList(ScriptList *list)
|
void ScriptList::RemoveList(ScriptList *list)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
|
||||||
|
|
||||||
if (list == this) {
|
if (list == this) {
|
||||||
this->Clear();
|
this->Clear();
|
||||||
} else {
|
return;
|
||||||
for (const auto &item : list->items) {
|
|
||||||
this->RemoveItem(item.first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
this->RemoveItems([&](const SQInteger &k, const SQInteger &) { return list->HasItem(k); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::KeepAboveValue(SQInteger value)
|
void ScriptList::KeepAboveValue(SQInteger value)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v <= value; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second <= value) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::KeepBelowValue(SQInteger value)
|
void ScriptList::KeepBelowValue(SQInteger value)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v >= value; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second >= value) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::KeepBetweenValue(SQInteger start, SQInteger end)
|
void ScriptList::KeepBetweenValue(SQInteger start, SQInteger end)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v <= start && v >= end; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second <= start || iter->second >= end) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::KeepValue(SQInteger value)
|
void ScriptList::KeepValue(SQInteger value)
|
||||||
{
|
{
|
||||||
this->modifications++;
|
this->RemoveItems([&](const SQInteger &, const SQInteger &v) { return v != value; });
|
||||||
|
|
||||||
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
|
||||||
next_iter = std::next(iter);
|
|
||||||
if (iter->second != value) this->RemoveItem(iter->first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptList::KeepTop(SQInteger count)
|
void ScriptList::KeepTop(SQInteger count)
|
||||||
@@ -849,13 +805,7 @@ void ScriptList::KeepBottom(SQInteger count)
|
|||||||
void ScriptList::KeepList(ScriptList *list)
|
void ScriptList::KeepList(ScriptList *list)
|
||||||
{
|
{
|
||||||
if (list == this) return;
|
if (list == this) return;
|
||||||
|
this->RemoveItems([&](const SQInteger &k, const SQInteger &) { return !list->HasItem(k); });
|
||||||
this->modifications++;
|
|
||||||
|
|
||||||
ScriptList tmp;
|
|
||||||
tmp.AddList(this);
|
|
||||||
tmp.RemoveList(list);
|
|
||||||
this->RemoveList(&tmp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SQInteger ScriptList::_get(HSQUIRRELVM vm)
|
SQInteger ScriptList::_get(HSQUIRRELVM vm)
|
||||||
|
|||||||
@@ -154,6 +154,17 @@ protected:
|
|||||||
*/
|
*/
|
||||||
void CopyList(const ScriptList *list);
|
void CopyList(const ScriptList *list);
|
||||||
|
|
||||||
|
template <class ValueFilter>
|
||||||
|
void RemoveItems(ValueFilter value_filter)
|
||||||
|
{
|
||||||
|
this->modifications++;
|
||||||
|
|
||||||
|
for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
|
||||||
|
next_iter = std::next(iter);
|
||||||
|
if (value_filter(iter->first, iter->second)) this->RemoveItem(iter->first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef std::set<SQInteger> ScriptItemList; ///< The list of items inside the bucket
|
typedef std::set<SQInteger> ScriptItemList; ///< The list of items inside the bucket
|
||||||
typedef std::map<SQInteger, ScriptItemList> ScriptListBucket; ///< The bucket list per value
|
typedef std::map<SQInteger, ScriptItemList> ScriptListBucket; ///< The bucket list per value
|
||||||
|
|||||||
Reference in New Issue
Block a user