@@ -12,48 +12,48 @@
#include "../../stdafx.h"
#include "script_list.hpp"
#include "../../debug.h"
#include "../../script/squirrel.hpp"
#include "../../safeguards.h"
/**
* 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.
int32 item_next; ///< The next item we will show.
int64 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 int32 Begin() = 0;
virtual int64 Begin() = 0;
* Stop iterating a sorter.
virtual void End() = 0;
* Get the next item of the sorter.
virtual int32 Next() = 0;
virtual int64 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.
@@ -82,35 +82,35 @@ private:
* Create a new sorter.
* @param list The list to sort.
ScriptListSorterValueAscending(ScriptList *list)
this->list = list;
this->End();
int32 Begin()
int64 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;
int32 item_current = this->item_next;
int64 item_current = this->item_next;
FindNext();
return item_current;
void End()
this->bucket_list = NULL;
this->has_no_more_items = true;
this->item_next = 0;
@@ -127,29 +127,29 @@ public:
if (this->bucket_list_iter == this->bucket_list->end()) {
this->bucket_iter++;
if (this->bucket_iter == this->list->buckets.end()) {
return;
int32 Next()
int64 Next()
if (this->IsEnd()) return 0;
void Remove(int item)
if (this->IsEnd()) return;
/* If we remove the 'next' item, skip to the next */
if (item == this->item_next) {
@@ -171,40 +171,40 @@ private:
ScriptListSorterValueDescending(ScriptList *list)
/* Go to the end of the bucket-list */
this->bucket_iter = this->list->buckets.end();
--this->bucket_iter;
/* Go to the end of the items in the bucket */
this->bucket_list_iter = this->bucket_list->end();
--this->bucket_list_iter;
@@ -224,29 +224,29 @@ public:
this->bucket_iter--;
} else {
this->bucket_list_iter--;
@@ -263,60 +263,60 @@ private:
ScriptListSorterItemAscending(ScriptList *list)
if (this->list->items.empty()) return 0;
this->item_iter = this->list->items.begin();
this->item_next = (*this->item_iter).first;
* Find the next item, and store that information.
void FindNext()
if (this->item_iter == this->list->items.end()) {
this->item_iter++;
if (this->item_iter != this->list->items.end()) item_next = (*this->item_iter).first;
@@ -336,34 +336,34 @@ private:
ScriptListSorterItemDescending(ScriptList *list)
this->item_iter = this->list->items.end();
--this->item_iter;
@@ -373,29 +373,29 @@ public:
if (this->item_iter == this->list->items.begin()) {
/* Use 'end' as marker for 'beyond begin' */
this->item_iter--;
@@ -411,71 +411,71 @@ ScriptList::ScriptList()
this->sorter = new ScriptListSorterValueDescending(this);
this->sorter_type = SORT_BY_VALUE;
this->sort_ascending = false;
this->initialized = false;
this->modifications = 0;
ScriptList::~ScriptList()
delete this->sorter;
bool ScriptList::HasItem(int32 item)
bool ScriptList::HasItem(int64 item)
return this->items.count(item) == 1;
void ScriptList::Clear()
this->modifications++;
this->items.clear();
this->buckets.clear();
this->sorter->End();
void ScriptList::AddItem(int32 item, int32 value)
void ScriptList::AddItem(int64 item, int64 value)
if (this->HasItem(item)) return;
this->items[item] = 0;
this->buckets[0].insert(item);
this->SetValue(item, value);
void ScriptList::RemoveItem(int32 item)
void ScriptList::RemoveItem(int64 item)
if (!this->HasItem(item)) return;
int32 value = this->GetValue(item);
int64 value = this->GetValue(item);
this->sorter->Remove(item);
this->buckets[value].erase(item);
if (this->buckets[value].empty()) this->buckets.erase(value);
this->items.erase(item);
int32 ScriptList::Begin()
int64 ScriptList::Begin()
this->initialized = true;
return this->sorter->Begin();
int32 ScriptList::Next()
int64 ScriptList::Next()
if (this->initialized == false) {
DEBUG(script, 0, "Next() is invalid as Begin() is never called");
return 0;
return this->sorter->Next();
bool ScriptList::IsEmpty()
return this->items.empty();
@@ -485,38 +485,38 @@ bool ScriptList::IsEnd()
DEBUG(script, 0, "IsEnd() is invalid as Begin() is never called");
return true;
return this->sorter->IsEnd();
int32 ScriptList::Count()
return (int32)this->items.size();
int32 ScriptList::GetValue(int32 item)
int64 ScriptList::GetValue(int64 item)
if (!this->HasItem(item)) return 0;
return this->items[item];
bool ScriptList::SetValue(int32 item, int32 value)
bool ScriptList::SetValue(int64 item, int64 value)
if (!this->HasItem(item)) return false;
int32 value_old = this->GetValue(item);
int64 value_old = this->GetValue(item);
if (value_old == value) return true;
this->buckets[value_old].erase(item);
if (this->buckets[value_old].empty()) this->buckets.erase(value_old);
this->items[item] = value;
this->buckets[value].insert(item);
void ScriptList::Sort(SorterType sorter, bool ascending)
@@ -564,55 +564,55 @@ void ScriptList::SwapList(ScriptList *li
this->items.swap(list->items);
this->buckets.swap(list->buckets);
Swap(this->sorter, list->sorter);
Swap(this->sorter_type, list->sorter_type);
Swap(this->sort_ascending, list->sort_ascending);
Swap(this->initialized, list->initialized);
Swap(this->modifications, list->modifications);
this->sorter->Retarget(this);
list->sorter->Retarget(list);
void ScriptList::RemoveAboveValue(int32 value)
void ScriptList::RemoveAboveValue(int64 value)
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(int32 value)
void ScriptList::RemoveBelowValue(int64 value)
if ((*iter).second < value) this->RemoveItem((*iter).first);
void ScriptList::RemoveBetweenValue(int32 start, int32 end)
void ScriptList::RemoveBetweenValue(int64 start, int64 end)
if ((*iter).second > start && (*iter).second < end) this->RemoveItem((*iter).first);
void ScriptList::RemoveValue(int32 value)
void ScriptList::RemoveValue(int64 value)
if ((*iter).second == value) this->RemoveItem((*iter).first);
void ScriptList::RemoveTop(int32 count)
@@ -688,55 +688,55 @@ void ScriptList::RemoveBottom(int32 coun
void ScriptList::RemoveList(ScriptList *list)
ScriptListMap *list_items = &list->items;
for (ScriptListMap::iterator iter = list_items->begin(); iter != list_items->end(); iter++) {
this->RemoveItem((*iter).first);
void ScriptList::KeepAboveValue(int32 value)
void ScriptList::KeepAboveValue(int64 value)
if ((*iter).second <= value) this->RemoveItem((*iter).first);
void ScriptList::KeepBelowValue(int32 value)
void ScriptList::KeepBelowValue(int64 value)
if ((*iter).second >= value) this->RemoveItem((*iter).first);
void ScriptList::KeepBetweenValue(int32 start, int32 end)
void ScriptList::KeepBetweenValue(int64 start, int64 end)
if ((*iter).second <= start || (*iter).second >= end) this->RemoveItem((*iter).first);
void ScriptList::KeepValue(int32 value)
void ScriptList::KeepValue(int64 value)
if ((*iter).second != value) this->RemoveItem((*iter).first);
void ScriptList::KeepTop(int32 count)
@@ -35,76 +35,76 @@ public:
static const bool SORT_ASCENDING = true;
/** Sort descending */
static const bool SORT_DESCENDING = false;
private:
ScriptListSorter *sorter; ///< Sorting algorithm
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.
typedef std::set<int32> ScriptItemList; ///< The list of items inside the bucket
typedef std::map<int32, ScriptItemList> ScriptListBucket; ///< The bucket list per value
typedef std::map<int32, int32> ScriptListMap; ///< List per item
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
ScriptListMap items; ///< The items in the list
ScriptListBucket buckets; ///< The items in the list, sorted by value
ScriptList();
~ScriptList();
#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(int32 item, int32 value);
void AddItem(int64 item, int64 value);
#else
void AddItem(int32 item, int32 value = 0);
void AddItem(int64 item, int64 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(int32 item);
void RemoveItem(int64 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(int32 item);
bool HasItem(int64 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.
int32 Begin();
int64 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.
int32 Next();
int64 Next();
* Check if a list is empty.
* @return true if the list is empty.
bool IsEmpty();
* Check if there is a element left. In other words, if this is false,
* the last call to Begin() or Next() returned a valid item.
* @return true if the current item is beyond end-of-list.
@@ -112,35 +112,35 @@ public:
* Returns the amount of items in the list.
* @return amount of items in the list.
int32 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.
int32 GetValue(int32 item);
int64 GetValue(int64 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(int32 item, int32 value);
bool SetValue(int64 item, int64 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.
* @see SORT_ASCENDING SORT_DESCENDING
void Sort(SorterType sorter, bool ascending);
* Add one list to another one.
@@ -153,88 +153,88 @@ public:
void AddList(ScriptList *list);
* Swap the contents of two lists.
* @param list The list that will be swapped with.
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(int32 value);
void RemoveAboveValue(int64 value);
* Removes all items with a lower value than 'value'.
* @param value the value below which all items are removed.
void RemoveBelowValue(int32 value);
void RemoveBelowValue(int64 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(int32 start, int32 end);
void RemoveBetweenValue(int64 start, int64 end);
* Remove all items with this value.
* @param value the value to remove.
void RemoveValue(int32 value);
void RemoveValue(int64 value);
* Remove the first count items.
* @param count the amount of items to remove.
void RemoveTop(int32 count);
* Remove the last count items.
void RemoveBottom(int32 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(int32 value);
void KeepAboveValue(int64 value);
* Keep all items with a lower value than 'value'.
* @param value the value below which all items are kept.
void KeepBelowValue(int32 value);
void KeepBelowValue(int64 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(int32 start, int32 end);
void KeepBetweenValue(int64 start, int64 end);
* Keep all items with this value.
* @param value the value to keep.
void KeepValue(int32 value);
void KeepValue(int64 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);
* Keep the last count items, i.e. remove everything except the last count items.
void KeepBottom(int32 count);
Status change: