00001 // LICENSE: (Please see the file COPYING for details) 00002 // 00003 // NUS - Nemesis Utilities System: A C++ application development framework 00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske 00005 // 00006 // This file is part of NUS 00007 // 00008 // This library is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU Lesser General Public 00010 // License as published by the Free Software Foundation version 2.1 00011 // of the License. 00012 // 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // Lesser General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU Lesser General Public 00019 // License along with this library; if not, write to the Free Software 00020 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 // 00022 #ifndef NSORTEDLIST_HPP 00023 #define NSORTEDLIST_HPP 00024 00040 template <typename T> 00041 class NSortedList: public NList<T> { 00042 public: 00046 NSortedList(void); 00047 00052 NSortedList(const T &val); 00053 00058 NSortedList(const NSortedList<T> &other); 00059 00065 NSortedList<T> &insert(const T &val); 00066 00067 private: 00068 NSortedList<T> &append(const T &val); 00069 00078 NListNode<T> *locate(const T &val); 00079 }; 00080 00081 00082 template <typename T> 00083 NSortedList<T>::NSortedList(void) 00084 : NList<T>() 00085 { 00086 00087 } 00088 00089 00090 template <typename T> 00091 NSortedList<T>::NSortedList(const T &val) 00092 : NList<T>(val) 00093 { 00094 00095 } 00096 00097 00098 template <typename T> 00099 NSortedList<T>::NSortedList(const NSortedList<T> &other) 00100 : NList<T>(other) 00101 { 00102 copy(other); 00103 } 00104 00105 00106 template <typename T> 00107 NListNode<T> *NSortedList<T>::locate(const T &val) { 00108 NList<T>::gotoFirst(); 00109 00110 while (NList<T>::m_node && NList<T>::m_node->next 00111 && NList<T>::m_node->data < val) 00112 { 00113 NList<T>::m_node = NList<T>::m_node->next; 00114 } 00115 00116 if (NList<T>::m_node) { 00117 return NList<T>::m_node->previous; 00118 } 00119 00120 return NList<T>::m_node; 00121 } 00122 00123 00124 template <typename T> 00125 NSortedList<T> &NSortedList<T>::insert(const T &val) { 00126 NListNode<T> *tmp = NULL; 00127 00128 if (contains(val)) { 00129 return *this; 00130 } 00131 00132 NList<T>::m_node = locate(val); 00133 if (NList<T>::m_node) { 00134 tmp = new NListNode<T>; 00135 tmp->data = val; 00136 00137 tmp->previous = NList<T>::m_node; 00138 tmp->next = NList<T>::m_node->next; 00139 00140 if (NList<T>::m_node->next) { 00141 NList<T>::m_node->next->previous = tmp; 00142 } 00143 NList<T>::m_node->next = tmp; 00144 00145 } 00146 else { 00147 // There's no smaller item than val. 00148 tmp = new NListNode<T>; 00149 tmp->data = val; 00150 tmp->next = NList<T>::m_first; 00151 00152 // If there's something in the list, fix it. 00153 if (NList<T>::m_first) { 00154 NList<T>::m_first->previous = tmp; 00155 } 00156 NList<T>::m_first = tmp; 00157 NList<T>::m_node = NList<T>::m_first; 00158 } 00159 00160 #ifdef NEXTRADEBUG 00161 std::cout << "Node: " << (void *) NList<T>::m_node << std::endl; 00162 std::cout << "Previous: " << (void *) NList<T>::m_node->previous << std::endl; 00163 std::cout << "Next: " << (void *) NList<T>::m_node->next << std::endl; 00164 #endif 00165 00166 NList<T>::m_count++; 00167 return *this; 00168 } 00169 00170 00171 #endif // NSORTEDLIST_HPP