00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef RAUL_LIST_HPP
00019 #define RAUL_LIST_HPP
00020
00021 #include <cstddef>
00022 #include <cassert>
00023 #include <raul/Deletable.hpp>
00024 #include <raul/AtomicPtr.hpp>
00025 #include <raul/AtomicInt.hpp>
00026
00027 namespace Raul {
00028
00029
00036 template <typename T>
00037 class List : public Raul::Deletable
00038 {
00039 public:
00040
00047 class Node : public Raul::Deletable {
00048 public:
00049 Node(T elem) : _elem(elem) {}
00050 virtual ~Node() {}
00051
00052 template <typename Y>
00053 Node(const typename List<Y>::Node& copy)
00054 : _elem(copy._elem), _prev(copy._prev), _next(copy._next)
00055 {}
00056
00057 Node* prev() const { return _prev.get(); }
00058 void prev(Node* ln) { _prev = ln; }
00059 Node* next() const { return _next.get(); }
00060 void next(Node* ln) { _next = ln; }
00061 T& elem() { return _elem;}
00062 const T& elem() const { return _elem; }
00063
00064 private:
00065 T _elem;
00066 AtomicPtr<Node> _prev;
00067 AtomicPtr<Node> _next;
00068 };
00069
00070
00071 List() : _size(0), _end_iter(this), _const_end_iter(this) {
00072 _end_iter._listnode = NULL;
00073 _const_end_iter._listnode = NULL;
00074 }
00075
00076 ~List();
00077
00078 void push_back(Node* elem);
00079 void push_back(T& elem);
00080
00081 void append(List<T>& list);
00082
00083 void clear();
00084
00086 unsigned size() const { return (unsigned)_size.get(); }
00087
00089 bool empty() { return (_head.get() == NULL); }
00090
00091 class iterator;
00092
00094 class const_iterator {
00095 public:
00096 const_iterator(const List<T>* const list);
00097 const_iterator(const iterator& i)
00098 : _list(i._list), _listnode(i._listnode) {}
00099
00100 inline const T& operator*();
00101 inline const T* operator->();
00102 inline const_iterator& operator++();
00103 inline bool operator!=(const const_iterator& iter) const;
00104 inline bool operator!=(const iterator& iter) const;
00105 inline bool operator==(const const_iterator& iter) const;
00106 inline bool operator==(const iterator& iter) const;
00107
00108 friend class List<T>;
00109
00110 private:
00111 const List<T>* _list;
00112 const typename List<T>::Node* _listnode;
00113 };
00114
00115
00117 class iterator {
00118 public:
00119 iterator(List<T>* const list);
00120
00121 inline T& operator*();
00122 inline T* operator->();
00123 inline iterator& operator++();
00124 inline bool operator!=(const iterator& iter) const;
00125 inline bool operator!=(const const_iterator& iter) const;
00126 inline bool operator==(const iterator& iter) const;
00127 inline bool operator==(const const_iterator& iter) const;
00128
00129 friend class List<T>;
00130 friend class List<T>::const_iterator;
00131
00132 private:
00133 const List<T>* _list;
00134 typename List<T>::Node* _listnode;
00135 };
00136
00137
00138 Node* erase(const iterator iter);
00139
00140 iterator find(const T& val);
00141
00142 iterator begin();
00143 const_iterator begin() const;
00144 const iterator end() const;
00145
00146 private:
00147 AtomicPtr<Node> _head;
00148 AtomicPtr<Node> _tail;
00149 AtomicInt _size;
00150 iterator _end_iter;
00151 const_iterator _const_end_iter;
00152 };
00153
00154
00155 }
00156
00157 #include "ListImpl.hpp"
00158
00159 #endif