src/InlineList.h
author David Anderson <dvander@alliedmods.net>
Sat Jul 28 22:03:38 2012 -0700 (2012-07-28)
changeset 121 06bf112c6944
parent 86 32ad0523b309
child 123 a97d0e5a5817
permissions -rw-r--r--
trigger build
     1 /*
     2  * Copyright (C) 2004-2012 David Anderson
     3  *
     4  * This file is part of SourcePawn.
     5  *
     6  * SourcePawn is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_ke_inline_list_h_
    19 #define _include_ke_inline_list_h_
    20 
    21 #include <stddef.h>
    22 
    23 namespace ke {
    24 
    25 template <typename T> class InlineList;
    26 
    27 template <typename T>
    28 class InlineListNode
    29 {
    30     friend class InlineList<T>;
    31 
    32   public:
    33     InlineListNode()
    34       : next_(NULL),
    35         prev_(NULL)
    36     {
    37     }
    38 
    39     InlineListNode(InlineListNode *next, InlineListNode *prev)
    40       : next_(next),
    41         prev_(prev)
    42     {
    43     }
    44 
    45   protected:
    46     InlineListNode *next_;
    47     InlineListNode *prev_;
    48 };
    49 
    50 template <typename T>
    51 class InlineList
    52 {
    53     typedef InlineListNode<T> Node;
    54 
    55     Node head_;
    56 
    57   public:
    58     InlineList()
    59       : head_(&head_, &head_)
    60     {
    61     }
    62 
    63   public:
    64     class iterator
    65     {
    66         friend class InlineList;
    67         Node *iter_;
    68         
    69       public:
    70         iterator(Node *iter)
    71           : iter_(iter)
    72         {
    73         }
    74 
    75         iterator & operator++() {
    76             iter_ = iter_->next_;
    77             return *this;
    78         }
    79         iterator operator ++(int) {
    80             iterator old(*this);
    81             iter_ = iter_->next_;
    82             return old;
    83         }
    84         T * operator *() {
    85             return static_cast<T *>(iter_);
    86         }
    87         T * operator ->() {
    88             return static_cast<T *>(iter_);
    89         }
    90         bool operator !=(const iterator &where) const {
    91             return iter_ != where.iter_;
    92         }
    93         bool operator ==(const iterator &where) const {
    94             return iter_ == where.iter_;
    95         }
    96         iterator prev() const {
    97             return iterator(iter_->prev_);
    98         }
    99         iterator next() const {
   100             return iterator(iter_->next_);
   101         }
   102     };
   103 
   104     iterator begin() {
   105         return iterator(head_.next_);
   106     }
   107     iterator end() {
   108         return iterator(&head_);
   109     }
   110     void append(Node *t) {
   111         t->prev_ = head_.prev_;
   112         t->next_ = &head_;
   113         head_.prev_->next_ = t;
   114         head_.prev_ = t;
   115     }
   116     iterator erase(iterator i) {
   117         iterator next = ++i;
   118         remove(i.iter_);
   119         return next;
   120     }
   121     void remove(Node *t) {
   122         t->prev_->next_ = t->next_;
   123         t->next_->prev_ = t->prev_;
   124     }
   125 };
   126 
   127 }
   128 
   129 #endif // _include_ke_inline_list_h_