1 //===- llvm/ADT/PackedVector.h - Packed values vector -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PackedVector class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_PACKEDVECTOR_H
15 #define LLVM_ADT_PACKEDVECTOR_H
17 #include "llvm/ADT/BitVector.h"
23 template <typename T, unsigned BitNum, typename BitVectorTy, bool isSigned>
24 class PackedVectorBase;
26 // This won't be necessary if we can specialize members without specializing
27 // the parent template.
28 template <typename T, unsigned BitNum, typename BitVectorTy>
29 class PackedVectorBase<T, BitNum, BitVectorTy, false> {
31 static T getValue(const BitVectorTy &Bits, unsigned Idx) {
33 for (unsigned i = 0; i != BitNum; ++i)
34 val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
38 static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
39 assert((val >> BitNum) == 0 && "value is too big");
40 for (unsigned i = 0; i != BitNum; ++i)
41 Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
45 template <typename T, unsigned BitNum, typename BitVectorTy>
46 class PackedVectorBase<T, BitNum, BitVectorTy, true> {
48 static T getValue(const BitVectorTy &Bits, unsigned Idx) {
50 for (unsigned i = 0; i != BitNum-1; ++i)
51 val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
52 if (Bits[(Idx << (BitNum-1)) + BitNum-1])
57 static void setValue(BitVectorTy &Bits, unsigned Idx, T val) {
60 Bits.set((Idx << (BitNum-1)) + BitNum-1);
62 assert((val >> (BitNum-1)) == 0 && "value is too big");
63 for (unsigned i = 0; i != BitNum-1; ++i)
64 Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
68 /// \brief Store a vector of values using a specific number of bits for each
69 /// value. Both signed and unsigned types can be used, e.g
71 /// PackedVector<signed, 2> vec;
73 /// will create a vector accepting values -2, -1, 0, 1. Any other value will hit
75 template <typename T, unsigned BitNum, typename BitVectorTy = BitVector>
76 class PackedVector : public PackedVectorBase<T, BitNum, BitVectorTy,
77 std::numeric_limits<T>::is_signed> {
79 using base = PackedVectorBase<T, BitNum, BitVectorTy,
80 std::numeric_limits<T>::is_signed>;
89 reference(PackedVector &vec, unsigned idx) : Vec(vec), Idx(idx) {}
91 reference &operator=(T val) {
92 Vec.setValue(Vec.Bits, Idx, val);
97 return Vec.getValue(Vec.Bits, Idx);
101 PackedVector() = default;
102 explicit PackedVector(unsigned size) : Bits(size << (BitNum-1)) {}
104 bool empty() const { return Bits.empty(); }
106 unsigned size() const { return Bits.size() >> (BitNum - 1); }
108 void clear() { Bits.clear(); }
110 void resize(unsigned N) { Bits.resize(N << (BitNum - 1)); }
112 void reserve(unsigned N) { Bits.reserve(N << (BitNum-1)); }
114 PackedVector &reset() {
119 void push_back(T val) {
121 (*this)[size()-1] = val;
124 reference operator[](unsigned Idx) {
125 return reference(*this, Idx);
128 T operator[](unsigned Idx) const {
129 return base::getValue(Bits, Idx);
132 bool operator==(const PackedVector &RHS) const {
133 return Bits == RHS.Bits;
136 bool operator!=(const PackedVector &RHS) const {
137 return Bits != RHS.Bits;
140 PackedVector &operator|=(const PackedVector &RHS) {
146 // Leave BitNum=0 undefined.
147 template <typename T> class PackedVector<T, 0>;
149 } // end namespace llvm
151 #endif // LLVM_ADT_PACKEDVECTOR_H