ordered_list.cpp

Go to the documentation of this file.
00001 #ifndef ORDERED_LIST_IMPLEMENTATION_FILE
00002 #define ORDERED_LIST_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : ordered_list                                                      *
00007 *  Author : M McLeod                                                          *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "ordered_list.h"
00019 
00020 namespace nodes {
00021 
00022 bool ordered_list::insert(ordered_node *new_node, int compare_mode, bool duplicates) {
00023 // Uses the ordered_node's compare function to find where to insert.
00024 // returns false if insert fails due to invalid parameter or unexpected condition.
00025     
00026     if (!new_node)
00027         return false;
00028 
00029     iterator iter = head();
00030     bool found = false;
00031     while (!found && !iter.is_tail()) {
00032         
00033         const node *cur_node = iter.observe();
00034         if (!cur_node) // list is empty
00035             found = true;
00036         else {
00037             const ordered_node *cur_onode = dynamic_cast<const ordered_node*>(cur_node);
00038             if (!cur_onode)
00039                 return false;
00040 
00041             int cmp = cur_onode -> compare(*new_node, compare_mode);
00042             if (cmp < 0)
00043                 found = true;
00044             else if (cmp == 0 && !duplicates) {
00045                 delete new_node;    // I own it and I'm not going to insert it.
00046                 return true;        // don't insert duplicate
00047             } else
00048                 iter++;
00049         }
00050     }
00051     list::insert(iter, new_node);
00052     return true;
00053 }
00054 
00055 
00056 const ordered_node *ordered_list::find( const ordered_node *to_find, 
00057                                         int compare_mode,
00058                                         list::iterator *use_iter) const
00059 {
00060 // Attempts to find a match for "to_find" and returns a pointer to it if successful.
00061 // "compare_mode" will be passed to ordered_node::compare() while searching.
00062 // If "use_iter" is non-null, search will occur using the provided iterator and
00063 // the returned node will be the closest one found to the iterator's current position.
00064 // Otherwise, search begins at the head.
00065 
00066     if (!to_find)
00067         return NULL;
00068 
00069     iterator iter = head();            
00070     if (!use_iter)
00071         use_iter = &iter;
00072 
00073     while (true) {
00074         
00075         const node *cur_node = use_iter -> observe();
00076         if (!cur_node)
00077             return NULL; // list is empty.            
00078         
00079         const ordered_node *cur_onode = dynamic_cast<const ordered_node*>(cur_node);
00080         if (!cur_onode)
00081             return NULL; // cast failed.
00082 
00083         int cmp = cur_onode -> compare(*to_find, compare_mode);
00084 
00085         if (cmp == 0)
00086             return cur_onode;
00087 
00088         if (cmp < 0) {
00089             (*use_iter)--;
00090             if (use_iter -> is_head())
00091                 return NULL;
00092         } else {
00093             (*use_iter)++;
00094             if (use_iter -> is_tail())
00095                 return NULL;
00096         }
00097     }
00098 }
00099 
00100 } // namespace.
00101 
00102 #endif //ORDERED_LIST_IMPLEMENTATION_FILE
00103 

Generated on Fri Nov 28 04:29:17 2008 for HOOPLE Libraries by  doxygen 1.5.1