summaryrefslogtreecommitdiff
path: root/include/llvm/ADT/alist.h
blob: 1af3e4a5c0c9d75aa1f3bce391b4f2153a93e686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//==- llvm/ADT/alist.h - Linked lists with hooks -----------------*- C++ -*-==//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the alist class template, and related infrastructure.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ADT_ALIST_H
#define LLVM_ADT_ALIST_H

#include "llvm/ADT/alist_node.h"
#include "llvm/ADT/STLExtras.h"

namespace llvm {

/// alist_iterator - An iterator class for alist.
///
template<class T, class LargestT = T, class ValueT = T,
         class NodeIterT = ilist_iterator<alist_node<T, LargestT> > >
class alist_iterator : public bidirectional_iterator<ValueT, ptrdiff_t> {
public:
  typedef bidirectional_iterator<ValueT, ptrdiff_t> super;
  typedef alist_node<T, LargestT> NodeTy;

private:
  /// NodeIter - The underlying iplist iterator that is being wrapped.
  NodeIterT NodeIter;

public:
  typedef size_t size_type;

  // FIX for MSVC++.  This should be reviewed more.
  // typedef typename super::pointer pointer;
  typedef ValueT* pointer;

  typedef typename super::reference reference;

  alist_iterator(NodeIterT NI) : NodeIter(NI) {}
  alist_iterator(pointer EP) : NodeIter(NodeTy::getNode(EP)) {}
  alist_iterator() : NodeIter() {}

  // This is templated so that we can allow constructing a const iterator from
  // a nonconst iterator...
  template<class V, class W, class X, class Y>
  alist_iterator(const alist_iterator<V, W, X, Y> &RHS)
    : NodeIter(RHS.getNodeIterUnchecked()) {}

  // This is templated so that we can allow assigning to a const iterator from
  // a nonconst iterator...
  template<class V, class W, class X, class Y>
  const alist_iterator &operator=(const alist_iterator<V, W, X, Y> &RHS) {
    NodeIter = RHS.getNodeIterUnchecked();
    return *this;
  }

  operator pointer() const { return NodeIter->getElement((T*)0); }

  reference operator*() const { return *NodeIter->getElement((T*)0); }
  pointer   operator->() const { return &operator*(); }

  bool operator==(const alist_iterator &RHS) const {
    return NodeIter == RHS.NodeIter;
  }
  bool operator!=(const alist_iterator &RHS) const {
    return NodeIter != RHS.NodeIter;
  }

  alist_iterator &operator--() {
    --NodeIter;
    return *this;
  }
  alist_iterator &operator++() {
    ++NodeIter;
    return *this;
  }
  alist_iterator operator--(int) {
    alist_iterator tmp = *this;
    --*this;
    return tmp;
  }
  alist_iterator operator++(int) {
    alist_iterator tmp = *this;
    ++*this;
    return tmp;
  }

  NodeIterT getNodeIterUnchecked() const { return NodeIter; }
};

// do not implement. this is to catch errors when people try to use
// them as random access iterators
template<class T, class LargestT, class ValueT, class NodeIterT>
void operator-(int, alist_iterator<T, LargestT, ValueT, NodeIterT>);
template<class T, class LargestT, class ValueT, class NodeIterT>
void operator-(alist_iterator<T, LargestT, ValueT, NodeIterT>,int);

template<class T, class LargestT, class ValueT, class NodeIterT>
void operator+(int, alist_iterator<T, LargestT, ValueT, NodeIterT>);
template<class T, class LargestT, class ValueT, class NodeIterT>
void operator+(alist_iterator<T, LargestT, ValueT, NodeIterT>,int);

// operator!=/operator== - Allow mixed comparisons without dereferencing
// the iterator, which could very likely be pointing to end().
template<class T, class V, class W, class X, class Y>
bool operator!=(T* LHS, const alist_iterator<V, W, X, Y> &RHS) {
  return LHS != RHS.getNodeIterUnchecked().getNodePtrUnchecked()
                                                            ->getElement((T*)0);
}
template<class T, class V, class W, class X, class Y>
bool operator==(T* LHS, const alist_iterator<V, W, X, Y> &RHS) {
  return LHS == RHS.getNodeIterUnchecked().getNodePtrUnchecked()
                                                            ->getElement((T*)0);
}

// Allow alist_iterators to convert into pointers to a node automatically when
// used by the dyn_cast, cast, isa mechanisms...

template<class From> struct simplify_type;

template<class V, class W, class X, class Y>
struct simplify_type<alist_iterator<V, W, X, Y> > {
  typedef alist_node<V, W> NodeTy;
  typedef NodeTy* SimpleType;

  static SimpleType
  getSimplifiedValue(const alist_iterator<V, W, X, Y> &Node) {
    return &*Node;
  }
};
template<class V, class W, class X, class Y>
struct simplify_type<const alist_iterator<V, W, X, Y> > {
  typedef alist_node<V, W> NodeTy;
  typedef NodeTy* SimpleType;

  static SimpleType
  getSimplifiedValue(const alist_iterator<V, W, X, Y> &Node) {
    return &*Node;
  }
};

/// Template traits for alist.  By specializing this template class, you
/// can register custom actions to be run when a node is added to or removed
/// from an alist. A common use of this is to update parent pointers.
///
template<class T, class LargestT = T>
class alist_traits {
public:
  typedef alist_iterator<T, LargestT> iterator;

  void addNodeToList(T *) {}
  void removeNodeFromList(T *) {}
  void transferNodesFromList(alist_traits &, iterator, iterator) {}
  void deleteNode(T *E) { delete alist_node<T, LargestT>::getNode(E); }
};

/// alist - This class is an ilist-style container that automatically
/// adds the next/prev pointers. It is designed to work in cooperation
/// with <llvm/Support/Recycler.h>.
///
template<class T, class LargestT = T>
class alist {
public:
  typedef alist_node<T, LargestT> NodeTy;
  typedef typename ilist<NodeTy>::size_type size_type;

private:
  /// NodeListTraits - ilist traits for NodeList.
  ///
  struct NodeListTraits : ilist_traits<alist_node<T, LargestT> > {
    alist_traits<T, LargestT> UserTraits;

    void addNodeToList(NodeTy *N) {
      UserTraits.addNodeToList(N->getElement((T*)0));
    }
    void removeNodeFromList(NodeTy *N) {
      UserTraits.removeNodeFromList(N->getElement((T*)0));
    }
    void transferNodesFromList(iplist<NodeTy, NodeListTraits> &L2,
                               ilist_iterator<NodeTy> first,
                               ilist_iterator<NodeTy> last) {
      UserTraits.transferNodesFromList(L2.UserTraits,
                                       iterator(first),
                                       iterator(last));
    }
  };

  /// NodeList - Doubly-linked list of nodes that have constructed
  /// contents and may be in active use.
  ///
  iplist<NodeTy, NodeListTraits> NodeList;

public:
  ~alist() { clear(); }

  typedef alist_iterator<T, LargestT, T, ilist_iterator<NodeTy> >
    iterator;
  typedef alist_iterator<T, LargestT, const T, ilist_iterator<const NodeTy> >
    const_iterator;
  typedef std::reverse_iterator<iterator> reverse_iterator;
  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

  iterator begin() { return iterator(NodeList.begin()); }
  iterator end() { return iterator(NodeList.end()); }
  const_iterator begin() const { return const_iterator(NodeList.begin()); }
  const_iterator end() const { return const_iterator(NodeList.end()); }
  reverse_iterator rbegin() { return reverse_iterator(NodeList.rbegin()); }
  reverse_iterator rend() { return reverse_iterator(NodeList.rend()); }
  const_reverse_iterator rbegin() const {
    return const_reverse_iterator(NodeList.rbegin());
  }
  const_reverse_iterator rend() const {
    return const_reverse_iterator(NodeList.rend());
  }

  typedef T& reference;
  typedef const T& const_reference;
  reference front() { return *NodeList.front().getElement((T*)0); }
  reference back()  { return *NodeList.back().getElement((T*)0); }
  const_reference front() const { return *NodeList.front().getElement((T*)0); }
  const_reference back()  const { return *NodeList.back().getElement((T*)0); }

  bool empty() const { return NodeList.empty(); }
  size_type size() const { return NodeList.size(); }

  void push_front(T *E) {
    NodeTy *N = alist_node<T, LargestT>::getNode(E);
    assert(N->getPrev() == 0);
    assert(N->getNext() == 0);
    NodeList.push_front(N);
  }
  void push_back(T *E) {
    NodeTy *N = alist_node<T, LargestT>::getNode(E);
    assert(N->getPrev() == 0);
    assert(N->getNext() == 0);
    NodeList.push_back(N);
  }
  iterator insert(iterator I, T *E) {
    NodeTy *N = alist_node<T, LargestT>::getNode(E);
    assert(N->getPrev() == 0);
    assert(N->getNext() == 0);
    return iterator(NodeList.insert(I.getNodeIterUnchecked(), N));
  }
  void splice(iterator where, alist &Other) {
    NodeList.splice(where.getNodeIterUnchecked(), Other.NodeList);
  }
  void splice(iterator where, alist &Other, iterator From) {
    NodeList.splice(where.getNodeIterUnchecked(), Other.NodeList,
                    From.getNodeIterUnchecked());
  }
  void splice(iterator where, alist &Other, iterator From,
              iterator To) {
    NodeList.splice(where.getNodeIterUnchecked(), Other.NodeList,
                    From.getNodeIterUnchecked(), To.getNodeIterUnchecked());
  }

  void pop_front() {
    erase(NodeList.begin());
  }
  void pop_back() {
    erase(prior(NodeList.end()));
  }
  iterator erase(iterator I) {
    iterator Next = next(I);
    NodeTy *N = NodeList.remove(I.getNodeIterUnchecked());
    NodeList.UserTraits.deleteNode(N->getElement((T*)0));
    return Next;
  }
  iterator erase(iterator first, iterator last) {
    while (first != last)
      first = erase(first);
    return last;
  }

  T *remove(T *E) {
    NodeTy *N = alist_node<T, LargestT>::getNode(E);
    return NodeList.remove(N)->getElement((T*)0);
  }

  void clear() {
    while (!empty()) pop_front();
  }

  alist_traits<T, LargestT> &getTraits() {
    return NodeList.UserTraits;
  }
};

}

#endif