4#ifndef SDSL_INT_VECTOR_MAPPER
5#define SDSL_INT_VECTOR_MAPPER
28template <u
int8_t t_w
idth = 0, std::ios_base::openmode t_mode = std::ios_base::out | std::ios_base::in>
31 static_assert(t_width <= 64,
"int_vector_mapper: width must be at most 64 bits.");
44 uint8_t * m_mapped_data =
nullptr;
45 uint64_t m_file_size_bytes = 0;
46 off_t m_data_offset = 0;
49 std::string m_file_name;
50 bool m_delete_on_close;
65 std::cerr <<
"int_vector_mapper: error unmapping file mapping'" << m_file_name <<
"': " << ret
69 if (t_mode & std::ios_base::out)
77 out.
seekp(0, std::ios::beg);
84 std::cerr <<
"int_vector_mapper: could not open file for header update" << std::endl;
93 if (t_mode & std::ios_base::out)
96 size_type current_bit_size = m_wrapper.m_size;
97 size_type data_size_in_bytes = ((current_bit_size + 63) >> 6) << 3;
98 if (m_file_size_bytes != data_size_in_bytes + m_data_offset)
103 std::string truncate_error =
104 std::string(
"int_vector_mapper: truncate error. ") + std::string(util::str_from_errno());
105 std::cerr << truncate_error;
115 std::cerr <<
"int_vector_mapper: error closing file mapping'" << m_file_name <<
"': " << ret
118 if (m_delete_on_close)
123 std::cerr <<
"int_vector_mapper: error deleting file '" << m_file_name <<
"': " << ret_code
128 m_wrapper.m_data =
nullptr;
129 m_wrapper.m_size = 0;
134 m_wrapper.m_data = ivm.m_wrapper.m_data;
135 m_wrapper.m_size = ivm.m_wrapper.m_size;
136 m_wrapper.
width(ivm.m_wrapper.width());
137 m_file_name = ivm.m_file_name;
138 m_delete_on_close = ivm.m_delete_on_close;
139 ivm.m_wrapper.m_data =
nullptr;
140 ivm.m_wrapper.m_size = 0;
141 ivm.m_mapped_data =
nullptr;
147 m_wrapper.m_data = ivm.m_wrapper.m_data;
148 m_wrapper.m_size = ivm.m_wrapper.m_size;
149 m_wrapper.
width(ivm.m_wrapper.width());
150 m_file_name = ivm.m_file_name;
151 m_delete_on_close = ivm.m_delete_on_close;
152 ivm.m_wrapper.m_data =
nullptr;
153 ivm.m_wrapper.m_size = 0;
154 ivm.m_mapped_data =
nullptr;
163 int_vector_mapper(
const std::string filename,
bool is_plain =
false,
bool delete_on_close =
false) :
165 m_file_name(filename),
166 m_delete_on_close(delete_on_close)
169 uint8_t int_width = t_width;
171 isfstream f(filename, std::ifstream::binary);
174 throw std::runtime_error(
"int_vector_mapper: file " + m_file_name +
" does not exist.");
186 if (8 != t_width and 16 != t_width and 32 != t_width and 64 != t_width)
188 throw std::runtime_error(
"int_vector_mapper: plain vector can "
189 "only be of width 8, 16, 32, 64.");
193 uint8_t byte_width = t_width / 8;
197 throw std::runtime_error(
"int_vector_mapper: plain vector not a multiple of byte: "
198 + std::to_string(m_file_size_bytes) +
" mod " + std::to_string(byte_width)
202 size_in_bits = m_file_size_bytes * 8;
209 std::string open_error =
210 std::string(
"int_vector_mapper: open file error.") + std::string(util::str_from_errno());
211 throw std::runtime_error(open_error);
215 m_wrapper.
width(int_width);
218 if (m_mapped_data ==
nullptr)
220 std::string mmap_error =
221 std::string(
"int_vector_mapper: mmap error. ") + std::string(util::str_from_errno());
222 throw std::runtime_error(mmap_error);
225 m_wrapper.m_size = size_in_bits;
226 free(m_wrapper.m_data);
227 m_wrapper.m_data = (uint64_t *)(m_mapped_data + m_data_offset);
236 return m_wrapper.
width();
238 void width(
const uint8_t new_int_width)
240 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'width'");
241 m_wrapper.
width(new_int_width);
245 return m_wrapper.
size();
249 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'bit_resize'");
251 if (m_file_size_bytes != new_size_in_bytes + m_data_offset)
258 std::cerr <<
"int_vector_mapper: error unmapping file mapping'" << m_file_name <<
"': " << ret
265 std::string truncate_error =
266 std::string(
"int_vector_mapper: truncate error. ") + std::string(util::str_from_errno());
267 throw std::runtime_error(truncate_error);
269 m_file_size_bytes = new_size_in_bytes + m_data_offset;
273 if (m_mapped_data ==
nullptr)
275 std::string mmap_error =
276 std::string(
"int_vector_mapper: mmap error. ") + std::string(util::str_from_errno());
277 throw std::runtime_error(mmap_error);
281 m_wrapper.m_data = (uint64_t *)(m_mapped_data + m_data_offset);
288 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'resize'");
295 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'begin'");
296 return m_wrapper.
begin();
300 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'end'");
301 return m_wrapper.
end();
305 return m_wrapper.
begin();
309 return m_wrapper.
end();
313 return m_wrapper.
begin();
317 return m_wrapper.
end();
321 return m_wrapper[idx];
325 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'operator[]'");
326 return m_wrapper[idx];
330 return m_wrapper.
data();
334 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'data'");
335 return m_wrapper.
data();
339 return m_wrapper.
get_int(idx, len);
343 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'set_int'");
344 m_wrapper.
set_int(idx, x, len);
348 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'push_back'");
354 m_wrapper.m_size = old_size;
357 m_wrapper.m_size +=
width();
358 m_wrapper[
size() - 1] = x;
362 size_t data_size_in_bits = 8 * (m_file_size_bytes - m_data_offset);
363 return data_size_in_bits /
width();
369 template <
class container>
372 return std::equal(
begin(),
end(), v.begin());
376 return m_wrapper == v;
380 return m_wrapper == v.m_wrapper;
382 template <
class container>
385 return !(*
this == v);
389 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'flip'");
394 return m_wrapper.
empty();
398template <u
int8_t t_w
idth = 0>
402 static std::string tmp_file(std::string
const & dir)
404 char tmp_file_name[1024] = {0};
406 auto ret = GetTempFileName(dir.c_str(),
"tmp_mapper_file_", 0, tmp_file_name);
409 throw std::runtime_error(
"could not create temporary file.");
412 sprintf(tmp_file_name,
"%s/tmp_mapper_file_%" PRIu64
"_XXXXXX.sdsl", dir.c_str(),
util::pid());
413 int fd = mkstemps(tmp_file_name, 5);
416 throw std::runtime_error(
"could not create temporary file.");
420 return std::string(tmp_file_name, strlen(tmp_file_name));
427 char tmp_dir_name[1024] = {0};
428 auto tmp_dir = GetTempPath(1024, tmp_dir_name);
429 auto file_name = tmp_file(tmp_dir_name);
431 auto file_name = tmp_file(
"/tmp");
437 auto file_name = tmp_file(config.
dir);
450template <u
int8_t t_w
idth = 0>
457 auto tmp =
create(file_name);
459 return std::move(tmp);
479template <std::ios_base::openmode t_mode = std::ios_base::out | std::ios_base::in>
482template <u
int8_t t_w
idth = 0>
bits.hpp contains the sdsl::bits class.
int_vector_mapper(const std::string filename, bool is_plain=false, bool delete_on_close=false)
int_vector_mapper & operator=(int_vector_mapper const &)=delete
int_vector_mapper()=delete
size_type bit_size() const
auto operator[](size_type const &idx) const -> typename int_vector< t_width >::const_reference
int_vector_mapper(int_vector_mapper const &)=delete
auto cbegin() const -> typename int_vector< t_width >::const_iterator
value_type get_int(size_type idx, const uint8_t len=64) const
bool operator==(int_vector< t_width > const &v) const
std::string file_name() const
void bit_resize(const size_type bit_size)
uint64_t const * data() const
bool operator==(int_vector_mapper const &v) const
bool operator!=(container const &v) const
auto begin() -> typename int_vector< t_width >::iterator
const size_type append_block_size
void width(const uint8_t new_int_width)
size_type capacity() const
void set_int(size_type idx, value_type x, const uint8_t len=64)
int_vector< t_width >::int_width_type width_type
int_vector_mapper(int_vector_mapper &&ivm)
static constexpr uint8_t fixed_int_width
int_vector_mapper & operator=(int_vector_mapper &&ivm)
auto end() const -> typename int_vector< t_width >::const_iterator
void push_back(value_type x)
auto cend() const -> typename int_vector< t_width >::const_iterator
bool operator==(container const &v) const
auto operator[](size_type const &idx) -> typename int_vector< t_width >::reference
auto end() -> typename int_vector< t_width >::iterator
int_vector< t_width >::size_type size_type
auto begin() const -> typename int_vector< t_width >::const_iterator
int_vector_mapper(std::string const &key, cache_config const &config)
int_vector< t_width >::difference_type difference_type
void resize(const size_type size)
int_vector< t_width >::value_type value_type
A proxy class that acts as a reference to an integer of length len bits in a int_vector.
A generic vector class for integers of width .
uint64_t const * data() const noexcept
Pointer to the raw data of the int_vector.
iterator end() noexcept
Iterator that points to the element after the last element of int_vector.
void flip()
Flip all bits of bit_vector.
bool empty() const noexcept
Equivalent to size() == 0.
value_type get_int(size_type idx, const uint8_t len=64) const
Get the integer value of the binary string of length len starting at position idx in the int_vector.
int_vector_size_type size_type
ptrdiff_t difference_type
int_vector_trait< t_width >::const_reference const_reference
int_vector_trait< t_width >::int_width_type int_width_type
size_type bit_size() const noexcept
The number of bits in the int_vector.
uint8_t width() const noexcept
Returns the width of the integers which are accessed via the [] operator.
size_type size() const noexcept
The number of elements in the int_vector.
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.
void set_int(size_type idx, value_type x, const uint8_t len=64)
Set the bits from position idx to idx+len-1 to the binary representation of integer x.
iterator begin() noexcept
Iterator that points to the first element of the int_vector.
bool is_open()
Is the stream close?
static void * mmap_file(int fd, uint64_t file_size, std::ios_base::openmode mode)
static int close_file_for_mmap(int fd)
static int mem_unmap(int fd, void *addr, const uint64_t size)
static int open_file_for_mmap(std::string &filename, std::ios_base::openmode mode)
static int truncate_file_mmap(int fd, const uint64_t new_size)
osfstream & seekp(pos_type pos)
static int_vector_mapper< t_width > create()
static int_vector_mapper< t_width > create(std::string const &file_name)
static int_vector_mapper< t_width > create(cache_config const &config)
static int_vector_mapper< t_width > create(std::string const &key, cache_config &config)
static int_vector_mapper< t_width > create(std::string const &file_name)
static int_vector_mapper< t_width > create(std::string const &file_name, size_t size, uint8_t int_width=t_width)
int_vector.hpp contains the sdsl::int_vector class.
io.hpp contains some methods for reading/writing sdsl structures.
memory_management.hpp contains two function for allocating and deallocating memory
Get the size of a file in bytes size_t file_size(std::string const &file)
Namespace for the succinct data structure library.
int_vector_mapper< t_width, std::ios_base::in > const read_only_mapper
int remove(std::string const &)
Remove a file.
std::string cache_file_name(std::string const &key, cache_config const &config)
Returns the file name of the resource.
void register_cache_file(std::string const &key, cache_config &config)
Register the existing resource specified by the key to the cache.
bool store_to_file(T const &v, std::string const &file)
Store a data structure to a file.
int_vector ::size_type size(range_type const &r)
Size of a range.
sfstream.hpp contains a two stream class which can be used to read/write from/to files or strings.
static constexpr uint64_t cnt(uint64_t x)
Counts the number of set bits in x.
static constexpr uint64_t lo_set[65]
lo_set[i] is a 64-bit word with the i least significant bits set and the high bits not set.
Helper class for construction process.
util.hpp contains some helper methods for int_vector and other stuff like demangle class names.