list.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: list.h,v 1.23 2004/04/22 10:08:19 oliver Exp $
00005 //
00006 
00007 #ifndef BALL_DATATYPE_LIST_H
00008 #define BALL_DATATYPE_LIST_H
00009 
00010 #ifndef BALL_COMMON_H
00011 # include <BALL/common.h>
00012 #endif
00013 
00014 #ifndef BALL_CONCEPT_VISITOR_H
00015 # include <BALL/CONCEPT/visitor.h>
00016 #endif
00017 
00018 #ifndef BALL_CONCEPT_PROCESSOR_H
00019 # include <BALL/CONCEPT/processor.h>
00020 #endif
00021 
00022 #include <list>
00023 
00024 namespace BALL 
00025 {
00031   template <typename Value>
00032   class List
00033     : public std::list<Value>
00034   {
00035     public:
00036 
00040 
00043     typedef typename std::list<Value>::iterator Iterator;
00044     // for STL compatibility
00045     typedef typename std::list<Value>::iterator iterator;
00046 
00049     typedef typename std::list<Value>::const_iterator ConstIterator;
00050     // for STL compatibility
00051     typedef typename std::list<Value>::const_iterator const_iterator;
00052 
00054 
00056 
00057     BALL_CREATE_DEEP(List)
00058 
00059     
00062     List()
00063       : std::list<Value>()
00064     {
00065     }
00066 
00072     List(const List& new_list, bool /* deep = true */)
00073       : std::list<Value>(new_list)
00074     {
00075     }
00076       
00079     void destroy()
00080     {
00081       std::list<Value>::clear();
00082     }
00083   
00086     virtual ~List()
00087     {
00088       std::list<Value>::clear();
00089     }
00090 
00092 
00094       
00099     void set(const List& list, bool /* deep */ = true)
00100     {
00101       std::list<Value>::clear();
00102 
00103       ConstIterator it = list.begin();
00104       for ( ; it != list.end(); ++it)
00105       {
00106         std::list<Value>::push_back(const_cast<Value&>(*it));
00107       }
00108     }
00109 
00112     const List& operator = (const List& list)
00113     {
00114       set(list);
00115       return *this;
00116     }
00117       
00119     void get(List& list, bool deep = true) const 
00120     {
00121       list.set(*this, deep);
00122     }
00123 
00125     void swap(List& list)
00126     {
00127       List<Value> temp;
00128       temp.set(*this);
00129       (*this).set(list);
00130       list.set(temp);
00131     }
00132 
00134 
00136 
00139     Size getSize() const
00140     {
00141       return (Size)std::list<Value>::size();
00142     }
00143 
00148     bool remove(const Value& item)
00149     {
00150       Iterator it = std::list<Value>::begin();
00151       for (; it != std::list<Value>::end(); ++it)
00152       {
00153         if (*it == item)
00154         {
00155           std::list<Value>::erase(it);
00156           return true;
00157         }
00158       }
00159       return false;
00160     }
00161     
00163 
00165 
00169     bool isEmpty() const
00170     {
00171       return (std::list<Value>::size() == 0);
00172     }
00173 
00175 
00177 
00182     virtual void host(Visitor<List<Value> >& visitor);
00183 
00185 
00187 
00192     bool apply(UnaryProcessor<Value>& processor)
00193     {
00194       if (!processor.start()) return false;
00195 
00196       for (Iterator it = std::list<Value>::begin(); it != std::list<Value>::end(); ++it)
00197       {
00198         Processor::Result result = processor(*it);
00199         if (result <= Processor::BREAK)
00200         {
00201           return (result == Processor::BREAK);
00202         }
00203       }
00204 
00205       return processor.finish();
00206     }
00207 
00209 
00213     bool operator == (const List<Value>& list) const
00214     {
00215       if (std::list<Value>::size() != list.size())
00216       {
00217         return false;
00218       }
00219 
00220       typename List<Value>::ConstIterator this_it = std::list<Value>::begin();
00221       typename List<Value>::ConstIterator list_it = list.begin();
00222 
00223       for (; this_it != std::list<Value>::end(); ++this_it)
00224       {
00225         if (!(*this_it == *list_it))
00226         {
00227           return false;
00228         }
00229         ++list_it;
00230       }
00231       return true;  
00232     }
00233 
00237     bool operator != (const List<Value>& list) const
00238     {
00239       return !(*this == list);
00240     }
00241 
00242   };
00243 
00244   template <typename Value>
00245   void List<Value>::host(Visitor<List<Value> >& visitor)
00246   {
00247     visitor.visit(*this);
00248   }
00249 } // namespace BALL
00250 
00251 #endif // BALL_DATATYPE_LIST_H