[llvm-commits] [llvm] r135365 - in /llvm/trunk: docs/ProgrammersManual.html include/llvm/ADT/TinyPtrVector.h

Chris Lattner sabre at nondot.org
Sun Jul 17 18:40:02 PDT 2011


Author: lattner
Date: Sun Jul 17 20:40:02 2011
New Revision: 135365

URL: http://llvm.org/viewvc/llvm-project?rev=135365&view=rev
Log:
introduce a new TinyPtrVector class.

Added:
    llvm/trunk/include/llvm/ADT/TinyPtrVector.h
Modified:
    llvm/trunk/docs/ProgrammersManual.html

Modified: llvm/trunk/docs/ProgrammersManual.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/ProgrammersManual.html?rev=135365&r1=135364&r2=135365&view=diff
==============================================================================
--- llvm/trunk/docs/ProgrammersManual.html (original)
+++ llvm/trunk/docs/ProgrammersManual.html Sun Jul 17 20:40:02 2011
@@ -59,6 +59,7 @@
       <li><a href="#dss_arrayref">llvm/ADT/ArrayRef.h</a></li>
       <li><a href="#dss_fixedarrays">Fixed Size Arrays</a></li>
       <li><a href="#dss_heaparrays">Heap Allocated Arrays</a></li>
+      <li><a href="#dss_tinyptrvector">"llvm/ADT/TinyPtrVector.h"</a></li>
       <li><a href="#dss_smallvector">"llvm/ADT/SmallVector.h"</a></li>
       <li><a href="#dss_vector"><vector></a></li>
       <li><a href="#dss_deque"><deque></a></li>
@@ -928,6 +929,24 @@
 
 <!-- _______________________________________________________________________ -->
 <h4>
+  <a name="dss_tinyptrvector">"llvm/ADT/TinyPtrVector.h"</a>
+</h4>
+
+
+<div>
+<p><tt>TinyPtrVector<Type></tt> is a highly specialized collection class
+that is optimized to avoid allocation in the case when a vector has zero or one
+elements.  It has two major restrictions: 1) it can only hold values of pointer
+type, and 2) it cannot hold a null pointer.</p>
+  
+<p>Since this container is highly specialized, it is rarely used.</p>
+  
+</div>
+    
+<div>
+
+<!-- _______________________________________________________________________ -->
+<h4>
   <a name="dss_smallvector">"llvm/ADT/SmallVector.h"</a>
 </h4>
 

Added: llvm/trunk/include/llvm/ADT/TinyPtrVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/TinyPtrVector.h?rev=135365&view=auto
==============================================================================
--- llvm/trunk/include/llvm/ADT/TinyPtrVector.h (added)
+++ llvm/trunk/include/llvm/ADT/TinyPtrVector.h Sun Jul 17 20:40:02 2011
@@ -0,0 +1,115 @@
+//===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- 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 Type class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_TINYPTRVECTOR_H
+#define LLVM_ADT_TINYPTRVECTOR_H
+
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/PointerUnion.h"
+
+namespace llvm {
+  
+/// TinyPtrVector - This class is specialized for cases where there are
+/// normally 0 or 1 element in a vector, but is general enough to go beyond that
+/// when required.
+///
+/// NOTE: This container doesn't allow you to store a null pointer into it.
+///
+template <typename EltTy>
+class TinyPtrVector {
+public:
+  typedef llvm::SmallVector<EltTy, 4> VecTy;
+  llvm::PointerUnion<EltTy, VecTy*> Val;
+  
+  TinyPtrVector() {}
+  TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
+    if (VecTy *V = Val.template dyn_cast<VecTy*>())
+      Val = new VecTy(*V);
+  }
+  ~TinyPtrVector() {
+    if (VecTy *V = Val.template dyn_cast<VecTy*>())
+      delete V;
+  }
+  
+  /// empty() - This vector can be empty if it contains no element, or if it
+  /// contains a pointer to an empty vector.
+  bool empty() const {
+    if (Val.isNull()) return true;
+    if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
+      return Vec->empty();
+    return false;
+  }
+  
+  unsigned size() const {
+    if (empty())
+      return 0;
+    if (Val. template is<EltTy>())
+      return 1;
+    return Val. template get<VecTy*>()->size();
+  }
+  
+  EltTy operator[](unsigned i) const {
+    assert(!Val.isNull() && "can't index into an empty vector");
+    if (EltTy V = Val.template dyn_cast<EltTy>()) {
+      assert(i == 0 && "tinyvector index out of range");
+      return V;
+    }
+    
+    assert(i < Val. template get<VecTy*>()->size() && 
+           "tinyvector index out of range");
+    return (*Val. template get<VecTy*>())[i];
+  }
+  
+  EltTy front() const {
+    assert(!empty() && "vector empty");
+    if (EltTy V = Val.template dyn_cast<EltTy>())
+      return V;
+    return Val.template get<VecTy*>()->front();
+  }
+  
+  void push_back(EltTy NewVal) {
+    assert(NewVal != 0 && "Can't add a null value");
+    
+    // If we have nothing, add something.
+    if (Val.isNull()) {
+      Val = NewVal;
+      return;
+    }
+    
+    // If we have a single value, convert to a vector.
+    if (EltTy V = Val.template  dyn_cast<EltTy>()) {
+      Val = new VecTy();
+      Val.template get<VecTy*>()->push_back(V);
+    }
+    
+    // Add the new value, we know we have a vector.
+    Val.template get<VecTy*>()->push_back(NewVal);
+  }
+  
+  void clear() {
+    // If we have a single value, convert to empty.
+    if (EltTy V = Val.template dyn_cast<EltTy>()) {
+      Val = (EltTy)0;
+    } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
+      // If we have a vector form, just clear it.
+      Vec->clear();
+    }
+    // Otherwise, we're already empty.
+  }
+  
+private:
+  void operator=(const TinyPtrVector&); // NOT IMPLEMENTED YET.
+};
+} // end namespace llvm
+
+#endif





More information about the llvm-commits mailing list