4#ifndef SDSL_INT_VECTOR_MAPPER
5#define SDSL_INT_VECTOR_MAPPER
17template <u
int8_t t_w
idth = 0, std::ios_base::openmode t_mode = std::ios_base::out | std::ios_base::in>
20 static_assert(t_width <= 64,
"int_vector_mapper: width must be at most 64 bits.");
33 uint8_t * m_mapped_data =
nullptr;
34 uint64_t m_file_size_bytes = 0;
35 off_t m_data_offset = 0;
38 std::string m_file_name;
39 bool m_delete_on_close;
54 std::cerr <<
"int_vector_mapper: error unmapping file mapping'" << m_file_name <<
"': " << ret
58 if (t_mode & std::ios_base::out)
66 out.
seekp(0, std::ios::beg);
73 std::cerr <<
"int_vector_mapper: could not open file for header update" << std::endl;
82 if (t_mode & std::ios_base::out)
85 size_type current_bit_size = m_wrapper.m_size;
86 size_type data_size_in_bytes = ((current_bit_size + 63) >> 6) << 3;
87 if (m_file_size_bytes != data_size_in_bytes + m_data_offset)
92 std::string truncate_error = std::string(
"int_vector_mapper: truncate error. ") +
93 std::string(util::str_from_errno());
94 std::cerr << truncate_error;
104 std::cerr <<
"int_vector_mapper: error closing file mapping'" << m_file_name <<
"': " << ret
107 if (m_delete_on_close)
112 std::cerr <<
"int_vector_mapper: error deleting file '" << m_file_name <<
"': " << ret_code
117 m_wrapper.m_data =
nullptr;
118 m_wrapper.m_size = 0;
123 m_wrapper.m_data = ivm.m_wrapper.m_data;
124 m_wrapper.m_size = ivm.m_wrapper.m_size;
125 m_wrapper.
width(ivm.m_wrapper.width());
126 m_file_name = ivm.m_file_name;
127 m_delete_on_close = ivm.m_delete_on_close;
128 ivm.m_wrapper.m_data =
nullptr;
129 ivm.m_wrapper.m_size = 0;
130 ivm.m_mapped_data =
nullptr;
136 m_wrapper.m_data = ivm.m_wrapper.m_data;
137 m_wrapper.m_size = ivm.m_wrapper.m_size;
138 m_wrapper.
width(ivm.m_wrapper.width());
139 m_file_name = ivm.m_file_name;
140 m_delete_on_close = ivm.m_delete_on_close;
141 ivm.m_wrapper.m_data =
nullptr;
142 ivm.m_wrapper.m_size = 0;
143 ivm.m_mapped_data =
nullptr;
152 int_vector_mapper(
const std::string filename,
bool is_plain =
false,
bool delete_on_close =
false)
154 , m_file_name(filename)
155 , m_delete_on_close(delete_on_close)
158 uint8_t int_width = t_width;
160 isfstream f(filename, std::ifstream::binary);
163 throw std::runtime_error(
"int_vector_mapper: file " + m_file_name +
" does not exist.");
172 if (8 != t_width and 16 != t_width and 32 != t_width and 64 != t_width)
174 throw std::runtime_error(
"int_vector_mapper: plain vector can "
175 "only be of width 8, 16, 32, 64.");
179 uint8_t byte_width = t_width / 8;
183 throw std::runtime_error(
"int_vector_mapper: plain vector not a multiple of byte: " +
188 size_in_bits = m_file_size_bytes * 8;
195 std::string open_error = std::string(
"int_vector_mapper: open file error.") +
196 std::string(util::str_from_errno());
197 throw std::runtime_error(open_error);
201 m_wrapper.
width(int_width);
204 if (m_mapped_data ==
nullptr)
206 std::string mmap_error = std::string(
"int_vector_mapper: mmap error. ") +
207 std::string(util::str_from_errno());
208 throw std::runtime_error(mmap_error);
211 m_wrapper.m_size = size_in_bits;
212 free(m_wrapper.m_data);
213 m_wrapper.m_data = (uint64_t *)(m_mapped_data + m_data_offset);
218 void width(
const uint8_t new_int_width)
220 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'width'");
221 m_wrapper.
width(new_int_width);
226 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'bit_resize'");
228 if (m_file_size_bytes != new_size_in_bytes + m_data_offset)
235 std::cerr <<
"int_vector_mapper: error unmapping file mapping'" << m_file_name <<
"': " << ret
242 std::string truncate_error = std::string(
"int_vector_mapper: truncate error. ") +
243 std::string(util::str_from_errno());
244 throw std::runtime_error(truncate_error);
246 m_file_size_bytes = new_size_in_bytes + m_data_offset;
250 if (m_mapped_data ==
nullptr)
252 std::string mmap_error = std::string(
"int_vector_mapper: mmap error. ") +
253 std::string(util::str_from_errno());
254 throw std::runtime_error(mmap_error);
258 m_wrapper.m_data = (uint64_t *)(m_mapped_data + m_data_offset);
265 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'resize'");
272 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'begin'");
273 return m_wrapper.
begin();
277 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'end'");
278 return m_wrapper.
end();
281 auto end() const -> typename
int_vector<t_width>::const_iterator {
return m_wrapper.
end(); }
286 return m_wrapper[idx];
290 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'operator[]'");
291 return m_wrapper[idx];
293 const uint64_t *
data()
const {
return m_wrapper.
data(); }
296 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'data'");
297 return m_wrapper.
data();
302 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'set_int'");
303 m_wrapper.
set_int(idx, x, len);
307 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'push_back'");
313 m_wrapper.m_size = old_size;
316 m_wrapper.m_size +=
width();
317 m_wrapper[
size() - 1] = x;
321 size_t data_size_in_bits = 8 * (m_file_size_bytes - m_data_offset);
322 return data_size_in_bits /
width();
325 template <
class container>
328 return std::equal(
begin(),
end(), v.begin());
332 template <
class container>
335 return !(*
this == v);
339 static_assert(t_mode & std::ios_base::out,
"int_vector_mapper: must be opened in in+out mode for 'flip'");
345template <u
int8_t t_w
idth = 0>
349 static std::string tmp_file(
const std::string & dir)
351 char tmp_file_name[1024] = { 0 };
353 auto ret = GetTempFileName(dir.c_str(),
"tmp_mapper_file_", 0, tmp_file_name);
354 if (ret == 0) {
throw std::runtime_error(
"could not create temporary file."); }
356 sprintf(tmp_file_name,
"%s/tmp_mapper_file_%" PRIu64
"_XXXXXX.sdsl", dir.c_str(),
util::pid());
357 int fd = mkstemps(tmp_file_name, 5);
358 if (fd == -1) {
throw std::runtime_error(
"could not create temporary file."); }
361 return std::string(tmp_file_name, strlen(tmp_file_name));
368 char tmp_dir_name[1024] = { 0 };
369 auto tmp_dir = GetTempPath(1024, tmp_dir_name);
370 auto file_name = tmp_file(tmp_dir_name);
372 auto file_name = tmp_file(
"/tmp");
378 auto file_name = tmp_file(config.
dir);
391template <u
int8_t t_w
idth = 0>
398 auto tmp =
create(file_name);
400 return std::move(tmp);
420template <std::ios_base::openmode t_mode = std::ios_base::out | std::ios_base::in>
423template <u
int8_t t_w
idth = 0>
int_vector_mapper(const std::string filename, bool is_plain=false, bool delete_on_close=false)
int_vector_mapper()=delete
bool operator!=(const container &v) const
size_type bit_size() const
auto cbegin() const -> typename int_vector< t_width >::const_iterator
value_type get_int(size_type idx, const uint8_t len=64) const
int_vector_mapper(const std::string &key, const cache_config &config)
std::string file_name() const
void bit_resize(const size_type bit_size)
const uint64_t * data() const
bool operator==(const int_vector< t_width > &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
auto operator[](const size_type &idx) -> typename int_vector< t_width >::reference
int_vector_mapper(const int_vector_mapper &)=delete
int_vector_mapper & operator=(int_vector_mapper &&ivm)
bool operator==(const int_vector_mapper &v) const
auto end() const -> typename int_vector< t_width >::const_iterator
int_vector_mapper & operator=(const int_vector_mapper &)=delete
void push_back(value_type x)
auto cend() const -> typename int_vector< t_width >::const_iterator
auto end() -> typename int_vector< t_width >::iterator
auto operator[](const size_type &idx) const -> typename int_vector< t_width >::const_reference
bool operator==(const container &v) const
int_vector< t_width >::size_type size_type
auto begin() const -> typename int_vector< t_width >::const_iterator
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 .
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.
const uint64_t * data() const noexcept
Pointer to the raw data of the 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(const std::string &file_name)
static int_vector_mapper< t_width > create(const cache_config &config)
static int_vector_mapper< t_width > create(const std::string &file_name)
static int_vector_mapper< t_width > create(const std::string &key, cache_config &config)
static int_vector_mapper< t_width > create(const std::string &file_name, size_t size, uint8_t int_width=t_width)
int_vector.hpp contains the sdsl::int_vector class.
memory_management.hpp contains two function for allocating and deallocating memory
int close(const int fd)
Get fd for file.
Get the size of a file in bytes size_t file_size(const std::string &file)
std::string to_string(const T &t, int w=1)
Namespace for the succinct data structure library.
std::string cache_file_name(const std::string &key, const cache_config &config)
Returns the file name of the resource.
void register_cache_file(const std::string &key, cache_config &config)
Register the existing resource specified by the key to the cache.
bool store_to_file(const T &v, const std::string &file)
Store a data structure to a file.
int remove(const std::string &)
Remove a file.
int_vector ::size_type size(const range_type &r)
Size of a range.
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.