Data Structures API Reference

MafiaNet includes several custom data structures optimized for networking.

DS_List

Dynamic array similar to std::vector.

template<class list_type>
class List

Array based implementation of a list.

Note

ONLY USE THIS FOR SHALLOW COPIES. I don’t bother with operator= to improve performance.

Public Functions

List()

Default constructor.

~List()
List(const List &original_copy)

Copy constructor.

Parameters:

original_copy[in] The list to duplicate

List &operator=(const List &original_copy)

Assign one list to another.

inline list_type &operator[](const unsigned int position) const

Access an element by its index in the array.

Parameters:

position[in] The index into the array.

Returns:

The element at position position.

inline list_type &Get(const unsigned int position) const

Access an element by its index in the array.

Parameters:

position[in] The index into the array.

Returns:

The element at position position.

void Push(const list_type &input, const char *file, unsigned int line)

Push an element at the end of the stack.

Parameters:

input[in] The new element.

inline list_type &Pop(void)

Pop an element from the end of the stack.

Pre:

Size()>0

Returns:

The element at the end.

void Insert(const list_type &input, const unsigned int position, const char *file, unsigned int line)

Insert an element at position position in the list.

Parameters:
  • input[in] The new element.

  • position[in] The position of the new element.

void Insert(const list_type &input, const char *file, unsigned int line)

Insert at the end of the list.

Parameters:

input[in] The new element.

inline void Replace(const list_type &input, const list_type filler, const unsigned int position, const char *file, unsigned int line)

Replace the value at position by input.

If the size of the list is less than position, it increase the capacity of the list and fill slot with filler.

Parameters:
  • input[in] The element to replace at position position.

  • filler[in] The element use to fill new allocated capacity.

  • position[in] The position of input in the list.

inline void Replace(const list_type &input)

Replace the last element of the list by input.

Parameters:

input[in] The element used to replace the last element.

void RemoveAtIndex(const unsigned int position)

Delete the element at position position.

Parameters:

position[in] The index of the element to delete

void RemoveAtIndexFast(const unsigned int position)

Delete the element at position position.

Note

- swaps middle with end of list, only use if list order does not matter

Parameters:

position[in] The index of the element to delete

inline void RemoveFromEnd(const unsigned num = 1)

Delete the element at the end of the list.

unsigned int GetIndexOf(const list_type &input) const

Returns the index of the specified item or MAX_UNSIGNED_LONG if not found.

Parameters:

input[in] The element to check for

Return values:
  • MAX_UNSIGNED_LONG – The object is not in the list

  • [Integer] – The index of the element in the list

Returns:

The index or position of input in the list.

inline unsigned int Size(void) const
Returns:

The number of elements in the list

void Clear(bool doNotDeallocateSmallBlocks, const char *file, unsigned int line)

Clear the list

void Preallocate(unsigned countNeeded, const char *file, unsigned int line)

Preallocate the list, so it needs fewer reallocations at runtime.

void Compress(const char *file, unsigned int line)

Frees overallocated members, to use the minimum memory necessary.

Attention

This is a slow operation

DS_Queue

FIFO queue implementation.

template<class queue_type>
class Queue

A queue implemented as an array with a read and write index.

Public Functions

Queue()
~Queue()
Queue(const Queue &original_copy)
bool operator=(const Queue &original_copy)
void Push(const queue_type &input, const char *file, unsigned int line)
void PushAtHead(const queue_type &input, unsigned index, const char *file, unsigned int line)
inline queue_type &operator[](unsigned int position) const
void RemoveAtIndex(unsigned int position)
inline queue_type Peek(void) const
inline queue_type PeekTail(void) const
inline queue_type Pop(void)
inline queue_type PopTail(void)
inline queue_type PopDeref(void)
inline unsigned int Size(void) const
inline bool IsEmpty(void) const
inline unsigned int AllocationSize(void) const
inline void Clear(const char *file, unsigned int line)
void Compress(const char *file, unsigned int line)
bool Find(const queue_type &q)
void ClearAndForceAllocation(int size, const char *file, unsigned int line)

DS_Map

Ordered map implementation.

template<class key_type, class data_type, int (*key_comparison_func)(const key_type&, const key_type&) = defaultMapKeyComparison<key_type>>
class Map

Note

IMPORTANT! If you use defaultMapKeyComparison then call IMPLEMENT_DEFAULT_COMPARISON or you will get an unresolved external linker error.

Public Functions

Map()
~Map()
Map(const Map &original_copy)
Map &operator=(const Map &original_copy)
data_type &Get(const key_type &key) const
data_type Pop(const key_type &key)
void Set(const key_type &key, const data_type &data)
void SetExisting(const key_type &key, const data_type &data)
void SetNew(const key_type &key, const data_type &data)
bool Has(const key_type &key) const
bool Delete(const key_type &key)
data_type &operator[](const unsigned int position) const
key_type GetKeyAtIndex(const unsigned int position) const
unsigned GetIndexAtKey(const key_type &key)
void RemoveAtIndex(const unsigned index)
void Clear(void)
unsigned Size(void) const

Public Static Functions

static inline void IMPLEMENT_DEFAULT_COMPARISON(void)
static inline int NodeComparisonFunc(const key_type &a, const MapNode &b)
struct MapNode

Public Functions

inline MapNode()
inline MapNode(key_type _key, data_type _data)
inline MapNode &operator=(const MapNode &input)
inline MapNode(const MapNode &input)

Public Members

key_type mapNodeKey
data_type mapNodeData

DS_OrderedList

Sorted list with binary search.

template<class key_type, class data_type, int (*default_comparison_function)(const key_type&, const data_type&) = defaultOrderedListComparison<key_type, data_type>>
class OrderedList

Note

IMPORTANT! If you use defaultOrderedListComparison then call IMPLEMENT_DEFAULT_COMPARISON or you will get an unresolved external linker error.

Public Functions

OrderedList()
~OrderedList()
OrderedList(const OrderedList &original_copy)
OrderedList &operator=(const OrderedList &original_copy)
bool HasData(const key_type &key, int (*cf)(const key_type&, const data_type&) = default_comparison_function) const

comparisonFunction must take a key_type and a data_type and return <0, ==0, or >0 If the data type has comparison operators already defined then you can just use defaultComparison

unsigned GetIndexFromKey(const key_type &key, bool *objectExists, int (*cf)(const key_type&, const data_type&) = default_comparison_function) const
data_type GetElementFromKey(const key_type &key, int (*cf)(const key_type&, const data_type&) = default_comparison_function) const
bool GetElementFromKey(const key_type &key, data_type &element, int (*cf)(const key_type&, const data_type&) = default_comparison_function) const
unsigned Insert(const key_type &key, const data_type &data, bool assertOnDuplicate, const char *file, unsigned int line, int (*cf)(const key_type&, const data_type&) = default_comparison_function)
unsigned Remove(const key_type &key, int (*cf)(const key_type&, const data_type&) = default_comparison_function)
unsigned RemoveIfExists(const key_type &key, int (*cf)(const key_type&, const data_type&) = default_comparison_function)
data_type &operator[](const unsigned int position) const
void RemoveAtIndex(const unsigned index)
void InsertAtIndex(const data_type &data, const unsigned index, const char *file, unsigned int line)
void InsertAtEnd(const data_type &data, const char *file, unsigned int line)
void RemoveFromEnd(const unsigned num = 1)
void Clear(bool doNotDeallocate, const char *file, unsigned int line)
unsigned Size(void) const

Public Static Functions

static inline void IMPLEMENT_DEFAULT_COMPARISON(void)

DS_Hash

Hash table implementation.

template<class key_type, class data_type, unsigned int HASH_SIZE, unsigned long (*hashFunction)(const key_type&)>
class Hash

Using a string as a identifier for a node, store an allocated pointer to that node.

Public Functions

Hash()

Default constructor.

~Hash()
void Push(key_type key, const data_type &input, const char *file, unsigned int line)
data_type *Peek(key_type key)
bool Pop(data_type &out, key_type key, const char *file, unsigned int line)
bool RemoveAtIndex(HashIndex index, const char *file, unsigned int line)
bool Remove(key_type key, const char *file, unsigned int line)
HashIndex GetIndexOf(key_type key)
bool HasData(key_type key)
data_type &ItemAtIndex(const HashIndex &index)
key_type KeyAtIndex(const HashIndex &index)
void GetAsList(DataStructures::List<data_type> &itemList, DataStructures::List<key_type> &keyList, const char *file, unsigned int line) const
unsigned int Size(void) const
void Clear(const char *file, unsigned int line)

Clear the list

struct Node

Public Functions

inline Node(key_type strIn, const data_type &_data)

Public Members

key_type string
data_type data
Node *next

DS_MemoryPool

Memory pool for efficient allocation.

template<class MemoryBlockType>
class MemoryPool

Very fast memory pool for allocating and deallocating structures that don’t have constructors or destructors. Contains a list of pages, each of which has an array of the user structures

Public Functions

MemoryPool()
~MemoryPool()
void SetPageSize(int size)
MemoryBlockType *Allocate(const char *file, unsigned int line)
void Release(MemoryBlockType *m, const char *file, unsigned int line)
void Clear(const char *file, unsigned int line)
inline int GetAvailablePagesSize(void) const
inline int GetUnavailablePagesSize(void) const
inline int GetMemoryPoolPageSize(void) const
struct MemoryWithPage

Public Members

MemoryBlockType userMemory
Page *parentPage
struct Page

Public Members

MemoryWithPage **availableStack
int availableStackSize
MemoryWithPage *block
Page *next
Page *prev

DS_ByteQueue

Byte queue for streaming data.

class ByteQueue

Public Functions

ByteQueue()
~ByteQueue()
void WriteBytes(const char *in, unsigned length, const char *file, unsigned int line)
bool ReadBytes(char *out, unsigned maxLengthToRead, bool peek)
unsigned GetBytesWritten(void) const
char *PeekContiguousBytes(unsigned int *outLength) const
void IncrementReadOffset(unsigned length)
void DecrementReadOffset(unsigned length)
void Clear(const char *file, unsigned int line)
void Print(void)

RakString

String class with networking optimizations.

class RakString

String class.

Has the following improvements over std::string -Reference counting: Suitable to store in lists -Variadic assignment operator -Doesn’t cause linker errors

Public Functions

RakString()
RakString(char input)
RakString(unsigned char input)
RakString(const unsigned char *format, ...)
RakString(const char *format, ...)
~RakString()
RakString(const RakString &rhs)
inline operator const char*() const

Implicit return of const char*.

inline const char *C_String(void) const

Same as std::string::c_str.

inline char *C_StringUnsafe(void)
RakString &operator=(const RakString &rhs)

Assigment operators.

RakString &operator=(const char *str)
RakString &operator=(char *str)
RakString &operator=(const unsigned char *str)
RakString &operator=(char unsigned *str)
RakString &operator=(const char c)
RakString &operator+=(const RakString &rhs)

Concatenation.

RakString &operator+=(const char *str)
RakString &operator+=(char *str)
RakString &operator+=(const unsigned char *str)
RakString &operator+=(char unsigned *str)
RakString &operator+=(const char c)
unsigned char operator[](const unsigned int position) const

Character index. Do not use to change the string however.

size_t Find(const char *stringToFind, size_t pos = 0)

String class find replacement Searches the string for the content specified in stringToFind and returns the position of the first occurrence in the string. Search only includes characters on or after position pos, ignoring any possible occurrences in previous locations.

Parameters:
  • stringToFind[in] The string to find inside of this object’s string

  • pos[in] The position in the string to start the search

Returns:

Returns the position of the first occurrence in the string.

bool operator==(const RakString &rhs) const

Equality.

bool operator==(const char *str) const
bool operator==(char *str) const
bool operator<(const RakString &right) const
bool operator<=(const RakString &right) const
bool operator>(const RakString &right) const
bool operator>=(const RakString &right) const
bool operator!=(const RakString &rhs) const

Inequality.

bool operator!=(const char *str) const
bool operator!=(char *str) const
const char *ToLower(void)

Change all characters to lowercase.

const char *ToUpper(void)

Change all characters to uppercase.

void Set(const char *format, ...)

Set the value of the string.

RakString Assign(const char *str, size_t pos, size_t n)

Sets a copy of a substring of str as the new content. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of str is reached before).

Parameters:
  • str[in] The string to copy in

  • pos[in] The position on str to start the copy

  • n[in] How many chars to copy

Returns:

Returns the string, note that the current string is set to that value as well

bool IsEmpty(void) const

Returns if the string is empty. Also, C_String() would return “”.

size_t GetLength(void) const

Returns the length of the string.

size_t GetLengthUTF8(void) const
void Replace(unsigned index, unsigned count, unsigned char c)

Replace character(s) in starting at index, for count, with c.

void SetChar(unsigned index, unsigned char c)

Replace character at index with c.

void SetChar(unsigned index, MafiaNet::RakString s)

Replace character at index with string s.

void Truncate(unsigned int length)

Make sure string is no longer than length.

void TruncateUTF8(unsigned int length)
RakString SubStr(unsigned int index, size_t count) const
void Erase(unsigned int index, unsigned int count)

Erase characters out of the string at index for count.

void TerminateAtFirstCharacter(char c)

Set the first instance of c with a null-terminator.

void TerminateAtLastCharacter(char c)

Set the last instance of c with a null-terminator.

void StartAfterFirstCharacter(char c)
void StartAfterLastCharacter(char c)
int GetCharacterCount(char c)

Returns how many occurances there are of c in the string.

void RemoveCharacter(char c)

Remove all instances of c.

void AppendBytes(const char *bytes, size_t count)
int StrCmp(const RakString &rhs) const

Compare strings (case sensitive)

int StrNCmp(const RakString &rhs, size_t num) const

Compare strings (case sensitive), up to num characters.

int StrICmp(const RakString &rhs) const

Compare strings (not case sensitive)

void Clear(void)

Clear the string.

void Printf(void)

Print the string to the screen.

void FPrintf(FILE *fp)

Print the string to a file.

bool IPAddressMatch(const char *IP)

Does the given IP address match the IP address encoded into this string, accounting for wildcards?

bool ContainsNonprintableExceptSpaces(void) const

Does the string contain non-printable characters other than spaces?

bool IsEmailAddress(void) const

Is this a valid email address?

MafiaNet::RakString &URLEncode(void)

URL Encode the string. See http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029/.

MafiaNet::RakString &URLDecode(void)

URL decode the string.

void SplitURI(MafiaNet::RakString &header, MafiaNet::RakString &domain, MafiaNet::RakString &path)

https://servers.api.rackspacecloud.com/v1.0 to https://, servers.api.rackspacecloud.com, /v1.0

MafiaNet::RakString &SQLEscape(void)

Scan for quote, double quote, and backslash and prepend with backslash.

MafiaNet::RakString &MakeFilePath(void)

Fix to be a file path, ending with /.

void Serialize(BitStream *bs) const

Serialize to a bitstream, uncompressed (slightly faster)

Parameters:

bs[out] Bitstream to serialize to

void SerializeCompressed(BitStream *bs, uint8_t languageId = 0, bool writeLanguageId = false) const

Serialize to a bitstream, compressed (better bandwidth usage)

Parameters:
  • bs[out] Bitstream to serialize to

  • languageId[in] languageId to pass to the StringCompressor class

  • writeLanguageId[in] encode the languageId variable in the stream. If false, 0 is assumed, and DeserializeCompressed will not look for this variable in the stream (saves bandwidth)

Pre:

StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup())

bool Deserialize(BitStream *bs)

Deserialize what was written by Serialize

Parameters:

bs[in] Bitstream to serialize from

Returns:

true if the deserialization was successful

bool DeserializeCompressed(BitStream *bs, bool readLanguageId = false)

Deserialize compressed string, written by SerializeCompressed

Parameters:
  • bs[in] Bitstream to serialize from

  • readLanguageId[in] If true, looks for the variable langaugeId in the data stream. Must match what was passed to SerializeCompressed

Returns:

true if the deserialization was successful

Pre:

StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup())

RakString(SharedString *_sharedString)

Public Members

SharedString *sharedString

Public Static Functions

static MafiaNet::RakString NonVariadic(const char *str)

Create a RakString with a value, without doing printf style parsing Equivalent to assignment operator

static unsigned long ToInteger(const char *str)

Hash the string into an unsigned int.

static unsigned long ToInteger(const RakString &rs)
static int ReadIntFromSubstring(const char *str, size_t pos, size_t n)

Read an integer out of a substring.

Parameters:
  • str[in] The string

  • pos[in] The position on str where the integer starts

  • n[in] How many chars to copy

static MafiaNet::RakString FormatForPOST(const char *uri, const char *contentType, const char *body, const char *extraHeaders = "")

Format as a POST command that can be sent to a webserver

Parameters:
  • uri[in] For example, masterserver2.raknet.com/testServer

  • contentType[in] For example, text/plain; charset=UTF-8

  • body[in] Body of the post

Returns:

Formatted string

static MafiaNet::RakString FormatForPUT(const char *uri, const char *contentType, const char *body, const char *extraHeaders = "")
static MafiaNet::RakString FormatForGET(const char *uri, const char *extraHeaders = "")

Format as a GET command that can be sent to a webserver

Parameters:

uri[in] For example, masterserver2.raknet.com/testServer?__gameId=comprehensivePCGame

Returns:

Formatted string

static MafiaNet::RakString FormatForDELETE(const char *uri, const char *extraHeaders = "")

Format as a DELETE command that can be sent to a webserver

Parameters:

uri[in] For example, masterserver2.raknet.com/testServer?__gameId=comprehensivePCGame&__rowId=1

Returns:

Formatted string

static void FreeMemory(void)

RakString uses a freeList of old no-longer used strings Call this function to clear this memory on shutdown

static void FreeMemoryNoMutex(void)
static void Serialize(const char *str, BitStream *bs)

Static version of the Serialize function.

static void SerializeCompressed(const char *str, BitStream *bs, uint8_t languageId = 0, bool writeLanguageId = false)

Static version of the SerializeCompressed function.

static bool Deserialize(char *str, BitStream *bs)

Static version of the Deserialize() function.

static bool DeserializeCompressed(char *str, BitStream *bs, bool readLanguageId = false)

Static version of the DeserializeCompressed() function.

static const char *ToString(int64_t i)
static const char *ToString(uint64_t i)
static inline size_t GetSizeToAllocate(size_t bytes)
static int RakStringComp(RakString const &key, RakString const &data)
static void LockMutex(void)
static void UnlockMutex(void)

Public Static Attributes

static SharedString emptyString
static DataStructures::List<SharedString*> freeList
struct SharedString

Public Members

SimpleMutex *refCountMutex
unsigned int refCount
size_t bytesUsed
char *bigString
char *c_str
char smallString[128 - sizeof(unsigned int) - sizeof(size_t) - sizeof(char*) * 2]

RakWString

Wide string class.

class RakWString

String class for Unicode.

Public Functions

RakWString()
RakWString(const RakString &right)
RakWString(const wchar_t *input)
RakWString(const RakWString &right)
RakWString(const char *input)
~RakWString()
inline operator wchar_t*() const

Implicit return of wchar_t*.

inline const wchar_t *C_String(void) const

Same as std::string::c_str.

RakWString &operator=(const RakWString &right)

Assignment operators.

RakWString &operator=(const RakString &right)
RakWString &operator=(const wchar_t *const str)
RakWString &operator=(wchar_t *str)
RakWString &operator=(const char *const str)
RakWString &operator=(char *str)
RakWString &operator+=(const RakWString &right)

Concatenation.

RakWString &operator+=(const wchar_t *const right)
RakWString &operator+=(wchar_t *right)
bool operator==(const RakWString &right) const

Equality.

bool operator<(const RakWString &right) const
bool operator<=(const RakWString &right) const
bool operator>(const RakWString &right) const
bool operator>=(const RakWString &right) const
bool operator!=(const RakWString &right) const

Inequality.

void Set(wchar_t *str)

Set the value of the string.

bool IsEmpty(void) const

Returns if the string is empty. Also, C_String() would return “”.

size_t GetLength(void) const

Returns the length of the string.

int StrCmp(const RakWString &right) const

Compare strings (case sensitive)

int StrICmp(const RakWString &right) const

Compare strings (not case sensitive)

void Clear(void)

Clear the string.

void Printf(void)

Print the string to the screen.

void FPrintf(FILE *fp)

Print the string to a file.

void Serialize(BitStream *bs) const

Serialize to a bitstream, uncompressed (slightly faster)

Parameters:

bs[out] Bitstream to serialize to

bool Deserialize(BitStream *bs)

Deserialize what was written by Serialize

Parameters:

bs[in] Bitstream to serialize from

Returns:

true if the deserialization was successful

Public Static Functions

static unsigned long ToInteger(const RakWString &rs)

Has the string into an unsigned int.

static void Serialize(const wchar_t *const str, BitStream *bs)

Static version of the Serialize function.

static bool Deserialize(wchar_t *str, BitStream *bs)

Static version of the Deserialize() function.

static bool Deserialize(wchar_t *str, size_t strLength, BitStream *bs)