KUDr@5189: /* $Id$ */ KUDr@6285: KUDr@6285: /** @file helpers.cpp */ KUDr@6285: KUDr@5189: #include "stdafx.h" KUDr@5189: KUDr@5189: #include "openttd.h" KUDr@5189: #include "engine.h" KUDr@5189: KUDr@5189: #include KUDr@5884: #include "misc/blob.hpp" KUDr@5189: KUDr@5189: /* Engine list manipulators - current implementation is only C wrapper around CBlobT (see yapf/blob.hpp) */ KUDr@5189: KUDr@5189: /* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT* always when we are called from C */ KUDr@5189: #define B (*(CBlobT*)el) KUDr@5189: KUDr@6285: /** Create Engine List (and initialize it to empty) KUDr@6285: * @param el list to be created KUDr@6285: */ KUDr@5189: void EngList_Create(EngineList *el) KUDr@5189: { KUDr@6285: /* call CBlobT constructor explicitly */ KUDr@5189: new (&B) CBlobT(); KUDr@5189: } KUDr@5189: KUDr@6285: /** Destroy Engine List (and free its contents) KUDr@6285: * @param el list to be destroyed KUDr@6285: */ KUDr@5189: void EngList_Destroy(EngineList *el) KUDr@5189: { KUDr@6285: /* call CBlobT destructor explicitly */ KUDr@5189: B.~CBlobT(); KUDr@5189: } KUDr@5189: KUDr@6285: /** Return number of items stored in the Engine List KUDr@6285: * @param el list for count inquiry KUDr@6285: * @return the desired count KUDr@6285: */ KUDr@5189: uint EngList_Count(const EngineList *el) KUDr@5189: { KUDr@5189: return B.Size(); KUDr@5189: } KUDr@5189: KUDr@6285: /** Add new item at the end of Engine List KUDr@6285: * @param el list o which to add an engine KUDr@6285: * @param eif engine to add to the list KUDr@6285: */ KUDr@5189: void EngList_Add(EngineList *el, EngineID eid) KUDr@5189: { KUDr@5189: B.Append(eid); KUDr@5189: } KUDr@5189: KUDr@6285: /** Return pointer to the items array held by Engine List KUDr@6285: * @param el list from which the array pointer has to be returned KUDr@6285: * @return the pointer required KUDr@6285: */ KUDr@5189: EngineID* EngList_Items(EngineList *el) KUDr@5189: { KUDr@5189: return B.Data(); KUDr@5189: } KUDr@5189: KUDr@6285: /** Clear the Engine List (by invalidating all its items == reseting item count to zero) KUDr@6285: * @param el list to be cleared KUDr@6285: */ KUDr@5189: void EngList_RemoveAll(EngineList *el) KUDr@5189: { KUDr@5189: B.Clear(); KUDr@5189: } KUDr@5189: KUDr@6285: /** Sort all items using qsort() and given 'CompareItems' function KUDr@6285: * @param el list to be sorted KUDr@6285: * @param compare function for evaluation of the quicksort KUDr@6285: */ KUDr@5189: void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare) KUDr@5189: { KUDr@5189: qsort(B.Data(), B.Size(), sizeof(**el), compare); KUDr@5189: } KUDr@5189: KUDr@6285: /** Sort selected range of items (on indices @ ) KUDr@6285: * @param el list to be sorted KUDr@6285: * @param compare function for evaluation of the quicksort KUDr@6285: * @param begin start of sorting KUDr@6285: * @param count of items to be sorted KUDr@6285: */ KUDr@5189: void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items) KUDr@5189: { KUDr@5189: assert(begin <= (uint)B.Size()); KUDr@5189: assert(begin + num_items <= (uint)B.Size()); KUDr@5189: qsort(B.Data() + begin, num_items, sizeof(**el), compare); KUDr@5189: } KUDr@5189: KUDr@5189: #undef B KUDr@5189: