Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inline_slist.h
Go to the documentation of this file.
1 // Copyright 2012 Google Inc.
20 
21 #ifndef PAGESPEED_KERNEL_BASE_INLINE_SLIST_H_
22 #define PAGESPEED_KERNEL_BASE_INLINE_SLIST_H_
23 
24 #include <cstddef>
25 
26 #include "base/logging.h"
28 
29 namespace net_instaweb {
30 
33 template<class T> class InlineSList;
34 
38 template<class T>
40  protected:
41  InlineSListElement() : next_(NULL) {}
42 
43  private:
44  friend class InlineSList<T>;
45  T* next() { return next_; }
46  void set_next(T* new_next) { next_ = new_next; }
47 
48  T* next_;
49 
50 };
51 
65 template<class T>
66 class InlineSList {
67  private:
78  class IterBase {
79  protected:
80  IterBase(const InlineSList<T>* list, T* node)
81  : list_(list), node_(node) {
82  }
83 
84  bool AtEnd() const {
85  return (node_ == NULL);
86  }
87 
88  void Advance() {
89  DCHECK(!AtEnd());
90  node_ = node_->next();
93  if (node_ == list_->tail_) {
94  node_ = NULL;
95  }
96  }
97 
98  T* Data() {
99  return node_->next();
100  }
101 
102  bool Equals(const IterBase& other) const {
103  return (node_ == other.node_) && (list_ == other.list_);
104  }
105 
106  private:
107  friend class InlineSList<T>;
108  const InlineSList<T>* list_;
109  T* node_;
110  };
111 
112  public:
134  class Iterator : public IterBase {
135  public:
136  Iterator& operator++() {
137  this->Advance();
138  return *this;
139  }
140 
141  T* Get() { return this->Data(); }
142  T* operator->() { return this->Data(); }
143  T& operator*() { return *this->Data(); }
144  bool operator==(const Iterator& other) const { return this->Equals(other); }
145  bool operator!=(const Iterator& other) const {
146  return !this->Equals(other);
147  }
149 
150  private:
151  friend class InlineSList<T>;
152  Iterator(const InlineSList<T>* list, T* prev) : IterBase(list, prev) {}
153  };
154 
155  typedef Iterator iterator;
156 
159  class ConstIterator : public IterBase {
160  public:
161  ConstIterator& operator++() {
162  this->Advance();
163  return *this;
164  }
165 
166  const T* Get() { return this->Data(); }
167  const T* operator->() { return this->Data(); }
168  const T& operator*() { return *this->Data(); }
169  bool operator==(const ConstIterator& other) const {
170  return this->Equals(other);
171  }
172  bool operator!=(const ConstIterator& other) const {
173  return !this->Equals(other);
174  }
176 
177  private:
178  friend class InlineSList<T>;
179  ConstIterator(const InlineSList<T>* list, T* prev) : IterBase(list, prev) {}
180  };
181 
182  typedef ConstIterator const_iterator;
183 
184  InlineSList() : tail_(NULL) {
185  }
186 
188  ~InlineSList();
189 
190  bool IsEmpty() const {
191  return (tail_ == NULL);
192  }
193 
194  void Append(T* node);
195 
201  void Erase(Iterator* iter);
202 
204  T* Last() {
205  DCHECK(!IsEmpty());
206  return tail_;
207  }
208 
209  const T* Last() const {
210  DCHECK(!IsEmpty());
211  return tail_;
212  }
213 
215 
218  iterator begin() { return Iterator(this, tail_); }
219  const_iterator begin() const { return ConstIterator(this, tail_); }
220 
222  iterator end() { return Iterator(this, NULL); }
223  const_iterator end() const { return ConstIterator(this, NULL); }
224 
225  private:
229  T* tail_;
230 
231 
232 };
233 
234 template<class T>
236  if (tail_ != NULL) {
237  T* node = tail_->next();
238  while (true) {
239  T* next = node->next();
240  delete node;
241  if (node == tail_) {
242  break;
243  } else {
244  node = next;
245  }
246  }
247  }
248  tail_ = NULL;
249 }
250 
251 template<class T>
252 inline void InlineSList<T>::Append(T* node) {
253  if (tail_ == NULL) {
254  tail_ = node;
255  node->set_next(node);
256  } else {
257  node->set_next(tail_->next());
258  tail_->set_next(node);
259  tail_ = node;
260  }
261 }
262 
263 template<class T>
264 inline void InlineSList<T>::Erase(Iterator* iter) {
265  DCHECK(!iter->AtEnd());
266 
267  T* iter_node = iter->node_;
268  T* target_node = iter_node->next();
269 
270  if (iter_node == target_node) {
272  tail_ = NULL;
273  iter->node_ = NULL;
274  } else {
275  iter_node->set_next(target_node->next());
276  if (target_node == tail_) {
278  tail_ = iter_node;
280  iter->node_ = NULL;
281  }
282  }
283  delete target_node;
284 }
285 
286 }
287 
288 #endif
T * Last()
Returns last item.
Definition: inline_slist.h:204
Definition: inline_slist.h:33
Definition: inline_slist.h:39
~InlineSList()
The destructor deletes all the nodes in the list.
Definition: inline_slist.h:235
void Erase(Iterator *iter)
Definition: inline_slist.h:264
iterator begin()
Iterator interface.
Definition: inline_slist.h:218
Definition: inline_slist.h:134
iterator end()
End iterators have their position at NULL.
Definition: inline_slist.h:222
Definition: inline_slist.h:159