dl_list.hh

Go to the documentation of this file.
00001 #ifndef DL_LIST_HH
00002 #define DL_LIST_HH
00003 
00004 // Copyright (c) 1994-1999 The University of Cincinnati.
00005 // All rights reserved.
00006 
00007 // UC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF 
00008 // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00009 // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
00010 // PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  UC SHALL NOT BE LIABLE
00011 // FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
00012 // RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
00013 // DERIVATIVES.
00014 
00015 // By using or copying this Software, Licensee agrees to abide by the
00016 // intellectual property laws, and all other applicable laws of the
00017 // U.S., and the terms of this license.
00018 
00019 
00020 // You may modify, distribute, and use the software contained in this package
00021 // under the terms of the "GNU LIBRARY GENERAL PUBLIC LICENSE" version 2,
00022 // June 1991. A copy of this license agreement can be found in the file
00023 // "LGPL", distributed with this archive.
00024 
00025 // Authors: Philip A. Wilsey            phil.wilsey@uc.edu
00026 //          Tim McBrayer                tmcbraye@ece.uc.edu
00027 //          Dale E. Martin              dmartin@cliftonlabs.com
00028 
00029 #include <list>
00030 #include <algorithm>
00031 #include <functional>
00032 #include <cassert>
00033 
00034 using std::list;
00035 using std::distance;
00036 using std::advance;
00037 using std::find;
00038 using std::equal_to;
00039 using std::bind2nd;
00040 
00041 template <class type>
00042 class dl_list {
00043   
00044 public:
00045   dl_list() : elementCount(0){
00046     my_iterator = my_list.begin();
00047   }
00048   ~dl_list(){}
00049     
00050   int num_elements(){
00051     return elementCount;
00052   }
00053 
00054   int size() {
00055     return num_elements();
00056   }
00057 
00058   void append( type *to_append ){
00059     my_list.push_back( to_append );
00060     elementCount++;
00061   }
00062 
00063   void prepend( type* to_prepend ) {
00064     my_list.push_front( to_prepend );
00065     elementCount++;
00066   }
00067 
00068   void insert_after( const type *after_me, type* new_object ) {
00069     typename list<type *>::iterator found = list_find( after_me );
00070     assert( found != my_list.end() );
00071     found++;
00072     my_list.insert( found, new_object );
00073     elementCount++;
00074   }
00075     
00076   bool remove( const type *to_remove ) {
00077     bool retval = false;
00078     typename list<type *>::iterator found = list_find( to_remove );
00079     if( found != my_list.end() ){
00080       if( my_iterator == found ){
00081         my_iterator = my_list.end();
00082       }
00083       my_list.erase( found );
00084       retval = true;
00085       elementCount--;
00086     }
00087     return retval;
00088   }
00089 
00090   type *successor( const type *to_succeed ) {
00091     type *retval = 0;
00092     my_iterator = list_find( to_succeed );
00093     if( my_iterator != my_list.end() ){
00094       my_iterator++;
00095       if( my_iterator != my_list.end() ){
00096         retval = *my_iterator;
00097       }
00098     }
00099     return retval;
00100   }
00101 
00102   type *predecessor( const type *to_precede ) {
00103     type *retval = 0;
00104     my_iterator = list_find( to_precede );
00105     if( my_iterator != my_list.end() ){
00106       my_iterator--;
00107       if( my_iterator != my_list.end() ){
00108         retval = *my_iterator;
00109       }
00110     }
00111     return retval;
00112   }
00113         
00114   type *first() {
00115     type *retval = 0;
00116     if( !my_list.empty() ){
00117       my_iterator = my_list.begin();
00118       retval = *my_iterator;
00119     }
00120     return retval;
00121   }
00122     
00123   type *last() {
00124     type *retval = 0;
00125     if( !my_list.empty() ){
00126       my_iterator = my_list.end();
00127       my_iterator--;
00128       retval = *my_iterator;
00129     }
00130     return retval;
00131   }
00132     
00135   int get_position(type* data) {
00136     int retval = -1;
00137     typename list<type *>::iterator element = list_find( data );
00138     if( element != my_list.end() ){
00139       retval = distance<typename list<type *>::iterator>( my_list.begin(), element );
00140     }
00141     
00142     return retval;
00143   }
00144 
00145   type *get_nth_element( int pos_to_get ){
00146     int i = 0;
00147     type *current = first();
00148     for( i = 0; i < pos_to_get && current != NULL; i++ ){
00149       current = successor( current );
00150     }
00151     return current;
00152   }
00153 
00154   dl_list<type> &
00155   operator=( dl_list<type> to_copy ){
00156     my_list.clear();
00157     my_list.insert( my_list.end(), to_copy.my_list.begin(), to_copy.my_list.end() );
00158     my_iterator = my_list.end();
00159     elementCount = to_copy.elementCount;
00160 
00161     return *this;
00162   }
00163 
00164   // Replace "to_replace" with replace with, without disturbing the rest
00165   // of the list.
00166   void _replace( type *to_replace, type *replace_with ){
00167     typename list<type *>::iterator found = list_find( to_replace );
00168     assert( found != my_list.end() );
00169     *found = replace_with;
00170   }
00171   
00172   bool in_list( type *to_find ){
00173     if( list_find( to_find ) == my_list.end() ){
00174       return false;
00175     }
00176     else{
00177       return true;
00178     }
00179   }
00180 
00181 private:
00182   unsigned int elementCount;
00183   list<type *> my_list;
00184   typename list<type *>::iterator my_iterator;
00185 
00186   typename list<type *>::iterator list_find( const type *to_find ){
00187     typename list<type *>::iterator retval = my_list.end();
00188     if( !my_list.empty() ){
00189       if( my_iterator != my_list.end() && *my_iterator == to_find ){
00190         retval = my_iterator;
00191       }
00192       else{
00193         retval = find( my_list.begin(), 
00194                        my_list.end(), 
00195                        to_find );
00196       }
00197     }
00198     return retval;
00199   }
00200 };
00201 
00202 #endif

Generated on Fri Mar 31 11:04:13 2006 for Savant by  doxygen 1.4.6