Share some code that is common between integer and
[oota-llvm.git] / lib / Support / FoldingSet.cpp
index 753eb240be80bd11bef43b5ab40899ee76b3e408..80140438714d99284d247802e6d39d7653b4f4ca 100644 (file)
@@ -15,9 +15,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/FoldingSet.h"
-#include "llvm/ADT/APFloat.h"
 #include "llvm/Support/MathExtras.h"
 #include <cassert>
+#include <cstring>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -57,14 +57,8 @@ void FoldingSetNodeID::AddFloat(float F) {
 void FoldingSetNodeID::AddDouble(double D) {
  AddInteger(DoubleToBits(D));
 }
-void FoldingSetNodeID::AddAPFloat(const APFloat& apf) {
-  APInt api = apf.convertToAPInt();
-  const uint64_t *p = api.getRawData();
-  for (unsigned i=0; i<api.getNumWords(); i++)
-    AddInteger(*p++);
-}
 void FoldingSetNodeID::AddString(const std::string &String) {
-  unsigned Size = String.size();
+  unsigned Size = static_cast<unsigned>(String.size());
   Bits.push_back(Size);
   if (!Size) return;
 
@@ -104,7 +98,7 @@ void FoldingSetNodeID::AddString(const std::string &String) {
 /// lookup the node in the FoldingSetImpl.
 unsigned FoldingSetNodeID::ComputeHash() const {
   // This is adapted from SuperFastHash by Paul Hsieh.
-  unsigned Hash = Bits.size();
+  unsigned Hash = static_cast<unsigned>(Bits.size());
   for (const unsigned *BP = &Bits[0], *E = BP+Bits.size(); BP != E; ++BP) {
     unsigned Data = *BP;
     Hash         += Data & 0xFFFF;
@@ -148,7 +142,7 @@ static FoldingSetImpl::Node *GetNextPtr(void *NextInBucketPtr) {
   return static_cast<FoldingSetImpl::Node*>(NextInBucketPtr);
 }
 
-/// GetBucketPtr - Provides a casting of a bucket pointer for isNode
+
 /// testing.
 static void **GetBucketPtr(void *NextInBucketPtr) {
   intptr_t Ptr = reinterpret_cast<intptr_t>(NextInBucketPtr);
@@ -176,7 +170,7 @@ FoldingSetImpl::FoldingSetImpl(unsigned Log2InitSize) : NumNodes(0) {
   memset(Buckets, 0, NumBuckets*sizeof(void*));
   
   // Set the very last bucket to be a non-null "pointer".
-  Buckets[NumBuckets] = reinterpret_cast<void*>(-2);
+  Buckets[NumBuckets] = reinterpret_cast<void*>(-1);
 }
 FoldingSetImpl::~FoldingSetImpl() {
   delete [] Buckets;
@@ -221,8 +215,10 @@ void FoldingSetImpl::GrowHashTable() {
 /// FindNodeOrInsertPos - Look up the node specified by ID.  If it exists,
 /// return it.  If not, return the insertion token that will make insertion
 /// faster.
-FoldingSetImpl::Node *FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
-                                                          void *&InsertPos) {
+FoldingSetImpl::Node
+*FoldingSetImpl::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
+                                     void *&InsertPos) {
+  
   void **Bucket = GetBucketFor(ID, Buckets, NumBuckets);
   void *Probe = *Bucket;
   
@@ -331,7 +327,8 @@ FoldingSetImpl::Node *FoldingSetImpl::GetOrInsertNode(FoldingSetImpl::Node *N) {
 
 FoldingSetIteratorImpl::FoldingSetIteratorImpl(void **Bucket) {
   // Skip to the first non-null non-self-cycle bucket.
-  while (*Bucket == 0 || GetNextPtr(*Bucket) == 0)
+  while (*Bucket != reinterpret_cast<void*>(-1) &&
+         (*Bucket == 0 || GetNextPtr(*Bucket) == 0))
     ++Bucket;
   
   NodePtr = static_cast<FoldingSetNode*>(*Bucket);
@@ -350,9 +347,16 @@ void FoldingSetIteratorImpl::advance() {
     // Skip to the next non-null non-self-cycle bucket.
     do {
       ++Bucket;
-    } while (*Bucket == 0 || GetNextPtr(*Bucket) == 0);
+    } while (*Bucket != reinterpret_cast<void*>(-1) &&
+             (*Bucket == 0 || GetNextPtr(*Bucket) == 0));
     
     NodePtr = static_cast<FoldingSetNode*>(*Bucket);
   }
 }
 
+//===----------------------------------------------------------------------===//
+// FoldingSetBucketIteratorImpl Implementation
+
+FoldingSetBucketIteratorImpl::FoldingSetBucketIteratorImpl(void **Bucket) {
+  Ptr = (*Bucket == 0 || GetNextPtr(*Bucket) == 0) ? (void*) Bucket : *Bucket;
+}