src/ai/api/ai_abstractlist.hpp
branchnoai
changeset 9593 012f29f59906
parent 9590 16cde7f36a88
child 9594 5009a30f320a
equal deleted inserted replaced
9592:c5c09cfde63a 9593:012f29f59906
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file ai_abstractlist.hpp a linked list which can keep item/value pairs */
       
     4 
       
     5 #ifndef AI_ABSTRACTLIST_HPP
       
     6 #define AI_ABSTRACTLIST_HPP
       
     7 
       
     8 #include "ai_object.hpp"
       
     9 #include <map>
       
    10 #include <set>
       
    11 
       
    12 class AIAbstractListSorter;
       
    13 
       
    14 /**
       
    15  * Class that creates a linked list which can keep item/value pairs.
       
    16  */
       
    17 class AIAbstractList : public AIObject {
       
    18 private:
       
    19 	AIAbstractListSorter *sorter;
       
    20 
       
    21 public:
       
    22 	typedef std::set<int32> AIItemList;               ///< The list of items inside the bucket
       
    23 	typedef std::map<int32, AIItemList> AIAbstractListBucket; ///< The bucket list per value
       
    24 	typedef std::map<int32, int32> AIAbstractListMap;         ///< List per item
       
    25 
       
    26 	AIAbstractListMap items;           ///< The items in the list
       
    27 	AIAbstractListBucket buckets;      ///< The items in the list, sorted by value
       
    28 
       
    29 protected:
       
    30 	/**
       
    31 	 * Add a single item to the list.
       
    32 	 * @param item the item to add. Should be unique, otherwise it is ignored.
       
    33 	 * @note the value is set to 0 by default.
       
    34 	 */
       
    35 	void AddItem(int32 item);
       
    36 
       
    37 	/**
       
    38 	 * Remove a single item from the list.
       
    39 	 * @param item the item to remove. If not existing, it is ignored.
       
    40 	 */
       
    41 	void RemoveItem(int32 item);
       
    42 
       
    43 public:
       
    44 	/**
       
    45 	 * The name of the class, needed by several sub-processes.
       
    46 	 */
       
    47 	static const char *GetClassName() { return "AIAbstractList"; }
       
    48 
       
    49 	/**
       
    50 	 * Constructor of the AIAbstractList.
       
    51 	 */
       
    52 	AIAbstractList();
       
    53 
       
    54 	/**
       
    55 	 * Destructor of the AIAbstractList.
       
    56 	 */
       
    57 	~AIAbstractList();
       
    58 
       
    59 	/**
       
    60 	 * Clear the list, making Count() returning 0 and IsEmpty() returning true.
       
    61 	 */
       
    62 	void Clear();
       
    63 
       
    64 	/**
       
    65 	 * Check if an item is in the list.
       
    66 	 * @param item the item to check for.
       
    67 	 * @return true if the item is in the list.
       
    68 	 */
       
    69 	bool HasItem(int32 item);
       
    70 
       
    71 	/**
       
    72 	 * Go to the beginning of the list.
       
    73 	 * @return the item value of the first item.
       
    74 	 */
       
    75 	int32 Begin();
       
    76 
       
    77 	/**
       
    78 	 * Go to the next item in the list.
       
    79 	 * @return the item value of the next item.
       
    80 	 * @note returns 0 if beyond end-of-list. Use HasNext() to check for end-of-list.
       
    81 	 */
       
    82 	int32 Next();
       
    83 
       
    84 	/**
       
    85 	 * Check if a list is empty.
       
    86 	 * @return true if the list is empty.
       
    87 	 **/
       
    88 	bool IsEmpty();
       
    89 
       
    90 	/**
       
    91 	 * Check if there is a next element. In other words, if this is true,
       
    92 	 *   Next() will return a valid item.
       
    93 	 * @return true if there is a next item.
       
    94 	 */
       
    95 	bool HasNext();
       
    96 
       
    97 	/**
       
    98 	 * Returns the amount of items in the list.
       
    99 	 * @return amount of items in the list.
       
   100 	 */
       
   101 	int32 Count();
       
   102 
       
   103 	/**
       
   104 	 * Get the value that belongs to this item.
       
   105 	 * @param item the item to get the value from
       
   106 	 * @return the value that belongs to this item.
       
   107 	 */
       
   108 	int32 GetValue(int32 item);
       
   109 
       
   110 	/**
       
   111 	 * Sort this list by item-value.
       
   112 	 * @param ascending if true, lowest value is on top, else at bottom.
       
   113 	 * @note the current item stays at the same place.
       
   114 	 */
       
   115 	void SortByItem(bool ascending);
       
   116 
       
   117 	/**
       
   118 	 * Sort this list by the value of the items.
       
   119 	 * @param ascending if true, lowest value is on top, else at bottom.
       
   120 	 * @note the current item stays at the same place.
       
   121 	 */
       
   122 	void SortByValue(bool ascending);
       
   123 
       
   124 	/**
       
   125 	 * Removes all items with a higher value than 'value'.
       
   126 	 * @param value the value above which all items are removed.
       
   127 	 */
       
   128 	void RemoveAboveValue(int32 value);
       
   129 
       
   130 	/**
       
   131 	 * Removes all items with a lower value than 'value'.
       
   132 	 * @param value the value below which all items are removed.
       
   133 	 */
       
   134 	void RemoveBelowValue(int32 value);
       
   135 
       
   136 	/**
       
   137 	 * Removes all items with a value above start and below end.
       
   138 	 * @param start the lower bound of the to be removed values (exclusive).
       
   139 	 * @param end   the upper bound of the to be removed valuens (exclusive).
       
   140 	 */
       
   141 	void RemoveBetweenValue(int32 start, int32 end);
       
   142 
       
   143 	/**
       
   144 	 * Remove all items with this value.
       
   145 	 * @param value the value to remove.
       
   146 	 */
       
   147 	void RemoveValue(int32 value);
       
   148 
       
   149 	/**
       
   150 	 * Keep all items with a higher value than 'value'.
       
   151 	 * @param value the value above which all items are kept.
       
   152 	 */
       
   153 	void KeepAboveValue(int32 value);
       
   154 
       
   155 	/**
       
   156 	 * Keep all items with a lower value than 'value'.
       
   157 	 * @param value the value below which all items are kept.
       
   158 	 */
       
   159 	void KeepBelowValue(int32 value);
       
   160 
       
   161 	/**
       
   162 	 * Keep all items with a value above start and below end.
       
   163 	 * @param start the lower bound of the to be kept values (exclusive).
       
   164 	 * @param end   the upper bound of the to be kept values (exclusive).
       
   165 	 */
       
   166 	void KeepBetweenValue(int32 start, int32 end);
       
   167 
       
   168 	/**
       
   169 	 * Keep all items with this value.
       
   170 	 * @param value the value to keep.
       
   171 	 **/
       
   172 	void KeepValue(int32 value);
       
   173 
       
   174 	/**
       
   175 	 * The definition how valuators should look.
       
   176 	 */
       
   177 	class Valuator {
       
   178 		/* Make this valuator a friend of AIAbstractList, so we can access the private.
       
   179 		 *  Nobody else should ever call Valuate. */
       
   180 		friend class AIAbstractList;
       
   181 	public:
       
   182 		/**
       
   183 		 * Virtual destructor, needed to make compilers happy.
       
   184 		 */
       
   185 		virtual ~Valuator() {}
       
   186 
       
   187 	private:
       
   188 		virtual int32 Valuate(int32 item) const = 0;
       
   189 	};
       
   190 
       
   191 	/**
       
   192 	 * Give all items a value defined by the valuator you give.
       
   193 	 * @note the valuator should be a valid instance.
       
   194 	 */
       
   195 	void Valuate(const AIAbstractList::Valuator &proc);
       
   196 };
       
   197 
       
   198 #ifdef DEFINE_SQUIRREL_CLASS
       
   199 namespace SQConvert {
       
   200 	/* Allow inner classes/structs to be used as Squirrel parameters */
       
   201 	template <> const AIAbstractList::Valuator &GetParam(ForceType<const AIAbstractList::Valuator &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AIAbstractList::Valuator *)instance; }
       
   202 
       
   203 	/* Allow AIAbstractList to be used as Squirrel parameter */
       
   204 	template <> AIAbstractList *GetParam(ForceType<AIAbstractList *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AIAbstractList *)instance; }
       
   205 }; // namespace SQConvert
       
   206 
       
   207 void SQAIAbstractListRegister(Squirrel *engine) {
       
   208 	DefSQClass <AIAbstractList> SQAIAbstractList("AIAbstractList");
       
   209 	SQAIAbstractList.PreRegister(engine);
       
   210 	SQAIAbstractList.AddConstructor(engine);
       
   211 
       
   212 	SQAIAbstractList.DefSQStaticMethod(engine, &AIAbstractList::GetClassName, "GetClassName", 1, "x");
       
   213 
       
   214 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Clear,              "Clear",              1, "x");
       
   215 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::HasItem,            "HasItem",            2, "xi");
       
   216 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Begin,              "Begin",              1, "x");
       
   217 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Next,               "Next",               1, "x");
       
   218 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::IsEmpty,            "IsEmpty",            1, "x");
       
   219 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::HasNext,            "HasNext",            1, "x");
       
   220 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Count,              "Count",              1, "x");
       
   221 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::GetValue,           "GetValue",           2, "xi");
       
   222 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::SortByItem,         "SortByItem",         2, "xb");
       
   223 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::SortByValue,        "SortByValue",        2, "xb");
       
   224 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::RemoveAboveValue,   "RemoveAboveValue",   2, "xi");
       
   225 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::RemoveBelowValue,   "RemoveBelowValue",   2, "xi");
       
   226 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::RemoveBetweenValue, "RemoveBetweenValue", 3, "xii");
       
   227 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::RemoveValue,        "RemoveValue",        2, "xi");
       
   228 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepAboveValue,     "KeepAboveValue",     2, "xi");
       
   229 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepBelowValue,     "KeepBelowValue",     2, "xi");
       
   230 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepBetweenValue,   "KeepBetweenValue",   3, "xii");
       
   231 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::KeepValue,          "KeepValue",          2, "xi");
       
   232 	SQAIAbstractList.DefSQMethod(engine, &AIAbstractList::Valuate,            "Valuate",            2, "xx");
       
   233 
       
   234 	SQAIAbstractList.PostRegister(engine);
       
   235 }
       
   236 #endif /* DEFINE_SQUIRREL_CLASS */
       
   237 
       
   238 #endif /* AI_LIST_HPP */