Following the recent synchronisation of all of the collection cl****es' function syntax, I have written some very simple function macros to make code for iterating through vectors a little easier. They are written as function macros not methods of the collection cl****es because of the need to specify a specific index for each iteration. They enable the following code:
for(unsigned short i=0; i<last_built.get_count(); i++ ) {
grund_t* gr = welt->lookup(last_built[i]);
...
to be replaced with code like this:
ITERATE(last_built,i)
{
grund_t* gr = welt->lookup(last_built[i]);
...
The function macros work with any collection cl**** that has two properties: (1) a "get_count()" method, that returns a 1 based index representing the number of elements currently contained in the collection; and (2) an overloaded [] operator allowing access to individual elements using a 0 based index. Here is the code for the function macro, which should be inserted at the top of the file of every collection cl**** with those properties:
#ifndef ITERATE
#define ITERATE(collection,i) for(uint16 i = 0; i < collection.get_count(); i++)
#endif
#ifndef ITERATE_PTR
#define ITERATE_PTR(collection,i) for(uint16 i = 0; i < collection->get_count(); i++)
#endif
If it would be helpful, I could try to make up a patch file against the standard version of Simutrans (they are currently integrated into Simutrans-Experimental) for easy integration into the trunk. These macros are helpful in that: (1) they reduce the time that it takes to write iteration loops for vector type collections; and (2) they reduce the possibility of errors such as the following:
for(unsigned short i = last_built.get_count(); i <= 0; i-- ) {
grund_t* gr = welt->lookup(last_built[i]);
...
Any comments would be appreciated :-)