truelight@9579: /* $Id$ */ truelight@9579: truelight@9593: /** @file ai_abstractlist.hpp a linked list which can keep item/value pairs */ truelight@9579: truelight@9593: #ifndef AI_ABSTRACTLIST_HPP truelight@9593: #define AI_ABSTRACTLIST_HPP truelight@9579: truelight@9579: #include "ai_object.hpp" truelight@9579: #include truelight@9579: #include truelight@9579: truelight@9593: class AIAbstractListSorter; truelight@9579: truelight@9579: /** truelight@9579: * Class that creates a linked list which can keep item/value pairs. truelight@9579: */ truelight@9593: class AIAbstractList : public AIObject { rubidium@9665: public: rubidium@9665: /** Type of sorter */ rubidium@9665: enum SorterType { truelight@9681: SORT_BY_VALUE, truelight@9681: SORT_BY_ITEM, rubidium@9665: }; rubidium@9665: truelight@9579: private: truelight@9593: AIAbstractListSorter *sorter; rubidium@9665: SorterType sorter_type; rubidium@9665: bool sort_ascending; truelight@9579: truelight@9579: public: truelight@9590: typedef std::set AIItemList; ///< The list of items inside the bucket truelight@9593: typedef std::map AIAbstractListBucket; ///< The bucket list per value truelight@9593: typedef std::map AIAbstractListMap; ///< List per item truelight@9579: truelight@9593: AIAbstractListMap items; ///< The items in the list truelight@9593: AIAbstractListBucket buckets; ///< The items in the list, sorted by value truelight@9579: truelight@9593: protected: 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@9593: public: truelight@9593: /** truelight@9593: * The name of the class, needed by several sub-processes. truelight@9593: */ truelight@9593: static const char *GetClassName() { return "AIAbstractList"; } truelight@9593: truelight@9593: /** truelight@9593: * Constructor of the AIAbstractList. truelight@9593: */ truelight@9593: AIAbstractList(); truelight@9593: truelight@9593: /** truelight@9593: * Destructor of the AIAbstractList. truelight@9593: */ truelight@9593: ~AIAbstractList(); truelight@9593: truelight@9593: /** truelight@9593: * Clear the list, making Count() returning 0 and IsEmpty() returning true. truelight@9593: */ truelight@9593: void Clear(); truelight@9593: 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@9664: * Set a value of an item directly. truelight@9664: * @param item the item to set the value for. truelight@9664: * @param value the value to give to the item truelight@9664: * @return true if we could set the item to value, false otherwise. truelight@9664: */ truelight@9664: bool SetValue(int32 item, int32 value); truelight@9664: truelight@9664: /** rubidium@9665: * Sort this list by the given sorter and direction. rubidium@9665: * @param sorter the type of sorter to use 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: */ rubidium@9665: void Sort(SorterType sorter, 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: /** rubidium@9665: * Remove the first count items. rubidium@9665: * @param count the amount of items to remove. rubidium@9665: */ rubidium@9665: void RemoveTop(int32 count); rubidium@9665: rubidium@9665: /** rubidium@9665: * Remove the last count items. rubidium@9665: * @param count the amount of items to remove. rubidium@9665: */ rubidium@9665: void RemoveBottom(int32 count); rubidium@9665: rubidium@9665: /** rubidium@9665: * Remove everything that is in the given list from this list (same item index that is). rubidium@9665: * @param list the list of items to remove. rubidium@9665: * @pre list != NULL rubidium@9665: */ rubidium@9665: void RemoveList(AIAbstractList *list); rubidium@9665: rubidium@9665: /** 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: /** rubidium@9665: * Keep the first count items, i.e. remove everything except the first count items. rubidium@9665: * @param count the amount of items to keep. rubidium@9665: */ rubidium@9665: void KeepTop(int32 count); rubidium@9665: rubidium@9665: /** rubidium@9665: * Keep the last count items, i.e. remove everything except the last count items. rubidium@9665: * @param count the amount of items to keep. rubidium@9665: */ rubidium@9665: void KeepBottom(int32 count); rubidium@9665: rubidium@9665: /** rubidium@9665: * Keps everything that is in the given list from this list (same item index that is). rubidium@9665: * @param list the list of items to keep. rubidium@9665: * @pre list != NULL rubidium@9665: */ rubidium@9665: void KeepList(AIAbstractList *list); rubidium@9665: rubidium@9665: /** truelight@9579: * The definition how valuators should look. truelight@9579: */ truelight@9679: class Valuator : public AIObject { truelight@9593: /* Make this valuator a friend of AIAbstractList, so we can access the private. truelight@9590: * Nobody else should ever call Valuate. */ truelight@9593: friend class AIAbstractList; 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@9593: void Valuate(const AIAbstractList::Valuator &proc); truelight@9579: }; truelight@9579: truelight@9579: #endif /* AI_LIST_HPP */