truelight@9579: /* $Id$ */ truelight@9579: truelight@9579: /** @file ai_list.hpp a linked list which can keep item/value pairs */ truelight@9579: truelight@9579: #ifndef AI_LIST_HPP truelight@9579: #define AI_LIST_HPP truelight@9579: truelight@9579: #include "ai_object.hpp" truelight@9579: #include truelight@9579: #include truelight@9579: truelight@9579: class AIListSorter; truelight@9579: truelight@9579: /** truelight@9579: * Class that creates a linked list which can keep item/value pairs. truelight@9579: */ truelight@9579: class AIList : public AIObject { truelight@9579: private: truelight@9579: AIListSorter *sorter; truelight@9579: truelight@9579: public: truelight@9590: typedef std::set AIItemList; ///< The list of items inside the bucket truelight@9590: typedef std::map AIListBucket; ///< The bucket list per value truelight@9590: typedef std::map AIListMap; ///< List per item truelight@9579: truelight@9579: AIListMap items; ///< The items in the list truelight@9579: AIListBucket buckets; ///< The items in the list, sorted by value truelight@9579: truelight@9579: public: truelight@9579: truelight@9579: /** truelight@9579: * The name of the class, needed by several sub-processes. truelight@9579: */ truelight@9579: static const char *GetClassName() { return "AIList"; } truelight@9579: truelight@9579: /** truelight@9579: * Constructor of the AIList. truelight@9579: */ truelight@9579: AIList(); truelight@9579: truelight@9579: /** truelight@9579: * Destructor of the AIList. truelight@9579: */ truelight@9579: ~AIList(); truelight@9579: truelight@9579: /** truelight@9579: * Clear the list, making Count() returning 0 and IsEmpty() returning true. truelight@9579: */ truelight@9579: void Clear(); truelight@9579: truelight@9579: /** truelight@9579: * Add a single item to the list. truelight@9579: * @param item the item to add. Should be unique, otherwise it is ignored. truelight@9579: * @note the value is set to 0 by default. truelight@9579: */ truelight@9579: void AddItem(int32 item); truelight@9579: truelight@9579: /** truelight@9579: * Remove a single item from the list. truelight@9579: * @param item the item to remove. If not existing, it is ignored. truelight@9579: */ truelight@9579: void RemoveItem(int32 item); truelight@9579: truelight@9579: /** truelight@9579: * Check if an item is in the list. truelight@9579: * @param item the item to check for. truelight@9579: * @return true if the item is in the list. truelight@9579: */ truelight@9579: bool HasItem(int32 item); truelight@9579: truelight@9579: /** truelight@9579: * Go to the beginning of the list. truelight@9579: * @return the item value of the first item. truelight@9579: */ truelight@9579: int32 Begin(); truelight@9579: truelight@9579: /** truelight@9579: * Go to the next item in the list. truelight@9579: * @return the item value of the next item. truelight@9579: * @note returns 0 if beyond end-of-list. Use HasNext() to check for end-of-list. truelight@9579: */ truelight@9579: int32 Next(); truelight@9579: truelight@9579: /** truelight@9579: * Check if a list is empty. truelight@9579: * @return true if the list is empty. truelight@9579: **/ truelight@9579: bool IsEmpty(); truelight@9579: truelight@9579: /** truelight@9579: * Check if there is a next element. In other words, if this is true, truelight@9579: * Next() will return a valid item. truelight@9579: * @return true if there is a next item. truelight@9579: */ truelight@9579: bool HasNext(); truelight@9579: truelight@9579: /** truelight@9579: * Returns the amount of items in the list. truelight@9579: * @return amount of items in the list. truelight@9579: */ truelight@9579: int32 Count(); truelight@9579: truelight@9579: /** truelight@9579: * Get the value that belongs to this item. truelight@9579: * @param item the item to get the value from truelight@9579: * @return the value that belongs to this item. truelight@9579: */ truelight@9579: int32 GetValue(int32 item); truelight@9579: truelight@9579: /** truelight@9579: * Sort this list by item-value. truelight@9579: * @param ascending if true, lowest value is on top, else at bottom. truelight@9579: * @note the current item stays at the same place. truelight@9579: */ truelight@9579: void SortByItem(bool ascending); truelight@9579: truelight@9579: /** truelight@9579: * Sort this list by the value of the items. truelight@9579: * @param ascending if true, lowest value is on top, else at bottom. truelight@9579: * @note the current item stays at the same place. truelight@9579: */ truelight@9579: void SortByValue(bool ascending); truelight@9579: truelight@9579: /** truelight@9579: * Removes all items with a higher value than 'value'. truelight@9579: * @param value the value above which all items are removed. truelight@9579: */ truelight@9579: void RemoveAboveValue(int32 value); truelight@9579: truelight@9579: /** truelight@9579: * Removes all items with a lower value than 'value'. truelight@9579: * @param value the value below which all items are removed. truelight@9579: */ truelight@9579: void RemoveBelowValue(int32 value); truelight@9579: truelight@9579: /** truelight@9579: * Removes all items with a value above start and below end. truelight@9579: * @param start the lower bound of the to be removed values (exclusive). truelight@9579: * @param end the upper bound of the to be removed valuens (exclusive). truelight@9579: */ truelight@9579: void RemoveBetweenValue(int32 start, int32 end); truelight@9579: truelight@9579: /** truelight@9579: * Remove all items with this value. truelight@9579: * @param value the value to remove. truelight@9579: */ truelight@9579: void RemoveValue(int32 value); truelight@9579: truelight@9579: /** truelight@9579: * Keep all items with a higher value than 'value'. truelight@9579: * @param value the value above which all items are kept. truelight@9579: */ truelight@9579: void KeepAboveValue(int32 value); truelight@9579: truelight@9579: /** truelight@9579: * Keep all items with a lower value than 'value'. truelight@9579: * @param value the value below which all items are kept. truelight@9579: */ truelight@9579: void KeepBelowValue(int32 value); truelight@9579: truelight@9579: /** truelight@9579: * Keep all items with a value above start and below end. truelight@9579: * @param start the lower bound of the to be kept values (exclusive). truelight@9579: * @param end the upper bound of the to be kept values (exclusive). truelight@9579: */ truelight@9579: void KeepBetweenValue(int32 start, int32 end); truelight@9579: truelight@9579: /** truelight@9579: * Keep all items with this value. truelight@9579: * @param value the value to keep. truelight@9579: **/ truelight@9579: void KeepValue(int32 value); truelight@9579: truelight@9579: /** truelight@9579: * The definition how valuators should look. truelight@9579: */ truelight@9579: class Valuator { truelight@9590: /* Make this valuator a friend of AIList, so we can access the private. truelight@9590: * Nobody else should ever call Valuate. */ truelight@9589: friend class AIList; truelight@9579: public: truelight@9590: /** truelight@9590: * Virtual destructor, needed to make compilers happy. truelight@9590: */ truelight@9579: virtual ~Valuator() {} truelight@9579: truelight@9589: private: truelight@9589: virtual int32 Valuate(int32 item) const = 0; truelight@9579: }; truelight@9579: truelight@9579: /** truelight@9579: * Give all items a value defined by the valuator you give. truelight@9579: * @note the valuator should be a valid instance. truelight@9579: */ truelight@9589: void Valuate(const AIList::Valuator &proc); truelight@9579: }; truelight@9579: truelight@9579: #ifdef DEFINE_SQUIRREL_CLASS truelight@9579: namespace SQConvert { truelight@9579: /* Allow inner classes/structs to be used as Squirrel parameters */ truelight@9589: template <> const AIList::Valuator &GetParam(ForceType, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIList::Valuator *)instance; } truelight@9579: truelight@9579: /* Allow AIList to be used as Squirrel parameter */ truelight@9579: template <> AIList *GetParam(ForceType, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIList *)instance; } truelight@9579: }; // namespace SQConvert truelight@9579: truelight@9579: void SQAIListRegister(Squirrel *engine) { truelight@9579: DefSQClass SQAIList("AIList"); truelight@9579: SQAIList.PreRegister(engine); truelight@9579: SQAIList.AddConstructor(engine); truelight@9579: truelight@9579: SQAIList.DefSQStaticMethod(engine, &AIList::GetClassName, "GetClassName", 1, "x"); truelight@9579: truelight@9579: SQAIList.DefSQMethod(engine, &AIList::Clear, "Clear", 1, "x"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::AddItem, "AddItem", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::RemoveItem, "RemoveItem", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::HasItem, "HasItem", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::Begin, "Begin", 1, "x"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::Next, "Next", 1, "x"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::IsEmpty, "IsEmpty", 1, "x"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::HasNext, "HasNext", 1, "x"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::Count, "Count", 1, "x"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::GetValue, "GetValue", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::SortByItem, "SortByItem", 2, "xb"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::SortByValue, "SortByValue", 2, "xb"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::RemoveAboveValue, "RemoveAboveValue", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::RemoveBelowValue, "RemoveBelowValue", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::RemoveBetweenValue, "RemoveBetweenValue", 3, "xii"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::RemoveValue, "RemoveValue", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::KeepAboveValue, "KeepAboveValue", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::KeepBelowValue, "KeepBelowValue", 2, "xi"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::KeepBetweenValue, "KeepBetweenValue", 3, "xii"); truelight@9579: SQAIList.DefSQMethod(engine, &AIList::KeepValue, "KeepValue", 2, "xi"); truelight@9584: SQAIList.DefSQMethod(engine, &AIList::Valuate, "Valuate", 2, "xx"); truelight@9579: truelight@9579: SQAIList.PostRegister(engine); truelight@9579: } truelight@9579: #endif /* DEFINE_SQUIRREL_CLASS */ truelight@9579: truelight@9579: #endif /* AI_LIST_HPP */