llvm.org GIT mirror llvm / 74ee8ac
Introduce PackedVector, useful for storing a vector of values using a specific number of bits for each value. Both signed and unsigned types can be used, e.g PackedVector<signed, 2> vec; will create a vector accepting values -2, -1, 0, 1. Any other value will hit an assertion. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@132325 91177308-0d34-0410-b5e6-96231b3b80d8 Argyrios Kyrtzidis 9 years ago
1 changed file(s) with 158 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- llvm/ADT/PackedVector.h - Packed values vector -----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the PackedVector class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ADT_PACKEDVECTOR_H
14 #define LLVM_ADT_PACKEDVECTOR_H
15
16 #include "llvm/ADT/BitVector.h"
17 #include
18
19 namespace llvm {
20
21 template
22 class PackedVectorBase;
23
24 // This won't be necessary if we can specialize members without specializing
25 // the parent template.
26 template
27 class PackedVectorBase {
28 protected:
29 static T getValue(const llvm::BitVector &Bits, unsigned Idx) {
30 T val = T();
31 for (unsigned i = 0; i != BitNum; ++i)
32 val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
33 return val;
34 }
35
36 static void setValue(llvm::BitVector &Bits, unsigned Idx, T val) {
37 assert((val >> BitNum) == 0 && "value is too big");
38 for (unsigned i = 0; i != BitNum; ++i)
39 Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
40 }
41 };
42
43 template
44 class PackedVectorBase {
45 protected:
46 static T getValue(const llvm::BitVector &Bits, unsigned Idx) {
47 T val = T();
48 for (unsigned i = 0; i != BitNum-1; ++i)
49 val = T(val | ((Bits[(Idx << (BitNum-1)) + i] ? 1UL : 0UL) << i));
50 if (Bits[(Idx << (BitNum-1)) + BitNum-1])
51 val = ~val;
52 return val;
53 }
54
55 static void setValue(llvm::BitVector &Bits, unsigned Idx, T val) {
56 if (val < 0) {
57 val = ~val;
58 Bits.set((Idx << (BitNum-1)) + BitNum-1);
59 }
60 assert((val >> (BitNum-1)) == 0 && "value is too big");
61 for (unsigned i = 0; i != BitNum-1; ++i)
62 Bits[(Idx << (BitNum-1)) + i] = val & (T(1) << i);
63 }
64 };
65
66 /// \brief Store a vector of values using a specific number of bits for each
67 /// value. Both signed and unsigned types can be used, e.g
68 /// @code
69 /// PackedVector vec;
70 /// @endcode
71 /// will create a vector accepting values -2, -1, 0, 1. Any other value will hit
72 /// an assertion.
73 template
74 class PackedVector : public PackedVectorBase
75 std::numeric_limits::is_signed> {
76 llvm::BitVector Bits;
77 typedef PackedVectorBase::is_signed> base;
78
79 public:
80 class reference {
81 PackedVector &Vec;
82 const unsigned Idx;
83
84 reference(); // Undefined
85 public:
86 reference(PackedVector &vec, unsigned idx) : Vec(vec), Idx(idx) { }
87
88 reference &operator=(T val) {
89 Vec.setValue(Vec.Bits, Idx, val);
90 return *this;
91 }
92 operator T() {
93 return Vec.getValue(Vec.Bits, Idx);
94 }
95 };
96
97 PackedVector() { }
98 explicit PackedVector(unsigned size) : Bits(size << (BitNum-1)) { }
99
100 bool empty() const { return Bits.empty(); }
101
102 unsigned size() const { return Bits.size() >> (BitNum-1); }
103
104 void clear() { Bits.clear(); }
105
106 void resize(unsigned N) { Bits.resize(N << (BitNum-1)); }
107
108 void reserve(unsigned N) { Bits.reserve(N << (BitNum-1)); }
109
110 PackedVector &reset() {
111 Bits.reset();
112 return *this;
113 }
114
115 void push_back(T val) {
116 resize(size()+1);
117 (*this)[size()-1] = val;
118 }
119
120 reference operator[](unsigned Idx) {
121 return reference(*this, Idx);
122 }
123
124 T operator[](unsigned Idx) const {
125 return base::getValue(Bits, Idx);
126 }
127
128 bool operator==(const PackedVector &RHS) const {
129 return Bits == RHS.Bits;
130 }
131
132 bool operator!=(const PackedVector &RHS) const {
133 return Bits != RHS.Bits;
134 }
135
136 const PackedVector &operator=(const PackedVector &RHS) {
137 Bits = RHS.Bits;
138 return *this;
139 }
140
141 PackedVector &operator|=(const PackedVector &RHS) {
142 Bits |= RHS.Bits;
143 return *this;
144 }
145
146 void swap(PackedVector &RHS) {
147 Bits.swap(RHS.Bits);
148 }
149 };
150
151 // Leave BitNum=0 undefined.
152 template
153 class PackedVector;
154
155 } // end llvm namespace
156
157 #endif