//****************************************************************// // Title: list.h // Description: template functions for simple list things // lists are handled as an array of objects // assumes that == is defined for any class this will be used for // Functions: // remove duplicates from list // remove contents of one list from another // append contents of one list to another // find intersection of two lists // // Author: Dr Michael A. Redmond // Date: 3/27/97 // Last Revised Date: 3/31/97 // // // Copyright (c) 1997. Dr. Michael A. Redmond // This software may not be distributed further without permission from // Dr Michael A. Redmond. // This software is distributed WITHOUT ANY WARRANTY. // //****************************************************************// // conditional compilation to avoid trying to include same thing twice #ifndef LIST_H #define LIST_H #include // remove duplicate objects from a list of objects // template function rather than in class template int remove_dup_objects(T objectlist[], int num_objects) { bool dup_found = true; while (dup_found) { // keep looping as long as we find a duplicate dup_found = false; int object_cnt = 0; while (!dup_found && (object_cnt < num_objects-1)) { // go through all the rest looking for dup for each int inner_cnt = object_cnt + 1; while (!dup_found && (inner_cnt < num_objects)) { // check any after the current one for a duplicate if (objectlist[object_cnt] == objectlist[inner_cnt]) { // found a dup clog << " found a dup" << endl; clog << " object: " << object_cnt << " : " << objectlist[object_cnt] << endl; clog << " with object: " << inner_cnt << " : " << objectlist[inner_cnt] << endl; // force an end to the loop to go back in with correct // number of objects dup_found = true; // replace the second with the last and decrement the count // (last is set to default values before decrement) objectlist[inner_cnt] = objectlist[num_objects - 1]; objectlist[num_objects - 1] = T(); num_objects--; } // end if inner_cnt++; } // end inner while object_cnt++; } // end middle while } // end outer while return num_objects; } // end function // remove matching objects from a list of objects // template function rather than in class template int remove_list_objects_from_list(const T objects_to_rem[], const int num_objects_to_rem, T startobjectlist[], int num_objects) { T tempobject; int match_object_cnt = 0; while (match_object_cnt < num_objects_to_rem) { // go through second list looking for match for each int source_cnt = 0; while (source_cnt < num_objects) { // check any after the current one for a match if (objects_to_rem[match_object_cnt] == startobjectlist[source_cnt]) { // found a match // replace the second with the last and decrement the count startobjectlist[source_cnt] = startobjectlist[num_objects - 1]; num_objects--; } // end if source_cnt++; } // end inner while match_object_cnt++; } // end outer while return num_objects; } // end function // append a second list to a given list of objects // template function rather than in class template int append_list_of_objects_to_list( T startobjectlist[], int num_objects, const T objects_to_append[], const int num_objects_to_append) { for (int cnt = 0;cnt < num_objects_to_append;cnt++) { startobjectlist[num_objects + cnt] = objects_to_append[cnt]; } return (num_objects_to_append + num_objects); } // find the intersection between two lists of objects // template function rather than in class template int ListIntersect( const T objlist1[], const int num_obj1, const T objlist2[], const int num_obj2, T intersect_list[]) { int match_object_cnt = 0; for (int cnt1 = 0; cnt1 < num_obj1;cnt1++) { // go through second list looking for match for each for (int cnt2 = 0; cnt2 < num_obj2;cnt2++) { if (objlist1[cnt1] == objlist2[cnt2]) { // found a match // put it in the list and increment the count intersect_list[match_object_cnt] = objlist1[cnt1]; match_object_cnt++; } // end if } // end inner for } // end outer for return match_object_cnt; } // end function #endif