ImutAVLTree now allocates tree nodes from the BumpPtrAllocator using
[oota-llvm.git] / include / llvm / ADT / FoldingSet.h
index 6b0463365a223b5fdfc4b8070f52b08d9a614c00..c567895594b5663f19a1c0d6794fdce5f308877f 100644 (file)
 #ifndef LLVM_ADT_FOLDINGSET_H
 #define LLVM_ADT_FOLDINGSET_H
 
+#include "llvm/Support/DataTypes.h"
 #include "llvm/ADT/SmallVector.h"
+#include <string>
 
 namespace llvm {
+  class APFloat;
 
 /// This folding set used for two purposes:
 ///   1. Given information about a node we want to create, look up the unique
@@ -112,12 +115,12 @@ private:
   ///
   unsigned NumBuckets;
   
-  /// NumNodes - Number of nodes in the folding set.  Growth occurs when NumNodes
+  /// NumNodes - Number of nodes in the folding set. Growth occurs when NumNodes
   /// is greater than twice the number of buckets.
   unsigned NumNodes;
   
 public:
-  FoldingSetImpl();
+  explicit FoldingSetImpl(unsigned Log2InitSize = 6);
   virtual ~FoldingSetImpl();
   
   // Forward declaration.
@@ -147,9 +150,11 @@ public:
     void AddPointer(const void *Ptr);
     void AddInteger(signed I);
     void AddInteger(unsigned I);
+    void AddInteger(int64_t I);
     void AddInteger(uint64_t I);
     void AddFloat(float F);
     void AddDouble(double D);
+    void AddAPFloat(const APFloat& apf);
     void AddString(const std::string &String);
     
     /// ComputeHash - Compute a strong hash value for this NodeID, used to 
@@ -215,20 +220,35 @@ protected:
 typedef FoldingSetImpl::Node FoldingSetNode;
 typedef FoldingSetImpl::NodeID FoldingSetNodeID;
 
-//===--------------------------------------------------------------------===//
+template<class T> class FoldingSetIterator;
+
+//===----------------------------------------------------------------------===//
 /// FoldingSet - This template class is used to instantiate a specialized
 /// implementation of the folding set to the node class T.  T must be a 
 /// subclass of FoldingSetNode and implement a Profile function.
 ///
 template<class T> class FoldingSet : public FoldingSetImpl {
 private:
-  /// GetNodeProfile - Each instantiatation of the FoldingSet 
+  /// GetNodeProfile - Each instantiatation of the FoldingSet needs to provide a
+  /// way to convert nodes into a unique specifier.
   virtual void GetNodeProfile(NodeID &ID, Node *N) const {
     T *TN = static_cast<T *>(N);
     TN->Profile(ID);
   }
   
 public:
+  explicit FoldingSet(unsigned Log2InitSize = 6)
+  : FoldingSetImpl(Log2InitSize)
+  {}
+  
+  typedef FoldingSetIterator<T> iterator;
+  iterator begin() { return iterator(Buckets); }
+  iterator end() { return iterator(Buckets+NumBuckets); }
+
+  typedef FoldingSetIterator<const T> const_iterator;
+  const_iterator begin() const { return const_iterator(Buckets); }
+  const_iterator end() const { return const_iterator(Buckets+NumBuckets); }
+
   /// GetOrInsertNode - If there is an existing simple Node exactly
   /// equal to the specified node, return it.  Otherwise, insert 'N' and
   /// return it instead.
@@ -240,8 +260,48 @@ public:
   /// return it.  If not, return the insertion token that will make insertion
   /// faster.
   T *FindNodeOrInsertPos(const FoldingSetNodeID &ID, void *&InsertPos) {
-    return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID,
-                                                                InsertPos));
+    return static_cast<T *>(FoldingSetImpl::FindNodeOrInsertPos(ID, InsertPos));
+  }
+};
+
+//===----------------------------------------------------------------------===//
+/// FoldingSetIteratorImpl - This is the common iterator support shared by all
+/// folding sets, which knows how to walk the folding set hash table.
+class FoldingSetIteratorImpl {
+protected:
+  FoldingSetNode *NodePtr;
+  FoldingSetIteratorImpl(void **Bucket);
+  void advance();
+  
+public:
+  bool operator==(const FoldingSetIteratorImpl &RHS) const {
+    return NodePtr == RHS.NodePtr;
+  }
+  bool operator!=(const FoldingSetIteratorImpl &RHS) const {
+    return NodePtr != RHS.NodePtr;
+  }
+};
+
+
+template<class T>
+class FoldingSetIterator : public FoldingSetIteratorImpl {
+public:
+  FoldingSetIterator(void **Bucket) : FoldingSetIteratorImpl(Bucket) {}
+  
+  T &operator*() const {
+    return *static_cast<T*>(NodePtr);
+  }
+  
+  T *operator->() const {
+    return static_cast<T*>(NodePtr);
+  }
+  
+  inline FoldingSetIterator& operator++() {          // Preincrement
+    advance();
+    return *this;
+  }
+  FoldingSetIterator operator++(int) {        // Postincrement
+    FoldingSetIterator tmp = *this; ++*this; return tmp;
   }
 };