Initial commit

This commit is contained in:
2026-04-03 00:22:39 -05:00
commit eca1e8c458
945 changed files with 218160 additions and 0 deletions

1768
Lib/Include/ZSTL/ZArray.hpp Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,855 @@
/*
ZBasicString.hpp
Author: James Russell <jcrussell@762studios.com>
Created: 12/25/2011
Purpose:
Basic ASCII string implementation.
License:
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#pragma once
#ifndef _ZBASICSTRING_HPP
#define _ZBASICSTRING_HPP
#include <ZSTL/ZArray.hpp>
#include <string.h> //for strcmp, memset, memmove
#include <ctype.h> //for isspace, tolower
#include <stdio.h> //for sprintf
#include <iostream> //for ostream
/*
* Turns out that _stricmp is a Windows specific function.
* strcasecmp() appears to be POSIX compliant.
*/
#ifndef _WIN32
#include <strings.h>
#endif
//Null Terminator Definition
#define ZBASICSTRING_NULL_TERMINATOR ((char)'\0')
//Default Capacity for a ZBasicString (not including the null terminator)
#ifndef ZBASICSTRING_DEFAULT_CAPACITY
#define ZBASICSTRING_DEFAULT_CAPACITY (127)
#endif
//Default buffer size for a ZBasicString
#ifndef ZBASICSTRING_DEFAULT_ALLOCATOR_STORAGE
#define ZBASICSTRING_DEFAULT_ALLOCATOR_STORAGE (128)
#endif
/*
Dynamic ASCII string implementation.
The template parameter A is the array allocator that will be used by the underlying array
that stores our characters. It should allocate arrays of type 'char'.
*/
template <typename A = ZArrayAllocator<char, ZBASICSTRING_DEFAULT_ALLOCATOR_STORAGE> >
class ZBasicString
{
protected:
//Array used to hold the characters
ZArray<char, A> StringArray;
//Integrity check
inline void CheckIntegrity()
{
#if ZSTL_CHECK_INTEGRITY
ZSTL_ASSERT(StringArray.Size() > 0, "StringArray is empty with no null terminator!");
ZSTL_ASSERT(StringArray[Length()] == ZBASICSTRING_NULL_TERMINATOR, "StringArray has no null terminator!");
#endif //ZSTL_CHECK_INTEGRITY
}
public:
/*
Default constructor. Constructs an empty string.
*/
ZBasicString()
: StringArray(ZBASICSTRING_DEFAULT_CAPACITY + 1)
{
//Make sure we have our null terminator
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
CheckIntegrity();
}
/*
Parameterized constructor. Constructs an empty string with the
specified capacity (not including the null terminator).
*/
explicit ZBasicString(size_t _capacity)
: StringArray(_capacity + 1)
{
//Make sure we have our null terminator
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
CheckIntegrity();
}
/*
Parameterized constructor. This constructor will initialize a copy of
the provided null-terminated string.
@param _string - the null-terminated string to initialize to
*/
ZBasicString(const char *_string)
: StringArray( _string != NULL ? strlen(_string) + 1 : 1)
{
//Set the string array size equal to the starting capacity, copy, and set null terminator
StringArray.Resize(StringArray.Capacity());
memmove(StringArray.Data(), _string, Length() * sizeof(char));
StringArray.Data()[Length()] = ZBASICSTRING_NULL_TERMINATOR;
CheckIntegrity();
}
/*
Parameterized constructor. This constructor will initialize a copy of the provided
string, but it will only copy in as many characters as defined. The null terminator
at the end is not required.
If a null terminator is present before the end of the string, the string will be
shortened such that the first encountered null terminator is at the end of the string.
@param _string - the string to initialize to
@param _count - the number of chars in the string
*/
ZBasicString(const char *_string, const size_t _count)
: StringArray(_string, _count, _count + 1)
{
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
StringArray.Resize(strlen(StringArray.Data()) + 1);
CheckIntegrity();
}
/*
Parameterized constructor. This constructor will initialize a string from the character
data contained in the provided array. The null terminator at the end is not required.
If a null terminator is present before the end of the string, the string will be
shortened such that the first encountered null terminator is at the end of the string.
@param _array - the provided character array to use as a string
*/
template <typename B>
explicit ZBasicString(const ZArray<char, B>& _array)
: StringArray(_array.Data(), _array.Size(), _array.Capacity() + 1)
{
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
StringArray.Resize(strlen(StringArray.Data()) + 1);
CheckIntegrity();
}
/*
Copy constructor.
@param _other - the other string
*/
ZBasicString(const ZBasicString<A>& _other)
: StringArray(_other.Array())
{
CheckIntegrity();
}
/*
Copy constructor. Used when the allocator type for the
other string is different.
@param B - the allocator type of the other string
@param _other - the other string
*/
template <typename B>
ZBasicString(const ZBasicString<B>& _other)
: StringArray(_other.Array())
{
CheckIntegrity();
}
/*
Slice Copy constructor. Constructs this string as a slice of another.
@param B - the allocator type of the other string
@param _other - the other string
*/
template <typename B>
ZBasicString(const ZBasicString<B>& _other, size_t _start, size_t _end)
: StringArray(_other.Array(), _start, _end)
{
if (_start == _end || StringArray.Back() != ZBASICSTRING_NULL_TERMINATOR)
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
CheckIntegrity();
}
/*
Destructor.
*/
~ZBasicString()
{
CheckIntegrity();
}
/*
[] operator overload. Allows indexing into the characters of this string.
Equivalent to a call to At.
@param _index - integer index into the string
@return (char&) - character at the given index
@assert - if the index is out of bounds
*/
char& operator [] (const size_t _index) const
{ return At(_index); }
/*
= operator overload. Makes this string equivalent to the other. Equivalent
to a call to Copy.
@param _other - the string to set this equal to
@return (ZBasicString<A>&) - this string
*/
ZBasicString<A>& operator = (const ZBasicString<A>& _other)
{ Copy(_other); return *this; }
/*
= operator overload. Makes this string equivalent to the other. Used when the underlying
allocator type is different. Equivalent to a call to Copy.
@param B - the allocator type for the other string
@param _other - the string to set this equal to
@return (ZBasicString<A>&) - this string
*/
template <typename B>
ZBasicString<A>& operator = (const ZBasicString<B>& _other)
{ Copy(_other); return *this; }
//C-String Implementation of operator =
ZBasicString<A>& operator = (const char* _other)
{ Copy(_other); return *this; }
/*
== operator overload. Determines if this string is equal to another. Equivalent
to a call to Equals.
@param _other - the string to compare to
@return (bool) - true if equal, false otherwise
*/
template <typename B>
bool operator == (const ZBasicString<B>& _other) const
{ return Equals(_other); }
//C-String Implementation of operator ==
bool operator == (const char* _other) const
{ return Equals(_other); }
/*
!= operator overload. Determines if this string is not equal to another.
Equivalent to a call to !Equals.
@param B - the allocator type for the other string
@param _other - the string to compare to
@return (bool) - true if not equal, false otherwise
*/
template <typename B>
bool operator != (const ZBasicString<B>& _other) const
{ return !Equals(_other); }
//C-String Implementation of operator !=
bool operator != (const char* _other) const
{ return !Equals(_other); }
/*
< operator overload. Lexographically compares strings.
@param B - the allocator type for the other string
@param _other - the string to compare to
@return (bool) - true if less than, false otherwise
*/
template <typename B>
bool operator < (const ZBasicString<B>& _other) const
{ return Compare(_other) < 0; }
//C-String implementation of operator <
bool operator < (const char* _other) const
{ return Compare(_other); }
/*
< operator overload. Lexographically compares strings. Equivalent to a call
to Compare.
@param B - the allocator type for the other string
@param _other - the string to compare to
@return (bool) - true if less than, false otherwise
*/
template <typename B>
bool operator <= (const ZBasicString<B>& _other) const
{ return Compare(_other) <= 0; }
/*
+ operator overload. Returns a string that is the concatenation of this
string and the provided string.
@param B - the allocator type for the other string
@param _other - the string to append
@return (ZBasicString<A>) - a string that is this string appended with _other
*/
template <typename B>
ZBasicString<A> operator + (const ZBasicString<B>& _other) const
{ ZBasicString<A> ret(*this); ret.Insert(ret.Length(), _other); return ret; }
//C-String implementation of operator +
ZBasicString<A> operator + (const char *_other) const
{ ZBasicString<A> ret(*this); ret.Insert(ret.Length(), _other); return ret; }
/*
+ operator overload. Returns a string that is the concatenation of this
string and the provided char.
@param _c - the character to append
@return (ZBasicString<A>) - a string that is this string appended with _c
*/
ZBasicString<A> operator + (const char& _c) const
{ ZBasicString<A> ret(*this); ret.PushBack(_c); return ret; }
/*
+= operator overload. Inserts the given string at the end of this string. Equivalent
to a call to Insert.
@param B - the allocator type for the other string
@param _other - the string to append
@return (ZBasicString<A>&) - this string
*/
template <typename B>
ZBasicString<A>& operator += (const ZBasicString<B>& _other)
{ Insert(Length(), _other); return *this; }
//C-String Implementation of operator +=
ZBasicString<A>& operator += (const char *_other)
{ Insert(Length(), _other); return *this; }
/*
+= operator overload. Pushes a character onto this string. Equivalent to a call to
PushBack.
@param _c - the character to append
@return (ZBasicString<A>&) - this string
*/
ZBasicString<A>& operator += (const char& _c)
{ PushBack(_c); return *this; }
/*
ZHashValue value override. Uses the Java 6 string hash function. Equivalent to a call
to Hash.
@return (ZHashValue) - hash code for this string
*/
operator ZHashValue () const
{ return Hash(); }
/*
public ZBasicString<A>::Allocator
Gets the allocator set on this string.
@return (A&) - the allocator instance
*/
A& Allocator()
{
return StringArray.Allocator();
}
/*
public ZBasicString<A>::Array
Gets this string's backing ZArray. The last character in the array is guaranteed to
be ZBASICSTRING_NULL_TERMINATOR. Try to keep it that way.
@return (ZArray<char, A>&) - the string as a ZArray.
*/
const ZArray<char, A>& Array() const
{
return StringArray;
}
/*
public ZBasicString<A>::At
Gets the character at the given signed index. Passing in an index of -1 will return
the last character, not the null terminator.
@param _index - the index
@return (char) - the character at the provided index
@assert - if the index is out of bounds
*/
char& At(const size_t _index) const
{
return StringArray.Data()[BoundsCheck(_index, Length())];
}
/*
public ZBasicString<A>::Back
Gets a reference to the character at the back of the string.
@return (char&) - the character at the end of the string
@assert - if the string is empty
*/
char& Back() const
{
return StringArray.Data()[BoundsCheck(Length() - 1, Length())];
}
/*
public ZBasicString<A>::BoundsCheck
Bounds checks the provided index against the provided boundary, ensuring it is less
than and asserting if an out of bounds access occurs. Calls to this will be compiled
out if ZSTL_DISABLE_RUNTIME_CHECKS is not zero.
@param _index - the index to convert
@param _boundary - the boundary to bounds check against
@return (size_t) - the unsigned index
@assert - if the index is not less than boundary
*/
size_t BoundsCheck(const size_t _index, const size_t _boundary) const
{
return StringArray.BoundsCheck( _index, _boundary );
}
/*
public ZBasicString<A>::Capacity
Returns the capacity of this string.
@return (size_t) - capacity of the string
*/
size_t Capacity() const
{
return StringArray.Capacity() - 1;
}
/*
public ZBasicString<A>::Clear
Clears out the string to the empty string.
@return (void)
*/
void Clear()
{
StringArray.Clear();
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
}
/*
public ZBasicString<A>::Clear
Clears out the string to the empty string and ensures the capacity of
the string to the provided capacity. Will reallocate if necessary.
@param _newCapacity - the new capacity to use
@return (void)
*/
void Clear(size_t _newCapacity)
{
StringArray.Clear(_newCapacity + 1);
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
}
/*
public ZBasicString<A>::Compare
Lexicographically compares this string with another.
@param B - the other string allocator type
@param _other - the other string
@return (int) - zero if lexicographically equivalent, less than zero if this < _other,
greater than zero if this > _other
*/
template <typename B>
int Compare(const ZBasicString<B>& _other) const
{
return strcmp(StringArray.Data(), _other.Data());
}
//C-String Implementation of Compare
int Compare(const char *_other) const
{
return strcmp(StringArray.Data(), _other);
}
/*
public ZBasicString<B>::Copy
Copies the given string data into this string.
@param B - the other string allocator type
@param _other - the other string
@return (void)
*/
template <typename B>
void Copy(const ZBasicString<B>& _other)
{
StringArray.Resize(_other.Length() + 1);
StringArray.Copy(_other.Array());
}
//C-String Implementation of Copy
void Copy(const char* _other)
{ Copy(ZBasicString<A>(_other)); }
/*
public ZBasicString<A>::Data
Gets the character data from this string (NULL terminated C-style string).
@return (char*) - this string as a char*
*/
char* Data() const
{
return StringArray.Data();
}
/*
public ZBasicString<A>::Empty
Determines if the string is the empty string.
@return (bool) - true if the string is empty string, false otherwise
*/
bool Empty() const
{
return StringArray.Data()[0] == ZBASICSTRING_NULL_TERMINATOR;
}
/*
public ZBasicString<A>::Equals
Determines if this string is lexicographically equivalent to another.
@param B - the allocator type of the other
@param _other - the other string
@return (bool) - true if equivalent, false otherwise
*/
template <typename B>
bool Equals(const ZBasicString<B>& _other) const
{
return Compare(_other) == 0;
}
//C-String Implementation of Equals
bool Equals(const char *_other) const
{ return Compare(_other) == 0; }
/*
public ZBasicString<A>::Erase
Erase function. Erases the character at the provided index.
@param _index - the index of the character to erase
@return (void)
*/
void Erase(const size_t _index)
{
size_t index = BoundsCheck(_index, Length());
Erase(index, index + 1);
}
/*
public ZBasicString<A>::Erase
Erase function. Erases characters between the given indices.
@param _start - the starting index
@param __end - the ending index index (exclusive)
@return (void)
@assert - if _start or _end out of bounds
if _end < _start
*/
void Erase(const size_t _start, const size_t _end)
{
if (_start == _end)
return;
size_t start = BoundsCheck(_start, Length());
size_t end = BoundsCheck(_end, Length() + 1);
#if !ZSTL_DISABLE_RUNTIME_CHECKS
ZSTL_ASSERT(start <= end, "ZBasicString: Cannot erase with _end < _start!");
#endif
StringArray.Erase(start, end);
CheckIntegrity();
}
/*
public ZBasicString<A>::Front
Gets a reference to the character at the front of the string.
@return (char&) - the character at the front of the string
@assert - if the string is empty
*/
char& Front() const
{
return StringArray.Data()[BoundsCheck(0, Length())];
}
/*
public ZBasicString<A>::Hash
Returns a hash value for the string. Uses the Java 6 string hashing method.
This does not include the null terminator of the string.
@return (ZHashValue) - the hash value for the string
*/
ZHashValue Hash() const
{
size_t i;
ZHashValue hash;
for (hash = 0, i = 0; i < Length(); i++)
hash = (hash << 5) - hash + (StringArray.Data())[i];
return hash;
}
/*
public ZBasicString<A>::Insert
Insert function. Inserts a character into this string.
@param _index - index to insert at
@param _char - character to insert
@return (void)
@assert - if index is invalid
*/
void Insert(const size_t _index, const char& _char)
{
size_t index = BoundsCheck(_index, Length() + 1);
StringArray.Insert(index, _char);
CheckIntegrity();
}
/*
public ZBasicString<A>::Insert
Insert function. Inserts a string into this string.
@param _index - index to insert at
@param _other - string to insert
@return (void)
@assert - if index is invalid
*/
void Insert(const size_t _index, const ZBasicString<A>& _other)
{
size_t index = BoundsCheck(_index, Length() + 1);
StringArray.Insert(index, _other.Array(), 0, _other.Length());
CheckIntegrity();
}
/*
public ZBasicString<A>::Length
Gives the length of the string, not including the null terminator.
@return (size_t) - length of the string
*/
size_t Length() const
{
return StringArray.Size() - 1;
}
/*
public ZBasicString<A>::Pop
Pops a character off the string.
@return (char) - the character removed from the string
@assert - if the string is empty
*/
char PopBack()
{
#if !ZSTL_DISABLE_RUNTIME_CHECKS
ZSTL_ASSERT(!Empty(), "ZBasicString: PopBack called on empty string!");
#endif
return StringArray.Erase(StringArray.Size() - 2);
}
/*
public ZBasicString<A>::PopFront
Pops a character off of the beginning of the string.
@return (char)
@assert - if the string is empty
*/
char PopFront()
{
#if !ZSTL_DISABLE_RUNTIME_CHECKS
ZSTL_ASSERT(!Empty(), "ZBasicString: PopBack called on empty string!");
#endif
return StringArray.PopFront();
}
/*
public ZBasicString<A>::PushBack
Pushes a character onto the string. Will allocate storage if necessary.
@param _char - character to push onto the string
@return (void)
*/
void PushBack(const char& _char)
{
StringArray.PushBack(ZBASICSTRING_NULL_TERMINATOR);
StringArray.At(StringArray.Size() - 2) = _char;
}
/*
public ZBasicString<A>::PushFront
Pushes a character onto the beginning of the string. Will allocate storage
if necessary.
@param _char - the character to push onto the string
@return (void)
*/
void PushFront(const char& _char)
{
StringArray.PushFront(_char);
};
/*
public ZBasicString<A>::Reserve
Changes the capacity of this string, reserving space in the underlying array for the
provided number of characters and the null terminator.
@param _capacity - the capacity to use
@return (void)
@assert - if the capacity is less than current size
*/
void Reserve(size_t _capacity)
{
StringArray.Reserve(_capacity + 1);
}
/*
public ZBasicString<A>::Resize
Resizes this string. If the string size is shrunk, removes the excess characters. If
the string size is grown, the whitespace character is appended that many times. Will
allocate storage if necessary.
@param _newSize - the new size of the string
@return (void)
*/
void Resize(size_t _newSize)
{
Resize(_newSize, ' ');
}
/*
public ZBasicString<A>::Resize
Resizes this string. If the string size is shrunk, removes the excess characters. If
the string size is grown, the provided character is appended that many times. Will
allocate storage if necessary.
@param _newSize - the new size of this string
@param _value - the value to append if size increases
@return (void)
*/
void Resize(size_t _newSize, const char& _value)
{
StringArray.PopBack(); //Remove null terminator
StringArray.Resize(_newSize + 1, _value); //Include room to replace the null terminator
StringArray.At(this->Length()) = ZBASICSTRING_NULL_TERMINATOR;
CheckIntegrity();
}
/*
public ZBasicString<A>::Swap
Swap operation, which swaps string content.
@param B - the allocator type of the other string
@param _other - string to swap contents with
@return (void)
*/
template <typename B>
void Swap(ZBasicString<B>& _other)
{
StringArray.Swap(_other.StringArray);
CheckIntegrity();
}
};
///////////////////////////////////////////
/* Non-Member Functions for ZBasicString */
///////////////////////////////////////////
/*
Non member addition function. Allows adding a const char* to a ZBasicString<A>
with the CString as the right side of the operator.
@param _lhs - string to append to the left side
@param _rhs - char * to append to
@return - a string that is the concatenation of _lhs with _rhs
*/
template <typename A>
ZBasicString<A> operator + (const ZBasicString<A>& _lhs, const char *_rhs)
{
return _lhs + ZBasicString<A>(_rhs);
}
/*
Non member addition function. Allows adding a ZBasicString<A> to a const char *
with the CString as the left side of the operator.
@param _lhs - char * to append to
@param _rhs - string to append to the left side
@return - a string that is the concatenation of _lhs with _rhs
*/
template <typename A>
ZBasicString<A> operator + (const char *_lhs, const ZBasicString<A>& _rhs)
{
return ZBasicString<A>(_lhs) + _rhs;
}
/*
Output stream operator overload for ZBasicString. Needed to ensure ZBasicString
behaves as expected when trying to use std::cout or std::cerr.
@param _os - the output stream
@param _str - the basic string
@return - the output stream provided
*/
template <typename A>
std::ostream& operator << (std::ostream& _os, const ZBasicString<A>& _str)
{
_os << _str.Data(); return _os;
}
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

1554
Lib/Include/ZSTL/ZList.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,802 @@
/*
ZListAlgo.hpp
Author: James Russell <jcrussell@762studios.com>
Created: 1/12/2012
Purpose:
Generalized algorithm implementations for use with ZList.
License:
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#pragma once
#ifndef _ZLISTALGO_HPP
#define _ZLISTALGO_HPP
#include <ZSTL/ZSTL.hpp>
namespace ZListAlgo
{
/*
public ZListAlgo<T, A, B>::Append
Appends a range of one list's elements to the back of another.
@param T - the type held by the list
@param A - the allocator used by the list
@param B - the allocator type used by the other list
@param _list - list to append elements to
@param _other - list to get elements from
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (void)
@assert - if _start, _end don't belong to _other
*/
template <typename T, typename A, typename B>
void Append(ZList<T, A>& _list, const ZList<T, B>& _other, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_other);
typename ZList<T, B>::Iterator itr;
for (itr = _start; itr !=_end; ++itr)
_list.PushBack(*itr);
}
/*
public ZListAlgo<T, A, B>::Append
Appends one list's elements to the back of another.
@param T - the type held by the list
@param A - the allocator used by the list
@param B - the allocator type used by the other list
@param _list - list to append elements to
@param _other - list to get elements from
@return (void)
*/
template <typename T, typename A, typename B>
void Append(ZList<T, A>& _list, const ZList<T, B>& _other)
{
Append(_list,_other, _other.Begin(), _other.End());
}
/*
public ZListAlgo<T, A, B>::Concatenate
Function to concatenate ranges of two lists together and returns the result.
@param _list - the first list to concatenate
@param _listStart - the starting iterator for _list
@param _listEnd - the ending iterator for _list (exclusive)
@param _other - the second list to concatenate
@param _otherStart - the starting iterator for _other
@param _otherEnd - the second iterator for _other (exclusive)
@return (ZList<T, A>) - concatenated list with copies of elements from lists.
*/
template <typename T, typename A, typename B>
ZList<T,A> Concatenate(const ZList<T, A>& _list, const ZListIterator<T>& _listStart, const ZListIterator<T>& _listEnd,
const ZList<T, B>& _other, const ZListIterator<T>& _otherStart, const ZListIterator<T>& _otherEnd)
{
URFP(_list); //TODO: Need to check to ensure _listStart, _listEnd iterate _list
URFP(_other); //TODO: Need to check to ensure _otherStart, _otherEnd iterate _other
ZListIterator<T> litr;
ZListIterator<T> oitr;
ZList<T,A> ret;
for (litr = _listStart; litr != _listEnd; ++litr)
ret.PushBack(*litr);
for (oitr = _otherStart; oitr != _otherEnd; ++oitr)
ret.PushBack(*oitr);
return ret;
}
/*
public ZListAlgo<T, A, B>::Concatenate
Function to concatenate two lists together.
@param _list - the first list to concatenate
@param _other - the second list to concatenate
@return (ZList<T,A>) - list of form [list, other], made of copies of their elements
*/
template <typename T, typename A, typename B>
ZList<T,A> Concatenate(const ZList<T, A>& _list, const ZList<T, B>& _other)
{
return Concatenate(_list, _list.Begin(), _list.End(), _other, _other.Begin(), _other.End());
}
/*
public ZListAlgo<T, A>::Contains
Determines if the list contains the given value between the given iterators.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to search in
@param _value - the value to look for
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (bool) - true if the range contains the value, false otherwise
*/
template <typename T, typename A>
bool Contains(const ZList<T, A>& _list, const T& _value, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
ZListIterator<T> itr;
for (itr = _start; itr != _end; ++itr)
{
if (*itr == _value)
return true;
}
return false;
}
/*
public ZListAlgo<T, A>::Count
Counts the number of occurrences of a value in the list between the given iterators.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to count elements in
@param _value - the value to look for
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (size_t) - the number of occurrences of the value
*/
template <typename T, typename A>
size_t Count(const ZList<T, A>& _list, const T& _value, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
size_t i;
typename ZList<T, A>::Iterator itr;
for (i = 0, itr = _start; itr != _end; ++itr)
{
if ((*itr) == _value)
i++;
}
return i;
}
/*
public ZListAlgo<T, A>::Count
Counts the number of occurrences of a value in the list.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to count elements in
@param _value - the value to look for
@return (size_t) - the number of occurrences of the value
*/
template <typename T, typename A>
size_t Count(const ZList<T, A>& _list, const T& _value)
{
return Count(_list, _value, _list.Begin(), _list.End());
}
/*
public ZListAlgo<T, A>::Excise
Excises a region from a list and returns it.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to excise a region from
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (ZList<T, A>) - the excised region
*/
template <typename T, typename A>
ZList<T,A> Excise(ZList<T, A>& _list, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
ZList<T, A> ret;
ZListIterator<T> itr;
itr = _start;
while (itr != _end)
ret.PushBack(_list.Erase(itr));
return ret;
}
/*
public ZListAlgo<T, A>::FindFirstOf
Searches for the first occurrence of the specified value in the list between
the given iterators.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to search in
@param _value - the value to search for
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (ZListIterator<T>) - the iterator location where the value is found (_end if not found)
*/
template <typename T, typename A>
ZListIterator<T> FindFirst(const ZList<T, A>& _list, const T& _value, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
ZListIterator<T> itr;
for (itr = _start; itr != _end; ++itr)
if ((*itr) == _value)
return itr;
return _list.End();
}
/*
public ZListAlgo<T, A>::FindFirstOf
Searches for the first occurrence of the specified value in the list.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to search in
@param _value - the value to search for
@return (ZListIterator<T>) - the iterator location where the value is found (_end if not found)
*/
template <typename T, typename A>
ZListIterator<T> FindFirst(const ZList<T, A>& _list, const T& _value)
{
return FindFirst(_list, _value, _list.Begin(), _list.End());
}
/*
public ZListAlgo<T, A>::Contains
Determines if the list contains the given value.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to search in
@param _value - the value to look for
@return (bool) - true if the list contains the value, false otherwise
*/
template <typename T, typename A>
bool Contains(const ZList<T, A>& _list, const T& _value)
{
return FindFirst(_list, _value) != _list.End();
}
/*
public ZListAlgo<T, A>::FirstNotOf
Function to find the first occurrence of a value that is not the given value in the given range and
returns an iterator to it.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to search in
@param _value - the value to avoid
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (ZListIterator<T>) - iterator pointing at the first non-match (_end if not found)
*/
template <typename T, typename A>
ZListIterator<T> FirstNotOf(const ZList<T, A>& _list, const T& _value, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
ZListIterator<T> itr;
for (itr = _start; itr != _end; ++itr)
if (!(*itr == _value))
break;
return itr;
}
/*
public ZListAlgo<T, A>::FirstNotOf
Function to find the first occurrence of a value that is not the given value and
returns an iterator to it.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to search in
@param _value - the value to search for the first nonoccurence of
@return (ZListIterator<T>) - iterator pointing at the first non-match (_end if not found)
*/
template <typename T, typename A>
ZListIterator<T> FirstNotOf(const ZList<T, A>& _list, const T& _value)
{
return FirstNotOf(_list, _value, _list.Begin(), _list.End());
}
/*
public ZListAlgo<T, A, B>::Prepend
Appends copies of a range of a list's elements to the front of another list.
@param T - the type held by the list
@param A - the allocator used by the list
@param B - the allocator type of the other list
@param _list - list to append elements to
@param _other - list to get elements from
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (void)
*/
template <typename T, typename A, typename B>
void Prepend(ZList<T, A>& _list, const ZList<T, B>& _other, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_other);
_list.Insert(_list.Begin(), _start, _end);
}
/*
public ZListAlgo<T, A, B>::Prepend
Appends copies of a list's elements to the front of another list.
@param T - the type held by the list
@param A - the allocator used by the list
@param B - the allocator type of the other list
@param _list - list to append elements to
@param _other - list to get elements from
@return (void)
*/
template <typename T, typename A, typename B>
void Prepend(ZList<T, A>& _list, const ZList<T, B>& _other)
{
Prepend(_list, _other, _other.Begin(), _other.End());
}
/*
public ZListAlgo<T, A>::Remove
Removes the first occurrence of the specified value from the list starting at
the given iterator location.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to remove elements from
@param _value - the value to remove
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (ZListIterator<T>) - iterator to the position after the removed element
*/
template <typename T, typename A>
ZListIterator<T> Remove(ZList<T, A>& _list, const T& _value, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
ZListIterator<T> itr = _start;
while (itr != _end)
{
if ((*itr) == _value)
{
_list.Erase(itr);
return itr;
}
++itr;
}
return _list.End();
}
/*
public ZListAlgo<T, A>::Remove
Removes the first occurrence of the specified value from the list.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to remove elements from
@param _value - the value to remove
@return (ZListIterator<T>) - iterator to the position after the removed element
*/
template <typename T, typename A>
ZListIterator<T> Remove(ZList<T, A>& _list, const T& _value)
{
return Remove(_list, _value, _list.Begin(), _list.End());
}
/*
public ZListAlgo<T, A>::RemoveAll
Removes all occurrences of the specified value from the list between the given iterators.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to remove elements from
@param _value - the value to remove
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (size_t) - the number of elements removed
*/
template <typename T, typename A>
size_t RemoveAll(ZList<T, A>& _list, const T& _value, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
size_t removed = 0;
ZListIterator<T> temp = _start;
while (temp != _end)
{
if (*temp == _value)
{
_list.Erase(temp);
removed++;
}
else
{
++temp; //Erase will increment above
}
}
return removed;
}
/*
public ZListAlgo<T, A>::RemoveAll
Removes all occurrences of the specified value from the list.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to remove elements from
@param _value - the value to remove
@return (size_t) - the number of elements removed
*/
template <typename T, typename A>
size_t RemoveAll(ZList<T, A>& _list, const T& _value)
{
return RemoveAll(_list, _value, _list.Begin(), _list.End());
}
/*
public ZListAlgo<T, A>::Reverse
Reverses the elements of a list in a given range.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to reverse a range on
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (void)
*/
template <typename T, typename A>
void Reverse(ZList<T, A>& _list, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
if (_list.Size() < 2)
return;
ZListIterator<T> start = _start;
ZListIterator<T> end = _end;
end.Prev();
T temp;
while (start != end)
{
temp = *start;
*start = *end;
*end = temp;
if (start.GetNode()->Next == end.GetNode() && end.GetNode()->Previous == start.GetNode())
break;
if (start.GetNode()->Next == end.GetNode()->Previous)
break;
++start;
--end;
}
}
/*
public ZListAlgo<T, A>::Reverse
Reverses the elements in a list.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to reverse
@return (void)
*/
template <typename T, typename A>
void Reverse(ZList<T, A>& _list)
{
Reverse(_list, _list.Begin(), _list.End());
}
/*
public ZListAlgo<T, A>::ReverseNodes
Reverses the elements of a list in a given range. Use when operator = for the
contained element is expensive.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to reverse a range on
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (void)
*/
template <typename T, typename A>
void ReverseNodes(ZList<T, A>& _list, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
ZListNode<T>* startBoundNode = _start.GetNode()->Previous;
ZListNode<T>* endBoundNode = _end.GetNode();
ZListNode<T>* currNode = _start.GetNode();
ZListNode<T>* temp;
// don't reverse sublist of only 1 element
if (currNode == endBoundNode->Previous)
return;
// setup the start node and end boundary
startBoundNode->Next = endBoundNode->Previous;
endBoundNode->Previous = currNode;
// swap node next and prev pointers
while (currNode != endBoundNode)
{
temp = currNode->Next;
ZSTL_ASSERT(temp != NULL, "temp null in Reverse!()\n");
currNode->Next = currNode->Previous;
currNode->Previous = temp;
currNode = temp;
}
// setup the end node and the first boundary
currNode->Previous = startBoundNode;
// setup the first node and the end boundary
currNode = _start.GetNode();
currNode->Next = endBoundNode;
}
/*
public ZListAlgo<T, A>::ReverseNodes
Reverses the nodes in a given list. Use when operator = for the
contained element is expensive.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to reverse
@return (void)
*/
template <typename T, typename A>
void ReverseNodes(ZList<T, A>& _list)
{
ReverseNodes(_list, _list.Begin(), _list.End());
}
/*
public ZListAlgo<T, A>::Slice
Gets a copy of the elements in a subset of a list.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to get a slice from
@param _start - the beginning (inclusive) of the slice
@param _end - the end (exclusive) of the slice
@return (ZList<T, A>) - slice list
*/
template <typename T, typename A>
ZList<T, A> Slice(const ZList<T, A>& _list, const ZListIterator<T>& _start, const ZListIterator<T>& _end)
{
URFP(_list); //TODO: Need to check to ensure _start, _end iterate _list
return ZList<T, A>(_start, _end);
}
/*
public ZListAlgo<CF, T, A>::Sort
Sorts the provided list in place between the given iterators using the provided comparator and algorithm.
@param CF - binary comparator functor to use [(const T&, const T&) -> int] that compares values
returns negative value if first value < second value
returns positive value if first value > second value
returns 0 if first value == second value
@param AF - ternary algorithm functor to use [(CF&, ZListNode<T>*, ZListNode<T>*) -> void] that sorts the list
the first argument is a reference to the provided comparator
the second argument is a pointer to the starting node
the third argument is a pointer to the ending node (exclusive)
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to sort
@param _comparator - the comparator function used to compare the values
@param _algorithm - the algorithm to use to sort the list
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (void)
*/
template <typename CF, typename AF, typename T, typename A>
void Sort(ZList<T, A>& _list, CF _comparator, AF _algorithm, ZListIterator<T>& _start, ZListIterator<T>& _end)
{
URFP(_list); //TODO: ensure that start/end came from list
_algorithm(_comparator, _start.GetNode(), _end.GetNode());
}
/*
public ZListAlgo<T, A>::Sort
Sorts the provided list in place using the default comparator (uses operator <) and
a non-stable merge sort.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to sort
@return (void)
*/
template <typename T, typename A>
void Sort(ZList<T, A>& _list)
{
ZComparator<T> comparator;
ZListMergeSort<T> algorithm;
ZListIterator<T> start = _list.Begin();
ZListIterator<T> end = _list.End();
Sort(_list, comparator, algorithm, start, end);
}
/*
public ZListAlgo<T, A>::Sort
Sorts the provided list in place between the given iterators using the default
comparator (uses operator <) and a non-stable merge sort.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to sort
@param _start - the starting iterator
@param _end - the ending iterator (exclusive)
@return (void)
*/
template <typename T, typename A>
void Sort(ZList<T, A>& _list, ZListIterator<T>& _start, ZListIterator<T>& _end)
{
ZComparator<T> comparator;
ZListMergeSort<T> algorithm;
Sort(_list, comparator, algorithm, _start, _end);
}
/*
public ZListAlgo<CF, T, A>::Sort
Sorts the provided list in place using the provided comparator and a
non-stable merge sort.
@param CF - binary comparator functor to use [(const T&, const T&) -> int] that compares values
returns negative value if first value < second value
returns positive value if first value > second value
returns 0 if first value == second value
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to sort
@param _comparator - the comparator function used to compare the values
@return (void)
*/
template <typename CF, typename T, typename A>
void Sort(ZList<T, A>& _list, CF _comparator)
{
ZListMergeSort<T> algorithm;
ZListIterator<T> begin = _list.Begin();
ZListIterator<T> end = _list.End();
Sort(_list, _comparator, algorithm, begin, end);
}
/*
public ZListAlgo<CF, T, A>::Sort
Sorts the provided list in place using the provided comparator and algorithm.
@param CF - binary comparator functor to use [(const T&, const T&) -> int] that compares values
returns negative value if first value < second value
returns positive value if first value > second value
returns 0 if first value == second value
@param AF - ternary algorithm functor to use [(CF&, ZListNode<T>*, ZListNode<T>*) -> void] that sorts the list
the first argument is a reference to the provided comparator
the second argument is a pointer to the starting node
the third argument is a pointer to the ending node (exclusive)
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to sort
@param _comparator - the comparator function used to compare the values
@param _algorithm - the algorithm to use to sort the list
@return (void)
*/
template <typename CF, typename AF, typename T, typename A>
void Sort(ZList<T, A>& _list, CF _comparator, AF _algorithm)
{
ZListIterator<T> begin = _list.Begin();
ZListIterator<T> end = _list.End();
Sort(_list, _comparator, _algorithm, begin, end);
}
/*
public ZListAlgo<T, A>::Split
Splits the list at the specified location. The provided list is modified to be the first half,
the returned list is the second half.
@param T - the type held by the list
@param A - the allocator used by the list
@param _list - the list to split
@param _itr - iterator location to split (invalidated by this call)
@return (ZList<T, A>) - the split second half of the list
*/
template <typename T, typename A>
ZList<T, A> Split(ZList<T, A>& _list, const ZListIterator<T>& _itr)
{
URFP(_list); //TODO: Need to check to ensure _itr iterates _list
ZListIterator<T> itr = _itr;
ZList<T, A> splitList(itr, _list.End());
_list.Clear(itr);
return splitList;
}
/*
public ZListAlgo<T, A>::SwapElements
Swaps the elements at two locations in this list. Both iterators are modified
to point to new nodes, but they remain at the same location.
@param T - the type held by the list
@param A - the allocator used by the list
@param _itr1 - iterator to the first element
@param _itr2 - iterator to the second element
@return (void)
*/
template <typename T, typename A>
void SwapElements(ZList<T, A>& _list, const ZListIterator<T>& _itr1, const ZListIterator<T>& _itr2)
{
URFP(_list); //TODO: Need to check to ensure _itr1, _itr2 iterates _list
T temp = *_itr2;
*_itr2 = *_itr1;
*_itr1 = temp;
}
}
#endif

206
Lib/Include/ZSTL/ZPair.hpp Normal file
View File

@@ -0,0 +1,206 @@
/*
ZPair.hpp
Author: James Russell <jcrussell@762studios.com>
Created: 9/12/2011
Purpose:
Templated tuple implementation.
License:
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#pragma once
#ifndef _ZPAIR_HPP
#define _ZPAIR_HPP
#include <ZSTL/ZSTLCommon.hpp>
//Forward Declaration of ZPair
template <typename T1, typename T2>
class ZPair;
/*
Forward Declaration of ZPair Method Implementation Structures
Existence of these structures allows for template specialization of
individual methods of the ZPair class. In order to specialize
a single method of ZPair for a function, simply specialize the corresponding
method implementation structure.
*/
//Forward Declaration of ZPair<T1, T2>::operator < struct
template <typename T1, typename T2>
struct ZPair_OperatorLessThanImpl {
inline static bool Call(const ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other);
};
//Forward Declaration of ZPair<T1, T2>::operator = struct
template <typename T1, typename T2>
struct ZPair_OperatorEqualsAssignImpl {
inline static ZPair<T1, T2>& Call(ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other);
};
//Forward Declaration of ZPair<T1, T2>::operator == struct
template <typename T1, typename T2>
struct ZPair_OperatorEqualsCompareImpl {
inline static bool Call(const ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other);
};
//Forward Declaration of ZPair<T1, T2>::operator != struct
template <typename T1, typename T2>
struct ZPair_OperatorNotEqualImpl {
inline static bool Call(const ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other);
};
//Forward Declaration of ZPair<T1, T2>::Swap struct
template <typename T1, typename T2>
struct ZPair_SwapImpl {
inline static ZPair<T2, T1> Call(const ZPair<T1, T2>& _self);
};
/*
Dynamic tuple implementation.
The template parameter T1 is the type of the first contained value.
The template parameter T2 is the type of the second contained value.
*/
template <typename T1, typename T2>
class ZPair
{
friend struct ZPair_OperatorLessThanImpl<T1, T2>;
friend struct ZPair_OperatorEqualsAssignImpl<T1, T2>;
friend struct ZPair_OperatorEqualsCompareImpl<T1, T2>;
friend struct ZPair_OperatorNotEqualImpl<T1, T2>;
friend struct ZPair_SwapImpl<T1, T2>;
public:
//The first value
T1 First;
//The second value
T2 Second;
/*
Default constructor.
*/
ZPair()
: First(), Second() { }
/*
Copy constructor.
@param _other - the other pair
*/
ZPair(const ZPair<T1, T2>& _other)
: First(_other.First), Second(_other.Second) { }
/*
Parameterized constructor.
@param _first - the first value
@param _second - the second value
*/
ZPair(const T1& _first, const T2& _second)
: First(_first), Second(_second) { }
/*
operator < override, used to ensure ZPair can be compared using the default ZComparator.
@param _other - the other pair
@return (bool) - true if this ZPair is less than the other, false otherwise
*/
bool operator < (const ZPair<T1, T2>& _other) const
{ return ZPair_OperatorLessThanImpl<T1, T2>::Call(*this, _other); }
/*
operator = override, used to assign the ZPair to another.
@param _other - the other pair
@return (ZPair<T1, T2>&) - this pair
*/
ZPair<T1, T2>& operator = (const ZPair<T1, T2>& _other)
{ return ZPair_OperatorEqualsAssignImpl<T1, T2>::Call(*this, _other); }
/*
operator == override.
@param _other - the other pair
@return (bool) - true if this pair is equal to the other, false otherwise
*/
bool operator == (const ZPair<T1, T2>& _other) const
{ return ZPair_OperatorEqualsCompareImpl<T1, T2>::Call(*this, _other); }
/*
operator != override.
@param _other - the other pair
@return (bool) - true if this pair is not equal to the other, false otherwise
*/
bool operator != (const ZPair<T1, T2>& _other) const
{ return ZPair_OperatorNotEqualImpl<T1, T2>::Call(*this, _other); }
/*
public ZPair<T1, T2>::Swap
Returns another pair that is has swapped the first and second values of this pair.
@return (ZPair<T2, T1>) - a pair with swapped first/second values
*/
ZPair<T2, T1> Swap() const
{ return ZPair_SwapImpl<T1, T2>::Call(*this); }
};
////////////////////////////////////////////
/* Non-Specialized Method Implementations */
////////////////////////////////////////////
template <typename T1, typename T2>
bool ZPair_OperatorLessThanImpl<T1, T2>::Call( const ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other )
{
if (_self.First < _other.First)
return true;
return (_self.Second < _other.Second);
}
template <typename T1, typename T2>
ZPair<T1, T2>& ZPair_OperatorEqualsAssignImpl<T1, T2>::Call( ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other )
{
_self.First = _other.First;
_self.Second = _other.Second;
return _self;
}
template <typename T1, typename T2>
bool ZPair_OperatorEqualsCompareImpl<T1, T2>::Call( const ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other )
{
if (_self.First == _other.First && _self.Second == _other.Second)
return true;
return false;
}
template <typename T1, typename T2>
bool ZPair_OperatorNotEqualImpl<T1, T2>::Call( const ZPair<T1, T2>& _self, const ZPair<T1, T2>& _other )
{
return !(_self == _other);
}
template <typename T1, typename T2>
ZPair<T2, T1> ZPair_SwapImpl<T1, T2>::Call( const ZPair<T1, T2>& _self )
{
return ZPair<T2, T1>(_self.Second, _self.First);
}
#endif

File diff suppressed because it is too large Load Diff

55
Lib/Include/ZSTL/ZSTL.hpp Normal file
View File

@@ -0,0 +1,55 @@
/*
ZSTL.hpp
Author: James Russell <jcrussell@762studios.com>
Created: 12/26/2011
Purpose:
Header file for ZSTL that includes all ZSTL containers. Container utility libraries
are not included - these must be included individually.
Defining the following features to 1 enables the feature on all ZSTL containers. Defining
to 0 disables the feature (default behavior if undefined).
ZSTL_CHECK_INTEGRITY
Checks integrity of the ZSTL containers after allocations or if non-const functions are called.
Used for debugging ZSTL. Useful if new methods are added.
ZSTL_DISABLE_RUNTIME_CHECKS
Disables runtime bounds and error checking on ZSTL containers, iterators, and algorithms.
License:
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#pragma once
#ifndef _ZSTL_HPP
#define _ZSTL_HPP
/* Version number constants for ZSTL */
#define ZSTL_VERSION_MAJOR 0x01
#define ZSTL_VERSION_MINOR 0x01
#define ZSTL_VERSION_PATCH 0x0000
#define ZSTL_VERSION (ZSTL_VERSION_MAJOR << 24) | (ZSTL_VERSION_MINOR << 16) | (ZSTL_VERSION_PATCH)
#define ZSTL_VERSION_STRING "1.1.0"
/* The ZSTL headers */
#include "ZSTLInvalidPos.hpp"
#include "ZSTLCommon.hpp"
#include "ZPair.hpp"
#include "ZArray.hpp"
#include "ZList.hpp"
#include "ZHashMap.hpp"
#include "ZRingBuffer.hpp"
#include "ZString.hpp"
#endif

View File

@@ -0,0 +1,383 @@
/*
ZSTLCommon.hpp
Author: James Russell <jcrussell@762studios.com>
Created: 9/13/2011
Purpose:
Common include file used by all ZSTL components.
License:
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#pragma once
#ifndef _ZSTLCOMMON_HPP
#define _ZSTLCOMMON_HPP
#include <pstdint.h> //Need this for our int types
#include <new> //This gives us (std::nothrow)
#include <assert.h> //This gives us assert
#include <stdlib.h> //This gives us a definition for NULL
//Hash Value Type
typedef uint64_t ZHashValue;
//Used to get rid of the (/W4 or -Wall) warning 'Unreferenced Formal Parameter'
#ifndef URFP
#define URFP(x) ((void)x)
#endif
//Default allocation macro used by ZSTL default allocators
#ifndef ZSTL_NEW
#define ZSTL_NEW(_type) new (std::nothrow) _type()
#endif
//Default delete macro used by ZSTL default allocators
#ifndef ZSTL_DEL
#define ZSTL_DEL(_object) delete _object
#endif
//Default array allocation macro used by ZSTL default allocators
#ifndef ZSTL_NEW_ARRAY
#define ZSTL_NEW_ARRAY(_type, _size) new (std::nothrow) _type[_size]
#endif
//Default array delete macro used by ZSTL default allocators
#ifndef ZSTL_DEL_ARRAY
#define ZSTL_DEL_ARRAY(_ptr, _size) ((void)_size); delete[] _ptr
#endif
//Default assert macro used by ZSTL
#ifndef ZSTL_ASSERT
#define ZSTL_ASSERT(condition, message) (assert(condition && message))
#endif
//Error condition macro used by ZSTL
#ifndef ZSTL_ERROR
#define ZSTL_ERROR(message) (assert(0 && message))
#endif
//Swap used in array quicksort (takes array, first index, second index)
#define ZSTL_ARRAY_ELEMENT_SWAP(_a, _i, _j) temp = _a[_j]; _a[_j] = _a[_i]; _a[_i] = temp
//Push used in list mergesort (takes list node and new node)
#define ZSTL_LIST_ELEMENT_PUSH(_l, _n) if (_l == NULL) { _l = _n; } else { _l->Next = _n; _n->Previous = _l; _l = _n; }
/*
public BoundsCheck<I>
Function that checks the provided index against the provided boundary, asserting if an
out of bounds access occurs. Calls to this will be compiled out if ZSTL_DISABLE_RUNTIME_CHECKS
is not zero.
@param I - the type of index
@param _index - index to check
@param _boundary - the (exclusive) boundary to use for bounds checking
@return (size_t) - unsigned index
@assert - if the index is not less than boundary
*/
template <typename I>
size_t BoundsCheck(I _index, I _boundary)
{
#if !ZSTL_DISABLE_RUNTIME_CHECKS
ZSTL_ASSERT(_index < _boundary, "ZSTL: Out of bounds access!");
#endif
return (size_t)_index;
}
/*
Node class, which is used by ZList to contain list data and previous / next pointers. Could
be used by other classes that also need basic list-like functionality.
The template parameter T is the type contained by the list node.
*/
template <typename T>
struct ZListNode
{
//Default Constructor
ZListNode()
: Next(NULL), Previous(NULL), Element() { }
//Parameterized Constructor
ZListNode(ZListNode<T>* _next, ZListNode<T>* _previous)
: Next(_next), Previous(_previous), Element() { }
//Parameterized Constructor
ZListNode(ZListNode<T>* _next, ZListNode<T>* _previous, const T& _value)
: Next(_next), Previous(_previous), Element(_value) { }
//The next node
ZListNode* Next;
//The previous node
ZListNode* Previous;
//The contained element
T Element;
};
/*
Comparator functor, used when a delegate is needed to provide comparison information on
elements. This implementation uses operator < on elements.
The template parameter T is the type we will be comparing to each other.
*/
template <typename T>
struct ZComparator
{
ZComparator() { } //Silence Sun C++ warnings about uninitialized structs
//operator overload which returns -1 if a < b, 0 if a == b, and 1 if a > b
int operator () (const T& _a, const T& _b) const
{
if (_a < _b)
{
return -1;
}
else if (_b < _a)
{
return 1;
}
else
{
return 0;
}
}
};
/*
Array quicksort functor. Uses a recursive in-place sort. This implementation is
not a stable sort.
The template parameter T is the type contained by the array we will be sorting.
*/
template <typename T>
struct ZArrayQuickSort
{
ZArrayQuickSort() { } //Silence Sun C++ warnings about uninitialized structs
//Helper function which partitions the array
template <typename CF>
inline size_t Partition(CF& _comparator, T* _array, size_t _left, size_t _right, size_t _pivot)
{
size_t i, j;
T temp;
//Get the value at the pivot point
T pivotValue = _array[_pivot];
//Move pivot to end
ZSTL_ARRAY_ELEMENT_SWAP(_array, _pivot, _right);
//Check values from left up to pivot
for (i = _left, j = _left; i < _right; i++)
{
//If less than the pivot value
if (_comparator(_array[i], pivotValue) < 0)
{
//Swap back and increment our 'target' index j
ZSTL_ARRAY_ELEMENT_SWAP(_array, i, j);
j++;
}
}
//Move pivot to final location (all values with index < j are < pivotValue)
ZSTL_ARRAY_ELEMENT_SWAP(_array, j, _right);
return j;
}
//Helper function which performs the sorting
template <typename CF>
void QuickSort(CF& _comparator, T* _array, size_t _left, size_t _right)
{
size_t pivot;
if (_right > _left)
{
//Center pivot point (guarded against overflow)
pivot = _left + (_right - _left) / 2;
//Get our next pivot after partitioning around the current
pivot = Partition(_comparator, _array, _left, _right, pivot);
//Sort the left partition
if (pivot != 0)
QuickSort(_comparator, _array, _left, pivot - 1);
//Sort the right partition
QuickSort(_comparator, _array, pivot + 1, _right);
}
}
//Functor operator () override
template <typename CF>
void operator () (CF& _comparator, T* _array, size_t _size)
{
QuickSort(_comparator, _array, 0, _size - 1);
}
};
/*
List merge sort functor. This implementation is a 'stable' sort.
The template parameter T is the type contained by the underlying list we will be sorting.
*/
template <typename T>
struct ZListMergeSort
{
ZListMergeSort() { } //Silence Sun C++ warnings about initialized structs
//Special 'Len' function which uses no end node
inline size_t Length(ZListNode<T>* _list)
{
size_t i = 0;
ZListNode<T>* node;
//for (i = 0, node = _list; node != NULL; i++, node = node->Next);
node = _list;
while (node != NULL)
{
node = node->Next;
i++;
}
return i;
}
//Helper function which merges two lists, returns the last node
template <typename CF>
inline ZListNode<T>* Merge(CF& _comparator, ZListNode<T>* _left, ZListNode<T>* _right)
{
ZListNode<T>* merged = NULL;
//While left and right still have elements
while (_left != NULL && _right != NULL)
{
//Compare first elements
if (_comparator(_left->Element, _right->Element) < 0)
{
//Add the left element
ZSTL_LIST_ELEMENT_PUSH(merged, _left);
_left = _left->Next;
}
else
{
//Add the right element
ZSTL_LIST_ELEMENT_PUSH(merged, _right);
_right = _right->Next;
}
}
//While the left still has elements
while (_left != NULL)
{
//Add them
ZSTL_LIST_ELEMENT_PUSH(merged, _left);
_left = _left->Next;
}
//While the right still has elements
while (_right != NULL)
{
//Add them
ZSTL_LIST_ELEMENT_PUSH(merged, _right);
_right = _right->Next;
}
//Return the end node
return merged;
}
//Helper function which merge sorts a list, returns the last node
template <typename CF>
inline ZListNode<T>* MergeSort(CF& _comparator, ZListNode<T>* _start)
{
size_t i;
size_t middle;
ZListNode<T>* left;
ZListNode<T>* right;
//If none or one element
if (_start == NULL || _start->Next == NULL)
return _start;
//Determine midpoint
middle = Length(_start) / 2;
//Set our left and right
left = right = _start;
//Set right to midpoint
for (i = 0; i < middle; i++)
right = right->Next;
//Seperate the list
right->Previous->Next = NULL;
right->Previous = NULL;
//Sort left and right recursively
MergeSort(_comparator, left);
MergeSort(_comparator, right);
//Set back our left list pointer
while (left != NULL && left->Previous != NULL)
left = left->Previous;
//Set back our right list pointer
while (right != NULL && right->Previous != NULL)
right = right->Previous;
//Return the last node from the merged lists
return Merge(_comparator, left, right);
}
//Functor operator () override
template <typename CF>
void operator () (CF& _comparator, ZListNode<T>* _start, ZListNode<T>* _end)
{
ZListNode<T>* prev;
//Check to see if list is empty
if (_start == _end)
return;
//Get the node before the starting node
prev = _start->Previous;
//Split the end node off
_end->Previous->Next = NULL;
_start->Previous = NULL;
//Reset the end node back
_end->Previous = MergeSort(_comparator, _start);
//Attach
_end->Previous->Next = _end;
//Reattach the node before _start
if (prev != NULL)
{
ZListNode<T>* current = _end;
while (current->Previous != NULL)
current = current->Previous;
current->Previous = prev;
prev->Next = current;
}
}
};
#endif

View File

@@ -0,0 +1,66 @@
/*
ZSTLInvalidPos.hpp
Author: James Russell <jcrussell@762studios.com>
Created: 12/27/2012
Purpose:
Header file for ZSTL::InvalidPos, which includes only a single definition for
the 'InvalidPos' structure. This acts as a common return value for most containers
wherein the return would be an invalid index or iterator.
License:
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#pragma once
#ifndef _ZSTLINVALIDPOS_HPP
#define _ZSTLINVALIDPOS_HPP
#include <stdlib.h> // size_t, NULL
//Predeclare ZListIterator type
template <typename T> class ZListIterator;
/* The ZSTL namespace */
namespace ZSTL
{
//Invalid Object structure, used to indicate an invalid position on many different types of containers
struct _InvalidObject
{
//Implicit cast to size_t
operator size_t () const
{ return ((size_t)-1); }
//Implicit (and templated) cast to ZListIterator<T>
template <typename T>
operator ZListIterator<T> () const
{ return ZListIterator<T>(NULL, NULL); }
};
/*
Indicator used to check when an algorithm has returned an invalid position or iterator.
Many algorithms return an index or iterator that needs to be checked against an 'invalid'
return, such as 'FindFirstOf' returning a size_t value for arrays and an iterator
for lists.
This structure can be used to check against all types of returns.
if (ZArrayAlgo::FindFirstOf(...) != ZSTL::InvalidPos)
...
if (ZListAlgo::FindFirstOf(...) != ZSTL::InvalidPos)
...
*/
const static _InvalidObject InvalidPos = _InvalidObject();
}
#endif

View File

@@ -0,0 +1,35 @@
/*
ZBasicString.hpp
Author: James Russell <jcrussell@762studios.com>
Created: 9/12/2011
Purpose:
String header, which will include string implementations and typedef
the standard string implementation.
License:
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://sam.zoy.org/wtfpl/COPYING for more details.
*/
#pragma once
#ifndef _ZSTRING_HPP
#define _ZSTRING_HPP
#include <ZSTL/ZBasicString.hpp> //Dynamic ASCII String
#include <ZSTL/ZBasicStringAlgo.hpp> //Algo methods for ZBasicString
//This typedef indicates what our base string type will be
typedef ZBasicString<> ZString;
//Temporary hack used to make ZStringAlgo -> ZBasicStringAlgo until ZStringAlgo
#define ZStringAlgo ZBasicStringAlgo
#endif