src/helpers.cpp
branchcpp_gui
changeset 6285 187e3ef04cc9
parent 5884 be0c8467aeb4
child 6308 646711c5feaa
equal deleted inserted replaced
6284:45d0233e7d79 6285:187e3ef04cc9
     1 /* $Id$ */
     1 /* $Id$ */
       
     2 
       
     3 /** @file helpers.cpp */
       
     4 
     2 #include "stdafx.h"
     5 #include "stdafx.h"
     3 
     6 
     4 #include "openttd.h"
     7 #include "openttd.h"
     5 #include "engine.h"
     8 #include "engine.h"
     6 
     9 
    10 /* Engine list manipulators - current implementation is only C wrapper around CBlobT<EngineID> (see yapf/blob.hpp) */
    13 /* Engine list manipulators - current implementation is only C wrapper around CBlobT<EngineID> (see yapf/blob.hpp) */
    11 
    14 
    12 /* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT<EngineID>* always when we are called from C */
    15 /* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT<EngineID>* always when we are called from C */
    13 #define B (*(CBlobT<EngineID>*)el)
    16 #define B (*(CBlobT<EngineID>*)el)
    14 
    17 
    15 /** Create Engine List (and initialize it to empty) */
    18 /** Create Engine List (and initialize it to empty)
       
    19  * @param el list to be created
       
    20  */
    16 void EngList_Create(EngineList *el)
    21 void EngList_Create(EngineList *el)
    17 {
    22 {
    18 	// call CBlobT constructor explicitly
    23 	/* call CBlobT constructor explicitly */
    19 	new (&B) CBlobT<EngineID>();
    24 	new (&B) CBlobT<EngineID>();
    20 }
    25 }
    21 
    26 
    22 /** Destroy Engine List (and free its contents) */
    27 /** Destroy Engine List (and free its contents)
       
    28  * @param el list to be destroyed
       
    29  */
    23 void EngList_Destroy(EngineList *el)
    30 void EngList_Destroy(EngineList *el)
    24 {
    31 {
    25 	// call CBlobT destructor explicitly
    32 	/* call CBlobT destructor explicitly */
    26 	B.~CBlobT<EngineID>();
    33 	B.~CBlobT<EngineID>();
    27 }
    34 }
    28 
    35 
    29 /** Return number of items stored in the Engine List */
    36 /** Return number of items stored in the Engine List
       
    37  * @param el list for count inquiry
       
    38  * @return the desired count
       
    39  */
    30 uint EngList_Count(const EngineList *el)
    40 uint EngList_Count(const EngineList *el)
    31 {
    41 {
    32 	return B.Size();
    42 	return B.Size();
    33 }
    43 }
    34 
    44 
    35 /** Add new item at the end of Engine List */
    45 /** Add new item at the end of Engine List
       
    46  * @param el list o which to add an engine
       
    47  * @param eif engine to add to the list
       
    48  */
    36 void EngList_Add(EngineList *el, EngineID eid)
    49 void EngList_Add(EngineList *el, EngineID eid)
    37 {
    50 {
    38 	B.Append(eid);
    51 	B.Append(eid);
    39 }
    52 }
    40 
    53 
    41 /** Return pointer to the items array held by Engine List */
    54 /** Return pointer to the items array held by Engine List
       
    55  * @param el list from which the array pointer has to be returned
       
    56  * @return the pointer required
       
    57  */
    42 EngineID* EngList_Items(EngineList *el)
    58 EngineID* EngList_Items(EngineList *el)
    43 {
    59 {
    44 	return B.Data();
    60 	return B.Data();
    45 }
    61 }
    46 
    62 
    47 /** Clear the Engine List (by invalidating all its items == reseting item count to zero) */
    63 /** Clear the Engine List (by invalidating all its items == reseting item count to zero)
       
    64  * @param el list to be cleared
       
    65  */
    48 void EngList_RemoveAll(EngineList *el)
    66 void EngList_RemoveAll(EngineList *el)
    49 {
    67 {
    50 	B.Clear();
    68 	B.Clear();
    51 }
    69 }
    52 
    70 
    53 /** Sort all items using qsort() and given 'CompareItems' function */
    71 /** Sort all items using qsort() and given 'CompareItems' function
       
    72  * @param el list to be sorted
       
    73  * @param compare function for evaluation of the quicksort
       
    74  */
    54 void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
    75 void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
    55 {
    76 {
    56 	qsort(B.Data(), B.Size(), sizeof(**el), compare);
    77 	qsort(B.Data(), B.Size(), sizeof(**el), compare);
    57 }
    78 }
    58 
    79 
    59 /** Sort selected range of items (on indices @ <begin, begin+num_items-1>) */
    80 /** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
       
    81  * @param el list to be sorted
       
    82  * @param compare function for evaluation of the quicksort
       
    83  * @param begin start of sorting
       
    84  * @param count of items to be sorted
       
    85  */
    60 void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
    86 void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
    61 {
    87 {
    62 	assert(begin <= (uint)B.Size());
    88 	assert(begin <= (uint)B.Size());
    63 	assert(begin + num_items <= (uint)B.Size());
    89 	assert(begin + num_items <= (uint)B.Size());
    64 	qsort(B.Data() + begin, num_items, sizeof(**el), compare);
    90 	qsort(B.Data() + begin, num_items, sizeof(**el), compare);