src/helpers.cpp
changeset 5475 2e6990a8c7c4
parent 5189 a2b200911b2c
child 5587 167d9a91ef02
equal deleted inserted replaced
5474:ac55aefc54f3 5475:2e6990a8c7c4
       
     1 /* $Id$ */
       
     2 #include "stdafx.h"
       
     3 
       
     4 EXTERN_C_BEGIN
       
     5 #include "openttd.h"
       
     6 #include "engine.h"
       
     7 EXTERN_C_END
       
     8 
       
     9 #include <new>
       
    10 #include "yapf/blob.hpp"
       
    11 
       
    12 /* Engine list manipulators - current implementation is only C wrapper around CBlobT<EngineID> (see yapf/blob.hpp) */
       
    13 
       
    14 /* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT<EngineID>* always when we are called from C */
       
    15 #define B (*(CBlobT<EngineID>*)el)
       
    16 
       
    17 /** Create Engine List (and initialize it to empty) */
       
    18 void EngList_Create(EngineList *el)
       
    19 {
       
    20 	// call CBlobT constructor explicitly
       
    21 	new (&B) CBlobT<EngineID>();
       
    22 }
       
    23 
       
    24 /** Destroy Engine List (and free its contents) */
       
    25 void EngList_Destroy(EngineList *el)
       
    26 {
       
    27 	// call CBlobT destructor explicitly
       
    28 	B.~CBlobT<EngineID>();
       
    29 }
       
    30 
       
    31 /** Return number of items stored in the Engine List */
       
    32 uint EngList_Count(const EngineList *el)
       
    33 {
       
    34 	return B.Size();
       
    35 }
       
    36 
       
    37 /** Add new item at the end of Engine List */
       
    38 void EngList_Add(EngineList *el, EngineID eid)
       
    39 {
       
    40 	B.Append(eid);
       
    41 }
       
    42 
       
    43 /** Return pointer to the items array held by Engine List */
       
    44 EngineID* EngList_Items(EngineList *el)
       
    45 {
       
    46 	return B.Data();
       
    47 }
       
    48 
       
    49 /** Clear the Engine List (by invalidating all its items == reseting item count to zero) */
       
    50 void EngList_RemoveAll(EngineList *el)
       
    51 {
       
    52 	B.Clear();
       
    53 }
       
    54 
       
    55 /** Sort all items using qsort() and given 'CompareItems' function */
       
    56 void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
       
    57 {
       
    58 	qsort(B.Data(), B.Size(), sizeof(**el), compare);
       
    59 }
       
    60 
       
    61 /** Sort selected range of items (on indices @ <begin, begin+num_items-1>) */
       
    62 void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
       
    63 {
       
    64 	assert(begin <= (uint)B.Size());
       
    65 	assert(begin + num_items <= (uint)B.Size());
       
    66 	qsort(B.Data() + begin, num_items, sizeof(**el), compare);
       
    67 }
       
    68 
       
    69 #undef B
       
    70