Changeset - r26977:2bcc83b9befe
[Not reviewed]
master
0 2 0
glx22 - 20 months ago 2023-03-04 14:51:01
glx@openttd.org
Codechange: Use SQInteger for generic numbers in script_list
2 files changed with 71 insertions and 71 deletions:
0 comments (0 inline, 0 general)
src/script/api/script_list.cpp
Show inline comments
 
@@ -19,47 +19,47 @@
 
 * Base class for any ScriptList sorter.
 
 */
 
class ScriptListSorter {
 
protected:
 
	ScriptList *list;       ///< The list that's being sorted.
 
	bool has_no_more_items; ///< Whether we have more items to iterate over.
 
	int64 item_next;        ///< The next item we will show.
 
	SQInteger item_next;    ///< The next item we will show.
 

	
 
public:
 
	/**
 
	 * Virtual dtor, needed to mute warnings.
 
	 */
 
	virtual ~ScriptListSorter() { }
 

	
 
	/**
 
	 * Get the first item of the sorter.
 
	 */
 
	virtual int64 Begin() = 0;
 
	virtual SQInteger Begin() = 0;
 

	
 
	/**
 
	 * Stop iterating a sorter.
 
	 */
 
	virtual void End() = 0;
 

	
 
	/**
 
	 * Get the next item of the sorter.
 
	 */
 
	virtual int64 Next() = 0;
 
	virtual SQInteger Next() = 0;
 

	
 
	/**
 
	 * See if the sorter has reached the end.
 
	 */
 
	bool IsEnd()
 
	{
 
		return this->list->buckets.empty() || this->has_no_more_items;
 
	}
 

	
 
	/**
 
	 * Callback from the list if an item gets removed.
 
	 */
 
	virtual void Remove(int item) = 0;
 
	virtual void Remove(SQInteger item) = 0;
 

	
 
	/**
 
	 * Attach the sorter to a new list. This assumes the content of the old list has been moved to
 
	 * the new list, too, so that we don't have to invalidate any iterators. Note that std::swap
 
	 * doesn't invalidate iterators on lists and maps, so that should be safe.
 
	 * @param target New list to attach to.
 
@@ -87,23 +87,23 @@ public:
 
	ScriptListSorterValueAscending(ScriptList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
	int64 Begin()
 
	SQInteger Begin()
 
	{
 
		if (this->list->buckets.empty()) return 0;
 
		this->has_no_more_items = false;
 

	
 
		this->bucket_iter = this->list->buckets.begin();
 
		this->bucket_list = &(*this->bucket_iter).second;
 
		this->bucket_list_iter = this->bucket_list->begin();
 
		this->item_next = *this->bucket_list_iter;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void End()
 
	{
 
@@ -132,22 +132,22 @@ public:
 
			this->bucket_list = &(*this->bucket_iter).second;
 
			this->bucket_list_iter = this->bucket_list->begin();
 
		}
 
		this->item_next = *this->bucket_list_iter;
 
	}
 

	
 
	int64 Next()
 
	SQInteger Next()
 
	{
 
		if (this->IsEnd()) return 0;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void Remove(int item)
 
	void Remove(SQInteger item)
 
	{
 
		if (this->IsEnd()) return;
 

	
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
@@ -176,13 +176,13 @@ public:
 
	ScriptListSorterValueDescending(ScriptList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
	int64 Begin()
 
	SQInteger Begin()
 
	{
 
		if (this->list->buckets.empty()) return 0;
 
		this->has_no_more_items = false;
 

	
 
		/* Go to the end of the bucket-list */
 
		this->bucket_iter = this->list->buckets.end();
 
@@ -191,13 +191,13 @@ public:
 

	
 
		/* Go to the end of the items in the bucket */
 
		this->bucket_list_iter = this->bucket_list->end();
 
		--this->bucket_list_iter;
 
		this->item_next = *this->bucket_list_iter;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void End()
 
	{
 
@@ -229,22 +229,22 @@ public:
 
		} else {
 
			this->bucket_list_iter--;
 
		}
 
		this->item_next = *this->bucket_list_iter;
 
	}
 

	
 
	int64 Next()
 
	SQInteger Next()
 
	{
 
		if (this->IsEnd()) return 0;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void Remove(int item)
 
	void Remove(SQInteger item)
 
	{
 
		if (this->IsEnd()) return;
 

	
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
@@ -268,21 +268,21 @@ public:
 
	ScriptListSorterItemAscending(ScriptList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
	int64 Begin()
 
	SQInteger Begin()
 
	{
 
		if (this->list->items.empty()) return 0;
 
		this->has_no_more_items = false;
 

	
 
		this->item_iter = this->list->items.begin();
 
		this->item_next = (*this->item_iter).first;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void End()
 
	{
 
@@ -299,22 +299,22 @@ public:
 
			return;
 
		}
 
		this->item_iter++;
 
		if (this->item_iter != this->list->items.end()) item_next = (*this->item_iter).first;
 
	}
 

	
 
	int64 Next()
 
	SQInteger Next()
 
	{
 
		if (this->IsEnd()) return 0;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void Remove(int item)
 
	void Remove(SQInteger item)
 
	{
 
		if (this->IsEnd()) return;
 

	
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
@@ -341,22 +341,22 @@ public:
 
	ScriptListSorterItemDescending(ScriptList *list)
 
	{
 
		this->list = list;
 
		this->End();
 
	}
 

	
 
	int64 Begin()
 
	SQInteger Begin()
 
	{
 
		if (this->list->items.empty()) return 0;
 
		this->has_no_more_items = false;
 

	
 
		this->item_iter = this->list->items.end();
 
		--this->item_iter;
 
		this->item_next = (*this->item_iter).first;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void End()
 
	{
 
@@ -378,22 +378,22 @@ public:
 
		} else {
 
			this->item_iter--;
 
		}
 
		if (this->item_iter != this->list->items.end()) item_next = (*this->item_iter).first;
 
	}
 

	
 
	int64 Next()
 
	SQInteger Next()
 
	{
 
		if (this->IsEnd()) return 0;
 

	
 
		int64 item_current = this->item_next;
 
		SQInteger item_current = this->item_next;
 
		FindNext();
 
		return item_current;
 
	}
 

	
 
	void Remove(int item)
 
	void Remove(SQInteger item)
 
	{
 
		if (this->IsEnd()) return;
 

	
 
		/* If we remove the 'next' item, skip to the next */
 
		if (item == this->item_next) {
 
			FindNext();
 
@@ -416,13 +416,13 @@ ScriptList::ScriptList()
 

	
 
ScriptList::~ScriptList()
 
{
 
	delete this->sorter;
 
}
 

	
 
bool ScriptList::HasItem(int64 item)
 
bool ScriptList::HasItem(SQInteger item)
 
{
 
	return this->items.count(item) == 1;
 
}
 

	
 
void ScriptList::Clear()
 
{
 
@@ -430,46 +430,46 @@ void ScriptList::Clear()
 

	
 
	this->items.clear();
 
	this->buckets.clear();
 
	this->sorter->End();
 
}
 

	
 
void ScriptList::AddItem(int64 item, int64 value)
 
void ScriptList::AddItem(SQInteger item, SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	if (this->HasItem(item)) return;
 

	
 
	this->items[item] = value;
 
	this->buckets[value].insert(item);
 
}
 

	
 
void ScriptList::RemoveItem(int64 item)
 
void ScriptList::RemoveItem(SQInteger item)
 
{
 
	this->modifications++;
 

	
 
	ScriptListMap::iterator item_iter = this->items.find(item);
 
	if (item_iter == this->items.end()) return;
 

	
 
	int64 value = item_iter->second;
 
	SQInteger value = item_iter->second;
 

	
 
	this->sorter->Remove(item);
 
	ScriptListBucket::iterator bucket_iter = this->buckets.find(value);
 
	assert(bucket_iter != this->buckets.end());
 
	bucket_iter->second.erase(item);
 
	if (bucket_iter->second.empty()) this->buckets.erase(bucket_iter);
 
	this->items.erase(item_iter);
 
}
 

	
 
int64 ScriptList::Begin()
 
SQInteger ScriptList::Begin()
 
{
 
	this->initialized = true;
 
	return this->sorter->Begin();
 
}
 

	
 
int64 ScriptList::Next()
 
SQInteger ScriptList::Next()
 
{
 
	if (!this->initialized) {
 
		Debug(script, 0, "Next() is invalid as Begin() is never called");
 
		return 0;
 
	}
 
	return this->sorter->Next();
 
@@ -486,31 +486,31 @@ bool ScriptList::IsEnd()
 
		Debug(script, 0, "IsEnd() is invalid as Begin() is never called");
 
		return true;
 
	}
 
	return this->sorter->IsEnd();
 
}
 

	
 
int32 ScriptList::Count()
 
SQInteger ScriptList::Count()
 
{
 
	return (int32)this->items.size();
 
	return this->items.size();
 
}
 

	
 
int64 ScriptList::GetValue(int64 item)
 
SQInteger ScriptList::GetValue(SQInteger item)
 
{
 
	ScriptListMap::const_iterator item_iter = this->items.find(item);
 
	return item_iter == this->items.end() ? 0 : item_iter->second;
 
}
 

	
 
bool ScriptList::SetValue(int64 item, int64 value)
 
bool ScriptList::SetValue(SQInteger item, SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	ScriptListMap::iterator item_iter = this->items.find(item);
 
	if (item_iter == this->items.end()) return false;
 

	
 
	int64 value_old = item_iter->second;
 
	SQInteger value_old = item_iter->second;
 
	if (value_old == value) return true;
 

	
 
	this->sorter->Remove(item);
 
	ScriptListBucket::iterator bucket_iter = this->buckets.find(value_old);
 
	assert(bucket_iter != this->buckets.end());
 
	bucket_iter->second.erase(item);
 
@@ -583,53 +583,53 @@ void ScriptList::SwapList(ScriptList *li
 
	Swap(this->initialized, list->initialized);
 
	Swap(this->modifications, list->modifications);
 
	this->sorter->Retarget(this);
 
	list->sorter->Retarget(list);
 
}
 

	
 
void ScriptList::RemoveAboveValue(int64 value)
 
void ScriptList::RemoveAboveValue(SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second > value) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::RemoveBelowValue(int64 value)
 
void ScriptList::RemoveBelowValue(SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second < value) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::RemoveBetweenValue(int64 start, int64 end)
 
void ScriptList::RemoveBetweenValue(SQInteger start, SQInteger end)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second > start && (*iter).second < end) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::RemoveValue(int64 value)
 
void ScriptList::RemoveValue(SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second == value) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::RemoveTop(int32 count)
 
void ScriptList::RemoveTop(SQInteger count)
 
{
 
	this->modifications++;
 

	
 
	if (!this->sort_ascending) {
 
		this->Sort(this->sorter_type, !this->sort_ascending);
 
		this->RemoveBottom(count);
 
@@ -660,13 +660,13 @@ void ScriptList::RemoveTop(int32 count)
 
				this->RemoveItem((*iter).first);
 
			}
 
			break;
 
	}
 
}
 

	
 
void ScriptList::RemoveBottom(int32 count)
 
void ScriptList::RemoveBottom(SQInteger count)
 
{
 
	this->modifications++;
 

	
 
	if (!this->sort_ascending) {
 
		this->Sort(this->sorter_type, !this->sort_ascending);
 
		this->RemoveTop(count);
 
@@ -711,60 +711,60 @@ void ScriptList::RemoveList(ScriptList *
 
		for (ScriptListMap::iterator iter = list_items->begin(); iter != list_items->end(); iter++) {
 
			this->RemoveItem((*iter).first);
 
		}
 
	}
 
}
 

	
 
void ScriptList::KeepAboveValue(int64 value)
 
void ScriptList::KeepAboveValue(SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second <= value) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::KeepBelowValue(int64 value)
 
void ScriptList::KeepBelowValue(SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second >= value) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::KeepBetweenValue(int64 start, int64 end)
 
void ScriptList::KeepBetweenValue(SQInteger start, SQInteger end)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second <= start || (*iter).second >= end) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::KeepValue(int64 value)
 
void ScriptList::KeepValue(SQInteger value)
 
{
 
	this->modifications++;
 

	
 
	for (ScriptListMap::iterator next_iter, iter = this->items.begin(); iter != this->items.end(); iter = next_iter) {
 
		next_iter = iter; next_iter++;
 
		if ((*iter).second != value) this->RemoveItem((*iter).first);
 
	}
 
}
 

	
 
void ScriptList::KeepTop(int32 count)
 
void ScriptList::KeepTop(SQInteger count)
 
{
 
	this->modifications++;
 

	
 
	this->RemoveBottom(this->Count() - count);
 
}
 

	
 
void ScriptList::KeepBottom(int32 count)
 
void ScriptList::KeepBottom(SQInteger count)
 
{
 
	this->modifications++;
 

	
 
	this->RemoveTop(this->Count() - count);
 
}
 

	
src/script/api/script_list.hpp
Show inline comments
 
@@ -39,15 +39,15 @@ private:
 
	SorterType sorter_type;       ///< Sorting type
 
	bool sort_ascending;          ///< Whether to sort ascending or descending
 
	bool initialized;             ///< Whether an iteration has been started
 
	int modifications;            ///< Number of modification that has been done. To prevent changing data while valuating.
 

	
 
public:
 
	typedef std::set<int64> ScriptItemList;                   ///< The list of items inside the bucket
 
	typedef std::map<int64, ScriptItemList> ScriptListBucket; ///< The bucket list per value
 
	typedef std::map<int64, int64> ScriptListMap;             ///< List per item
 
	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, SQInteger> ScriptListMap;         ///< List per item
 

	
 
	ScriptListMap items;           ///< The items in the list
 
	ScriptListBucket buckets;      ///< The items in the list, sorted by value
 

	
 
	ScriptList();
 
	~ScriptList();
 
@@ -55,48 +55,48 @@ public:
 
#ifdef DOXYGEN_API
 
	/**
 
	 * Add a single item to the list.
 
	 * @param item the item to add. Should be unique, otherwise it is ignored.
 
	 * @param value the value to assign.
 
	 */
 
	void AddItem(int64 item, int64 value);
 
	void AddItem(SQInteger item, SQInteger value);
 
#else
 
	void AddItem(int64 item, int64 value = 0);
 
	void AddItem(SQInteger item, SQInteger value = 0);
 
#endif /* DOXYGEN_API */
 

	
 
	/**
 
	 * Remove a single item from the list.
 
	 * @param item the item to remove. If not existing, it is ignored.
 
	 */
 
	void RemoveItem(int64 item);
 
	void RemoveItem(SQInteger item);
 

	
 
	/**
 
	 * Clear the list, making Count() returning 0 and IsEmpty() returning true.
 
	 */
 
	void Clear();
 

	
 
	/**
 
	 * Check if an item is in the list.
 
	 * @param item the item to check for.
 
	 * @return true if the item is in the list.
 
	 */
 
	bool HasItem(int64 item);
 
	bool HasItem(SQInteger item);
 

	
 
	/**
 
	 * Go to the beginning of the list and return the item. To get the value use list.GetValue(list.Begin()).
 
	 * @return the first item.
 
	 * @note returns 0 if beyond end-of-list. Use IsEnd() to check for end-of-list.
 
	 */
 
	int64 Begin();
 
	SQInteger Begin();
 

	
 
	/**
 
	 * Go to the next item in the list and return the item. To get the value use list.GetValue(list.Next()).
 
	 * @return the next item.
 
	 * @note returns 0 if beyond end-of-list. Use IsEnd() to check for end-of-list.
 
	 */
 
	int64 Next();
 
	SQInteger Next();
 

	
 
	/**
 
	 * Check if a list is empty.
 
	 * @return true if the list is empty.
 
	 */
 
	bool IsEmpty();
 
@@ -109,30 +109,30 @@ public:
 
	bool IsEnd();
 

	
 
	/**
 
	 * Returns the amount of items in the list.
 
	 * @return amount of items in the list.
 
	 */
 
	int32 Count();
 
	SQInteger Count();
 

	
 
	/**
 
	 * Get the value that belongs to this item.
 
	 * @param item the item to get the value from
 
	 * @return the value that belongs to this item.
 
	 */
 
	int64 GetValue(int64 item);
 
	SQInteger GetValue(SQInteger item);
 

	
 
	/**
 
	 * Set a value of an item directly.
 
	 * @param item the item to set the value for.
 
	 * @param value the value to give to the item
 
	 * @return true if we could set the item to value, false otherwise.
 
	 * @note Changing values of items while looping through a list might cause
 
	 *  entries to be skipped. Be very careful with such operations.
 
	 */
 
	bool SetValue(int64 item, int64 value);
 
	bool SetValue(SQInteger item, SQInteger value);
 

	
 
	/**
 
	 * Sort this list by the given sorter and direction.
 
	 * @param sorter    the type of sorter to use
 
	 * @param ascending if true, lowest value is on top, else at bottom.
 
	 * @note the current item stays at the same place.
 
@@ -157,88 +157,88 @@ public:
 
	void SwapList(ScriptList *list);
 

	
 
	/**
 
	 * Removes all items with a higher value than 'value'.
 
	 * @param value the value above which all items are removed.
 
	 */
 
	void RemoveAboveValue(int64 value);
 
	void RemoveAboveValue(SQInteger value);
 

	
 
	/**
 
	 * Removes all items with a lower value than 'value'.
 
	 * @param value the value below which all items are removed.
 
	 */
 
	void RemoveBelowValue(int64 value);
 
	void RemoveBelowValue(SQInteger value);
 

	
 
	/**
 
	 * Removes all items with a value above start and below end.
 
	 * @param start the lower bound of the to be removed values (exclusive).
 
	 * @param end   the upper bound of the to be removed values (exclusive).
 
	 */
 
	void RemoveBetweenValue(int64 start, int64 end);
 
	void RemoveBetweenValue(SQInteger start, SQInteger end);
 

	
 
	/**
 
	 * Remove all items with this value.
 
	 * @param value the value to remove.
 
	 */
 
	void RemoveValue(int64 value);
 
	void RemoveValue(SQInteger value);
 

	
 
	/**
 
	 * Remove the first count items.
 
	 * @param count the amount of items to remove.
 
	 */
 
	void RemoveTop(int32 count);
 
	void RemoveTop(SQInteger count);
 

	
 
	/**
 
	 * Remove the last count items.
 
	 * @param count the amount of items to remove.
 
	 */
 
	void RemoveBottom(int32 count);
 
	void RemoveBottom(SQInteger count);
 

	
 
	/**
 
	 * Remove everything that is in the given list from this list (same item index that is).
 
	 * @param list the list of items to remove.
 
	 * @pre list != null
 
	 */
 
	void RemoveList(ScriptList *list);
 

	
 
	/**
 
	 * Keep all items with a higher value than 'value'.
 
	 * @param value the value above which all items are kept.
 
	 */
 
	void KeepAboveValue(int64 value);
 
	void KeepAboveValue(SQInteger value);
 

	
 
	/**
 
	 * Keep all items with a lower value than 'value'.
 
	 * @param value the value below which all items are kept.
 
	 */
 
	void KeepBelowValue(int64 value);
 
	void KeepBelowValue(SQInteger value);
 

	
 
	/**
 
	 * Keep all items with a value above start and below end.
 
	 * @param start the lower bound of the to be kept values (exclusive).
 
	 * @param end   the upper bound of the to be kept values (exclusive).
 
	 */
 
	void KeepBetweenValue(int64 start, int64 end);
 
	void KeepBetweenValue(SQInteger start, SQInteger end);
 

	
 
	/**
 
	 * Keep all items with this value.
 
	 * @param value the value to keep.
 
	 */
 
	void KeepValue(int64 value);
 
	void KeepValue(SQInteger value);
 

	
 
	/**
 
	 * Keep the first count items, i.e. remove everything except the first count items.
 
	 * @param count the amount of items to keep.
 
	 */
 
	void KeepTop(int32 count);
 
	void KeepTop(SQInteger count);
 

	
 
	/**
 
	 * Keep the last count items, i.e. remove everything except the last count items.
 
	 * @param count the amount of items to keep.
 
	 */
 
	void KeepBottom(int32 count);
 
	void KeepBottom(SQInteger count);
 

	
 
	/**
 
	 * Keeps everything that is in the given list from this list (same item index that is).
 
	 * @param list the list of items to keep.
 
	 * @pre list != null
 
	 */
0 comments (0 inline, 0 general)