tudocomp
– The TU Dortmund Compression Framework
divsufsort_bufwrapper.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 
7 
9 namespace tdc {
10 namespace libdivsufsort {
11 
12 // Wrapper for buffers with unsigned value types
13 template<typename buffer_t>
14 class BufferWrapper {
15 private:
16  buffer_t& m_buffer;
17 
18  class Accessor {
19  private:
20  buffer_t& m_buffer;
21  saidx_t m_index;
22 
23  public:
24  inline Accessor(buffer_t& buffer, saidx_t i) : m_buffer(buffer), m_index(i) {}
25  inline operator saidx_t() { return saidx_t(m_buffer[m_index]); }
26  inline Accessor& operator=(saidx_t v) { m_buffer[m_index] = v; return *this; }
27 
28  inline Accessor& operator=(const Accessor& other) {
29  m_buffer[m_index] = other.m_buffer[other.m_index];
30  return *this;
31  }
32  };
33 
34 public:
35  BufferWrapper(buffer_t& buffer) : m_buffer(buffer) {}
36  inline Accessor operator[](saidx_t i) { return Accessor(m_buffer, i); }
37 };
38 
39 // special accessor for DynamicIntVector, where sign casts are not that simple
40 template<>
41 class BufferWrapper<DynamicIntVector>::Accessor {
42 private:
43  DynamicIntVector& m_buffer;
44  saidx_t m_index;
45 
46  const int m_shift;
47 
48  inline saidx_t to_signed(uint64_t v, int p = 0) {
49  // NB: Need to use portable arithmetic shift instruction
50  // to ensure shifting a signed value causes a proper sign extension
51  return shift_by(shift_by(int64_t(v), m_shift), -m_shift);
52  }
53 
54  inline uint64_t to_unsigned(saidx_t v, int p = 0) {
55  return (uint64_t(int64_t(v)) << m_shift) >> m_shift;
56  }
57 
58  std::string pm(uint64_t v) {
59  std::stringstream ss;
60  for(size_t i = 0; i < 64; i++) {
61  ss << int((v & (1ull << (64 - i - 1))) != 0);
62  }
63  return ss.str();
64  }
65 
66 public:
67  inline Accessor(DynamicIntVector& buffer, saidx_t i):
68  m_buffer(buffer),
69  m_index(i),
70  m_shift(64 - buffer.width())
71  {
72  }
73 
74  inline operator saidx_t() { return to_signed(m_buffer[m_index]); }
75  inline Accessor& operator=(saidx_t v) { m_buffer[m_index] = to_unsigned(v); return *this; }
76 
77  inline Accessor& operator=(const Accessor& other) {
78  m_buffer[m_index] = other.m_buffer[other.m_index];
79  return *this;
80  }
81 };
82 
83 }} //ns
85 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
uint_impl_t & operator=(const uint_impl_t &b)
Definition: uint_t.hpp:43
constexpr T shift_by(T value, int amount) noexcept
IntVector< dynamic_t > DynamicIntVector
Represents an integer vector with unspecified (dynamic) bit width.
Definition: IntVector.hpp:553