src/helpers.cpp
author truebrain
Mon, 16 Jun 2008 14:43:19 +0000
branchnoai
changeset 10978 13fd0364b2c6
parent 10455 22c441f5adf9
permissions -rw-r--r--
(svn r13532) [NoAI] -Fix: in MultiPlayer SignID wasn't set correctly, causing weird effects. Code is now more unified, which should avoid simular effects in the future (tnx to glx for initial patch)
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     1
/* $Id$ */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
     2
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 9837
diff changeset
     3
/** @file helpers.cpp Some not-so-generic helper functions. */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
     4
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     5
#include "stdafx.h"
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     6
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     7
#include "openttd.h"
9837
c9ec4f82e0d0 (svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents: 9601
diff changeset
     8
#include "engine_func.h"
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
     9
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    10
#include <new>
5884
be0c8467aeb4 (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: 5838
diff changeset
    11
#include "misc/blob.hpp"
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    12
c3f55e80901c (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) */
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    14
c3f55e80901c (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 */
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    16
#define B (*(CBlobT<EngineID>*)el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    17
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    18
/** Create Engine List (and initialize it to empty)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    19
 * @param el list to be created
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    20
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    21
void EngList_Create(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    22
{
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    23
	/* call CBlobT constructor explicitly */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    24
	new (&B) CBlobT<EngineID>();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    25
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    26
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    27
/** Destroy Engine List (and free its contents)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    28
 * @param el list to be destroyed
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    29
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    30
void EngList_Destroy(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    31
{
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    32
	/* call CBlobT destructor explicitly */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    33
	B.~CBlobT<EngineID>();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    34
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    35
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    36
/** Return number of items stored in the Engine List
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    37
 * @param el list for count inquiry
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    38
 * @return the desired count
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    39
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    40
uint EngList_Count(const EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    41
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    42
	return B.Size();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    43
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    44
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    45
/** Add new item at the end of Engine List
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    46
 * @param el list o which to add an engine
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 6505
diff changeset
    47
 * @param eid engine to add to the list
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    48
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    49
void EngList_Add(EngineList *el, EngineID eid)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    50
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    51
	B.Append(eid);
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    52
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    53
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    54
/** Return pointer to the items array held by Engine List
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    55
 * @param el list from which the array pointer has to be returned
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    56
 * @return the pointer required
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    57
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    58
EngineID* EngList_Items(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    59
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    60
	return B.Data();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    61
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    62
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    63
/** Clear the Engine List (by invalidating all its items == reseting item count to zero)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    64
 * @param el list to be cleared
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    65
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    66
void EngList_RemoveAll(EngineList *el)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    67
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    68
	B.Clear();
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    69
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    70
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    71
/** Sort all items using qsort() and given 'CompareItems' function
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    72
 * @param el list to be sorted
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    73
 * @param compare function for evaluation of the quicksort
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    74
 */
5189
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    75
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    76
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    77
	qsort(B.Data(), B.Size(), sizeof(**el), compare);
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    78
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    79
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    80
/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    81
 * @param el list to be sorted
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    82
 * @param compare function for evaluation of the quicksort
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    83
 * @param begin start of sorting
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 6505
diff changeset
    84
 * @param num_items count of items to be sorted
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 5884
diff changeset
    85
 */
5189
c3f55e80901c (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)
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    87
{
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    88
	assert(begin <= (uint)B.Size());
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    89
	assert(begin + num_items <= (uint)B.Size());
c3f55e80901c (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);
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    91
}
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    92
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    93
#undef B
c3f55e80901c (svn r7301) -Fix: (r7299) Forgotten $Id$ and eol-style (peter1138)
KUDr
parents: 5187
diff changeset
    94