123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
- /*================================================================
- *
- * Project : Freyja
- * Author : Mongoose
- * Website : http://www.westga.edu/~stu7440/
- * Email : stu7440@westga.edu
- * Object : Vector
- * License : No use w/o permission (C) 2002 Mongoose
- * Comments: mtk template vector
- *
- * UINT_MAX is an error condition, used in place of -1
- *
- * This file was generated using Mongoose's C++
- * template generator script. <stu7440@westga.edu>
- *
- *-- History ------------------------------------------------
- *
- * 2002.08.31:
- * Mongoose - Created
- ================================================================*/
-
-
- #ifndef _VECTOR_H_
- #define _VECTOR_H_
-
- #include <stdlib.h>
- #include <stdio.h>
-
- #ifdef DEBUG_MEMORY
- #include <memory_test.h>
- #endif
-
-
- template <class Object> class Vector
- {
- public:
-
- Vector()
- {
- mData = 0x0;
- mError = 0x0;
-
- mReserve = 0;
- mStart = 0;
- mEnd = 0;
-
- mIndex = 0;
- }
-
-
- Vector(Vector &vector)
- {
- mData = 0x0;
- mError = 0x0;
-
- mReserve = 0;
- mStart = 0;
- mEnd = 0;
-
- copy(vector);
- }
-
-
- Vector(unsigned int size)
- {
- mData = 0x0;
- mError = 0x0;
-
- mReserve = 0;
- mStart = 0;
- mEnd = 0;
-
- mIndex = 0;
-
- resize(size);
- }
-
-
- ~Vector()
- {
- if (!empty() && mData)
- {
- delete [] mData;
- }
-
- clear();
- mReserve = 0;
- }
-
-
- void clear()
- {
- mStart = 0;
- mEnd = 0;
-
- mIndex = 0;
- }
-
-
- void erase()
- {
- for (start(); forward(); next())
- {
- if (current())
- {
- delete current();
- }
- }
-
- clear();
- }
-
-
- void reserve(unsigned int count)
- {
- unsigned int i;
- Object *swap = 0x0;
-
-
- if (count > mReserve)
- {
- swap = mData;
- mReserve = count;
- mData = new Object[count];
- }
-
- if (swap)
- {
- for (i = begin(); i < end(); ++i)
- {
- mData[i] = swap[i];
- }
-
- delete [] swap;
- }
- }
-
-
- void resize(unsigned int count)
- {
- resize(count, 0x0);
- }
-
-
- void resize(unsigned int count, Object object)
- {
- unsigned int i;
-
-
- reserve(count);
-
- for (i = 0; i < count; ++i)
- {
- if (i < begin() || i >= end())
- {
- mData[i] = object;
- }
- }
-
- mEnd = count;
- }
-
-
- void pushBack()
- {
- pushBack(0x0);
- }
-
-
- void pushBack(Object object)
- {
- resize(size() + 1);
- mData[size()-1] = object;
- }
-
-
- bool empty()
- {
- return (begin() == end());
- }
-
-
- unsigned int capacity()
- {
- return mReserve;
- }
-
-
- unsigned int begin()
- {
- return mStart;
- }
-
-
- unsigned int end()
- {
- return mEnd;
- }
-
-
- unsigned int size()
- {
- return mEnd;
- }
-
-
- void copy(Vector<Object> &vector)
- {
- unsigned int i;
-
-
- if (vector.capacity() > capacity())
- {
- resize(vector.capacity());
- }
-
- mStart = vector.begin();
- mEnd = vector.end();
-
- for (i = vector.begin(); i < vector.end(); ++i)
- {
- mData[i] = vector[i];
- //Add(list->Current()); // WTFBBQ?
- }
- }
-
-
- void qSort(int (*compareFunc)(const void *, const void *))
- {
- qsort(mData, end(), sizeof(Object), compareFunc);
- }
-
-
- void swap(unsigned int index, unsigned int index2)
- {
- if (index < begin() || index > end())
- return;
-
- if (index2 < begin() || index2 > end())
- return;
-
- Object swap = mData[index];
- mData[index] = mData[index2];
- mData[index2] = swap;
- }
-
-
- void assign(unsigned int index, Object object)
- {
- mData[index] = object;
- }
-
-
- Object operator [] (unsigned int index)
- {
- if (mData == 0x0 || index < begin() || index > end() ||
- index >= size() || empty())
- return mError;
-
- return mData[index];
- }
-
-
- void print(void (*print_func)(Object))
- {
- unsigned int i;
-
-
- for (i = begin(); i < end(); ++i)
- {
- if (print_func)
- {
- (*print_func)(mData[i]);
- }
-
- fflush(stdout);
- }
-
- printf("\n");
- }
-
-
- // Built-in iterator methods ////////////////////////////////
-
- void start(unsigned int index)
- {
- if (mData == 0x0 || index < begin() || index > end() ||
- index >= size() || empty())
- return;
-
- mIndex = index;
- }
-
- void start()
- {
- mIndex = begin();
- }
-
-
- void finish()
- {
- mIndex = end() - 1;
- }
-
-
- bool forward()
- {
- return (mIndex < end());
- }
-
-
- bool backward()
- {
- return (mIndex + 1 > begin());
- }
-
-
- void next()
- {
- if (mIndex < end())
- ++mIndex;
- }
-
- void prev()
- {
- --mIndex;
- }
-
-
- void setError(Object object)
- {
- mError = object;
- }
-
-
- unsigned int getCurrentIndex()
- {
- return mIndex;
- }
-
-
- unsigned int setCurrentIndex(unsigned int index)
- {
- if (index < end())
- {
- mIndex = index;
- }
- }
-
-
- Object current()
- {
- return mData[mIndex];
- }
-
-
- /* Requires objects to support '==' */
- bool find(Object object)
- {
- for (start(); forward(); next())
- {
- if (object == current())
- {
- return true;
- }
- }
-
- return false;
- }
-
- // Instead of appending objects this apptempts replacing 'removed' objects
- unsigned int add(Object object)
- {
- if (begin() > 0)
- {
- mData[begin() - 1] = object;
- --mStart;
-
- return begin();
- }
-
- pushBack(object);
- return size();
- }
-
-
- // Hhhmm... dangerous and fun - this gets your data out of order
- void remove(unsigned int index)
- {
- mData[index] = mData[begin()];
- ++mStart;
- }
-
- private:
-
- Object *mData;
-
- Object mError;
-
- unsigned int mReserve;
- unsigned int mStart;
- unsigned int mEnd;
-
- unsigned int mIndex;
- };
-
- #endif
|