include/am-inlinelist.h
changeset 0 ba3d4abaf3f1
child 11 cfe800908837
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/am-inlinelist.h	Fri Aug 23 20:34:17 2013 -0700
     1.3 @@ -0,0 +1,142 @@
     1.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
     1.5 +//
     1.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
     1.7 +// All rights reserved.
     1.8 +// 
     1.9 +// Redistribution and use in source and binary forms, with or without
    1.10 +// modification, are permitted provided that the following conditions are met:
    1.11 +// 
    1.12 +//  * Redistributions of source code must retain the above copyright notice, this
    1.13 +//    list of conditions and the following disclaimer.
    1.14 +//  * Redistributions in binary form must reproduce the above copyright notice,
    1.15 +//    this list of conditions and the following disclaimer in the documentation
    1.16 +//    and/or other materials provided with the distribution.
    1.17 +//  * Neither the name of AlliedModders LLC nor the names of its contributors
    1.18 +//    may be used to endorse or promote products derived from this software
    1.19 +//    without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.31 +// POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +#ifndef _include_amtl_inline_list_h_
    1.34 +#define _include_amtl_inline_list_h_
    1.35 +
    1.36 +#include <stddef.h>
    1.37 +
    1.38 +namespace ke {
    1.39 +
    1.40 +template <typename T> class InlineList;
    1.41 +
    1.42 +template <typename T>
    1.43 +class InlineListNode
    1.44 +{
    1.45 +  friend class InlineList<T>;
    1.46 +
    1.47 +  public:
    1.48 +  InlineListNode()
    1.49 +    : next_(NULL),
    1.50 +    prev_(NULL)
    1.51 +  {
    1.52 +  }
    1.53 +
    1.54 +  InlineListNode(InlineListNode *next, InlineListNode *prev)
    1.55 +    : next_(next),
    1.56 +    prev_(prev)
    1.57 +  {
    1.58 +  }
    1.59 +
    1.60 +  protected:
    1.61 +  InlineListNode *next_;
    1.62 +  InlineListNode *prev_;
    1.63 +};
    1.64 +
    1.65 +template <typename T>
    1.66 +class InlineList
    1.67 +{
    1.68 +  typedef InlineListNode<T> Node;
    1.69 +
    1.70 +  Node head_;
    1.71 +
    1.72 + public:
    1.73 +  InlineList()
    1.74 +    : head_(&head_, &head_)
    1.75 +  {
    1.76 +  }
    1.77 +
    1.78 + public:
    1.79 +  class iterator
    1.80 +  {
    1.81 +    friend class InlineList;
    1.82 +    Node *iter_;
    1.83 +
    1.84 +   public:
    1.85 +    iterator(Node *iter)
    1.86 +      : iter_(iter)
    1.87 +    {
    1.88 +    }
    1.89 +
    1.90 +    iterator & operator ++() {
    1.91 +      iter_ = iter_->next;
    1.92 +      return *iter_;
    1.93 +    }
    1.94 +    iterator operator ++(int) {
    1.95 +      iterator old(*this);
    1.96 +      iter_ = iter_->next_;
    1.97 +      return old;
    1.98 +    }
    1.99 +    T * operator *() {
   1.100 +      return static_cast<T *>(iter_);
   1.101 +    }
   1.102 +    T * operator ->() {
   1.103 +      return static_cast<T *>(iter_);
   1.104 +    }
   1.105 +    bool operator !=(const iterator &where) const {
   1.106 +      return iter_ != where.iter_;
   1.107 +    }
   1.108 +    bool operator ==(const iterator &where) const {
   1.109 +      return iter_ == where.iter_;
   1.110 +    }
   1.111 +    iterator prev() const {
   1.112 +      iterator p(iter_->prev_);
   1.113 +      return p;
   1.114 +    }
   1.115 +    iterator next() const {
   1.116 +      iterator p(iter_->next_);
   1.117 +      return p;
   1.118 +    }
   1.119 +  };
   1.120 +
   1.121 +  iterator begin() {
   1.122 +    return iterator(head_.next_);
   1.123 +  }
   1.124 +
   1.125 +  iterator end() {
   1.126 +    return iterator(&head_);
   1.127 +  }
   1.128 +
   1.129 +  void erase(Node *t) {
   1.130 +    t->prev_->next_ = t->next_;
   1.131 +    t->next_->prev_ = t->prev_;
   1.132 +  }
   1.133 +
   1.134 +  void insert(Node *t) {
   1.135 +    t->prev_ = head_.prev_;
   1.136 +    t->next_ = &head_;
   1.137 +    head_.prev_->next_ = t;
   1.138 +    head_.prev_ = t;
   1.139 +  }
   1.140 +};
   1.141 +
   1.142 +}
   1.143 +
   1.144 +#endif // _include_amtl_inline_list_h_
   1.145 +