Import backup from 2004-06-03

This commit is contained in:
josh committed 2017-07-25 22:23:46 -04:00
1 parent b4aca026d4
commit b5ba5b63fe
11 files changed
+203 -25

No files matched your search

+31 -10
View File
@@ -1,4 +1,10 @@
// LinkedList.h
// implements a LinkedList container for HOS
// Author: Josh Holtrop
// Date: 05/22/04
// Modified: 06/03/04
template <typename Element>
class LinkedList
{
@@ -42,12 +48,12 @@ public:
{
return theNode->data;
}
Element & operator++()
Element & operator++() //prefix
{
theNode = theNode->next;
return theNode->data;
}
Element & operator++(int dum)
Element & operator++(int dum) //postfix
{
theNode = theNode->next;
return theNode->prev->data;
@@ -71,35 +77,50 @@ public:
{
return theNode->data;
}
Element & operator++()
Element & operator++() //prefix
{
theNode = theNode->prev;
return theNode->data;
}
Element & operator++(int dum)
Element & operator++(int dum) //postfix
{
theNode = theNode->prev;
return theNode->next->data;
}
};
/* Basic constructor */
LinkedList();
int numElements() { return count; }
iterator begin() { return new iterator(firstNode); }
iterator end() { return new iterator(lastNode->next); }
reverse_iterator rbegin() { return new reverse_iterator(lastNode); }
reverse_iterator rend() { return new reverse_iterator(firstNode->prev); }
/* How many elements in the list */
int size() { return count; }
/* Return some iterators for traversing the list */
iterator begin() { return iterator(firstNode); }
iterator end() { return iterator(lastNode->next); }
reverse_iterator rbegin() { return reverse_iterator(lastNode); }
reverse_iterator rend() { return reverse_iterator(firstNode->prev); }
/* 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();
firstNode = lastNode = new LinkedNode(); //head node
count = 0;
}
+24 -8
View File
@@ -1,18 +1,17 @@
// string.cpp
// implements c++ string object
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 06/01/04
#include "string.h"
#include "asmfuncs.h"
#include "cstring.h"
#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 = new char; //myChars must be a valid pointer at all times
*myChars = 0;
}
@@ -94,6 +93,7 @@ string & string::operator+=(const string & str)
memcpy(newStr + myLength, str.myChars, str.myLength + 1);
delete[] myChars;
myChars = newStr;
myLength += str.myLength;
return *this;
}
@@ -104,6 +104,7 @@ string & string::operator+=(const char *cstring)
memcpy(newStr + myLength, cstring, strlen(cstring) + 1);
delete[] myChars;
myChars = newStr;
myLength += strlen(cstring);
return *this;
}
@@ -114,6 +115,7 @@ string & string::operator-=(const string & str)
memcpy(newStr + str.myLength, myChars, myLength + 1);
delete[] myChars;
myChars = newStr;
myLength += str.myLength;
return *this;
}
@@ -124,6 +126,7 @@ string & string::operator-=(const char *cstring)
memcpy(newStr + strlen(cstring), myChars, myLength + 1);
delete[] myChars;
myChars = newStr;
myLength += strlen(cstring);
return *this;
}
@@ -157,7 +160,7 @@ 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 + myLength, str2.myChars, str2.myLength + 1);
memcpy(myChars + str1.myLength, str2.myChars, str2.myLength + 1);
}
string::string(const string & str1, const char *cstring)
@@ -165,7 +168,7 @@ 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 + myLength, cstring, strlen(cstring) + 1);
memcpy(myChars + str1.myLength, cstring, strlen(cstring) + 1);
}
string::string(const char *cstring, const string & str)
@@ -173,7 +176,7 @@ 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 + myLength, str.myChars, str.myLength + 1);
memcpy(myChars + strlen(cstring), str.myChars, str.myLength + 1);
}
string::string(const string & str1, char chr)
@@ -193,4 +196,17 @@ string::string(char chr, const string & str1)
*myChars = chr;
}
const char & string::operator[](unsigned int index) const
{
if (index >= 0 && 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 >= 0 && index < myLength)
return myChars[index];
return *myChars; //if index is invalid, return a pointer to the trailing 0
}
+40 -3
View File
@@ -1,37 +1,56 @@
// string.h
// implements c++ string object
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 06/01/04
// Modified: 06/03/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 equals operators */
bool operator==(const string & second) 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);
@@ -39,13 +58,26 @@ public:
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 string & operator+=(const char *cstring, string & str)
{ return str -= cstring; }
@@ -78,6 +110,11 @@ static inline string operator+(const string & str1, char 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