8#ifndef INCLUDED_INT_VECTOR_BUFFER
9#define INCLUDED_INT_VECTOR_BUFFER
25template <u
int8_t t_w
idth = 0>
35 static_assert(t_width <= 64,
"int_vector_buffer: width must be at most 64 bits.");
38 std::string m_filename;
40 bool m_need_to_write =
false;
42 uint64_t m_offset = 0;
43 uint64_t m_buffersize = 8;
48 void read_block(
const uint64_t idx)
50 m_begin = (idx / m_buffersize) * m_buffersize;
51 if (m_begin >= m_size)
57 m_ifile.seekg(m_offset + (m_begin *
width()) / 8);
58 assert(m_ifile.good());
59 m_ifile.read((
char *)m_buffer.data(), (m_buffersize *
width()) / 8);
60 if ((uint64_t)m_ifile.gcount() < (m_buffersize *
width()) / 8)
64 assert(m_ifile.good());
65 for (uint64_t i = m_size - m_begin; i < m_buffersize; ++i)
77 m_ofile.
seekp(m_offset + (m_begin *
width()) / 8);
78 assert(m_ofile.good());
79 if (m_begin + m_buffersize >= m_size)
82 uint64_t wb = ((m_size - m_begin) *
width() + 7) / 8;
83 m_ofile.write((
char *)m_buffer.
data(), wb);
87 m_ofile.write((
char *)m_buffer.
data(), (m_buffersize *
width()) / 8);
90 assert(m_ofile.good());
91 m_need_to_write =
false;
96 uint64_t read(
const uint64_t idx)
100 if (idx < m_begin or m_begin + m_buffersize <= idx)
105 return m_buffer[idx - m_begin];
109 void write(
const uint64_t idx,
const uint64_t value)
113 if (idx < m_begin or m_begin + m_buffersize <= idx)
122 m_need_to_write =
true;
123 m_buffer[idx - m_begin] = value;
144 std::ios::openmode mode = std::ios::in,
145 const uint64_t buffer_size = 1024 * 1024,
146 const uint8_t int_width = t_width,
147 bool const is_plain =
false)
150 assert(!(mode & std::ios::app));
151 mode &= ~std::ios::app;
152 m_buffer.width(int_width);
165 m_ofile.open(m_filename, mode | std::ios::out | std::ios::binary);
166 assert(m_ofile.good());
167 m_ifile.open(m_filename, std::ios::in | std::ios::binary);
168 assert(m_ifile.good());
169 if (mode & std::ios::in)
174 m_ifile.seekg(0, std::ios_base::end);
175 size = m_ifile.tellg() * 8;
181 m_buffer.width(
width);
183 assert(m_ifile.good());
191 m_filename(std::move(ivb.m_filename)),
192 m_buffer(std::move(ivb.m_buffer)),
193 m_need_to_write(ivb.m_need_to_write),
194 m_offset(ivb.m_offset),
195 m_buffersize(ivb.m_buffersize),
201 m_ifile.open(m_filename, std::ios::in | std::ios::binary);
202 m_ofile.open(m_filename, std::ios::in | std::ios::out | std::ios::binary);
203 assert(m_ifile.good());
204 assert(m_ofile.good());
208 ivb.m_need_to_write =
false;
210 ivb.m_buffersize = 8;
227 m_filename = ivb.m_filename;
228 m_ifile.open(m_filename, std::ios::in | std::ios::binary);
229 m_ofile.open(m_filename, std::ios::in | std::ios::out | std::ios::binary);
230 assert(m_ifile.good());
231 assert(m_ofile.good());
234 m_need_to_write = ivb.m_need_to_write;
235 m_offset = ivb.m_offset;
236 m_buffersize = ivb.m_buffersize;
238 m_begin = ivb.m_begin;
242 ivb.m_need_to_write =
false;
244 ivb.m_buffersize = 8;
253 return m_buffer.width();
271 assert(m_buffersize *
width() % 8 == 0);
272 return (m_buffersize *
width()) / 8;
288 uint64_t element_buffersize =
290 m_buffersize = element_buffersize + 7 - (element_buffersize + 7) % 8;
293 if (0 != m_buffersize)
300 return m_ifile.good() and m_ofile.good();
306 return m_ifile.is_open() and m_ofile.is_open();
314 assert(m_ifile.good());
315 assert(m_ofile.good());
318 m_ofile.open(m_filename, std::ios::out | std::ios::binary);
319 assert(m_ofile.good());
320 m_ifile.open(m_filename, std::ios::in | std::ios::binary);
321 assert(m_ifile.good());
322 assert(m_ofile.good());
324 m_need_to_write =
false;
339 return reference(
this, idx);
345 write(m_size, value);
352 void close(
bool remove_file =
false)
362 m_ofile.seekp(0, std::ios::beg);
364 assert(m_ofile.good());
365 uint64_t wb = (
size + 7) / 8;
368 m_ofile.seekp(m_offset + wb);
369 assert(m_ofile.good());
370 m_ofile.write(
"\0\0\0\0\0\0\0\0", 8 - wb % 8);
371 assert(m_ofile.good());
376 assert(m_ifile.good());
378 assert(m_ofile.good());
388 return iterator(*
this, 0);
393 return iterator(*
this,
size());
408 m_int_vector_buffer(_int_vector_buffer),
414 operator uint64_t()
const
416 return m_int_vector_buffer->read(m_idx);
422 m_int_vector_buffer->write(m_idx, val);
429 return *
this = (uint64_t)(x);
437 uint64_t x = m_int_vector_buffer->read(m_idx);
438 m_int_vector_buffer->write(m_idx, x + 1);
445 uint64_t val = (uint64_t) *
this;
453 uint64_t x = m_int_vector_buffer->read(m_idx);
454 m_int_vector_buffer->write(m_idx, x - 1);
461 uint64_t val = (uint64_t) *
this;
469 uint64_t w = m_int_vector_buffer->read(m_idx);
470 m_int_vector_buffer->write(m_idx, w + x);
477 uint64_t w = m_int_vector_buffer->read(m_idx);
478 m_int_vector_buffer->write(m_idx, w - x);
484 return (uint64_t) *
this == (uint64_t)x;
489 return (uint64_t) *
this < (uint64_t)x;
544 return *
this -= (-i);
552 return *
this += (-i);
571 return &m_ivb == &(it.m_ivb) and m_idx == it.m_idx;
576 return !(*
this == it);
580 return (m_idx - it.m_idx);
reference operator*() const
iterator operator-(difference_type i) const
sdsl::int_vector_buffer< t_width >::difference_type difference_type
sdsl::int_vector_buffer< t_width >::value_type value_type
difference_type operator-(iterator const &it)
iterator(int_vector_buffer< t_width > &ivb, uint64_t idx=0)
bool operator!=(iterator const &it) const
std::random_access_iterator_tag iterator_category
bool operator==(iterator const &it) const
iterator & operator-=(difference_type i)
iterator operator+(difference_type i) const
iterator & operator+=(difference_type i)
sdsl::int_vector_buffer< t_width >::reference reference
uint64_t operator++(int)
Postfix increment of the proxy object.
bool operator<(reference const &x) const
reference & operator-=(const uint64_t x)
Subtract assign from the proxy object.
reference & operator--()
Prefix decrement of the proxy object.
reference(reference const &)=default
reference & operator+=(const uint64_t x)
Add assign from the proxy object.
reference & operator=(reference const &x)
Assignment operator.
uint64_t operator--(int)
Postfix decrement of the proxy object.
bool operator==(reference const &x) const
reference & operator++()
Prefix increment of the proxy object.
reference & operator=(uint64_t const &val)
Assignment operator for write operations.
void reset()
Delete all content and set size to 0.
int_vector_buffer(const std::string filename, std::ios::openmode mode=std::ios::in, const uint64_t buffer_size=1024 *1024, const uint8_t int_width=t_width, bool const is_plain=false)
Constructor for int_vector_buffer.
void close(bool remove_file=false)
Close the int_vector_buffer.
reference operator[](uint64_t idx)
[] operator
int_vector_buffer< t_width > & operator=(int_vector_buffer &&ivb)
Move assignment operator.
int_vector< t_width >::difference_type difference_type
int_vector< t_width >::value_type value_type
bool good()
Returns whether state of underlying streams are good.
void buffersize(uint64_t buffersize)
Set the buffersize in bytes.
std::string filename() const
uint64_t buffersize() const
Returns the buffersize in bytes.
void push_back(const uint64_t value)
Appends the given element value to the end of the int_vector_buffer.
uint8_t width() const
Returns the width of the integers which are accessed via the [] operator.
int_vector_buffer()
Constructor.
int_vector_buffer(int_vector_buffer &&ivb)
Move constructor.
bool is_open()
Returns whether underlying streams are currently associated to a file.
~int_vector_buffer()
Destructor.
A generic vector class for integers of width .
uint64_t const * data() const noexcept
Pointer to the raw data of the int_vector.
ptrdiff_t difference_type
int_vector_trait< t_width >::value_type value_type
static size_t read_header(int_vector_size_type &size, int_width_type &int_width, std::istream &in)
Read the size and int_width of a int_vector.
static uint64_t write_header(uint64_t size, uint8_t int_width, std::ostream &out)
Write the size and int_width of a int_vector.
osfstream & seekp(pos_type pos)
int_vector.hpp contains the sdsl::int_vector class.
void set_to_value(t_int_vec &v, uint64_t k)
Set all entries of int_vector to value k.
Namespace for the succinct data structure library.
int remove(std::string const &)
Remove a file.
sfstream.hpp contains a two stream class which can be used to read/write from/to files or strings.
util.hpp contains some helper methods for int_vector and other stuff like demangle class names.