00001 #ifndef ORDERED_LIST_IMPLEMENTATION_FILE
00002 #define ORDERED_LIST_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00024
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)
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;
00046 return true;
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
00061
00062
00063
00064
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;
00078
00079 const ordered_node *cur_onode = dynamic_cast<const ordered_node*>(cur_node);
00080 if (!cur_onode)
00081 return NULL;
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 }
00101
00102 #endif //ORDERED_LIST_IMPLEMENTATION_FILE
00103