|
From: <ce...@us...> - 2003-06-23 00:36:31
|
Update of /cvsroot/devmaster/engine/include
In directory sc8-pr-cvs1:/tmp/cvs-serv28464/include
Added Files:
t_node.h t_queue.h t_stack.h
Log Message:
--- NEW FILE: t_node.h ---
// t_node.h
#ifndef __t_node_h_
#define __t_node_h_
#include <iostream>
/////////////////////////////////////////////////
/*
class: t_node
this class is a TEMPLATED node class that is
used for linked list *t_list*
*/
/////////////////////////////////////////////////
template <class node>
class t_node
{
public:
node element;
t_node<node>* next;
t_node()
{
element = node();
next = NULL;
}
t_node(const node& ne)
{
element = ne;
next = NULL;
}
};
/////////////////////////////////////////////////
#endif
--- NEW FILE: t_queue.h ---
//////////////////////////////////////////////////
// created by robert beatty : bertobot at cox dot net
// copyright 2003
// use freely - if you change anything, ANYTHING, email me and let me know
//
//////////////////////////////////////////////////
#ifndef __t_queue_h_
#define __t_queue_h_
//////////////////////////////////////////////////
#include "t_node.h"
/////////////////////////////////////////////////
class t_queue_exception
{
public:
t_queue_exception()
{
printf("t_queue.h, t_queue::get(): accessing NULL object\n");
}
t_queue_exception(unsigned int index)
{
printf("t_queue.h t_queue::operator[]: index out of range (index: %d)\n", index);
}
};
//////////////////////////////////////////////////
template <class any>
class t_queue
{
protected:
t_node<any>
*head,
*tail;
unsigned int size;
public:
t_queue();
t_queue(const t_queue<any>&);
t_queue operator = (const t_queue<any>&);
any& operator [] (const unsigned int&);
bool enque(const any&);
bool deque(any&);
bool deque();
any& get();
void clear();
bool empty() const;
unsigned int length() const;
virtual ~t_queue();
};
//////////////////////////////////////////////////
//////////////////////////////////////////////////
template <class any>
t_queue<any>::t_queue()
{
head = tail = NULL;
size = 0;
}
//////////////////////////////////////////////////
template <class any>
t_queue<any>::t_queue(const t_queue<any>& rhs)
{
t_node<any> *t = rhs.head;
while (t)
{
enque(t->element);
}
}
//////////////////////////////////////////////////
template <class any>
t_queue<any> t_queue<any>::operator = (const t_queue<any>& rhs)
{
// check for self assignement
if (this == &rhs)
return *this;
// clear everything
clear();
// copy
t_node<any> *t = rhs.head;
while (t)
{
enque(t->element);
}
return *this;
}
//////////////////////////////////////////////////
template <class any>
any& t_queue<any>::operator [] (const unsigned int& index)
{
if ( (index >= size) || (index < 0) )
throw t_queue_exception(index);
t_node<any> *t = head;
for (unsigned int i = 0; i < index; i++)
t = t->next;
return t->element;
}
//////////////////////////////////////////////////
template <class any>
bool t_queue<any>::enque(const any& e)
{
t_node<any> *t = new t_node<any>(e);
// check if assertion succeeded. if fail, return false
// if no head, head insert
if (!head)
tail = head = t;
// otherwise, tail insert
else
{
tail->next = t;
tail = t;
}
size++;
return true;
}
//////////////////////////////////////////////////
template <class any>
bool t_queue<any>::deque(any& e)
{
if (!empty())
{
e = tail->element;
return deque();
}
return false;
}
//////////////////////////////////////////////////
template <class any>
bool t_queue<any>::deque()
{
if (!empty())
{
t_node<any> *t = head;
if (tail == head)
{
delete head;
tail = head = NULL;
}
else
{
// find tail successor
while (t && t->next && t->next->next)
t = t->next;
delete tail;
t->next = NULL;
tail = t;
}
size--;
return true;
}
return false;
}
//////////////////////////////////////////////////
template <class any>
any& t_queue<any>::get()
{
if (!empty())
return tail->element;
throw t_queue_exception();
}
//////////////////////////////////////////////////
template <class any>
void t_queue<any>::clear()
{
while (!empty())
deque();
}
//////////////////////////////////////////////////
template <class any>
bool t_queue<any>::empty() const
{
return (head == NULL);
}
//////////////////////////////////////////////////
template <class any>
unsigned int t_queue<any>::length() const
{
return size;
}
//////////////////////////////////////////////////
template <class any>
t_queue<any>::~t_queue()
{
clear();
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
#endif
--- NEW FILE: t_stack.h ---
//////////////////////////////////////////////////
// created by robert beatty : bertobot at cox dot net
// copyright 2003
// use freely - if you change anything, ANYTHING, email me and let me know
//
//////////////////////////////////////////////////
#ifndef __t_stack_h_
#define __t_stack_h_
//////////////////////////////////////////////////
#include "t_node.h"
/////////////////////////////////////////////////
class t_stack_exception
{
public:
t_stack_exception()
{
printf("t_stack.h, t_stack::get(): accessing NULL object\n");
}
t_stack_exception(unsigned int index)
{
printf("t_stack.h t_stack::operator[]: index out of range (index: %d)\n", index);
}
};
//////////////////////////////////////////////////
template <class any>
class t_stack
{
protected:
t_node<any> *head;
unsigned int size;
public:
t_stack();
t_stack(const t_stack<any>&);
t_stack operator = (const t_stack<any>&);
any& operator [] (const unsigned int&);
bool push(const any&);
bool pop(any&);
bool pop();
any& get();
void clear();
bool empty() const;
unsigned int length() const;
virtual ~t_stack();
};
//////////////////////////////////////////////////
//////////////////////////////////////////////////
template <class any>
t_stack<any>::t_stack()
{
head = NULL;
size = 0;
}
//////////////////////////////////////////////////
template <class any>
t_stack<any>::t_stack(const t_stack<any>& rhs)
{
t_node<any> *t = rhs.head;
while (t)
{
push(t->element);
}
}
//////////////////////////////////////////////////
template <class any>
t_stack<any> t_stack<any>::operator = (const t_stack<any>& rhs)
{
// check for self assignement
if (this == &rhs)
return *this;
// clear everything
clear();
// copy
t_node<any> *t = rhs.head;
while (t)
{
push(t->element);
}
return *this;
}
//////////////////////////////////////////////////
template <class any>
any& t_stack<any>::operator [] (const unsigned int& index)
{
if ( (index >= size) || (index < 0) )
throw t_stack_exception(index);
t_node<any> *t = head;
for (unsigned int i = 0; i < index; i++)
t = t->next;
return t->element;
}
//////////////////////////////////////////////////
template <class any>
bool t_stack<any>::push(const any& e)
{
t_node<any> *t = new t_node<any>(e);
// check if assertion succeeded. if fail, return false
// head insert
t->next = head;
head = t;
size++;
return true;
}
//////////////////////////////////////////////////
template <class any>
bool t_stack<any>::pop(any& e)
{
if (!empty())
{
e = head->element;
return pop();
}
return false;
}
//////////////////////////////////////////////////
template <class any>
bool t_stack<any>::pop()
{
if (!empty())
{
t_node<any> *t = head->next;
delete head;
head = t;
size--;
return true;
}
return false;
}
//////////////////////////////////////////////////
template <class any>
any& t_stack<any>::get()
{
if (!empty())
return head->element;
throw t_stack_exception();
}
//////////////////////////////////////////////////
template <class any>
void t_stack<any>::clear()
{
while (!empty())
pop();
}
//////////////////////////////////////////////////
template <class any>
bool t_stack<any>::empty() const
{
return (head == NULL);
}
//////////////////////////////////////////////////
template <class any>
unsigned int t_stack<any>::length() const
{
return size;
}
//////////////////////////////////////////////////
template <class any>
t_stack<any>::~t_stack()
{
clear();
}
//////////////////////////////////////////////////
//////////////////////////////////////////////////
#endif
|