source: golgotha/src/i4/inc/isllist.hh

Last change on this file was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 2.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9//{{{ Intrusive singly linked list
10//
11//  Notes:
12//    The templatized class MUST have member:
13//      T* next;
14//
15
16#ifndef ISLLIST_HPP
17#define ISLLIST_HPP
18#include "arch.hh"      // for definition of i4_bool
19
20template <class T>
21class i4_isl_list
22{
23protected:
24  typedef T* link;
25  link list;
26
27public:
28  T *first() { return list; }
29  i4_isl_list() : list(0) {}
30
31  class iterator
32  {
33    friend class i4_isl_list<T>;
34  protected:
35    link node;
36
37  public:
38    iterator() : node(0) {}
39    iterator(const iterator &p) : node(p.node) {}
40    iterator(link p) : node(p) {}
41
42    int operator==(const iterator &p) const { return (node == p.node); }
43    int operator!=(const iterator &p) const { return (node != p.node); }
44   
45    T& operator*() const { return *node; }
46    T* operator->() const { return node; }
47
48    iterator& operator++()
49    {
50      node = node->next;
51      return *this;
52    }
53
54    iterator& operator++(int)
55    {
56      node = node->next;
57      return *this;
58    }
59  };
60 
61  iterator end()   const { return 0; }
62  iterator begin() const { return list; }
63
64  i4_bool empty() const { return begin() == end(); }
65
66  iterator insert_after(iterator _pos, T &item)
67  {
68    item.next = (*_pos.node).next;
69    _pos.node->next = &item;
70
71    return &item;
72  }
73  void erase_after(iterator _pos)
74  {
75    _pos.node->next = _pos.node->next->next;
76  }
77 
78  void insert_end(T &item)
79  {
80    item.next=0;
81    if (!list)
82      list=&item;
83    else
84    {
85      link last=list;
86      while (last->next)
87        last=last->next;
88      last->next=&item;
89    }   
90  }
91 
92  void insert(T& item) { item.next = list; list = &item; }
93  void erase() { list = list->next; }
94
95  void destroy_all()
96  {
97    link p;
98   
99    while (list)
100    {
101      p = list;
102      erase();
103      delete p;
104    }
105  }
106 
107  iterator find(T *item)
108  {
109    iterator p=begin();
110    for (;p!=end();++p)
111      if (p.node==item)
112        return p;
113    return end();
114  }
115 
116  void swap(i4_isl_list &other)
117  {
118    link other_list=other.list;
119    other.list=list;
120    list=other_list;
121  }
122 
123  T *find_and_unlink(T *item)
124  {
125    iterator p=begin(), last=end();
126    for (;p!=end(); ++p)
127      if (p.node==item)
128      {     
129        if (last!=end())
130          erase_after(last);
131        else
132          erase();   
133   
134        return item;
135      }
136      else last=p;
137    return 0;
138  }
139};
140
141#endif
142
Note: See TracBrowser for help on using the repository browser.