src/helpers.cpp
author rubidium
Mon, 31 Mar 2008 00:17:39 +0000
changeset 8786 1823ff88a054
parent 6481 85ad87daf4b0
child 9111 48ce04029fe4
permissions -rw-r--r--
(svn r12490) -Codechange: rename engine.h to engine_func.h and remove unneeded inclusions of engine.h and/or replace them with engine_type.h.
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     1
/* $Id$ */
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
     2
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
     3
/** @file helpers.cpp */
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
     4
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     5
#include "stdafx.h"
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     6
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     7
#include "openttd.h"
8786
1823ff88a054 (svn r12490) -Codechange: rename engine.h to engine_func.h and remove unneeded inclusions of engine.h and/or replace them with engine_type.h.
rubidium
parents: 6481
diff changeset
     8
#include "engine_func.h"
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     9
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    10
#include <new>
5633
e1905dacc378 (svn r8092) -Codechange: header files with miscellaneous template classes (smart pointers, blob, array, hashtable, etc.) moved from src/yapf to src/misc as they can now be used anywhere.
KUDr
parents: 5587
diff changeset
    11
#include "misc/blob.hpp"
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    12
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    13
/* Engine list manipulators - current implementation is only C wrapper around CBlobT<EngineID> (see yapf/blob.hpp) */
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    14
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    15
/* we cannot expose CBlobT directly to C so we must cast EngineList* to CBlobT<EngineID>* always when we are called from C */
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    16
#define B (*(CBlobT<EngineID>*)el)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    17
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    18
/** Create Engine List (and initialize it to empty)
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    19
 * @param el list to be created
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    20
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    21
void EngList_Create(EngineList *el)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    22
{
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    23
	/* call CBlobT constructor explicitly */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    24
	new (&B) CBlobT<EngineID>();
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    25
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    26
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    27
/** Destroy Engine List (and free its contents)
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    28
 * @param el list to be destroyed
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    29
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    30
void EngList_Destroy(EngineList *el)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    31
{
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    32
	/* call CBlobT destructor explicitly */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    33
	B.~CBlobT<EngineID>();
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    34
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    35
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    36
/** Return number of items stored in the Engine List
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    37
 * @param el list for count inquiry
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    38
 * @return the desired count
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    39
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    40
uint EngList_Count(const EngineList *el)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    41
{
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    42
	return B.Size();
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    43
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    44
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    45
/** Add new item at the end of Engine List
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    46
 * @param el list o which to add an engine
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6179
diff changeset
    47
 * @param eid engine to add to the list
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    48
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    49
void EngList_Add(EngineList *el, EngineID eid)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    50
{
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    51
	B.Append(eid);
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    52
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    53
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    54
/** Return pointer to the items array held by Engine List
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    55
 * @param el list from which the array pointer has to be returned
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    56
 * @return the pointer required
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    57
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    58
EngineID* EngList_Items(EngineList *el)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    59
{
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    60
	return B.Data();
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    61
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    62
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    63
/** Clear the Engine List (by invalidating all its items == reseting item count to zero)
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    64
 * @param el list to be cleared
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    65
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    66
void EngList_RemoveAll(EngineList *el)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    67
{
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    68
	B.Clear();
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    69
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    70
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    71
/** Sort all items using qsort() and given 'CompareItems' function
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    72
 * @param el list to be sorted
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    73
 * @param compare function for evaluation of the quicksort
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    74
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    75
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    76
{
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    77
	qsort(B.Data(), B.Size(), sizeof(**el), compare);
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    78
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    79
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    80
/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    81
 * @param el list to be sorted
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    82
 * @param compare function for evaluation of the quicksort
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    83
 * @param begin start of sorting
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6179
diff changeset
    84
 * @param num_items count of items to be sorted
6179
d19b0137d8e4 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5633
diff changeset
    85
 */
5189
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    86
void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    87
{
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    88
	assert(begin <= (uint)B.Size());
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    89
	assert(begin + num_items <= (uint)B.Size());
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    90
	qsort(B.Data() + begin, num_items, sizeof(**el), compare);
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    91
}
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    92
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    93
#undef B
a2b200911b2c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    94