-//===-- Support/ilist - Intrusive Linked List Template ----------*- C++ -*-===//
+//===-- llvm/ADT/ilist - Intrusive Linked List Template ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines classes to implement an intrusive doubly linked list class
-// (ie each node of the list must contain a next and previous field for the
+// (i.e. each node of the list must contain a next and previous field for the
// list.
//
// The ilist_traits trait class is used to gain access to the next and previous
//
// The ilist class itself, should be a plug in replacement for list, assuming
// that the nodes contain next/prev pointers. This list replacement does not
-// provides a constant time size() method, so be careful to use empty() when you
+// provide a constant time size() method, so be careful to use empty() when you
// really want to know if it's empty.
//
// The ilist class is implemented by allocating a 'tail' node when the list is
-// created (using ilist_traits<>::createEndMarker()). This tail node is
+// created (using ilist_traits<>::createSentinel()). This tail node is
// absolutely required because the user must be able to compute end()-1. Because
// of this, users of the direct next/prev links will see an extra link on the
// end of the list, which should be ignored.
//
//===----------------------------------------------------------------------===//
-#ifndef SUPPORT_ILIST
-#define SUPPORT_ILIST
+#ifndef LLVM_ADT_ILIST
+#define LLVM_ADT_ILIST
-#include <Support/iterator>
+#include "llvm/ADT/iterator"
#include <cassert>
+#include <cstdlib>
+
+namespace llvm {
template<typename NodeTy, typename Traits> class iplist;
template<typename NodeTy> class ilist_iterator;
static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
- static NodeTy *createNode() { return new NodeTy(); }
static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
+ static NodeTy *createSentinel() { return new NodeTy(); }
+ static void destroySentinel(NodeTy *N) { delete N; }
void addNodeToList(NodeTy *NTy) {}
void removeNodeFromList(NodeTy *NTy) {}
// Increment and decrement operators...
ilist_iterator &operator--() { // predecrement - Back up
NodePtr = Traits::getPrev(NodePtr);
- assert(NodePtr && "--'d off the beginning of an ilist!");
+ assert(Traits::getNext(NodePtr) && "--'d off the beginning of an ilist!");
return *this;
}
ilist_iterator &operator++() { // preincrement - Advance
return tmp;
}
-
- // Dummy operators to make errors apparent...
- template<class X> void operator+(X Val) {}
- template<class X> void operator-(X Val) {}
-
// Internal interface, do not use...
pointer getNodePtrUnchecked() const { return NodePtr; }
};
+// do not implement. this is to catch errors when people try to use
+// them as random access iterators
+template<typename T>
+void operator-(int, ilist_iterator<T>);
+template<typename T>
+void operator-(ilist_iterator<T>,int);
+
+template<typename T>
+void operator+(int, ilist_iterator<T>);
+template<typename T>
+void operator+(ilist_iterator<T>,int);
+
+// operator!=/operator== - Allow mixed comparisons without dereferencing
+// the iterator, which could very likely be pointing to end().
+template<typename T>
+bool operator!=(const T* LHS, const ilist_iterator<const T> &RHS) {
+ return LHS != RHS.getNodePtrUnchecked();
+}
+template<typename T>
+bool operator==(const T* LHS, const ilist_iterator<const T> &RHS) {
+ return LHS == RHS.getNodePtrUnchecked();
+}
+template<typename T>
+bool operator!=(T* LHS, const ilist_iterator<T> &RHS) {
+ return LHS != RHS.getNodePtrUnchecked();
+}
+template<typename T>
+bool operator==(T* LHS, const ilist_iterator<T> &RHS) {
+ return LHS == RHS.getNodePtrUnchecked();
+}
+
+
// Allow ilist_iterators to convert into pointers to a node automatically when
// used by the dyn_cast, cast, isa mechanisms...
//===----------------------------------------------------------------------===//
//
-// iplist - The subset of list functionality that can safely be used on nodes of
-// polymorphic types, ie a heterogeneus list with a common base class that holds
-// the next/prev pointers...
-//
+/// iplist - The subset of list functionality that can safely be used on nodes
+/// of polymorphic types, i.e. a heterogenous list with a common base class that
+/// holds the next/prev pointers. The only state of the list itself is a single
+/// pointer to the head of the list.
+///
+/// This list can be in one of three interesting states:
+/// 1. The list may be completely unconstructed. In this case, the head
+/// pointer is null. When in this form, any query for an iterator (e.g.
+/// begin() or end()) causes the list to transparently change to state #2.
+/// 2. The list may be empty, but contain a sentinal for the end iterator. This
+/// sentinal is created by the Traits::createSentinel method and is a link
+/// in the list. When the list is empty, the pointer in the iplist points
+/// to the sentinal. Once the sentinal is constructed, it
+/// is not destroyed until the list is.
+/// 3. The list may contain actual objects in it, which are stored as a doubly
+/// linked list of nodes. One invariant of the list is that the predecessor
+/// of the first node in the list always points to the last node in the list,
+/// and the successor pointer for the sentinal (which always stays at the
+/// end of the list) is always null.
+///
template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
class iplist : public Traits {
- NodeTy *Head, *Tail;
+ mutable NodeTy *Head;
+
+ // Use the prev node pointer of 'head' as the tail pointer. This is really a
+ // circularly linked list where we snip the 'next' link from the sentinel node
+ // back to the first node in the list (to preserve assertions about going off
+ // the end of the list).
+ NodeTy *getTail() { return getPrev(Head); }
+ const NodeTy *getTail() const { return getPrev(Head); }
+ void setTail(NodeTy *N) const { setPrev(Head, N); }
+
+ /// CreateLazySentinal - This method verifies whether the sentinal for the
+ /// list has been created and lazily makes it if not.
+ void CreateLazySentinal() const {
+ if (Head != 0) return;
+ Head = Traits::createSentinel();
+ setNext(Head, 0);
+ setTail(Head);
+ }
static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
- iplist() : Head(this->createNode()), Tail(Head) {
- setNext(Head, 0);
- setPrev(Head, 0);
+ iplist() : Head(0) {}
+ ~iplist() {
+ if (!Head) return;
+ clear();
+ Traits::destroySentinel(getTail());
}
- ~iplist() { clear(); delete Tail; }
- // Iterator creation methods...
- iterator begin() { return iterator(Head); }
- const_iterator begin() const { return const_iterator(Head); }
- iterator end() { return iterator(Tail); }
- const_iterator end() const { return const_iterator(Tail); }
+ // Iterator creation methods.
+ iterator begin() {
+ CreateLazySentinal();
+ return iterator(Head);
+ }
+ const_iterator begin() const {
+ CreateLazySentinal();
+ return const_iterator(Head);
+ }
+ iterator end() {
+ CreateLazySentinal();
+ return iterator(getTail());
+ }
+ const_iterator end() const {
+ CreateLazySentinal();
+ return const_iterator(getTail());
+ }
- // reverse iterator creation methods...
+ // reverse iterator creation methods.
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
- const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
+ const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
+
- // Miscellaneous inspection routines...
+ // Miscellaneous inspection routines.
size_type max_size() const { return size_type(-1); }
- bool empty() const { return Head == Tail; }
+ bool empty() const { return Head == 0 || Head == getTail(); }
// Front and back accessor functions...
reference front() {
}
reference back() {
assert(!empty() && "Called back() on empty list!");
- return *getPrev(Tail);
+ return *getPrev(getTail());
}
const_reference back() const {
assert(!empty() && "Called back() on empty list!");
- return *getPrev(Tail);
+ return *getPrev(getTail());
}
void swap(iplist &RHS) {
abort(); // Swap does not use list traits callback correctly yet!
std::swap(Head, RHS.Head);
- std::swap(Tail, RHS.Tail);
}
iterator insert(iterator where, NodeTy *New) {
setNext(New, CurNode);
setPrev(New, PrevNode);
- if (PrevNode)
+ if (CurNode != Head) // Is PrevNode off the beginning of the list?
setNext(PrevNode, New);
else
Head = New;
NodeTy *NextNode = getNext(Node);
NodeTy *PrevNode = getPrev(Node);
- if (PrevNode)
+ if (Node != Head) // Is PrevNode off the beginning of the list?
setNext(PrevNode, NextNode);
else
Head = NextNode;
setPrev(NextNode, PrevNode);
IT = NextNode;
- removeNodeFromList(Node); // Notify traits that we added a node...
+ removeNodeFromList(Node); // Notify traits that we removed a node...
+
+ // Set the next/prev pointers of the current node to null. This isn't
+ // strictly required, but this catches errors where a node is removed from
+ // an ilist (and potentially deleted) with iterators still pointing at it.
+ // When those iterators are incremented or decremented, they will assert on
+ // the null next/prev pointer instead of "usually working".
+ setNext(Node, 0);
+ setPrev(Node, 0);
return Node;
}
//
void transfer(iterator position, iplist &L2, iterator first, iterator last) {
assert(first != last && "Should be checked by callers");
+
if (position != last) {
+ // Note: we have to be careful about the case when we move the first node
+ // in the list. This node is the list sentinel node and we can't move it.
+ NodeTy *ThisSentinel = getTail();
+ setTail(0);
+ NodeTy *L2Sentinel = L2.getTail();
+ L2.setTail(0);
+
// Remove [first, last) from its old position.
NodeTy *First = &*first, *Prev = getPrev(First);
NodeTy *Next = last.getNodePtrUnchecked(), *Last = getPrev(Next);
setPrev(PosNext, Last);
transferNodesFromList(L2, First, PosNext);
+
+ // Now that everything is set, restore the pointers to the list sentinals.
+ L2.setTail(L2Sentinel);
+ setTail(ThisSentinel);
}
}
//
size_type size() const {
-#if __GNUC__ == 3
- size_type Result = std::distance(begin(), end());
-#else
+ if (Head == 0) return 0; // Don't require construction of sentinal if empty.
+#if __GNUC__ == 2
+ // GCC 2.95 has a broken std::distance
size_type Result = 0;
std::distance(begin(), end(), Result);
-#endif
return Result;
+#else
+ return std::distance(begin(), end());
+#endif
}
iterator erase(iterator first, iterator last) {
return last;
}
- void clear() { erase(begin(), end()); }
+ void clear() { if (Head) erase(begin(), end()); }
// Front and back inserters...
void push_front(NodeTy *val) { insert(begin(), val); }
void resize(size_type newsize) { resize(newsize, NodeTy()); }
};
+} // End llvm namespace
+
namespace std {
// Ensure that swap uses the fast list swap...
template<class Ty>
- void swap(iplist<Ty> &Left, iplist<Ty> &Right) {
+ void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) {
Left.swap(Right);
}
} // End 'std' extensions...