8#ifndef INCLUDED_SDSL_IO
9#define INCLUDED_SDSL_IO
26int remove(
const std::string &);
46 check(T *) ->
typename std::is_same<decltype(std::declval<T>().serialize(std::declval<std::ostream &>(),
47 std::declval<structure_tree_node *>(),
48 std::declval<std::string>())),
51 return std::true_type();
54 static constexpr std::false_type
check(...)
56 return std::false_type();
58 typedef decltype(check<X>(
nullptr))
type;
59 static constexpr bool value = type::value;
69 check(T *) ->
typename std::is_same<decltype(std::declval<T>().load(std::declval<std::istream &>())),
void>
::type
71 return std::true_type();
74 static constexpr std::false_type
check(...)
76 return std::false_type();
78 typedef decltype(check<X>(
nullptr))
type;
79 static constexpr bool value = type::value;
87 out.write((
char *)&t,
sizeof(t));
88 size_t written_bytes =
sizeof(t);
95inline size_t write_member<std::string>(
const std::string & t,
101 size_t written_bytes = 0;
102 written_bytes +=
write_member(t.size(), out, child,
"length");
103 out.write(t.c_str(), t.size());
104 written_bytes += t.size();
106 return written_bytes;
113 in.read((
char *)&t,
sizeof(t));
118inline void read_member<std::string>(std::string & t, std::istream & in)
122 char * buf =
new char[
size];
124 std::string temp(buf,
size);
130typename std::enable_if<has_serialize<X>::value,
typename X::size_type>::type
133 return x.serialize(out, v, name);
137typename std::enable_if<std::is_standard_layout<X>::value && std::is_trivial<X>::value, uint64_t>::type
147 std::string name =
"")
154typename std::enable_if<has_load<X>::value,
void>::type
load(X & x, std::istream & in)
160typename std::enable_if<std::is_standard_layout<X>::value && std::is_trivial<X>::value,
void>::type
load(
168void load(std::vector<X> & x, std::istream & in)
186template <
typename t_
int_vec>
187bool load_vector_from_file(t_int_vec & v,
const std::string & file, uint8_t num_bytes = 1, uint8_t max_int_width = 64)
189 if ((uint8_t)0 == num_bytes)
193 else if (num_bytes ==
'd')
195 uint64_t x = 0, max_x = 0;
196 isfstream in(file, std::ios::in | std::ios::binary);
197 if (!in) {
return false; }
200 std::vector<uint64_t> tmp;
204 max_x = std::max(x, max_x);
207 v.resize(tmp.size());
208 for (
size_t i = 0; i < tmp.size(); ++i) { v[i] = tmp[i]; }
226 isfstream in(file, std::ios::in | std::ios::binary);
229 v.width(std::min((
int)8 * num_bytes, (
int)max_int_width));
231 if (8 == t_int_vec::fixed_int_width and 1 == num_bytes)
243 uint8_t cur_byte = 0;
246 size_t read = in.gcount();
247 uint8_t * begin = buf.data();
248 uint8_t * end = begin + read;
251 x |= ((uint64_t)(*begin)) << (cur_byte * 8);
253 if (cur_byte == num_bytes)
261 }
while (idx < v.size());
285 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
290 std::cerr <<
"ERROR: store_to_file(const char *v, const std::string&)" << std::endl;
294 uint64_t n = strlen((
const char *)v);
301template <u
int8_t t_w
idth>
302bool store_to_file(
const int_vector<t_width> & v,
const std::string & file);
305template <
typename int_type,
typename t_
int_vec>
308 osfstream out(file, std::ios::out | std::ios::binary);
314 out.write((
char *)&x,
sizeof(int_type));
327 std::string name =
"",
328 const T * t =
nullptr)
331 size_t written_bytes = 0;
333 return written_bytes;
354 int overflow(
int c) {
return traits_type::not_eof(c); }
356 std::streamsize
xsputn(
char const *, std::streamsize n) {
return n; }
384 v, name,
"std::vector<" + util::class_name(vec[0]) +
">");
385 size_t written_bytes = 0;
386 for (
const auto & x : vec) { written_bytes +=
serialize(x, out, child,
"[]"); }
388 return written_bytes;
409template <format_type F,
typename X>
415 if (st_node.get()->children.size() > 0)
417 for (
const auto & child : st_node.get()->children) { sdsl::write_structure_tree<F>(child.second.get(), out); }
421template <format_type F,
typename X>
424 std::ofstream out(file);
425 write_structure<F>(x, out);
431 typedef std::unique_ptr<structure_tree_node> up_stn_type;
434 sdsl::write_structure_tree<F>(st_node.get(), out);
437template <
typename X,
typename... Xs>
448inline uint64_t
_parse_number(std::string::const_iterator & c,
const std::string::const_iterator & end)
450 std::string::const_iterator s = c;
451 while (c != end and isdigit(*c)) ++c;
452 if (c > s) {
return std::stoull(std::string(s, c)); }
460template <
typename t_csa>
467template <
typename t_cst>
474template <
typename t_csa>
481template <
typename t_cst>
487template <
typename t_csx,
typename t_alph =
typename t_csx::alphabet_category>
493template <
typename t_csx>
499template <
typename t_csx>
532template <
typename t_
idx>
534 const std::string & format,
538 typename t_idx::index_category cat;
539 const typename t_idx::csa_type & csa =
_idx_csa(idx, cat);
540 std::vector<std::string> res(csa.size());
542 for (std::string::const_iterator c = format.begin(), s = c; c != format.end(); s = c)
544 while (c != format.end() and *c !=
'%') ++c;
547 std::vector<std::string> to_copy(csa.size(), std::string(s, c));
548 transform(res.begin(), res.end(), to_copy.begin(), res.begin(), std::plus<std::string>());
550 if (c == format.end())
break;
553 if (c == format.end())
break;
560 if (c == format.end())
break;
561 for (uint64_t i = 0; i < csa.size(); ++i)
570 case 'L': res[i] +=
_idx_lcp_val(idx, i, w, cat);
break;
580 for (uint64_t k = 0; (w > 0 and k < w) or (0 == w and k < csa.size()); ++k)
582 if (0 == csa.text[(csa[i] + k) % csa.size()])
599 for (uint64_t k = 0; (w > 0 and k < w) or (0 == w and k < csa.size()); ++k)
601 if (0 == csa.text[(i + k) % csa.size()])
616 case '%': res[i] +=
"%";
break;
621 for (
size_t i = 0; i < res.size(); ++i) out << res[i] << std::endl;
633 return config.
dir +
"/" + key +
"_" + config.
id +
".sdsl";
704inline std::string
tmp_file(
const std::string & filename, std::string name_part =
"")
714 if (add_type_hash) { file = cache_file_name<T>(key, config); }
721 if (util::verbose) { std::cerr <<
"Load `" << file << std::endl; }
726 std::cerr <<
"WARNING: Could not load file '";
727 std::cerr << file <<
"'" << std::endl;
740 if (add_type_hash) { file = cache_file_name<T>(key, config); }
747 config.
file_map[std::string(key)] = file;
752 std::cerr <<
"WARNING: store_to_cache: could not store file `" << file <<
"`" << std::endl;
761 if (add_type_hash) { file = cache_file_name<T>(key, config); }
770 std::cerr <<
"WARNING: delete_from_cache: could not delete file `" << file <<
"`" << std::endl;
793 uint64_t hash_value = util::hashvalue_of_classname(t);
800 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
803 if (util::verbose) { std::cerr <<
"ERROR: store_to_file not successful for: `" << file <<
"`" << std::endl; }
808 if (util::verbose) { std::cerr <<
"INFO: store_to_file: `" << file <<
"`" << std::endl; }
815 std::string checkfile = file +
"_check";
816 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
821 std::cerr <<
"ERROR: store_to_checked_file not successful for: `" << checkfile <<
"`" << std::endl;
832 std::string checkfile = file +
"_check";
833 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
838 std::cerr <<
"ERROR: store_to_checked_file(const char *v, const std::string&)" << std::endl;
849 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
854 std::cerr <<
"ERROR: store_to_file(const std::string& v, const std::string&)" << std::endl;
858 out.write(v.data(), v.size());
863template <u
int8_t t_w
idth>
866 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
869 std::cerr <<
"ERROR: util::store_to_file:: Could not open file `" << file <<
"`" << std::endl;
874 if (util::verbose) { std::cerr <<
"INFO: store_to_file: `" << file <<
"`" << std::endl; }
881template <u
int8_t t_w
idth>
884 std::string checkfile = file +
"_check";
885 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
888 std::cerr <<
"ERROR: util::store_to_checked_file: Could not open check file `" << checkfile <<
"`" << std::endl;
893 if (util::verbose) { std::cerr <<
"INFO: store_to_checked_file: `" << checkfile <<
"`" << std::endl; }
903 isfstream in(file, std::ios::binary | std::ios::in);
906 if (util::verbose) { std::cerr <<
"Could not load file `" << file <<
"`" << std::endl; }
911 if (util::verbose) { std::cerr <<
"Load file `" << file <<
"`" << std::endl; }
918 isfstream in(file +
"_check", std::ios::binary | std::ios::in);
921 if (util::verbose) { std::cerr <<
"Could not load check file `" << file <<
"_check`" << std::endl; }
926 if (hash_value != util::hashvalue_of_classname(v))
930 std::cerr <<
"File `" << file <<
"` is not an instance of the class `"
931 << sdsl::util::demangle2(
typeid(T).name()) <<
"`" << std::endl;
938template <
typename t_iv>
939inline typename std::enable_if<std::is_same<
typename t_iv::index_category,
940 iv_tag>::value or std::is_same<typename t_iv::index_category, csa_tag>::value or
941 std::is_same<
typename t_iv::index_category,
943 std::ostream &>::type
946 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
949 if (it + 1 != end) os <<
" ";
954template <
typename t_iv>
955inline typename std::enable_if<std::is_same<typename t_iv::index_category, wt_tag>::value, std::ostream &>::type
958 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
961 if (it + 1 != end and std::is_same<typename t_iv::alphabet_category, int_alphabet_tag>::value) os <<
" ";
966template <
typename t_
int>
967inline typename std::enable_if<std::is_integral<t_int>::value, std::ostream &>::type
operator<<(
969 const std::vector<t_int> & v)
971 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
974 if (it + 1 != end) os <<
" ";
979template <
typename t_iv>
980inline typename std::enable_if<std::is_same<typename t_iv::category, csa_member_tag>::value, std::ostream &>::type
983 for (
auto it = v.begin(), end = v.end(); it != end; ++it)
986 if (it + 1 != end and std::is_same<typename t_iv::alphabet_category, int_alphabet_tag>::value) os <<
" ";
cereal.hpp offers cereal support
A generic vector class for integers of width .
size_type size() const noexcept
The number of elements in the int_vector.
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, const std::string &name, const std::string &type)
static void add_size(structure_tree_node *v, uint64_t value)
const uint64_t SDSL_BLOCK_SIZE
int_vector ::size_type size_type
size_t file_size(const std::string &name)
Get the file size.
int truncate(const int fd, size_t new_size)
Get the content with fd.
Get the size of a file in bytes size_t file_size(const std::string &file)
Returns the directory of a file A trailing will be removed std::string dirname(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.
bool load_from_checked_file(T &v, const std::string &file)
bool load_from_cache(T &v, const std::string &key, const cache_config &config, bool add_type_hash=false)
std::enable_if< has_serialize< X >::value, typenameX::size_type >::type serialize(const X &x, std::ostream &out, structure_tree_node *v=nullptr, std::string name="")
size_t block_size(void *ptr)
bool remove_from_cache(const std::string &key, cache_config &config, bool add_type_hash=false)
std::string _idx_lcp_val(const t_csa &, uint64_t, uint64_t, csa_tag)
Internal function used by csXprintf.
void load_vector(std::vector< T > &, std::istream &)
Load all elements of a vector from a input stream.
uint64_t serialize_vector(const std::vector< T > &, std::ostream &, sdsl::structure_tree_node *v=nullptr, std::string="")
Serialize each element of an std::vector.
bool store_to_plain_array(t_int_vec &v, const std::string &file)
Store an int_vector as plain int_type array to disk.
bool load_vector_from_file(t_int_vec &v, const std::string &file, uint8_t num_bytes=1, uint8_t max_int_width=64)
from disk.
size_t write_member(const T &t, std::ostream &out, sdsl::structure_tree_node *v=nullptr, std::string name="")
void read_member(T &t, std::istream &in)
std::string tmp_file(const cache_config &config, std::string name_part="")
Returns a name for a temporary file. I.e. the name was not used before.
std::enable_if< has_load< X >::value, void >::type load(X &x, std::istream &in)
uint64_t _parse_number(std::string::const_iterator &c, const std::string::const_iterator &end)
Internal function used by csXprintf.
bool load_from_file(T &v, const std::string &file)
Load sdsl-object v from a file.
void csXprintf(std::ostream &out, const std::string &format, const t_idx &idx, char sentinel=default_sentinel< t_idx >::value)
Prints members of CSAs and CSTs.
bool store_to_checked_file(const T &t, const std::string &file)
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.
bool cache_file_exists(const std::string &key, const cache_config &config)
Checks if the resource specified by the key exists in the cache.
void _write_structure(std::unique_ptr< structure_tree_node > &st_node, X x, Xs... xs)
void write_structure(const X &x, std::ostream &out)
bool store_to_cache(const T &v, const std::string &key, cache_config &config, bool add_type_hash=false)
Stores the object v as a resource in the cache.
const t_csa & _idx_csa(const t_csa &t, csa_tag)
Internal function used by csXprintf.
int remove(const std::string &)
Remove a file.
int_vector ::size_type size(const range_type &r)
Size of a range.
size_t serialize_empty_object(std::ostream &, structure_tree_node *v=nullptr, std::string name="", const T *t=nullptr)
std::ostream & operator<<(std::ostream &os, const bp_interval< t_int > &interval)
T::size_type size_in_bytes(const T &t)
Get the size of a data structure in bytes.
double size_in_mega_bytes(const T &t)
Get the size of a data structure in mega bytes (MiB).
void add_hash(const T &t, std::ostream &out)
Contains declarations and definitions of data structure concepts.
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.