src/ai/api/ai_abstractlist.cpp
branchnoai
changeset 9752 bd87e54186f2
parent 9731 9b1552d0fd9b
child 9755 d1d7e7738985
equal deleted inserted replaced
9751:556f25efbc54 9752:bd87e54186f2
     1 /* $Id$ */
     1 /* $Id$ */
     2 
     2 
     3 /** @file ai_abstractlist.cpp Implementation of AIAbstractList */
     3 /** @file ai_abstractlist.cpp Implementation of AIAbstractList */
     4 
     4 
     5 #include "ai_abstractlist.hpp"
     5 #include "ai_abstractlist.hpp"
       
     6 #include "../../debug.h"
     6 
     7 
     7 /**
     8 /**
     8  * Base class for any AIAbstractList sorter.
     9  * Base class for any AIAbstractList sorter.
     9  */
    10  */
    10 class AIAbstractListSorter {
    11 class AIAbstractListSorter {
    44 
    45 
    45 public:
    46 public:
    46 	AIAbstractListSorterValueAscending(AIAbstractList *list)
    47 	AIAbstractListSorterValueAscending(AIAbstractList *list)
    47 	{
    48 	{
    48 		this->list = list;
    49 		this->list = list;
       
    50 		this->bucket_list = NULL;
    49 	}
    51 	}
    50 
    52 
    51 	int32 Begin()
    53 	int32 Begin()
    52 	{
    54 	{
    53 		if (this->list->buckets.empty()) return 0;
    55 		if (this->list->buckets.empty()) return 0;
    58 		return *bucket_list_iter;
    60 		return *bucket_list_iter;
    59 	}
    61 	}
    60 
    62 
    61 	int32 Next()
    63 	int32 Next()
    62 	{
    64 	{
    63 		if (this->list->buckets.empty()) return 0;
    65 		if (this->list->buckets.empty() || this->bucket_list == NULL) return 0;
    64 
    66 
    65 		this->bucket_list_iter++;
    67 		this->bucket_list_iter++;
    66 		if (this->bucket_list_iter == this->bucket_list->end()) {
    68 		if (this->bucket_list_iter == this->bucket_list->end()) {
    67 			this->bucket_iter++;
    69 			this->bucket_iter++;
    68 			if (this->bucket_iter == this->list->buckets.end()) return 0;
    70 			if (this->bucket_iter == this->list->buckets.end()) return 0;
    72 		return *bucket_list_iter;
    74 		return *bucket_list_iter;
    73 	}
    75 	}
    74 
    76 
    75 	bool HasNext()
    77 	bool HasNext()
    76 	{
    78 	{
    77 		if (this->list->buckets.empty()) return false;
    79 		if (this->list->buckets.empty() || this->bucket_list == NULL) return false;
    78 
    80 
    79 		return this->bucket_iter != this->list->buckets.end() && this->bucket_list_iter != this->bucket_list->end();
    81 		return this->bucket_iter != this->list->buckets.end() && this->bucket_list_iter != this->bucket_list->end();
    80 	}
    82 	}
    81 };
    83 };
    82 
    84 
    91 
    93 
    92 public:
    94 public:
    93 	AIAbstractListSorterValueDescending(AIAbstractList *list)
    95 	AIAbstractListSorterValueDescending(AIAbstractList *list)
    94 	{
    96 	{
    95 		this->list = list;
    97 		this->list = list;
       
    98 		this->bucket_list = NULL;
    96 	}
    99 	}
    97 
   100 
    98 	int32 Begin()
   101 	int32 Begin()
    99 	{
   102 	{
   100 		if (this->list->buckets.empty()) return 0;
   103 		if (this->list->buckets.empty()) return 0;
   105 		return *bucket_list_iter;
   108 		return *bucket_list_iter;
   106 	}
   109 	}
   107 
   110 
   108 	int32 Next()
   111 	int32 Next()
   109 	{
   112 	{
   110 		if (this->list->buckets.empty()) return 0;
   113 		if (this->list->buckets.empty() || this->bucket_list == NULL) return 0;
   111 
   114 
   112 		this->bucket_list_iter++;
   115 		this->bucket_list_iter++;
   113 		if (this->bucket_list_iter == this->bucket_list->rend()) {
   116 		if (this->bucket_list_iter == this->bucket_list->rend()) {
   114 			this->bucket_iter++;
   117 			this->bucket_iter++;
   115 			if (this->bucket_iter == this->list->buckets.rend()) return 0;
   118 			if (this->bucket_iter == this->list->buckets.rend()) return 0;
   119 		return *bucket_list_iter;
   122 		return *bucket_list_iter;
   120 	}
   123 	}
   121 
   124 
   122 	bool HasNext()
   125 	bool HasNext()
   123 	{
   126 	{
   124 		if (this->list->buckets.empty()) return false;
   127 		if (this->list->buckets.empty() || this->bucket_list == NULL) return false;
   125 
   128 
   126 		return this->bucket_iter != this->list->buckets.rend() && this->bucket_list_iter != this->bucket_list->rend();
   129 		return this->bucket_iter != this->list->buckets.rend() && this->bucket_list_iter != this->bucket_list->rend();
   127 	}
   130 	}
   128 };
   131 };
   129 
   132 
   207 {
   210 {
   208 	/* Default sorter */
   211 	/* Default sorter */
   209 	this->sorter         = new AIAbstractListSorterValueDescending(this);
   212 	this->sorter         = new AIAbstractListSorterValueDescending(this);
   210 	this->sorter_type    = SORT_BY_VALUE;
   213 	this->sorter_type    = SORT_BY_VALUE;
   211 	this->sort_ascending = false;
   214 	this->sort_ascending = false;
       
   215 	this->initialized    = false;
   212 }
   216 }
   213 
   217 
   214 AIAbstractList::~AIAbstractList()
   218 AIAbstractList::~AIAbstractList()
   215 {
   219 {
   216 	delete this->sorter;
   220 	delete this->sorter;
   244 	this->items.erase(item);
   248 	this->items.erase(item);
   245 }
   249 }
   246 
   250 
   247 int32 AIAbstractList::Begin()
   251 int32 AIAbstractList::Begin()
   248 {
   252 {
       
   253 	this->initialized = true;
   249 	return this->sorter->Begin();
   254 	return this->sorter->Begin();
   250 }
   255 }
   251 
   256 
   252 int32 AIAbstractList::Next()
   257 int32 AIAbstractList::Next()
   253 {
   258 {
       
   259 	if (this->initialized == false) {
       
   260 		DEBUG(ai, 0, "ERROR: sNext() is invalid as Begin() is never called");
       
   261 		return false;
       
   262 	}
   254 	return this->sorter->Next();
   263 	return this->sorter->Next();
   255 }
   264 }
   256 
   265 
   257 bool AIAbstractList::IsEmpty()
   266 bool AIAbstractList::IsEmpty()
   258 {
   267 {
   259 	return this->items.empty();
   268 	return this->items.empty();
   260 }
   269 }
   261 
   270 
   262 bool AIAbstractList::HasNext()
   271 bool AIAbstractList::HasNext()
   263 {
   272 {
       
   273 	if (this->initialized == false) {
       
   274 		DEBUG(ai, 0, "ERROR: HasNext() is invalid as Begin() is never called");
       
   275 		return false;
       
   276 	}
   264 	return this->sorter->HasNext();
   277 	return this->sorter->HasNext();
   265 }
   278 }
   266 
   279 
   267 int32 AIAbstractList::Count()
   280 int32 AIAbstractList::Count()
   268 {
   281 {