14 #include <type_traits> 23 #include <sdsl/bits.hpp> 24 #include <glog/logging.h> 31 namespace int_vector {
33 struct BitPackingVectorSliceBase {};
36 struct BitPackingVectorSliceBase<dynamic_t> {
37 typedef DynamicIntValueType internal_data_type;
38 typedef dynamic_t value_type;
40 ConstGenericView<internal_data_type> m_vec;
45 inline BitPackingVectorSliceBase():
46 m_vec(), m_real_size(0), m_width(64), m_offset(0) {}
47 inline BitPackingVectorSliceBase(
const BitPackingVectorSliceBase& other):
48 m_vec(other.m_vec), m_real_size(other.m_real_size), m_width(other.m_width), m_offset(other.m_offset) {}
50 inline uint8_t raw_width()
const {
return m_width; }
51 inline void set_width_raw(uint8_t width) { m_width = width; }
56 struct BitPackingVectorSlice: BitPackingVectorSliceBase<T> {
57 typedef typename BitPackingVectorSliceBase<T>::value_type value_type;
59 typedef ConstIntRef<value_type> const_reference;
61 typedef ConstIntPtr<value_type> const_pointer;
63 typedef const_pointer const_iterator;
65 typedef typename std::reverse_iterator<const_iterator> const_reverse_iterator;
67 typedef ptrdiff_t difference_type;
68 typedef size_t size_type;
70 typedef typename BitPackingVectorSliceBase<T>::internal_data_type internal_data_type;
73 friend bool operator==(
const BitPackingVectorSlice<M>& lhs,
const BitPackingVectorSlice<M>& rhs);
75 inline static uint64_t backing2bits_w(
size_t n) {
76 return uint64_t(
sizeof(internal_data_type) * CHAR_BIT) * uint64_t(n);
78 inline uint64_t backing2bits(
size_t n)
const {
79 return backing2bits_w(n);
82 inline static uint64_t elem2bits_w(
size_t n, uint8_t w) {
83 return uint64_t(w) * uint64_t(n);
85 inline uint64_t elem2bits(
size_t n)
const {
86 return elem2bits_w(n, this->width());
89 inline static uint64_t bits2backing_w(uint64_t bits) {
93 return ((bits - 1) / backing2bits_w(1)) + 1;
95 inline uint64_t bits2backing(uint64_t bits)
const {
96 return bits2backing_w(bits);
99 inline static uint64_t bits2elem_w(uint64_t bits, uint8_t w) {
103 return ((bits - 1) / elem2bits_w(1, w)) + 1;
105 inline uint64_t bits2elem(uint64_t bits)
const {
106 return bits2elem_w(bits, this->width());
109 struct PosAndOffset {
size_t pos; uint8_t offset; };
110 inline static PosAndOffset bitpos2backingpos_w(uint64_t bits) {
111 return PosAndOffset {
112 bits / backing2bits_w(1),
113 uint8_t(bits % backing2bits_w(1))
116 inline PosAndOffset bitpos2backingpos(uint64_t bits)
const {
117 return bitpos2backingpos_w(bits);
120 inline explicit BitPackingVectorSlice(): BitPackingVectorSliceBase<T>::BitPackingVectorSliceBase() {}
122 inline BitPackingVectorSlice(ConstGenericView<internal_data_type> raw, size_type n, uint8_t width, uint8_t offset):
123 BitPackingVectorSlice()
126 this->m_real_size = n;
127 this->m_offset = offset;
128 this->set_width_raw(width);
131 inline BitPackingVectorSlice(
const IntVector<dynamic_t>& vec):
132 BitPackingVectorSlice(ConstGenericView<internal_data_type>(vec.data(), bits2backing_w(elem2bits_w(vec.size(), vec.width()))), vec.size(), vec.width(), 0)
135 inline BitPackingVectorSlice (
const BitPackingVectorSlice& other):
136 BitPackingVectorSliceBase<T>(other) {}
138 inline BitPackingVectorSlice&
operator=(
const BitPackingVectorSlice& other) {
139 this->m_vec = other.m_vec;
140 this->m_real_size = other.m_real_size;
141 this->m_offset = other.m_offset;
142 this->set_width_raw(other.width());
146 inline const_iterator begin()
const {
147 using Base =
typename int_vector::IntPtrBase<const_pointer>;
148 auto x = bitpos2backingpos(this->m_offset);
149 return const_pointer(Base(this->m_vec.data() + x.pos, x.offset, this->width()));
152 inline const_iterator end()
const {
153 using Base =
typename int_vector::IntPtrBase<const_pointer>;
154 auto x = bitpos2backingpos(this->m_offset + elem2bits(this->m_real_size));
155 return const_pointer(Base(this->m_vec.data() + x.pos, x.offset, this->width()));
158 inline const_reverse_iterator rbegin()
const {
159 return std::reverse_iterator<const_iterator>(end());
162 inline const_reverse_iterator rend()
const {
163 return std::reverse_iterator<const_iterator>(begin());
166 inline const_iterator cbegin()
const {
170 inline const_iterator cend()
const {
174 inline const_reverse_iterator crbegin()
const {
178 inline const_reverse_iterator crend()
const {
182 inline size_type size()
const {
183 return this->m_real_size;
186 inline uint64_t bit_size()
const {
187 return elem2bits(this->m_real_size);
190 inline size_type max_size()
const {
195 inline bool empty()
const {
199 inline const_reference operator[](size_type n)
const {
200 using Base =
typename int_vector::IntPtrBase<const_pointer>;
202 auto x = bitpos2backingpos(this->m_offset + elem2bits(n));
203 return const_reference(const_pointer(Base(this->m_vec.data() + x.pos, x.offset, this->width())));
206 inline void range_check(size_type n)
const {
208 std::stringstream ss;
209 ss <<
"Out-of-range access of IntVector: index is ";
211 ss <<
", size() is ";
213 throw std::out_of_range(ss.str());
217 inline const_reference at(size_type n)
const {
219 return operator[](n);
222 inline const_reference front()
const {
223 return operator[](0);
226 inline const_reference back()
const {
227 return operator[](size() - 1);
230 inline uint8_t width()
const {
231 return this->raw_width();
234 inline BitPackingVectorSlice slice(
size_t from,
size_t to =
size_t(-1))
const {
235 if (to ==
size_t(-1)) {
239 auto from_bits = bitpos2backingpos(this->m_offset + elem2bits(from));
240 auto to_bits = bitpos2backingpos(this->m_offset + elem2bits(to));
242 auto from_idx = from_bits.pos;
243 auto to_idx = to_bits.pos;
244 if (to_bits.offset != 0) {
248 auto raw = this->m_vec.slice(from_idx, to_idx);
250 return BitPackingVectorSlice(raw, to - from, width(), from_bits.offset);
255 bool operator==(
const BitPackingVectorSlice<N>& lhs,
const BitPackingVectorSlice<N>& rhs) {
256 DCHECK(lhs.width() == rhs.width());
257 if (lhs.size() == rhs.size()) {
258 return std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin());
264 bool operator!=(
const BitPackingVectorSlice<N>& lhs,
const BitPackingVectorSlice<N>& rhs) {
265 return !(lhs == rhs);
269 bool operator<(const BitPackingVectorSlice<N>& lhs,
const BitPackingVectorSlice<N>& rhs) {
270 return std::lexicographical_compare(lhs.cbegin(), lhs.cend(), rhs.cbegin(), rhs.cend());
274 bool operator<=(const BitPackingVectorSlice<N>& lhs,
const BitPackingVectorSlice<N>& rhs) {
279 bool operator>(
const BitPackingVectorSlice<N>& lhs,
const BitPackingVectorSlice<N>& rhs) {
280 return std::lexicographical_compare(rhs.cbegin(), rhs.cend(), lhs.cbegin(), lhs.cend());
284 bool operator>=(
const BitPackingVectorSlice<N>& lhs,
const BitPackingVectorSlice<N>& rhs) {
290 using BitPackingVectorSlice = int_vector::BitPackingVectorSlice<T>;
Contains the text compression and encoding framework.
uint_impl_t & operator=(const uint_impl_t &b)
bool operator>(const IntVector< T > &lhs, const IntVector< T > &rhs)
bool operator==(const IntVector< T > &lhs, const IntVector< T > &rhs)
bool operator>=(const IntVector< T > &lhs, const IntVector< T > &rhs)
bool operator!=(const IntVector< T > &lhs, const IntVector< T > &rhs)