Import backup from 2004-07-09

This commit is contained in:
josh committed 2017-07-25 22:23:47 -04:00
1 parent f3fe4ef3b8
commit 0729754659
76 files changed
+470 -7676

No files matched your search

-210
View File
@@ -1,210 +0,0 @@
// LinkedList.h
// implements a LinkedList container for HOS
// Author: Josh Holtrop
// Date: 05/22/04
// Modified: 06/09/04
#ifndef __HOS_LINKEDLIST__
#define __HOS_LINKEDLIST__ __HOS_LINKEDLIST__
template <typename Element>
class LinkedList
{
private:
class LinkedNode
{
public:
LinkedNode()
{
prev = next = 0;
}
LinkedNode(Element dataElement)
{
data = dataElement;
prev = next = 0;
}
Element data;
LinkedNode *next;
LinkedNode *prev;
};
LinkedNode *firstNode;
LinkedNode *lastNode;
int count;
public:
class iterator
{
private:
LinkedNode *theNode;
public:
iterator(LinkedNode & node)
{
theNode = &node;
}
iterator(LinkedNode *node)
{
theNode = node;
}
Element & operator*()
{
return theNode->data;
}
void operator++() //prefix
{
theNode = theNode->next;
}
void operator++(int dum) //postfix
{
theNode = theNode->next;
}
bool operator==(const iterator & second) const
{ return theNode == second.theNode; }
bool operator!=(const iterator & second) const
{ return theNode != second.theNode; }
};
class reverse_iterator
{
private:
LinkedNode *theNode;
public:
reverse_iterator(LinkedNode & node)
{
theNode = &node;
}
reverse_iterator(LinkedNode *node)
{
theNode = node;
}
Element & operator*()
{
return theNode->data;
}
void operator++() //prefix
{
theNode = theNode->prev;
}
void operator++(int dum) //postfix
{
theNode = theNode->prev;
}
bool operator==(const reverse_iterator & second) const
{ return theNode == second.theNode; }
bool operator!=(const reverse_iterator & second) const
{ return theNode != second.theNode; }
};
/* Basic constructor */
LinkedList();
/* How many elements in the list */
int size() { return count; }
/* Return some iterators for traversing the list */
iterator begin() { return iterator(firstNode->next); }
iterator end() { return iterator(lastNode->next); }
reverse_iterator rbegin() { return reverse_iterator(lastNode); }
reverse_iterator rend() { return reverse_iterator(firstNode); }
/* Insert an element, its position will be index */
LinkedList & insert(int index, Element e);
/* Append an element to the end of the list */
LinkedList & push_back(Element e);
/* Remove an element at position index */
LinkedList & remove(int index);
/* Pop an element from the end of the list */
LinkedList & pop_back();
/* Direct access to an element in the list */
Element & operator[](int index);
};
template <typename T>
LinkedList<T>::LinkedList()
{
firstNode = lastNode = new LinkedNode(); //head node
count = 0;
}
template <typename Element>
LinkedList<Element> & LinkedList<Element>::insert(int index, Element e)
{
if (index == count)
push_back(e);
else if (index >= 0 && index < count)
{
LinkedNode *nptr = firstNode;
for (int i = 0; i <= index; i++)
nptr = nptr->next;
LinkedNode *newptr = new LinkedNode(e);
newptr->next = nptr;
newptr->prev = nptr->prev;
newptr->prev->next = newptr;
nptr->prev = newptr;
count++;
}
return *this;
}
template <typename Element>
LinkedList<Element> & LinkedList<Element>::push_back(Element e)
{
lastNode->next = new LinkedNode(e);
lastNode->next->prev = lastNode;
lastNode = lastNode->next;
count++;
return *this;
}
template <typename Element>
LinkedList<Element> & LinkedList<Element>::remove(int index)
{
if (index == count-1)
pop_back();
else if (index >= 0 && index < count)
{
LinkedNode *nptr = firstNode;
for (int i = 0; i <= index; i++)
nptr = nptr->next;
nptr->prev->next = nptr->next;
nptr->next->prev = nptr->prev;
delete nptr;
count--;
}
return *this;
}
template <typename Element>
LinkedList<Element> & LinkedList<Element>::pop_back()
{
if (count)
{
lastNode = lastNode->prev;
delete lastNode->next;
lastNode->next = 0;
count--;
}
return *this;
}
template <typename Element>
Element & LinkedList<Element>::operator[](int index)
{
if (index >= 0 && index < count)
{
LinkedNode *nptr = firstNode;
for (int i = 0; i <= index; i++)
nptr = nptr->next;
return nptr->data;
}
return firstNode->data;
}
#endif
-91
View File
@@ -1,91 +0,0 @@
// cstring.c
// Author: Josh Holtrop
// Created: 02/26/04
// Modified: 06/01/04
// Implements c string functions
#include "string.h"
#include "asmfuncs.h"
char *strcat(char *dest, const char *src)
{
strcpy(dest+strlen(dest), src);
return dest;
}
//Splits a string into multiple strings by turning all characters
// equal to delim into null values (string termination character)
//Returns the number of strings after the split, 1 if no delim chars
int string_split(char *str, char delim)
{
if (strlen(str) < 1)
return 0; //empty string
int count = 1;
for (; *str; str++)
{
if (*str == delim)
{
count++;
*str = 0;
}
}
return count;
}
//Advances a char pointer to the byte after the current string's
// null-terminating character
//Useful after calling string_split()
//Returns a pointer to the following string
char *string_advance(char *str)
{
for (; *str; str++);
return str+1;
}
void rtrim(char *str)
{
str += strlen(str); //now points to the null character at the end of the string
str--;
for (;;)
{
if ((*str == ' ') || (*str == '\t') || (*str == '\n'))
*str-- = 0;
else
break;
}
}
char *ucase(char *str)
{
char *ret = str;
for (;;)
{
if (*str == 0)
break;
if ((*str >= 'a') && (*str <= 'z'))
*str = (*str) - 32;
str++;
}
return ret;
}
char *lcase(char *str)
{
char *ret = str;
for (;;)
{
if (*str == 0)
break;
if ((*str >= 'A') && (*str <= 'Z'))
*str = (*str) + 32;
str++;
}
return ret;
}
-19
View File
@@ -1,19 +0,0 @@
// cstring.h
// Author: Josh Holtrop
// Created: 02/26/04
// Modified: 06/01/04
// Implements c string functions
#ifndef __HOS_CSTRING__
#define __HOS_CSTRING__ __HOS_CSTRING__
char *strcat(char *dest, const char *src);
int string_split(char *str, char delim);
char *string_advance(char *str);
void rtrim(char *str);
char *ucase(char *str);
char *lcase(char *str);
#endif
-279
View File
@@ -1,279 +0,0 @@
// string.cpp
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 06/07/04
#include "string.h" //string class declaration
#include "asmfuncs.h" //memcpy(void *dest, void *src, int n), strlen(char *str)
string::string()
{
myLength = 0;
myChars = new char; //myChars must be a valid pointer at all times
*myChars = 0;
}
string::~string()
{
delete[] myChars;
}
string::string(const string & orig)
{
myLength = orig.myLength;
myChars = new char[myLength + 1];
memcpy(myChars, orig.myChars, myLength + 1);
}
string::string(const char *cstring)
{
myLength = strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, cstring, myLength + 1);
}
string & string::operator=(const string & orig)
{
if (this != &orig)
{
delete[] myChars;
myLength = orig.myLength;
myChars = new char[myLength + 1];
memcpy(myChars, orig.myChars, myLength + 1);
}
return *this;
}
string & string::operator=(const char *cstring)
{
if (myChars != cstring)
{
delete[] myChars;
myLength = strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, cstring, myLength + 1);
}
return *this;
}
char * string::data() const { return myChars; }
int string::size() const { return myLength; }
string & string::operator+=(const string & str)
{
char *newStr = new char[myLength + str.myLength + 1];
memcpy(newStr, myChars, myLength);
memcpy(newStr + myLength, str.myChars, str.myLength + 1);
delete[] myChars;
myChars = newStr;
myLength += str.myLength;
return *this;
}
string & string::operator+=(const char *cstring)
{
char *newStr = new char[myLength + strlen(cstring) + 1];
memcpy(newStr, myChars, myLength);
memcpy(newStr + myLength, cstring, strlen(cstring) + 1);
delete[] myChars;
myChars = newStr;
myLength += strlen(cstring);
return *this;
}
string & string::operator-=(const string & str)
{
char *newStr = new char[myLength + str.myLength + 1];
memcpy(newStr, str.myChars, str.myLength);
memcpy(newStr + str.myLength, myChars, myLength + 1);
delete[] myChars;
myChars = newStr;
myLength += str.myLength;
return *this;
}
string & string::operator-=(const char *cstring)
{
char *newStr = new char[myLength + strlen(cstring) + 1];
memcpy(newStr, cstring, strlen(cstring));
memcpy(newStr + strlen(cstring), myChars, myLength + 1);
delete[] myChars;
myChars = newStr;
myLength += strlen(cstring);
return *this;
}
string & string::operator+=(char chr)
{
myLength++;
char *newStr = new char[myLength + 1];
memcpy(newStr, myChars, myLength);
newStr[myLength - 1] = chr;
newStr[myLength] = 0;
delete[] myChars;
myChars = newStr;
return *this;
}
string & string::operator-=(char chr)
{
myLength++;
char *newStr = new char[myLength + 1];
memcpy(newStr + 1, myChars, myLength);
*newStr = chr;
delete[] myChars;
myChars = newStr;
return *this;
}
string::string(const string & str1, const string & str2)
{
myLength = str1.myLength + str2.myLength;
myChars = new char[myLength + 1];
memcpy(myChars, str1.myChars, str1.myLength);
memcpy(myChars + str1.myLength, str2.myChars, str2.myLength + 1);
}
string::string(const string & str1, const char *cstring)
{
myLength = str1.myLength + strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, str1.myChars, str1.myLength);
memcpy(myChars + str1.myLength, cstring, strlen(cstring) + 1);
}
string::string(const char *cstring, const string & str)
{
myLength = str.myLength + strlen(cstring);
myChars = new char[myLength + 1];
memcpy(myChars, cstring, strlen(cstring));
memcpy(myChars + strlen(cstring), str.myChars, str.myLength + 1);
}
string::string(const string & str1, char chr)
{
myLength = str1.myLength + 1;
myChars = new char[myLength + 1];
memcpy(myChars, str1.myChars, myLength);
myChars[myLength - 1] = chr;
myChars[myLength] = 0;
}
string::string(char chr, const string & str1)
{
myLength = str1.myLength + 1;
myChars = new char[myLength + 1];
memcpy(myChars + 1, str1.myChars, myLength + 1);
*myChars = chr;
}
const char & string::operator[](unsigned int index) const
{
if (index < myLength)
return myChars[index];
return *myChars; //if index is invalid, return a pointer to the trailing 0
}
char & string::operator[](unsigned int index)
{
if (index < myLength)
return myChars[index];
return *myChars; //if index is invalid, return a pointer to the trailing 0
}
bool string::operator==(const string & second) const
{
if (myLength != second.myLength)
return false;
for (unsigned int i = 0; i < myLength; i++)
{
if (myChars[i] != second.myChars[i])
return false;
}
return true;
}
bool string::operator==(const char *cstring) const
{
if (myLength != strlen(cstring))
return false;
for (unsigned int i = 0; i < myLength; i++)
{
if (myChars[i] != cstring[i])
return false;
}
return true;
}
bool string::operator!=(const string & second) const
{ return !operator==(second); }
bool string::operator!=(const char *cstring) const
{ return !operator==(cstring); }
bool string::operator<(const string & second) const
{
char *c1 = myChars, *c2 = second.myChars;
while (*c1 == *c2 && *c1)
{
c1++;
c2++;
}
return *c1 < *c2;
}
bool string::operator>(const string & second) const
{
char *c1 = myChars, *c2 = second.myChars;
while (*c1 == *c2 && *c1)
{
c1++;
c2++;
}
return *c1 > *c2;
}
bool string::operator<(const char *cstring) const
{
char *c1 = myChars;
while (*c1 == *cstring && *c1)
{
c1++;
cstring++;
}
return *c1 < *cstring;
}
bool string::operator>(const char *cstring) const
{
char *c1 = myChars;
while (*c1 == *cstring && *c1)
{
c1++;
cstring++;
}
return *c1 > *cstring;
}
bool string::operator<=(const string & second) const
{ return !operator>(second); }
bool string::operator>=(const string & second) const
{ return !operator<(second); }
bool string::operator<=(const char *cstring) const
{ return operator<(cstring) || operator==(cstring); }
bool string::operator>=(const char *cstring) const
{ return operator>(cstring) || operator==(cstring); }
-145
View File
@@ -1,145 +0,0 @@
// string.h
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 06/09/04
#ifndef __HOS_STRING__
#define __HOS_STRING__ __HOS_STRING__
class string
{
private:
/* myChars is a pointer to a character array
* It will always be a valid pointer, not null.
* The constructor is responsible for initializing
* it to point to a 1-char array with myChars[0] == 0 */
char * myChars;
/* myLength holds how many characters are in the
* string, not including the trailing null value
* (ASCII 0 character) */
unsigned int myLength;
public:
/* Basic constructors */
string();
~string();
/* Copy constructor */
string(const string & orig);
/* Construct strings from c-style strings:
* Allows declarations like string s = "data"; */
string(const char *cstring);
/* Assignment operators */
string & operator=(const string & orig);
string & operator=(const char *cstring);
/* Boolean comparison operators */
bool operator==(const string & second) const;
bool operator==(const char *cstring) const;
bool operator!=(const string & second) const;
bool operator!=(const char *cstring) const;
bool operator<(const string & second) const;
bool operator>(const string & second) const;
bool operator<(const char *cstring) const;
bool operator>(const char *cstring) const;
bool operator<=(const string & second) const;
bool operator>=(const string & second) const;
bool operator<=(const char *cstring) const;
bool operator>=(const char *cstring) const;
/* Construct strings made up of combinations of other
* strings, c-style strings, and characters */
string(const string & str1, const string & str2);
string(const string & str1, const char *cstring);
string(const char *cstring, const string & str);
string(const string & str1, char chr);
string(char chr, const string & str1);
/* Append and prepend operators */
string & operator+=(const string & str);
string & operator+=(const char *cstring);
string & operator-=(const string & str);
string & operator-=(const char *cstring);
string & operator+=(char chr);
string & operator-=(char chr);
/* Direct character-access operators */
const char & operator[](unsigned int index) const;
char & operator[](unsigned int index);
/* Returns handle to actual character-array for programs
* that need raw data instead of a string object */
char * data() const;
/* Returns the size of the string. This is myLength
* and is the number of characters in the string
* not counting the trailing null (ASCII 0) character */
int size() const;
};
static inline bool operator==(char *cstring, const string & str)
{ return str == cstring; }
static inline bool operator!=(char *cstring, const string & str)
{ return str != cstring; }
static inline bool operator<(const char *cstring, const string & str)
{ return str > cstring; }
static inline bool operator>(const char *cstring, const string & str)
{ return str < cstring; }
static inline bool operator<=(const char *cstring, const string & str)
{ return str >= cstring; }
static inline bool operator>=(const char *cstring, const string & str)
{ return str <= cstring; }
static inline string & operator+=(const char *cstring, string & str)
{ return str -= cstring; }
static inline string & operator-=(const char *cstring, string & str)
{ return str += cstring; }
static inline string & operator+=(char chr, string & str)
{ return str -= chr; }
static inline string & operator-=(char chr, string & str)
{ return str += chr; }
static inline string operator+(const string & str1, const string & str2)
{ return string(str1, str2); }
static inline string operator+(const string & str1, const char *cstring)
{ return string(str1, cstring); }
static inline string operator-(const string & str1, const string & str2)
{ return string(str2, str1); }
static inline string operator-(const string & str1, const char *cstring)
{ return string(cstring, str1); }
static inline string operator+(const string & str1, char chr)
{ return string(str1, chr); }
static inline string operator-(const string & str1, char chr)
{ return string(chr, str1); }
static inline string operator+(char chr, const string & str1)
{ return string(chr, str1); }
static inline string operator-(char chr, const string & str1)
{ return string(str1, chr); }
#endif