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