home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-24 | 10.3 KB | 431 lines | [TEXT/CWIE] |
- //Copyright (c) 1997 Aidan Cully
- //All rights reserved
-
- //TList::TData::TData()
- // uses:
- // Used internally by TList and TData to initialize the data contained
- // in a TData structure
- // in:
- // none
- // out:
- // none
- // side effects:
- // TList::TData is initialized
- template <class T>
- TData<T>::TData( T item ):
- mData( item )
- {
- mNext = 0l;
- mPrev = 0l;
- }
-
- //TList::TData::AddPrev()
- // uses:
- // Used internally by TList to maintain the structure of the list
- // in:
- // none
- // out:
- // returns Boolean
- // TRUE: an element could be added
- // FALSE: an element could not be added
- // side effects:
- // A new element is placed after the current one in the TList's data linked list
- template <class T>
- Boolean TData<T>::AddNext( T item )
- {
- //Is there an element already after this one?
- if( !mNext ) {
- //No, so we just have to create a new one and initialize it
- mNext = new TData( item );
- if( !mNext )
- return( false );
- mNext->mPrev = this;
- } else {
- //Yes, so we have to keep track of the old element
- TData *oldNext = mNext;
- //Now we can create and initialize the new element
- mNext = new TData( item );
- if( !mNext )
- return( false );
- mNext->mNext = oldNext;
- oldNext->mPrev = mNext;
- mNext->mPrev = this;
- }
- return( true );
- }
-
- //TList::TData::AddPrev()
- // uses:
- // Used internally by TList to maintain the structure of the list
- // in:
- // none
- // out:
- // returns Boolean
- // TRUE: an element could be added
- // FALSE: an element could not be added
- // side effects:
- // A new element is placed before the current one in the TList's data linked list
- template <class T>
- Boolean TData<T>::AddPrev( T item )
- {
- //Is there an element already before us?
- if( !mPrev ) {
- //No, so all we have to do is create a new element there
- //and initialize it's data properly
- mPrev = new TData( item );
- if( !mPrev )
- return( false );
- mPrev->mNext = this;
- } else {
- //Yes, so make sure we don't lose track of the old element
- TData *oldPrev = mPrev;
- //Create and initialize the old elements
- mPrev = new TData( item );
- if( !mPrev )
- return( false );
- mPrev->mPrev = oldPrev;
- oldPrev->mNext = mPrev;
- mPrev->mNext = this;
- }
- return( true );
- }
-
- //TList::TList()
- // uses:
- // This is the TList constructor.
- // in:
- // none
- // out:
- // none
- // side effects:
- // mNode is initialized to 0
- template <class T>
- TList<T>::TList():
- mNode( 0 )
- {
- }
-
- //TList::~TList()
- // uses:
- // This is the TList destructor. It cleans up any memory allocated by the TList during
- // the course of its use
- // in:
- // none
- // out:
- // none
- // side effects:
- // all the data elements are officially dead
- template <class T>
- TList<T>::~TList()
- {
- //Is there data?
- if( MoveFirst() )
- //Then get rid of it!
- while( Remove() )
- ;
- }
-
- //TList::AddPrev()
- // uses:
- // Call this function to add a new element before the current one in the TList
- // in:
- // none
- // out:
- // returns Boolean
- // true if a new element could be added
- // false if a new element could not be added
- // side effects:
- // A new element is inserted before the current one, and the new element becomes the
- // current element.
- template <class T>
- Boolean TList<T>::AddPrev( T item )
- {
- //Do we have a place to store the data?
- if( !mNode ) {
- //No, so we have to create one
- mNode = new TData<T>( item );
- if( !mNode )
- return( false );
- } else {
- //Yes, so we have to tack on to it
- if( !mNode->AddPrev( item ) )
- return( false );
- mNode = mNode->mPrev;
- }
- return( true );
- }
-
- //TList::AddNext()
- // uses:
- // Call this function to add a new element after the current one in the TList
- // in:
- // none
- // out:
- // returns Boolean
- // true if a new element could be added
- // false if a new element could not be added
- // side effects:
- // A new element is inserted after the current one, and the new element becomes the
- // current element.
- template <class T>
- Boolean TList<T>::AddNext( T item )
- {
- //Do we have a place to store the data?
- if( !mNode ) {
- //No, so we have to create one
- mNode = new TData<T>( item );
- if( !mNode )
- return( false );
- } else {
- //Yes, so we have to tack on to it
- if( !mNode->AddNext( item ) )
- return( false );
- mNode = mNode->mNext;
- }
- return( true );
- }
-
- //TList::MovePrev()
- // uses:
- // Use this function to set the current element to the element just before the present
- // current element
- // in:
- // none
- // out:
- // returns Boolean
- // true if the current element could be moved
- // false if the current element could not be moved
- // side effects:
- // The current element is changed to the element just before the present current element
- template <class T>
- Boolean TList<T>::MovePrev()
- {
- if( !mNode )
- return( false );
- if( !mNode->mPrev )
- return( false );
- mNode = mNode->mPrev;
- return( true );
- }
-
- //TList::MoveNext()
- // uses:
- // Use this function to set the current element to the element just after the present
- // current element
- // in:
- // none
- // out:
- // returns Boolean
- // true if the current element could be moved
- // false if the current element could not be moved
- // side effects:
- // The current element is changed to the element just after the present current element
- template <class T>
- Boolean TList<T>::MoveNext()
- {
- if( !mNode )
- return( false );
- if( !mNode->mNext )
- return( false );
- mNode = mNode->mNext;
- return( true );
- }
-
- //TList::MoveFirst()
- // uses:
- // Sets the current element to the first element in the list
- // in:
- // none
- // out:
- // returns Boolean
- // true if there are elements in the list
- // false if the list contains no elements
- // side effects:
- // The current element now points to the beginning of the list
- template <class T>
- Boolean TList<T>::MoveFirst()
- {
- //Is there data?
- if( !mNode )
- return( false );
- //Go back as far as we can
- while( MovePrev() )
- ;
- return( true );
- }
-
- //TList::MoveLast()
- // uses:
- // Sets the current element to the first last in the list
- // in:
- // none
- // out:
- // returns Boolean
- // true if there are elements in the list
- // false if the list contains no elements
- // side effects:
- // The current element now points to the end of the list
- template <class T>
- Boolean TList<T>::MoveLast()
- {
- //Is there data?
- if( !mNode )
- return( false );
- //Go as far forward as we can
- while( MoveNext() )
- ;
- return( true );
- }
-
- //TList::GetData()
- // uses:
- // Use this function to find out what data is held in the current element of the list
- // in:
- // none
- // out:
- // UInt32 data
- // This value is filled in with the current data in the list
- // returns Boolean
- // true if there is data in the list
- // false if the list contains no data
- // side effects:
- // data is set to the data pointed to by the current element in the list
- template <class T>
- Boolean TList<T>::GetData( T &data )
- {
- if( !mNode )
- return( false );
- data= mNode->mData;
- return( true );
- }
-
- //TList::SetData()
- // uses:
- // Use this function to set the data held in the current element of the list
- // in:
- // UInt32 data
- // This value contains what we want the data in the list to look like
- // out:
- // returns Boolean
- // true if there is storage for the data
- // false if there is no storage
- // side effects:
- // The data pointed to by the current list element is overwritten by the value passed
- template <class T>
- Boolean TList<T>::SetData( T newData )
- {
- if( !mNode )
- return( false );
- mNode->mData= newData;
- return( true );
- }
-
- //TList::GoToElem()
- // uses:
- // Used to set the current element of the list to an |absolute value|, rather than
- // always changing the value relative to the current one. Usually used in conjunction
- // with TList::FindIndex()
- // in:
- // SInt16 theIndex
- // This is the absolute index of the element you want the list to point to
- // out:
- // returns Boolean
- // true if theIndex is within the bounds of the list
- // false if theIndex is not within the bounds of the list
- // side effects:
- // The list's current element is set to theIndex'th element in the list.
- template <class T>
- Boolean TList<T>::GoToElem( SInt16 theIndex )
- {
- if( !MoveFirst() )
- return( false );
- if( theIndex < 0 )
- return( false );
- while( theIndex-- )
- if( !MoveNext() )
- return( false );
- return( true );
- }
-
- //TList::FindIndex()
- // uses:
- // Used for searching a list for an element with particular data
- // in:
- // UInt32 theData
- // The data we are looking for
- // out:
- // returns SInt16
- // On success, returns the index of the element in the array.
- // -1 if theData does not exist in the list
- // side effects:
- // Don't rely on any. If you want to use this index, use the function TList::GoToElem()
- template <class T>
- SInt16 TList<T>::FindIndex( T theData )
- {
- SInt16 theIndex = 0;
- T temp;
-
- if( !MoveFirst() )
- //There is no data! AAaargh!
- return( -1 );
- GetData( temp );
- //Loop through every element until we find theData.
- while( temp != theData )
- if( MoveNext() ) {
- theIndex++;
- GetData(temp);
- } else
- //Didn't find it.
- return( -1 );
- //If the data didn't exist, we wouldn't have gotten this far.
- return( theIndex );
- }
-
- //TList::Remove()
- // uses:
- // Used to free up the memory held by the current element in the array. The current
- // element is set to the current next if available, otherwise to the current previous.
- // If neither is available, there is no current element.
- // in:
- // none
- // out:
- // returns Boolean
- // true if there is still data in the list
- // false if the list contains no data
- // side effects:
- // the present current element is no more, and instead it points to the present next
- // element if available, otherwise it points to the current previous element. If neither
- // is available, there is no current element.
- template <class T>
- Boolean TList<T>::Remove()
- {
- //What is the situation here?
- if( !mNode )
- //No data, return false
- return( false );
- if( !(mNode->mPrev || mNode->mNext) ) {
- //This is the only element in the list, don't have to worry about losing track
- //of the previous/next.
- delete mNode;
- mNode = 0l;
- } else if( !mNode->mNext ) {
- //There is no next element, so we set the current element back one
- mNode = mNode->mPrev;
- delete mNode->mNext;
- mNode->mNext = 0l;
- } else {
- //There is a next element, keep track of it.
- TData<T> *oldNode = mNode;
- mNode = mNode->mNext;
- //Is there a previous element?
- if( oldNode->mPrev ) {
- //Yes, so we have to make sure the mNext/mPrev's point to the right place
- mNode->mPrev = oldNode->mPrev;
- oldNode->mPrev->mNext = mNode;
- } else
- //No, so we don't have to worry about the previous node
- mNode->mPrev = 0l;
- delete oldNode;
- }
- return( true );
- }