blob: 48552a56fc4731ffe2d3dbf2a46bfeea70632d9e [file] [log] [blame]
Chris Lattnerc95dc982007-01-27 07:10:46 +00001//===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner24757de2007-01-27 07:18:32 +000010// This file implements the SmallPtrSet class. See SmallPtrSet.h for an
11// overview of the algorithm.
Chris Lattnerc95dc982007-01-27 07:10:46 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/SmallPtrSet.h"
16using namespace llvm;
17
18bool SmallPtrSetImpl::insert(void *Ptr) {
19 if (isSmall()) {
20 // Check to see if it is already in the set.
21 for (void **APtr = SmallArray, **E = SmallArray+NumElements;
22 APtr != E; ++APtr)
23 if (*APtr == Ptr)
24 return false;
25
26 // Nope, there isn't. If we stay small, just 'pushback' now.
27 if (NumElements < CurArraySize-1) {
28 SmallArray[NumElements++] = Ptr;
29 return true;
30 }
31 // Otherwise, hit the big set case, which will call grow.
32 }
33
34 // If more than 3/4 of the array is full, grow.
35 if (NumElements*4 >= CurArraySize*3)
36 Grow();
37
38 // Okay, we know we have space. Find a hash bucket.
39 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
40 if (*Bucket == Ptr) return false; // Already inserted, good.
41
42 // Otherwise, insert it!
43 *Bucket = Ptr;
44 ++NumElements; // Track density.
45 return true;
46}
47
48void * const *SmallPtrSetImpl::FindBucketFor(void *Ptr) const {
49 unsigned Bucket = Hash(Ptr);
50 unsigned ArraySize = CurArraySize;
51 unsigned ProbeAmt = 1;
52 void *const *Array = CurArray;
53 void *const *Tombstone = 0;
54 while (1) {
55 // Found Ptr's bucket?
56 if (Array[Bucket] == Ptr)
57 return Array+Bucket;
58
59 // If we found an empty bucket, the pointer doesn't exist in the set.
60 // Return a tombstone if we've seen one so far, or the empty bucket if
61 // not.
62 if (Array[Bucket] == getEmptyMarker())
63 return Tombstone ? Tombstone : Array+Bucket;
64
65 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
66 // prefer to return it than something that would require more probing.
67 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
68 Tombstone = Array+Bucket; // Remember the first tombstone found.
69
70 // It's a hash collision or a tombstone. Reprobe.
71 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
72 }
73}
74
75/// Grow - Allocate a larger backing store for the buckets and move it over.
76///
77void SmallPtrSetImpl::Grow() {
78 // Allocate at twice as many buckets, but at least 128.
79 unsigned OldSize = CurArraySize;
80 unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
81
82 void **OldBuckets = CurArray;
83 bool WasSmall = isSmall();
84
85 // Install the new array. Clear all the buckets to empty.
86 CurArray = new void*[NewSize+1];
87 CurArraySize = NewSize;
88 memset(CurArray, -1, NewSize*sizeof(void*));
89
90 // The end pointer, always valid, is set to a valid element to help the
91 // iterator.
92 CurArray[NewSize] = 0;
93
94 // Copy over all the elements.
95 if (WasSmall) {
96 // Small sets store their elements in order.
97 for (void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
98 BucketPtr != E; ++BucketPtr) {
99 void *Elt = *BucketPtr;
100 *const_cast<void**>(FindBucketFor(Elt)) = Elt;
101 }
102 } else {
103 // Copy over all valid entries.
104 for (void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
105 BucketPtr != E; ++BucketPtr) {
106 // Copy over the element if it is valid.
107 void *Elt = *BucketPtr;
108 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
109 *const_cast<void**>(FindBucketFor(Elt)) = Elt;
110 }
111
112 delete [] OldBuckets;
113 }
114}