14 #include <type_traits> 23 #include <sdsl/bits.hpp> 24 #include <glog/logging.h> 29 namespace int_vector {
41 struct ConstIntegerBaseTrait<int_vector::IntRef<T>> {
42 using Dispatch =
typename int_vector::IntRepr<T>::IntOpDispatch;
45 struct IntegerBaseTrait<int_vector::IntRef<T>> {
46 using Dispatch =
typename int_vector::IntRepr<T>::IntOpDispatch;
49 struct ConstIntegerBaseTrait<int_vector::ConstIntRef<T>> {
50 using Dispatch =
typename int_vector::IntRepr<T>::IntOpDispatch;
56 struct iterator_traits<
tdc::int_vector::IntPtr<T>> {
57 typedef ptrdiff_t difference_type;
58 typedef typename tdc::int_vector::IntRepr<T>::value_type value_type;
59 typedef tdc::int_vector::IntPtr<T> pointer;
60 typedef tdc::int_vector::IntRef<T> reference;
61 typedef std::random_access_iterator_tag iterator_category;
64 struct iterator_traits<
tdc::int_vector::ConstIntPtr<T>> {
65 typedef ptrdiff_t difference_type;
66 typedef typename tdc::int_vector::IntRepr<T>::value_type value_type;
67 typedef tdc::int_vector::ConstIntPtr<T> pointer;
68 typedef tdc::int_vector::ConstIntRef<T> reference;
69 typedef std::random_access_iterator_tag iterator_category;
74 namespace int_vector {
81 class IntPtrBase<ConstIntPtr<T>>:
82 public IntRepr<T>::ConstIntPtrBase {
84 using IntRepr<T>::ConstIntPtrBase::ConstIntPtrBase;
88 class IntPtrBase<IntPtr<T>>:
89 public IntRepr<T>::IntPtrBase {
91 using IntRepr<T>::IntPtrBase::IntPtrBase;
93 inline operator IntPtrBase<ConstIntPtr<T>>()
const {
94 return IntPtrBase<ConstIntPtr<T>>(this->m_ptr, this->m_bit_offset, this->data_bit_size());
98 template<
class Self,
class Ptr>
101 template<
class Self,
class T>
102 class GenericIntPtr: IntPtrBase<Self> {
104 friend class GenericIntRef<IntRef<T>, IntPtr<T>>;
105 friend class GenericIntRef<ConstIntRef<T>, ConstIntPtr<T>>;
106 friend class IntRef<T>;
107 friend class ConstIntRef<T>;
108 friend struct IntegerBaseTrait<IntRef<T>>;
109 friend struct ConstIntegerBaseTrait<IntRef<T>>;
110 friend struct ConstIntegerBaseTrait<ConstIntRef<T>>;
111 friend typename IntRepr<T>::IntOpDispatch;
117 IntPtrBase<Self>(nullptr, 0, 0) {}
118 GenericIntPtr(
const IntPtrBase<Self>& other):
119 IntPtrBase<Self>(other) {}
120 GenericIntPtr(
const GenericIntPtr& other):
121 IntPtrBase<Self>(other){}
124 this->set_data_bit_size(other.data_bit_size());
125 this->m_ptr = other.m_ptr;
126 this->m_bit_offset = other.m_bit_offset;
127 return static_cast<Self&
>(*this);
131 DynamicIntValueType
const* tmp = this->m_ptr;
132 bits::move_right(tmp, this->m_bit_offset, this->data_bit_size());
133 this->m_ptr = (DynamicIntValueType*) tmp;
134 return static_cast<Self&
>(*this);
138 DynamicIntValueType
const* tmp = this->m_ptr;
139 bits::move_left(tmp, this->m_bit_offset, this->data_bit_size());
140 this->m_ptr = (DynamicIntValueType*) tmp;
141 return static_cast<Self&
>(*this);
144 friend Self operator+(
const Self& lhs,
const ptrdiff_t& rhs) {
147 for(
size_t i = 0; i < size_t(rhs); i++) {
151 for(
size_t i = 0; i < size_t(-rhs); i++) {
158 friend Self operator+(
const ptrdiff_t& lhs,
const Self& rhs) {
return rhs + lhs; }
159 friend Self operator-(
const Self& lhs,
const ptrdiff_t& rhs) {
return lhs + (-rhs); }
161 friend ptrdiff_t operator-(
const Self& lhs,
const Self& rhs) {
163 DCHECK(lhs.data_bit_size() == rhs.data_bit_size());
164 auto ptr_diff = lhs.m_ptr - rhs.m_ptr;
165 auto bit_count = ptr_diff *
sizeof(DynamicIntValueType) * CHAR_BIT;
166 bit_count += lhs.m_bit_offset;
167 bit_count -= rhs.m_bit_offset;
168 bit_count /= lhs.data_bit_size();
172 friend bool operator==(
const Self& lhs,
const Self& rhs) {
173 DCHECK(lhs.data_bit_size() == rhs.data_bit_size());
174 return (lhs.m_ptr == rhs.m_ptr)
175 && (lhs.m_bit_offset == rhs.m_bit_offset);
177 friend bool operator<(
const Self& lhs,
const Self& rhs) {
179 DCHECK(lhs.data_bit_size() == rhs.data_bit_size());
180 if (lhs.m_ptr < rhs.m_ptr)
return true;
181 if ((lhs.m_ptr == rhs.m_ptr) && (lhs.m_bit_offset < rhs.m_bit_offset))
return true;
184 friend bool operator!=(
const Self& lhs,
const Self& rhs) {
return !(lhs == rhs); }
185 friend bool operator>(
const Self& lhs,
const Self& rhs) {
return !(lhs < rhs); }
186 friend bool operator>=(
const Self& lhs,
const Self& rhs) {
return (lhs > rhs) || (lhs == rhs); }
187 friend bool operator<=(
const Self& lhs,
const Self& rhs) {
return (lhs < rhs) || (lhs == rhs); }
189 Self& operator+=(
const ptrdiff_t& v) {
auto&
self =
static_cast<Self&
>(*this);
self = (
self + v);
return self; }
190 Self& operator-=(
const ptrdiff_t& v) {
auto&
self =
static_cast<Self&
>(*this);
self = (
self - v);
return self; }
192 Self operator++(
int) {
auto self =
static_cast<Self&
>(*this); ++(*this);
return self; }
193 Self operator--(
int) {
auto self =
static_cast<Self&
>(*this); --(*this);
return self; }
195 ConstIntRef<T> operator[](
size_t i)
const;
196 ConstIntRef<T> operator*()
const;
200 class ConstIntPtr:
public GenericIntPtr<ConstIntPtr<T>, T> {
202 using GenericIntPtr<ConstIntPtr<T>, T>::GenericIntPtr;
203 inline ConstIntPtr(): GenericIntPtr<ConstIntPtr<T>, T>::GenericIntPtr() {}
204 inline ConstIntPtr(
const ConstIntPtr& other): GenericIntPtr<ConstIntPtr<T>, T>::GenericIntPtr(other) {}
206 ConstIntPtr&
operator=(
const ConstIntPtr& other) {
207 return ((GenericIntPtr<ConstIntPtr<T>, T>&) *
this) = other;
212 class IntPtr:
public GenericIntPtr<IntPtr<T>, T> {
214 using GenericIntPtr<IntPtr<T>, T>::GenericIntPtr;
215 inline IntPtr(): GenericIntPtr<IntPtr<T>, T>::GenericIntPtr() {}
216 inline IntPtr(
const IntPtr& other): GenericIntPtr<IntPtr<T>, T>::GenericIntPtr(other) {}
219 return ((GenericIntPtr<IntPtr<T>, T>&) *
this) = other;
222 inline IntRef<T> operator*();
223 inline IntRef<T> operator[](
size_t i);
225 inline operator ConstIntPtr<T>()
const {
226 return ConstIntPtr<T>(IntPtrBase<ConstIntPtr<T>>((
const IntPtrBase<IntPtr<T>>&) *
this));
230 template<
class Self,
class Ptr>
231 class GenericIntRef {
232 using T =
typename Ptr::tag_type;
234 friend class IntPtr<T>;
235 friend class ConstIntPtr<T>;
236 friend struct IntegerBaseTrait<IntRef<T>>;
237 friend struct ConstIntegerBaseTrait<IntRef<T>>;
238 friend struct ConstIntegerBaseTrait<ConstIntRef<T>>;
239 friend typename IntRepr<T>::IntOpDispatch;
243 using value_type =
typename IntRepr<T>::value_type;
245 inline GenericIntRef() =
delete;
246 explicit GenericIntRef(
const Ptr& ptr): m_ptr(ptr) {}
248 operator value_type()
const {
249 return IntRepr<T>::mem_rw::read_int(
250 m_ptr.m_ptr, m_ptr.m_bit_offset, this->m_ptr.data_bit_size());
257 public GenericIntRef<ConstIntRef<T>, ConstIntPtr<T>>,
258 public ConstIntegerBaseCombiner<
259 ConstIntegerBaseWithSelf<ConstIntRef<T>>,
260 ConstIntegerBaseWith32<ConstIntRef<T>, unsigned char>,
261 ConstIntegerBaseWith32<ConstIntRef<T>, char>,
262 ConstIntegerBaseWith32<ConstIntRef<T>, signed char>,
263 ConstIntegerBaseWith32<ConstIntRef<T>, unsigned short int>,
264 ConstIntegerBaseWith32<ConstIntRef<T>, signed short int>,
265 ConstIntegerBaseWith32<ConstIntRef<T>, unsigned int>,
266 ConstIntegerBaseWith32<ConstIntRef<T>, signed int>,
267 ConstIntegerBaseWith64<ConstIntRef<T>, unsigned long int>,
268 ConstIntegerBaseWith64<ConstIntRef<T>, signed long int>,
269 ConstIntegerBaseWith64<ConstIntRef<T>, unsigned long long int>,
270 ConstIntegerBaseWith64<ConstIntRef<T>, signed long long int>,
271 ConstIntegerBaseWith64<ConstIntRef<T>, T>
274 inline ConstIntRef() =
delete;
275 explicit ConstIntRef(
const ConstIntPtr<T>& ptr): GenericIntRef<ConstIntRef<T>, ConstIntPtr<T>>::GenericIntRef(ptr) {}
277 inline ConstIntPtr<T> operator&() {
return this->m_ptr; }
282 public GenericIntRef<IntRef<T>, IntPtr<T>>,
283 public IntegerBaseCombiner<
284 IntegerBaseWithSelf<IntRef<T>>,
285 IntegerBaseWith32<IntRef<T>, unsigned char>,
286 IntegerBaseWith32<IntRef<T>, char>,
287 IntegerBaseWith32<IntRef<T>, signed char>,
288 IntegerBaseWith32<IntRef<T>, unsigned short int>,
289 IntegerBaseWith32<IntRef<T>, signed short int>,
290 IntegerBaseWith32<IntRef<T>, unsigned int>,
291 IntegerBaseWith32<IntRef<T>, signed int>,
292 IntegerBaseWith64<IntRef<T>, unsigned long int>,
293 IntegerBaseWith64<IntRef<T>, signed long int>,
294 IntegerBaseWith64<IntRef<T>, unsigned long long int>,
295 IntegerBaseWith64<IntRef<T>, signed long long int>,
296 IntegerBaseWith64<IntRef<T>, T>
299 using typename GenericIntRef<IntRef<T>, IntPtr<T>>::value_type;
301 inline IntRef() =
delete;
302 explicit IntRef(
const IntPtr<T>& ptr): GenericIntRef<IntRef<T>, IntPtr<T>>::GenericIntRef(ptr) {}
304 inline IntRef&
operator=(value_type other) {
305 IntRepr<T>::mem_rw::write_int(
306 this->m_ptr.m_ptr, other,
307 this->m_ptr.m_bit_offset,
308 this->m_ptr.data_bit_size()
314 inline IntRef&
operator=(
const IntRef& other) {
315 return operator=(other.operator value_type());
318 inline IntRef&
operator=(
const ConstIntRef<T>& other);
320 inline IntPtr<T> operator&() {
return this->m_ptr; }
322 inline operator ConstIntRef<T>()
const {
323 return ConstIntRef<T>(this->m_ptr);
333 inline IntRef<T> IntPtr<T>::operator*() {
334 return IntRef<T>(*this);
337 template<
class Self,
class T>
338 ConstIntRef<T> GenericIntPtr<Self, T>::operator*()
const {
339 return ConstIntRef<T>(
static_cast<const Self&
>(*this));
342 template<
class Self,
class T>
343 ConstIntRef<T> GenericIntPtr<Self, T>::operator[](
size_t i)
const {
346 return ConstIntRef<T>(
static_cast<const Self&
>(x));
350 inline IntRef<T> IntPtr<T>::operator[](
size_t i) {
357 inline void swap(IntRef<T>& lhs, IntRef<T>& rhs) noexcept {
359 lhs = rhs.operator T();
364 inline void swap(IntRef<T>& lhs, T& rhs) noexcept {
371 inline void swap(T& lhs, IntRef<T>& rhs) noexcept {
373 lhs = rhs.operator T();
377 static_assert(
sizeof(GenericIntPtr<ConstIntPtr<uint_t<40>>, uint_t<40>>) <= (
sizeof(
void*) * 2),
378 "make sure this is reasonably small");
383 using IntRef = int_vector::IntRef<T>;
386 using ConstIntRef = int_vector::ConstIntRef<T>;
389 using IntPtr = int_vector::IntPtr<T>;
392 using ConstIntPtr = int_vector::ConstIntPtr<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)
void swap(IntVector< T > &lhs, 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)