Usermode/libc++ - system_error and vector implementation
[tpg/acess2.git] / Usermode / Libraries / libc++.so_src / include_exp / list
1 /*
2  * Acess2 C++ Library
3  * - By John Hodge (thePowersGang)
4  *
5  * list (header)
6  * - List container
7  */
8 #ifndef _LIBCXX_LIST_
9 #define _LIBCXX_LIST_
10
11 #include <cstddef>
12 #include "allocator"
13
14 namespace std {
15
16 namespace _bits {
17 template <class T,class Alloc> class list_iterator;
18 template <class T> class list_item;
19 }
20
21 template <class T, class Alloc = allocator<T> >
22 class list
23 {
24         typedef ::std::_bits::list_item<T>      item_type;
25         typedef ::std::_bits::list_item<const T>        const_item_type;
26         friend class ::std::_bits::list_iterator<item_type,Alloc>;
27         
28         typedef typename Alloc::template rebind<item_type>::other       item_allocator;
29 public:
30         typedef T value_type;
31         typedef Alloc   allocator_type;
32         typedef typename allocator_type::reference      reference;
33         typedef typename allocator_type::const_reference        const_reference;
34         typedef typename allocator_type::pointer        pointer;
35         typedef typename allocator_type::const_pointer  const_pointer;
36         typedef _bits::list_iterator<T,Alloc>   iterator;
37         typedef _bits::list_iterator<const T,Alloc>     const_iterator;
38         typedef int     difference_type;
39         typedef size_t  size_type;
40
41 private:
42         item_allocator  m_item_allocator;
43         allocator_type  m_allocator;
44         item_type       *m_start;
45         item_type       *m_end;
46
47 public:
48         list(const allocator_type& alloc = allocator_type()):
49                 m_item_allocator(),
50                 m_allocator(alloc),
51                 m_start(0), m_end(0)
52         {
53         }
54         list(size_t n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()):
55                 list()
56         {
57                 assign(n, val);
58         }
59         list(const list& x);
60         ~list() {
61                 clear();
62         }
63         
64         list& operator =(const list& x);
65         
66         iterator begin() {
67                 return iterator(m_start);
68         }
69         const_iterator begin() const {
70                 return const_iterator(m_start);
71         }
72
73         iterator end() {
74                 return iterator(0);
75         }
76         const_iterator end() const {
77                 return const_iterator(0);
78         }
79         
80         bool empty() const {
81                 return !m_start;
82         }
83         size_t size() const;
84         size_t max_size() const;
85         
86         T& front();
87         const T& front() const;
88         T& back();
89         const T& back() const;
90         
91         void assign(size_type n, const value_type& val) {
92                 clear();
93                 for( size_t i = 0; i < n; i ++ )
94                 {
95                         push_back(val);
96                 }
97         }
98         
99         void push_front(const value_type& val) {
100                 insert(front(), val);
101         }
102         void pop_front() {
103                 erase(front());
104         }
105         void push_back(const value_type& val) {
106                 insert(end(), val);
107         }
108         void pop_back() {
109                 erase(end());
110         }
111         
112         iterator insert(iterator position, const value_type& val) {
113                 item_type *newi = m_item_allocator.allocate(1);
114                 m_allocator.construct(&newi->value, val);
115                 if( position == end() ) {
116                         newi->next = 0;
117                         newi->prev = m_end;
118                         m_end->next = newi;
119                         m_end = newi;
120                 }
121                 else if( position == begin() ) {
122                         newi->next = m_start;
123                         newi->prev = 0;
124                         m_start->prev = newi;
125                         m_start = newi;
126                 }
127                 else {
128                         newi->prev = position.cur->prev;
129                         newi->next = position.cur;
130                         position.cur->prev->next = newi;
131                         position.cur->prev = newi;
132                 }
133                 return ++position;
134         }
135         void insert(iterator position, size_type n, const value_type& val) {
136                 for( size_type i = 0; i < n; i ++ )
137                 {
138                         position = insert(position, val);
139                 }
140         }
141         iterator erase(iterator position) {
142                 if( position == end() ) {
143                 }
144                 else {
145                         item_type *oldi = position.cur;
146                         position ++;
147                         
148                         if(oldi->prev)
149                                 oldi->prev->next = oldi->next;
150                         else
151                                 m_start = oldi->next;
152                         if(oldi->next)
153                                 oldi->next->prev = oldi->prev;
154                         else
155                                 m_end = oldi->prev;
156                         
157                         m_allocator.destroy(&oldi->value);
158                         m_item_allocator.deallocate(oldi, 1);
159                 }
160                 return position;
161         }
162         
163         void clear() {
164                 while( m_start ) {
165                         item_type* item = m_start;
166                         m_start = m_start->next;
167                         delete item;
168                 }
169         }
170 };
171
172
173 namespace _bits {
174
175 template <class T>
176 struct list_item
177 {
178         typedef T       value_type;
179         list_item<T>    *next;
180         list_item<T>    *prev;
181         value_type      value;
182 };
183
184 template <class T, class Alloc>
185 class list_iterator//:
186         //public bidirectional_iterator_tag;
187 {
188         list_item<T>    *cur;
189         friend class ::std::list<T, Alloc>;
190 public:
191         bool operator == (const list_iterator& other) const {
192                 return cur == other.cur;
193         }
194         bool operator != (const list_iterator& other) const {
195                 return cur != other.cur;
196         }
197         
198         T& operator * () {
199                 return cur->value;
200         }
201         T& operator -> () {
202                 return cur->value;
203         }
204         list_iterator& operator ++ () {
205                 cur = cur->next;
206                 return *this;
207         }
208         list_iterator& operator -- () {
209                 cur = cur->prev;
210                 return *this;
211         }
212
213 private:
214         list_iterator(list_item<T> *item):
215                 cur(item)
216         {
217         }
218 };
219
220 };      // namespace _bits
221
222 };      // namespace std
223
224 #endif
225
226 // vim: ft=cpp
227

UCC git Repository :: git.ucc.asn.au