2
Copyright (C) 2000-2007 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; version 2 of the License.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License
14
along with this program; see the file COPYING. If not, write to the
15
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20
/* mySTL list implements a simple list
24
#ifndef mySTL_LIST_HPP
25
#define mySTL_LIST_HPP
28
#include "helpers.hpp"
40
Sun Forte 7 C++ v. 5.4 needs class 'node' public to be visible to
41
the nested class 'iterator' (a non-standard behaviour).
47
node(T t) : prev_(0), next_(0), value_(t) {}
54
list() : head_(0), tail_(0), sz_(0) {}
64
size_t size() const { return sz_; }
65
bool empty() const { return sz_ == 0; }
70
explicit iterator(node* p = 0) : current_(p) {}
74
return current_->value_;
79
return &(operator*());
82
iterator& operator++()
84
current_ = current_->next_;
88
iterator& operator--()
90
current_ = current_->prev_;
94
iterator operator++(int)
97
current_ = current_->next_;
101
iterator operator--(int)
103
iterator tmp = *this;
104
current_ = current_->prev_;
108
bool operator==(const iterator& other) const
110
return current_ == other.current_;
113
bool operator!=(const iterator& other) const
115
return current_ != other.current_;
118
friend class list<T>;
122
class reverse_iterator {
125
explicit reverse_iterator(node* p = 0) : current_(p) {}
129
return current_->value_;
132
T* operator->() const
134
return &(operator*());
137
reverse_iterator& operator++()
139
current_ = current_->prev_;
143
reverse_iterator& operator--()
145
current_ = current_->next_;
149
reverse_iterator operator++(int)
151
reverse_iterator tmp = *this;
152
current_ = current_->prev_;
156
reverse_iterator operator--(int)
158
reverse_iterator tmp = *this;
159
current_ = current_->next_;
163
bool operator==(const reverse_iterator& other) const
165
return current_ == other.current_;
168
bool operator!=(const reverse_iterator& other) const
170
return current_ != other.current_;
173
friend class list<T>;
176
bool erase(iterator);
178
iterator begin() const { return iterator(head_); }
179
reverse_iterator rbegin() const { return reverse_iterator(tail_); }
180
iterator end() const { return iterator(); }
181
reverse_iterator rend() const { return reverse_iterator(); }
183
typedef iterator const_iterator; // for now
194
list(const list&); // hide copy
195
list& operator=(const list&); // and assign
205
for (; start; start = next_) {
206
next_ = start->next_;
214
void list<T>::push_front(T t)
216
void* mem = GetMemory(sizeof(node));
217
node* add = new (reinterpret_cast<yassl_pointer>(mem)) node(t);
232
void list<T>::pop_front()
238
else if (head_ == tail_)
241
head_ = head_->next_;
251
T list<T>::front() const
253
if (head_ == 0) return T();
254
return head_->value_;
259
void list<T>::push_back(T t)
261
void* mem = GetMemory(sizeof(node));
262
node* add = new (reinterpret_cast<yassl_pointer>(mem)) node(t);
277
void list<T>::pop_back()
283
else if (tail_ == head_)
286
tail_ = tail_->prev_;
296
T list<T>::back() const
298
if (tail_ == 0) return T();
299
return tail_->value_;
304
typename list<T>::node* list<T>::look_up(T t)
308
if (list == 0) return 0;
310
for (; list; list = list->next_)
311
if (list->value_ == t)
319
bool list<T>::remove(T t)
321
node* del = look_up(t);
325
else if (del == head_)
327
else if (del == tail_)
330
del->prev_->next_ = del->next_;
331
del->next_->prev_ = del->prev_;
342
bool list<T>::erase(iterator iter)
344
node* del = iter.current_;
348
else if (del == head_)
350
else if (del == tail_)
353
del->prev_->next_ = del->next_;
354
del->next_->prev_ = del->prev_;
367
#endif // mySTL_LIST_HPP