UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
list.cpp File Reference

LINKED LIST implementation. More...

#include "list.h"
#include "common.h"
#include "mem.h"
#include <assert.h>
#include <string.h>

Go to the source code of this file.

Functions

static linkedList_tLIST_AllocateEntry (void *const data, linkedList_t *const next=0)
 
static void LIST_AppendEntry (linkedList_t **list, linkedList_t *const entry)
 
linkedList_tLIST_Add (linkedList_t **listDest, void const *data, size_t length)
 Adds an entry to a new or to an already existing linked list. More...
 
const linkedList_tLIST_ContainsString (const linkedList_t *list, const char *string)
 Searches for the first occurrence of a given string. More...
 
linkedList_tLIST_GetPointer (linkedList_t *list, const void *data)
 Searches for the first occurrence of a given pointer. More...
 
static linkedList_tLIST_AllocateString (char const *data, linkedList_t *const next=0)
 
void LIST_AddStringSorted (linkedList_t **listDest, const char *data)
 
void LIST_PrependString (linkedList_t **listDest, const char *data)
 Adds a string as first entry to a linked list. More...
 
void LIST_AddString (linkedList_t **listDest, const char *data)
 Adds an string to a new or to an already existing linked list. The string is copied here. More...
 
void LIST_AddPointer (linkedList_t **listDest, void *data)
 Adds just a pointer to a new or to an already existing linked list. More...
 
bool LIST_RemoveEntry (linkedList_t **list, linkedList_t *entry)
 Removes one entry from the linked list. More...
 
void LIST_Delete (linkedList_t **list)
 
bool LIST_Remove (linkedList_t **list, const void *data)
 
static linkedList_t_LIST_Sort (linkedList_t *list, linkedListSort_t sorter, const void *userData)
 This is the actual sort function. Notice that it returns the new head of the list. (It has to, because the head will not generally be the same element after the sort.) More...
 
void LIST_Sort (linkedList_t **list, linkedListSort_t sorter, const void *userData)
 
linkedList_tLIST_CopyStructure (linkedList_t *src)
 Copies the list structure data - but not the content from the original list. We are only using pointers here. More...
 
bool LIST_IsEmpty (const linkedList_t *list)
 Checks whether the given list is empty. More...
 
int LIST_Count (const linkedList_t *list)
 
voidLIST_GetByIdx (linkedList_t *list, int index)
 Get an entry of a linked list by its index in the list. More...
 
voidLIST_GetRandom (linkedList_t *list)
 

Detailed Description

LINKED LIST implementation.

Definition in file list.cpp.

Function Documentation

static linkedList_t* _LIST_Sort ( linkedList_t list,
linkedListSort_t  sorter,
const void userData 
)
static

This is the actual sort function. Notice that it returns the new head of the list. (It has to, because the head will not generally be the same element after the sort.)

Note
see http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html

Definition at line 227 of file list.cpp.

References i, and linkedList_t::next.

Referenced by LIST_Sort().

linkedList_t* LIST_Add ( linkedList_t **  listDest,
void const *  data,
size_t  length 
)

Adds an entry to a new or to an already existing linked list.

See also
LIST_AddString
LIST_RemoveEntry
Returns
Returns a pointer to the data that has been added, wrapped in a linkedList_t
Todo:
Optimize this to not allocate memory for every entry - but use a hunk

Definition at line 54 of file list.cpp.

References cgi, com_genericPool, cgame_import_s::LIST_Add(), LIST_AllocateEntry(), LIST_AppendEntry(), and Mem_PoolAllocTypeN.

Referenced by LIST_Add().

void LIST_AddPointer ( linkedList_t **  listDest,
void data 
)

Adds just a pointer to a new or to an already existing linked list.

See also
LIST_Add
LIST_RemoveEntry
Todo:
Optimize this to not allocate memory for every entry - but use a hunk

Definition at line 153 of file list.cpp.

References LIST_AllocateEntry(), LIST_AppendEntry(), and linkedList_t::ptr.

Referenced by GAME_AppendTeamMember(), GAME_GetImportData(), GAME_LoadTeamInfo(), GAME_Spawn(), HUD_PopupFiremodeReservation(), LIST_CopyStructure(), and UI_CvarListenerNodeBind().

void LIST_AddString ( linkedList_t **  listDest,
const char *  data 
)
void LIST_AddStringSorted ( linkedList_t **  listDest,
const char *  data 
)

Definition at line 107 of file list.cpp.

References LIST_AllocateString(), linkedList_t::next, and Q_StringSort().

Referenced by _AddToListBlock(), GAME_GetImportData(), and TEST_F().

static linkedList_t* LIST_AllocateEntry ( void *const  data,
linkedList_t *const  next = 0 
)
static
static linkedList_t* LIST_AllocateString ( char const *  data,
linkedList_t *const  next = 0 
)
static

Definition at line 102 of file list.cpp.

References LIST_AllocateEntry(), and Mem_StrDup.

Referenced by LIST_AddString(), LIST_AddStringSorted(), and LIST_PrependString().

static void LIST_AppendEntry ( linkedList_t **  list,
linkedList_t *const  entry 
)
static

Append entry to end of list.

Definition at line 41 of file list.cpp.

References linkedList_t::next.

Referenced by LIST_Add(), LIST_AddPointer(), and LIST_AddString().

const linkedList_t* LIST_ContainsString ( const linkedList_t list,
const char *  string 
)

Searches for the first occurrence of a given string.

Returns
the linkedList_t pointer if the string is found, otherwise nullptr
Note
if string is nullptr, the function returns nullptr
See also
LIST_AddString

Definition at line 73 of file list.cpp.

References linkedList_t::data, linkedList_t::next, and Q_streq.

Referenced by _AddToListBlock(), GAME_ChangeEquip(), GAME_GetImportData(), TEST_F(), UI_AddCvarListener_f(), UI_RemoveCvarListener_f(), and UI_UpdateInvisOptions().

linkedList_t* LIST_CopyStructure ( linkedList_t src)

Copies the list structure data - but not the content from the original list. We are only using pointers here.

Definition at line 321 of file list.cpp.

References cgi, dest, LIST_AddPointer(), cgame_import_s::LIST_CopyStructure(), and LIST_Foreach.

void* LIST_GetByIdx ( linkedList_t list,
int  index 
)

Get an entry of a linked list by its index in the list.

Parameters
[in]listLinked list to get the entry from.
[in]indexThe index of the entry in the linked list.
Returns
A void pointer of the content in the list-entry.

Definition at line 362 of file list.cpp.

References linkedList_t::data, i, LIST_IsEmpty(), and linkedList_t::next.

Referenced by CL_ChangeSkin_f(), GAME_GetImportData(), HUD_ShotReserve_f(), LIST_GetRandom(), TEST_F(), UI_ExecuteLuaMethod(), and UI_GetTextFromList().

linkedList_t* LIST_GetPointer ( linkedList_t list,
const void data 
)

Searches for the first occurrence of a given pointer.

Returns
the linkedList_t pointer if the string is found, otherwise nullptr
Note
if data is nullptr, the function returns nullptr
O(n)
Only use this for small linked lists

Definition at line 91 of file list.cpp.

References linkedList_t::data, and linkedList_t::next.

Referenced by GAME_GetImportData(), LIST_Remove(), and UI_CvarListenerNodeBind().

void* LIST_GetRandom ( linkedList_t list)

Definition at line 381 of file list.cpp.

References LIST_Count(), and LIST_GetByIdx().

Referenced by GAME_GetImportData(), TEST_F(), and UI_MapInfo().

void LIST_PrependString ( linkedList_t **  listDest,
const char *  data 
)

Adds a string as first entry to a linked list.

Parameters
listDestThe linked list to add the string, too. If this is nullptr, a new list is created
dataThe string to add to the list
See also
LIST_AddString
Todo:
Optimize this to not allocate memory for every entry - but use a hunk

Definition at line 127 of file list.cpp.

References LIST_AllocateString().

Referenced by GAME_GetImportData(), and TEST_F().

bool LIST_Remove ( linkedList_t **  list,
const void data 
)
bool LIST_RemoveEntry ( linkedList_t **  list,
linkedList_t entry 
)

Removes one entry from the linked list.

See also
LIST_AddString
LIST_Add
LIST_AddPointer
LIST_Delete
Returns
true if the removal was successful, false otherwise.

Definition at line 172 of file list.cpp.

References linkedList_t::data, Mem_Free, linkedList_t::next, and linkedList_t::ptr.

Referenced by GAME_GetImportData(), LIST_Remove(), TEST_F(), and UI_RemoveCvarListener_f().

void LIST_Sort ( linkedList_t **  list,
linkedListSort_t  sorter,
const void userData 
)

Definition at line 310 of file list.cpp.

References _LIST_Sort(), cgi, and LIST_IsEmpty().