8 #ifndef INCLUDED_INT_VECTOR_BUFFER
9 #define INCLUDED_INT_VECTOR_BUFFER
23 template <u
int8_t t_w
idth = 0>
32 static_assert(t_width <= 64,
"int_vector_buffer: width must be at most 64 bits.");
35 std::string m_filename;
37 bool m_need_to_write =
false;
39 uint64_t m_offset = 0;
40 uint64_t m_buffersize = 8;
45 void read_block(
const uint64_t idx)
47 m_begin = (idx / m_buffersize) * m_buffersize;
51 m_ifile.
seekg(m_offset + (m_begin *
width()) / 8);
52 assert(m_ifile.good());
53 m_ifile.read((
char *)m_buffer.
data(), (m_buffersize *
width()) / 8);
54 if ((uint64_t)m_ifile.gcount() < (m_buffersize *
width()) / 8) { m_ifile.clear(); }
55 assert(m_ifile.good());
56 for (uint64_t i = m_size - m_begin; i < m_buffersize; ++i) { m_buffer[i] = 0; }
65 m_ofile.
seekp(m_offset + (m_begin *
width()) / 8);
66 assert(m_ofile.good());
67 if (m_begin + m_buffersize >= m_size)
70 uint64_t wb = ((m_size - m_begin) *
width() + 7) / 8;
71 m_ofile.write((
char *)m_buffer.
data(), wb);
75 m_ofile.write((
char *)m_buffer.
data(), (m_buffersize *
width()) / 8);
78 assert(m_ofile.good());
79 m_need_to_write =
false;
84 uint64_t read(
const uint64_t idx)
88 if (idx < m_begin or m_begin + m_buffersize <= idx)
93 return m_buffer[idx - m_begin];
97 void write(
const uint64_t idx,
const uint64_t value)
101 if (idx < m_begin or m_begin + m_buffersize <= idx)
106 if (m_size <= idx) { m_size = idx + 1; }
107 m_need_to_write =
true;
108 m_buffer[idx - m_begin] = value;
126 std::ios::openmode mode = std::ios::in,
127 const uint64_t buffer_size = 1024 * 1024,
128 const uint8_t int_width = t_width,
129 const bool is_plain =
false)
132 assert(!(mode & std::ios::app));
133 mode &= ~std::ios::app;
134 m_buffer.
width(int_width);
147 m_ofile.
open(m_filename, mode | std::ios::out | std::ios::binary);
148 assert(m_ofile.good());
149 m_ifile.
open(m_filename, std::ios::in | std::ios::binary);
150 assert(m_ifile.good());
151 if (mode & std::ios::in)
156 m_ifile.
seekg(0, std::ios_base::end);
165 assert(m_ifile.good());
173 : m_filename(std::move(ivb.m_filename))
174 , m_buffer(std::move(ivb.m_buffer))
175 , m_need_to_write(ivb.m_need_to_write)
176 , m_offset(ivb.m_offset)
177 , m_buffersize(ivb.m_buffersize)
179 , m_begin(ivb.m_begin)
183 m_ifile.
open(m_filename, std::ios::in | std::ios::binary);
184 m_ofile.
open(m_filename, std::ios::in | std::ios::out | std::ios::binary);
185 assert(m_ifile.good());
186 assert(m_ofile.good());
190 ivb.m_need_to_write =
false;
192 ivb.m_buffersize = 8;
206 m_filename = ivb.m_filename;
207 m_ifile.
open(m_filename, std::ios::in | std::ios::binary);
208 m_ofile.
open(m_filename, std::ios::in | std::ios::out | std::ios::binary);
209 assert(m_ifile.good());
210 assert(m_ofile.good());
213 m_need_to_write = ivb.m_need_to_write;
214 m_offset = ivb.m_offset;
215 m_buffersize = ivb.m_buffersize;
217 m_begin = ivb.m_begin;
221 ivb.m_need_to_write =
false;
223 ivb.m_buffersize = 8;
233 uint64_t
size()
const {
return m_size; }
236 std::string
filename()
const {
return m_filename; }
241 assert(m_buffersize *
width() % 8 == 0);
242 return (m_buffersize *
width()) / 8;
259 m_buffersize = element_buffersize + 7 - (element_buffersize + 7) % 8;
262 if (0 != m_buffersize) read_block(0);
266 bool good() {
return m_ifile.good() and m_ofile.good(); }
279 assert(m_ifile.good());
280 assert(m_ofile.good());
283 m_ofile.
open(m_filename, std::ios::out | std::ios::binary);
284 assert(m_ofile.good());
285 m_ifile.
open(m_filename, std::ios::in | std::ios::binary);
286 assert(m_ifile.good());
287 assert(m_ofile.good());
289 m_need_to_write =
false;
305 void push_back(
const uint64_t value) { write(m_size, value); }
311 void close(
bool remove_file =
false)
321 m_ofile.
seekp(0, std::ios::beg);
323 assert(m_ofile.good());
324 uint64_t wb = (
size + 7) / 8;
327 m_ofile.
seekp(m_offset + wb);
328 assert(m_ofile.good());
329 m_ofile.write(
"\0\0\0\0\0\0\0\0", 8 - wb % 8);
330 assert(m_ofile.good());
335 assert(m_ifile.good());
337 assert(m_ofile.good());
357 : m_int_vector_buffer(_int_vector_buffer)
363 operator uint64_t()
const {
return m_int_vector_buffer->read(m_idx); }
368 m_int_vector_buffer->write(m_idx, val);
378 uint64_t x = m_int_vector_buffer->read(m_idx);
379 m_int_vector_buffer->write(m_idx, x + 1);
386 uint64_t val = (uint64_t) *
this;
394 uint64_t x = m_int_vector_buffer->read(m_idx);
395 m_int_vector_buffer->write(m_idx, x - 1);
402 uint64_t val = (uint64_t) *
this;
410 uint64_t w = m_int_vector_buffer->read(m_idx);
411 m_int_vector_buffer->write(m_idx, w + x);
418 uint64_t w = m_int_vector_buffer->read(m_idx);
419 m_int_vector_buffer->write(m_idx, w - x);
429 :
public std::iterator<std::random_access_iterator_tag, value_type, difference_type, value_type *, reference>
472 if (i < 0)
return *
this -= (-i);
479 if (i < 0)
return *
this += (-i);
reference operator*() const
iterator operator-(difference_type i) const
bool operator!=(const iterator &it) const
iterator & operator+=(difference_type i)
iterator(int_vector_buffer< t_width > &ivb, uint64_t idx=0)
difference_type operator-(const iterator &it)
iterator operator+(difference_type i) const
iterator & operator-=(difference_type i)
bool operator==(const iterator &it) const
uint64_t operator++(int)
Postfix increment of the proxy object.
bool operator==(const reference &x) const
reference & operator+=(const uint64_t x)
Add assign from the proxy object.
reference & operator-=(const uint64_t x)
Subtract assign from the proxy object.
reference & operator++()
Prefix increment of the proxy object.
uint64_t operator--(int)
Postfix decrement of the proxy object.
reference & operator=(reference &x)
Assignment operator.
reference & operator=(const uint64_t &val)
Assignment operator for write operations.
reference & operator--()
Prefix decrement of the proxy object.
bool operator<(const reference &x) const
uint64_t size() const
Returns the number of elements currently stored.
void reset()
Delete all content and set size to 0.
void close(bool remove_file=false)
Close the int_vector_buffer.
reference operator[](uint64_t idx)
[] operator
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, const bool is_plain=false)
Constructor for int_vector_buffer.
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
Returns the filename.
int_vector_buffer< t_width > & operator=(int_vector_buffer &&ivb)
Move assignment operator.
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 .
ptrdiff_t difference_type
const uint64_t * data() const noexcept
Pointer to the raw data of the int_vector.
uint8_t width() const noexcept
Returns the width of the integers which are accessed via the [] operator.
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.
bool is_open()
Is the stream close?
buf_ptr_type open(const std::string &file, std::ios_base::openmode mode=std::ios_base::in)
Open the stream.
void close()
Close the stream.
isfstream & seekg(pos_type pos)
void close()
Close the stream.
bool is_open()
Is the stream close?
buf_ptr_type open(const std::string &file, std::ios_base::openmode mode=std::ios_base::out)
Open the stream.
osfstream & seekp(pos_type pos)
int_vector.hpp contains the sdsl::int_vector class.
iterators.hpp contains an generic iterator for random access containers.
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(const std::string &)
Remove a file.