8#ifndef INCLUDED_SDSL_IO
9#define INCLUDED_SDSL_IO
21#include <unordered_map>
35template <u
int8_t = 0u>
38int remove(std::string
const &);
55 static constexpr auto check(T *) ->
56 typename std::is_same<decltype(std::declval<T>().serialize(std::declval<std::ostream &>(),
57 std::declval<structure_tree_node *>(),
58 std::declval<std::string>())),
59 typename T::size_type>
::type
61 return std::true_type();
64 static constexpr std::false_type
check(...)
66 return std::false_type();
68 typedef decltype(check<X>(
nullptr))
type;
69 static constexpr bool value = type::value;
78 static constexpr auto check(T *) ->
79 typename std::is_same<decltype(std::declval<T>().load(std::declval<std::istream &>())),
void>
::type
81 return std::true_type();
84 static constexpr std::false_type
check(...)
86 return std::false_type();
88 typedef decltype(check<X>(
nullptr))
type;
89 static constexpr bool value = type::value;
97 out.write((
char *)&t,
sizeof(t));
98 size_t written_bytes =
sizeof(t);
100 return written_bytes;
109 size_t written_bytes = 0;
110 written_bytes +=
write_member(t.size(), out, child,
"length");
111 out.write(t.c_str(), t.size());
112 written_bytes += t.size();
114 return written_bytes;
121 in.read((
char *)&t,
sizeof(t));
128 std::string::size_type
size;
130 char * buf =
new char[
size];
132 std::string temp(buf,
size);
138typename std::enable_if<has_serialize<X>::value,
typename X::size_type>::type
141 return x.serialize(out, v, name);
145typename std::enable_if<std::is_standard_layout<X>::value && std::is_trivial<X>::value, uint64_t>::type
160typename std::enable_if<has_load<X>::value,
void>::type
load(X & x, std::istream & in)
166typename std::enable_if<std::is_standard_layout<X>::value && std::is_trivial<X>::value,
void>::type
173void load(std::vector<X> & x, std::istream & in)
175 typename std::vector<X>::size_type
size;
191template <
typename t_
int_vec>
192bool load_vector_from_file(t_int_vec & v, std::string
const & file, uint8_t num_bytes = 1, uint8_t max_int_width = 64)
194 if ((uint8_t)0 == num_bytes)
198 else if (num_bytes ==
'd')
200 uint64_t x = 0, max_x = 0;
201 isfstream in(file, std::ios::in | std::ios::binary);
208 std::vector<uint64_t> tmp;
212 max_x = std::max(x, max_x);
215 v.resize(tmp.size());
216 for (
size_t i = 0; i < tmp.size(); ++i)
231 if (file_size % num_bytes != 0)
233 throw std::logic_error(
"file size " +
util::to_string(file_size) +
" of \"" + file
237 isfstream in(file, std::ios::in | std::ios::binary);
240 v.width(std::min((
int)8 * num_bytes, (
int)max_int_width));
241 v.resize(file_size / num_bytes);
242 if (8 == t_int_vec::fixed_int_width and 1 == num_bytes)
244 in.read((
char *)v.data(), file_size);
254 uint8_t cur_byte = 0;
258 size_t read = in.gcount();
259 uint8_t * begin = buf.data();
260 uint8_t * end = begin + read;
263 x |= ((uint64_t)(*begin)) << (cur_byte * 8);
265 if (cur_byte == num_bytes)
274 while (idx < v.size());
298 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
303 std::cerr <<
"ERROR: store_to_file(const char *v, const std::string&)" << std::endl;
307 uint64_t n = strlen((
char const *)v);
314template <u
int8_t t_w
idth>
315bool store_to_file(int_vector<t_width>
const & v, std::string
const & file);
318template <
typename int_type,
typename t_
int_vec>
321 osfstream out(file, std::ios::out | std::ios::binary);
324 for (
typename t_int_vec::size_type i = 0; i < v.size(); ++i)
327 out.write((
char *)&x,
sizeof(int_type));
342 size_t written_bytes = 0;
344 return written_bytes;
367 return traits_type::not_eof(c);
373 std::streamsize
xsputn(
char const *, std::streamsize n)
403 size_t written_bytes = 0;
404 for (
auto const & x : vec)
406 written_bytes +=
serialize(x, out, child,
"[]");
409 return written_bytes;
427 for (
typename std::vector<T>::size_type i = 0; i < vec.size(); ++i)
433template <format_type F,
typename X>
439 if (st_node.get()->children.size() > 0)
441 for (
auto const & child : st_node.get()->children)
443 sdsl::write_structure_tree<F>(child.second.get(), out);
448template <format_type F,
typename X>
451 std::ofstream out(file);
452 write_structure<F>(x, out);
458 typedef std::unique_ptr<structure_tree_node> up_stn_type;
461 sdsl::write_structure_tree<F>(st_node.get(), out);
464template <
typename X,
typename... Xs>
476inline uint64_t
_parse_number(std::string::const_iterator & c, std::string::const_iterator
const & end)
478 std::string::const_iterator s = c;
479 while (c != end and isdigit(*c))
483 return std::stoull(std::string(s, c));
492template <
typename t_csa>
499template <
typename t_cst>
506template <
typename t_csa>
513template <
typename t_cst>
519template <
typename t_csx,
typename t_alph =
typename t_csx::alphabet_category>
525template <
typename t_csx>
531template <
typename t_csx>
564template <
typename t_
idx>
566 std::string
const & format,
570 typename t_idx::index_category cat;
571 const typename t_idx::csa_type & csa =
_idx_csa(idx, cat);
572 std::vector<std::string> res(csa.size());
573 bool truncate =
false;
574 for (std::string::const_iterator c = format.begin(), s = c; c != format.end(); s = c)
576 while (c != format.end() and *c !=
'%')
580 std::vector<std::string> to_copy(csa.size(), std::string(s, c));
581 transform(res.begin(), res.end(), to_copy.begin(), res.begin(), std::plus<std::string>());
583 if (c == format.end())
587 if (c == format.end())
595 if (c == format.end())
597 for (uint64_t i = 0; i < csa.size(); ++i)
633 for (uint64_t k = 0; (w > 0 and k < w) or (0 == w and k < csa.size()); ++k)
635 if (0 == csa.text[(csa[i] + k) % csa.size()])
654 for (uint64_t k = 0; (w > 0 and k < w) or (0 == w and k < csa.size()); ++k)
656 if (0 == csa.text[(i + k) % csa.size()])
678 for (
size_t i = 0; i < res.size(); ++i)
679 out << res[i] << std::endl;
690 if (config.
file_map.count(key) != 0)
694 return config.
dir +
"/" + key +
"_" + config.
id +
".sdsl";
765inline std::string
tmp_file(std::string
const & filename, std::string name_part =
"")
777 file = cache_file_name<T>(key, config);
787 std::cerr <<
"Load `" << file << std::endl;
793 std::cerr <<
"WARNING: Could not load file '";
794 std::cerr << file <<
"'" << std::endl;
809 file = cache_file_name<T>(key, config);
817 config.
file_map[std::string(key)] = file;
822 std::cerr <<
"WARNING: store_to_cache: could not store file `" << file <<
"`" << std::endl;
833 file = cache_file_name<T>(key, config);
846 std::cerr <<
"WARNING: delete_from_cache: could not delete file `" << file <<
"`" << std::endl;
869 uint64_t hash_value = util::hashvalue_of_classname(t);
876 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
881 std::cerr <<
"ERROR: store_to_file not successful for: `" << file <<
"`" << std::endl;
889 std::cerr <<
"INFO: store_to_file: `" << file <<
"`" << std::endl;
897 std::string checkfile = file +
"_check";
898 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
903 std::cerr <<
"ERROR: store_to_checked_file not successful for: `" << checkfile <<
"`" << std::endl;
914 std::string checkfile = file +
"_check";
915 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
920 std::cerr <<
"ERROR: store_to_checked_file(const char *v, const std::string&)" << std::endl;
931 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
936 std::cerr <<
"ERROR: store_to_file(const std::string& v, const std::string&)" << std::endl;
940 out.write(v.data(), v.size());
945template <u
int8_t t_w
idth>
948 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
951 std::cerr <<
"ERROR: util::store_to_file:: Could not open file `" << file <<
"`" << std::endl;
958 std::cerr <<
"INFO: store_to_file: `" << file <<
"`" << std::endl;
966template <u
int8_t t_w
idth>
969 std::string checkfile = file +
"_check";
970 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
973 std::cerr <<
"ERROR: util::store_to_checked_file: Could not open check file `" << checkfile <<
"`" << std::endl;
980 std::cerr <<
"INFO: store_to_checked_file: `" << checkfile <<
"`" << std::endl;
991 isfstream in(file, std::ios::binary | std::ios::in);
996 std::cerr <<
"Could not load file `" << file <<
"`" << std::endl;
1004 std::cerr <<
"Load file `" << file <<
"`" << std::endl;
1009template <
typename T>
1012 isfstream in(file +
"_check", std::ios::binary | std::ios::in);
1017 std::cerr <<
"Could not load check file `" << file <<
"_check`" << std::endl;
1021 uint64_t hash_value;
1023 if (hash_value != util::hashvalue_of_classname(v))
1027 std::cerr <<
"File `" << file <<
"` is not an instance of the class `"
1028 << sdsl::util::demangle2(
typeid(T).name()) <<
"`" << std::endl;
1035template <
typename t_iv>
1036inline typename std::enable_if<std::is_same<typename t_iv::index_category, iv_tag>::value
1037 or std::is_same<typename t_iv::index_category, csa_tag>::value
1038 or std::is_same<typename t_iv::index_category, lcp_tag>::value,
1039 std::ostream &>::type
1042 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
1051template <
typename t_iv>
1052inline typename std::enable_if<std::is_same<typename t_iv::index_category, wt_tag>::value, std::ostream &>::type
1055 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
1058 if (it + 1 != end and std::is_same<typename t_iv::alphabet_category, int_alphabet_tag>::value)
1064template <
typename t_
int>
1065inline typename std::enable_if<std::is_integral<t_int>::value, std::ostream &>::type
1066operator<<(std::ostream & os, std::vector<t_int>
const & v)
1068 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
1077template <
typename t_iv>
1078inline typename std::enable_if<std::is_same<typename t_iv::category, csa_member_tag>::value, std::ostream &>::type
1081 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
1084 if (it + 1 != end and std::is_same<typename t_iv::alphabet_category, int_alphabet_tag>::value)
bits.hpp contains the sdsl::bits class.
A generic vector class for integers of width .
size_type serialize(std::ostream &out, structure_tree_node *v=nullptr, std::string name="") const
Serializes the int_vector to a stream.
void close()
Close the stream.
void close()
Close the stream.
static structure_tree_node * add_child(structure_tree_node *v, std::string const &name, std::string const &type)
static void add_size(structure_tree_node *v, uint64_t value)
const uint64_t SDSL_BLOCK_SIZE
Returns the directory of a file A trailing will be removed std::string dirname(std::string file)
Get the size of a file in bytes size_t file_size(std::string const &file)
std::string to_string(T const &t, int w=1)
Namespace for the succinct data structure library.
void csXprintf(std::ostream &out, std::string const &format, t_idx const &idx, char sentinel=default_sentinel< t_idx >::value)
Prints members of CSAs and CSTs.
void read_member< std::string >(std::string &t, std::istream &in)
bool store_to_cache(T const &v, std::string const &key, cache_config &config, bool add_type_hash=false)
Stores the object v as a resource in the cache.
std::ostream & operator<<(std::ostream &os, bp_interval< t_int > const &interval)
void write_structure(X const &x, std::ostream &out)
size_t block_size(void *ptr)
bool cache_file_exists(std::string const &key, cache_config const &config)
Checks if the resource specified by the key exists in the cache.
int remove(std::string const &)
Remove a file.
void load_vector(std::vector< T > &, std::istream &)
Load all elements of a vector from a input stream.
double size_in_mega_bytes(T const &t)
Get the size of a data structure in mega bytes (MiB).
std::string _idx_lcp_val(t_csa const &, uint64_t, uint64_t, csa_tag)
Internal function used by csXprintf.
std::string tmp_file(cache_config const &config, std::string name_part="")
Returns a name for a temporary file. I.e. the name was not used before.
std::string cache_file_name(std::string const &key, cache_config const &config)
Returns the file name of the resource.
size_t write_member(T const &t, std::ostream &out, sdsl::structure_tree_node *v=nullptr, std::string name="")
bool load_from_file(T &v, std::string const &file)
Load sdsl-object v from a file.
void add_hash(T const &t, std::ostream &out)
void register_cache_file(std::string const &key, cache_config &config)
Register the existing resource specified by the key to the cache.
T::size_type size_in_bytes(T const &t)
Get the size of a data structure in bytes.
void read_member(T &t, std::istream &in)
std::enable_if< has_load< X >::value, void >::type load(X &x, std::istream &in)
std::enable_if< has_serialize< X >::value, typenameX::size_type >::type serialize(X const &x, std::ostream &out, structure_tree_node *v=nullptr, std::string name="")
bool load_from_cache(T &v, std::string const &key, cache_config const &config, bool add_type_hash=false)
bool remove_from_cache(std::string const &key, cache_config &config, bool add_type_hash=false)
bool load_vector_from_file(t_int_vec &v, std::string const &file, uint8_t num_bytes=1, uint8_t max_int_width=64)
from disk.
uint64_t serialize_vector(std::vector< T > const &, std::ostream &, sdsl::structure_tree_node *v=nullptr, std::string="")
Serialize each element of an std::vector.
bool load_from_checked_file(T &v, std::string const &file)
size_t write_member< std::string >(std::string const &t, std::ostream &out, sdsl::structure_tree_node *v, std::string name)
uint64_t _parse_number(std::string::const_iterator &c, std::string::const_iterator const &end)
Internal function used by csXprintf.
t_csa const & _idx_csa(t_csa const &t, csa_tag)
Internal function used by csXprintf.
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.
void _write_structure(std::unique_ptr< structure_tree_node > &st_node, X x, Xs... xs)
bool store_to_checked_file(T const &t, std::string const &file)
size_t serialize_empty_object(std::ostream &, structure_tree_node *v=nullptr, std::string name="", T const *t=nullptr)
bool store_to_plain_array(t_int_vec &v, std::string const &file)
Store an int_vector as plain int_type array to disk.
Contains declarations and definitions of data structure concepts.
sfstream.hpp contains a two stream class which can be used to read/write from/to files or strings.
static constexpr uint32_t hi(uint64_t x)
Position of the most significant set bit the 64-bit word x.
Helper class for construction process.
decltype(check< X >(nullptr)) type
static constexpr std::false_type check(...)
static constexpr auto check(T *) -> typename std::is_same< decltype(std::declval< T >().load(std::declval< std::istream & >())), void >::type
static constexpr bool value
decltype(check< X >(nullptr)) type
static constexpr std::false_type check(...)
static constexpr auto check(T *) -> typename std::is_same< decltype(std::declval< T >().serialize(std::declval< std::ostream & >(), std::declval< structure_tree_node * >(), std::declval< std::string >())), typename T::size_type >::type
static constexpr bool value
std::streamsize xsputn(char const *, std::streamsize n)
sdsl::nullstream::nullbuf m_sbuf
structure_tree.hpp contains a helper class which can represent the memory structure of a class.
util.hpp contains some helper methods for int_vector and other stuff like demangle class names.