00001 #ifndef DL_LIST_HH
00002 #define DL_LIST_HH
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00165
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