1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/InlineList.h Tue Jul 24 23:53:34 2012 -0700
1.3 @@ -0,0 +1,129 @@
1.4 +/*
1.5 + * Copyright (C) 2004-2012 David Anderson
1.6 + *
1.7 + * This file is part of SourcePawn.
1.8 + *
1.9 + * SourcePawn is free software: you can redistribute it and/or modify it under
1.10 + * the terms of the GNU General Public License as published by the Free
1.11 + * Software Foundation, either version 3 of the License, or (at your option)
1.12 + * any later version.
1.13 + *
1.14 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License along with
1.19 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
1.20 + */
1.21 +#ifndef _include_ke_inline_list_h_
1.22 +#define _include_ke_inline_list_h_
1.23 +
1.24 +#include <stddef.h>
1.25 +
1.26 +namespace ke {
1.27 +
1.28 +template <typename T> class InlineList;
1.29 +
1.30 +template <typename T>
1.31 +class InlineListNode
1.32 +{
1.33 + friend class InlineList<T>;
1.34 +
1.35 + public:
1.36 + InlineListNode()
1.37 + : next_(NULL),
1.38 + prev_(NULL)
1.39 + {
1.40 + }
1.41 +
1.42 + InlineListNode(InlineListNode *next, InlineListNode *prev)
1.43 + : next_(next),
1.44 + prev_(prev)
1.45 + {
1.46 + }
1.47 +
1.48 + protected:
1.49 + InlineListNode *next_;
1.50 + InlineListNode *prev_;
1.51 +};
1.52 +
1.53 +template <typename T>
1.54 +class InlineList
1.55 +{
1.56 + typedef InlineListNode<T> Node;
1.57 +
1.58 + Node head_;
1.59 +
1.60 + public:
1.61 + InlineList()
1.62 + : head_(&head_, &head_)
1.63 + {
1.64 + }
1.65 +
1.66 + public:
1.67 + class iterator
1.68 + {
1.69 + friend class InlineList;
1.70 + Node *iter_;
1.71 +
1.72 + public:
1.73 + iterator(Node *iter)
1.74 + : iter_(iter)
1.75 + {
1.76 + }
1.77 +
1.78 + iterator & operator++() {
1.79 + iter_ = iter_->next_;
1.80 + return *this;
1.81 + }
1.82 + iterator operator ++(int) {
1.83 + iterator old(*this);
1.84 + iter_ = iter_->next_;
1.85 + return old;
1.86 + }
1.87 + T * operator *() {
1.88 + return static_cast<T *>(iter_);
1.89 + }
1.90 + T * operator ->() {
1.91 + return static_cast<T *>(iter_);
1.92 + }
1.93 + bool operator !=(const iterator &where) const {
1.94 + return iter_ != where.iter_;
1.95 + }
1.96 + bool operator ==(const iterator &where) const {
1.97 + return iter_ == where.iter_;
1.98 + }
1.99 + iterator prev() const {
1.100 + return iterator(iter_->prev_);
1.101 + }
1.102 + iterator next() const {
1.103 + return iterator(iter_->next_);
1.104 + }
1.105 + };
1.106 +
1.107 + iterator begin() {
1.108 + return iterator(head_.next_);
1.109 + }
1.110 + iterator end() {
1.111 + return iterator(&head_);
1.112 + }
1.113 + void append(Node *t) {
1.114 + t->prev_ = head_.prev_;
1.115 + t->next_ = &head_;
1.116 + head_.prev_->next_ = t;
1.117 + head_.prev_ = t;
1.118 + }
1.119 + iterator erase(iterator i) {
1.120 + iterator next = ++i;
1.121 + remove(i.iter_);
1.122 + return next;
1.123 + }
1.124 + void remove(Node *t) {
1.125 + t->prev_->next_ = t->next_;
1.126 + t->next_->prev_ = t->prev_;
1.127 + }
1.128 +};
1.129 +
1.130 +}
1.131 +
1.132 +#endif // _include_ke_inline_list_h_