SDSL 3.0.2
Succinct Data Structure Library
Loading...
Searching...
No Matches
io.hpp
Go to the documentation of this file.
1// Copyright (c) 2016, the SDSL Project Authors. All rights reserved.
2// Please see the AUTHORS file for details. Use of this source code is governed
3// by a BSD license that can be found in the LICENSE file.
8#ifndef INCLUDED_SDSL_IO
9#define INCLUDED_SDSL_IO
10
11#include <algorithm>
12#include <cctype>
13#include <functional>
14#include <iostream>
15#include <map>
16#include <memory>
17#include <stdexcept>
18#include <string>
19#include <type_traits>
20#include <typeinfo>
21#include <unordered_map>
22#include <utility>
23#include <vector>
24
25#include <sdsl/bits.hpp>
26#include <sdsl/config.hpp>
27#include <sdsl/platform.hpp>
29#include <sdsl/sfstream.hpp>
31#include <sdsl/util.hpp>
32
33namespace sdsl
34{
35template <uint8_t = 0u>
36class int_vector;
37
38int remove(std::string const &);
39
40template <typename T>
41void load_vector(std::vector<T> &, std::istream &);
42
43template <typename T>
44uint64_t
45serialize_vector(std::vector<T> const &, std::ostream &, sdsl::structure_tree_node * v = nullptr, std::string = "");
46
47// has_serialize<X>::value is true if class X has
48// implement method serialize
49// Adapted solution from jrok's proposal:
50// http://stackoverflow.com/questions/87372/check-if-a-class-has-a-member-function-of-a-given-signature
51template <typename X>
53{
54 template <typename T>
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
60 {
61 return std::true_type();
62 }
63 template <typename>
64 static constexpr std::false_type check(...)
65 {
66 return std::false_type();
67 }
68 typedef decltype(check<X>(nullptr)) type;
69 static constexpr bool value = type::value;
70};
71
72// has_load<X>::value is true if class X has
73// implement method load
74template <typename X>
76{
77 template <typename T>
78 static constexpr auto check(T *) ->
79 typename std::is_same<decltype(std::declval<T>().load(std::declval<std::istream &>())), void>::type
80 {
81 return std::true_type();
82 }
83 template <typename>
84 static constexpr std::false_type check(...)
85 {
86 return std::false_type();
87 }
88 typedef decltype(check<X>(nullptr)) type;
89 static constexpr bool value = type::value;
90};
91
92// Writes primitive-typed variable t to stream out
93template <typename T>
94size_t write_member(T const & t, std::ostream & out, sdsl::structure_tree_node * v = nullptr, std::string name = "")
95{
96 sdsl::structure_tree_node * child = sdsl::structure_tree::add_child(v, name, util::class_name(t));
97 out.write((char *)&t, sizeof(t));
98 size_t written_bytes = sizeof(t);
99 sdsl::structure_tree::add_size(child, written_bytes);
100 return written_bytes;
101}
102
103// Specialization for std::string
104template <>
105inline size_t
106write_member<std::string>(std::string const & t, std::ostream & out, sdsl::structure_tree_node * v, std::string name)
107{
108 structure_tree_node * child = structure_tree::add_child(v, name, util::class_name(t));
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();
113 structure_tree::add_size(v, written_bytes);
114 return written_bytes;
115}
116
117// Writes primitive-typed variable t to stream out
118template <typename T>
119void read_member(T & t, std::istream & in)
120{
121 in.read((char *)&t, sizeof(t));
122}
123
124// Specialization for std::string
125template <>
126inline void read_member<std::string>(std::string & t, std::istream & in)
127{
128 std::string::size_type size;
129 read_member(size, in);
130 char * buf = new char[size];
131 in.read(buf, size);
132 std::string temp(buf, size);
133 delete[] buf;
134 t = std::move(temp);
135}
136
137template <typename X>
138typename std::enable_if<has_serialize<X>::value, typename X::size_type>::type
139serialize(X const & x, std::ostream & out, structure_tree_node * v = nullptr, std::string name = "")
140{
141 return x.serialize(out, v, name);
142}
143
144template <typename X>
145typename std::enable_if<std::is_standard_layout<X>::value && std::is_trivial<X>::value, uint64_t>::type
146serialize(X const & x, std::ostream & out, structure_tree_node * v = nullptr, std::string name = "")
147{
148 return write_member(x, out, v, name);
149}
150
151template <typename X>
152uint64_t
153serialize(std::vector<X> const & x, std::ostream & out, structure_tree_node * v = nullptr, std::string name = "")
154{
155
156 return serialize(x.size(), out, v, name) + serialize_vector(x, out, v, name);
157}
158
159template <typename X>
160typename std::enable_if<has_load<X>::value, void>::type load(X & x, std::istream & in)
161{
162 x.load(in);
163}
164
165template <typename X>
166typename std::enable_if<std::is_standard_layout<X>::value && std::is_trivial<X>::value, void>::type
167load(X & x, std::istream & in)
168{
169 read_member(x, in);
170}
171
172template <typename X>
173void load(std::vector<X> & x, std::istream & in)
174{
175 typename std::vector<X>::size_type size;
176 load(size, in);
177 x.resize(size);
178 load_vector(x, in);
179}
180
182
186template <typename T>
187bool load_from_file(T & v, std::string const & file);
188
190// TODO: Remove ENDIAN dependency.
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)
193{
194 if ((uint8_t)0 == num_bytes)
195 { // if byte size is variable read int_vector<0> from file
196 return load_from_file(v, file);
197 }
198 else if (num_bytes == 'd')
199 {
200 uint64_t x = 0, max_x = 0;
201 isfstream in(file, std::ios::in | std::ios::binary);
202 if (!in)
203 {
204 return false;
205 }
206 else
207 {
208 std::vector<uint64_t> tmp;
209 while (in >> x)
210 {
211 tmp.push_back(x);
212 max_x = std::max(x, max_x);
213 }
214 v.width(bits::hi(max_x) + 1);
215 v.resize(tmp.size());
216 for (size_t i = 0; i < tmp.size(); ++i)
217 {
218 v[i] = tmp[i];
219 }
220 return true;
221 }
222 }
223 else
224 {
225 off_t file_size = util::file_size(file);
226 if (file_size == 0)
227 {
228 v.resize(0);
229 return true;
230 }
231 if (file_size % num_bytes != 0)
232 {
233 throw std::logic_error("file size " + util::to_string(file_size) + " of \"" + file
234 + "\" is not a multiple of " + util::to_string(num_bytes));
235 return false;
236 }
237 isfstream in(file, std::ios::in | std::ios::binary);
238 if (in)
239 {
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)
243 { // if int_vector<8> is created from byte alphabet file
244 in.read((char *)v.data(), file_size);
245 }
246 else
247 {
248 size_t idx = 0;
249 const size_t block_size = conf::SDSL_BLOCK_SIZE * num_bytes;
250 std::vector<uint8_t> buf(block_size);
251 // TODO: check for larger alphabets with num_bytes*8 = v::fixed_int_width
252
253 uint64_t x = 0; // value
254 uint8_t cur_byte = 0;
255 do
256 {
257 in.read((char *)buf.data(), block_size);
258 size_t read = in.gcount();
259 uint8_t * begin = buf.data();
260 uint8_t * end = begin + read;
261 while (begin < end)
262 {
263 x |= ((uint64_t)(*begin)) << (cur_byte * 8);
264 ++cur_byte;
265 if (cur_byte == num_bytes)
266 {
267 v[idx++] = x;
268 cur_byte = 0;
269 x = 0ULL;
270 }
271 ++begin;
272 }
273 }
274 while (idx < v.size());
275 in.close();
276 }
277 return true;
278 }
279 else
280 {
281 return false;
282 }
283 }
284}
285
287
292template <typename T>
293bool store_to_file(T const & v, std::string const & file);
294
296inline bool store_to_file(char const * v, std::string const & file)
297{
298 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
299 if (!out)
300 {
301 if (util::verbose)
302 {
303 std::cerr << "ERROR: store_to_file(const char *v, const std::string&)" << std::endl;
304 return false;
305 }
306 }
307 uint64_t n = strlen((char const *)v);
308 out.write(v, n);
309 out.close();
310 return true;
311}
312
314template <uint8_t t_width>
315bool store_to_file(int_vector<t_width> const & v, std::string const & file);
316
318template <typename int_type, typename t_int_vec>
319bool store_to_plain_array(t_int_vec & v, std::string const & file)
320{
321 osfstream out(file, std::ios::out | std::ios::binary);
322 if (out)
323 {
324 for (typename t_int_vec::size_type i = 0; i < v.size(); ++i)
325 {
326 int_type x = v[i];
327 out.write((char *)&x, sizeof(int_type));
328 }
329 return true;
330 }
331 else
332 {
333 return false;
334 }
335}
336
337template <typename T>
338size_t
339serialize_empty_object(std::ostream &, structure_tree_node * v = nullptr, std::string name = "", T const * t = nullptr)
340{
341 structure_tree_node * child = structure_tree::add_child(v, name, util::class_name(*t));
342 size_t written_bytes = 0;
343 structure_tree::add_size(child, written_bytes);
344 return written_bytes;
345}
346
348
351template <typename T>
352typename T::size_type size_in_bytes(T const & t);
353
355
358template <typename T>
359double size_in_mega_bytes(T const & t);
360
361struct nullstream : std::ostream
362{
363 struct nullbuf : std::streambuf
364 {
365 int overflow(int c)
366 {
367 return traits_type::not_eof(c);
368 }
369 int xputc(int)
370 {
371 return 0;
372 }
373 std::streamsize xsputn(char const *, std::streamsize n)
374 {
375 return n;
376 }
377 int sync()
378 {
379 return 0;
380 }
382 nullstream() : std::ios(&m_sbuf), std::ostream(&m_sbuf), m_sbuf()
383 {}
384};
385
387
395template <typename T>
396uint64_t
397serialize_vector(std::vector<T> const & vec, std::ostream & out, sdsl::structure_tree_node * v, std::string name)
398{
399 if (vec.size() > 0)
400 {
402 sdsl::structure_tree::add_child(v, name, "std::vector<" + util::class_name(vec[0]) + ">");
403 size_t written_bytes = 0;
404 for (auto const & x : vec)
405 {
406 written_bytes += serialize(x, out, child, "[]");
407 }
408 structure_tree::add_size(child, written_bytes);
409 return written_bytes;
410 }
411 else
412 {
413 return 0;
414 }
415}
416
418
424template <typename T>
425void load_vector(std::vector<T> & vec, std::istream & in)
426{
427 for (typename std::vector<T>::size_type i = 0; i < vec.size(); ++i)
428 {
429 load(vec[i], in);
430 }
431}
432
433template <format_type F, typename X>
434void write_structure(X const & x, std::ostream & out)
435{
436 std::unique_ptr<structure_tree_node> st_node(new structure_tree_node("name", "type"));
437 nullstream ns;
438 serialize(x, ns, st_node.get(), "");
439 if (st_node.get()->children.size() > 0)
440 {
441 for (auto const & child : st_node.get()->children)
442 {
443 sdsl::write_structure_tree<F>(child.second.get(), out);
444 }
445 }
446}
447
448template <format_type F, typename X>
449void write_structure(X const & x, std::string file)
450{
451 std::ofstream out(file);
452 write_structure<F>(x, out);
453}
454
455template <format_type F, typename... Xs>
456void write_structure(std::ostream & out, Xs... xs)
457{
458 typedef std::unique_ptr<structure_tree_node> up_stn_type;
459 up_stn_type st_node(new structure_tree_node("name", "type"));
460 _write_structure(st_node, xs...);
461 sdsl::write_structure_tree<F>(st_node.get(), out);
462}
463
464template <typename X, typename... Xs>
465void _write_structure(std::unique_ptr<structure_tree_node> & st_node, X x, Xs... xs)
466{
467 nullstream ns;
468 serialize(x, ns, st_node.get(), "");
469 _write_structure(st_node, xs...);
470}
471
472inline void _write_structure(std::unique_ptr<structure_tree_node> &)
473{}
474
476inline uint64_t _parse_number(std::string::const_iterator & c, std::string::const_iterator const & end)
477{
478 std::string::const_iterator s = c;
479 while (c != end and isdigit(*c))
480 ++c;
481 if (c > s)
482 {
483 return std::stoull(std::string(s, c));
484 }
485 else
486 {
487 return 0;
488 }
489}
490
492template <typename t_csa>
493t_csa const & _idx_csa(t_csa const & t, csa_tag)
494{
495 return t;
496}
497
499template <typename t_cst>
500const typename t_cst::csa_type & _idx_csa(t_cst const & t, cst_tag)
501{
502 return t.csa;
503}
504
506template <typename t_csa>
507std::string _idx_lcp_val(t_csa const &, uint64_t, uint64_t, csa_tag)
508{
509 return "";
510}
511
513template <typename t_cst>
514std::string _idx_lcp_val(t_cst const & t, uint64_t i, uint64_t w, cst_tag)
515{
516 return util::to_string(t.lcp[i], w);
517}
518
519template <typename t_csx, typename t_alph = typename t_csx::alphabet_category>
521{
522 static char const value = '$';
523};
524
525template <typename t_csx>
527{
528 static char const value = '$';
529};
530
531template <typename t_csx>
533{
534 static char const value = '0';
535};
536
538
564template <typename t_idx>
565void csXprintf(std::ostream & out,
566 std::string const & format,
567 t_idx const & idx,
568 char sentinel = default_sentinel<t_idx>::value)
569{
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)
575 {
576 while (c != format.end() and *c != '%')
577 ++c; // string before the next `%`
578 if (c > s)
579 { // copy format string part
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>());
582 }
583 if (c == format.end())
584 break;
585 ++c; // skip `%`
586 uint64_t w = _parse_number(c, format.end()); // element width
587 if (c == format.end())
588 break;
589 uint64_t W = 0; // character width
590 if (':' == *c)
591 {
592 ++c;
593 W = _parse_number(c, format.end());
594 }
595 if (c == format.end())
596 break;
597 for (uint64_t i = 0; i < csa.size(); ++i)
598 {
599 switch (*c)
600 {
601 case 'I':
602 res[i] += util::to_string(i, w);
603 break;
604 case 'S':
605 res[i] += util::to_string(csa[i], w);
606 break;
607 case 's':
608 res[i] += util::to_string(csa.isa[i], w);
609 break;
610 case 'P':
611 res[i] += util::to_string(csa.psi[i], w);
612 break;
613 case 'p':
614 res[i] += util::to_string(csa.lf[i], w);
615 break;
616 case 'L':
617 res[i] += _idx_lcp_val(idx, i, w, cat);
618 break;
619 case 'B':
620 if (0 == csa.bwt[i])
621 {
622 res[i] += util::to_string(sentinel, w);
623 }
624 else
625 {
626 res[i] += util::to_string(csa.bwt[i], w);
627 }
628 break;
629 case 'U':
630 truncate = true;
632 case 'T':
633 for (uint64_t k = 0; (w > 0 and k < w) or (0 == w and k < csa.size()); ++k)
634 {
635 if (0 == csa.text[(csa[i] + k) % csa.size()])
636 {
637 res[i] += util::to_string(sentinel, W);
638 if (truncate)
639 {
640 truncate = false;
641 break;
642 }
643 }
644 else
645 {
646 res[i] += util::to_string(csa.text[(csa[i] + k) % csa.size()], W);
647 }
648 }
649 break;
650 case 'u':
651 truncate = true;
653 case 't':
654 for (uint64_t k = 0; (w > 0 and k < w) or (0 == w and k < csa.size()); ++k)
655 {
656 if (0 == csa.text[(i + k) % csa.size()])
657 {
658 res[i] += util::to_string(sentinel, W);
659 if (truncate)
660 {
661 truncate = false;
662 break;
663 }
664 }
665 else
666 {
667 res[i] += util::to_string(csa.text[(i + k) % csa.size()], W);
668 }
669 }
670 break;
671 case '%':
672 res[i] += "%";
673 break;
674 }
675 }
676 ++c;
677 }
678 for (size_t i = 0; i < res.size(); ++i)
679 out << res[i] << std::endl;
680}
681
683
688inline std::string cache_file_name(std::string const & key, cache_config const & config)
689{
690 if (config.file_map.count(key) != 0)
691 {
692 return config.file_map.at(key);
693 }
694 return config.dir + "/" + key + "_" + config.id + ".sdsl";
695}
696
698
703template <typename T>
704std::string cache_file_name(std::string const & key, cache_config const & config)
705{
706 return cache_file_name(key + "_" + util::class_to_hash(T()), config);
707}
708
710
717inline void register_cache_file(std::string const & key, cache_config & config)
718{
719 std::string file_name = cache_file_name(key, config);
720 isfstream in(file_name);
721 if (in)
722 { // if file exists, register it.
723 config.file_map[key] = file_name;
724 }
725}
726
728
733inline bool cache_file_exists(std::string const & key, cache_config const & config)
734{
735 std::string file_name = cache_file_name(key, config);
736 isfstream in(file_name);
737 if (in)
738 {
739 in.close();
740 return true;
741 }
742 return false;
743}
744
746
752template <typename T>
753bool cache_file_exists(std::string const & key, cache_config const & config)
754{
755 return cache_file_exists(key + "_" + util::class_to_hash(T()), config);
756}
757
759inline std::string tmp_file(cache_config const & config, std::string name_part = "")
760{
761 return config.dir + "/" + util::to_string(util::pid()) + "_" + util::to_string(util::id()) + name_part + ".sdsl";
762}
763
765inline std::string tmp_file(std::string const & filename, std::string name_part = "")
766{
767 return util::dirname(filename) + "/" + util::to_string(util::pid()) + "_" + util::to_string(util::id()) + name_part
768 + ".sdsl";
769}
770
771template <typename T>
772bool load_from_cache(T & v, std::string const & key, cache_config const & config, bool add_type_hash = false)
773{
774 std::string file;
775 if (add_type_hash)
776 {
777 file = cache_file_name<T>(key, config);
778 }
779 else
780 {
781 file = cache_file_name(key, config);
782 }
783 if (load_from_file(v, file))
784 {
785 if (util::verbose)
786 {
787 std::cerr << "Load `" << file << std::endl;
788 }
789 return true;
790 }
791 else
792 {
793 std::cerr << "WARNING: Could not load file '";
794 std::cerr << file << "'" << std::endl;
795 return false;
796 }
797}
798
800
803template <typename T>
804bool store_to_cache(T const & v, std::string const & key, cache_config & config, bool add_type_hash = false)
805{
806 std::string file;
807 if (add_type_hash)
808 {
809 file = cache_file_name<T>(key, config);
810 }
811 else
812 {
813 file = cache_file_name(key, config);
814 }
815 if (store_to_file(v, file))
816 {
817 config.file_map[std::string(key)] = file;
818 return true;
819 }
820 else
821 {
822 std::cerr << "WARNING: store_to_cache: could not store file `" << file << "`" << std::endl;
823 return false;
824 }
825}
826
827template <typename T>
828bool remove_from_cache(std::string const & key, cache_config & config, bool add_type_hash = false)
829{
830 std::string file;
831 if (add_type_hash)
832 {
833 file = cache_file_name<T>(key, config);
834 }
835 else
836 {
837 file = cache_file_name(key, config);
838 }
839 config.file_map.erase(key);
840 if (sdsl::remove(file) == 0)
841 {
842 return true;
843 }
844 else
845 {
846 std::cerr << "WARNING: delete_from_cache: could not delete file `" << file << "`" << std::endl;
847 return false;
848 }
849}
850
851//==================== Template functions ====================
852
853template <typename T>
854typename T::size_type size_in_bytes(T const & t)
855{
856 nullstream ns;
857 return serialize(t, ns);
858}
859
860template <typename T>
861double size_in_mega_bytes(T const & t)
862{
863 return size_in_bytes(t) / (1024.0 * 1024.0);
864}
865
866template <typename T>
867void add_hash(T const & t, std::ostream & out)
868{
869 uint64_t hash_value = util::hashvalue_of_classname(t);
870 write_member(hash_value, out);
871}
872
873template <typename T>
874bool store_to_file(T const & t, std::string const & file)
875{
876 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
877 if (!out)
878 {
879 if (util::verbose)
880 {
881 std::cerr << "ERROR: store_to_file not successful for: `" << file << "`" << std::endl;
882 }
883 return false;
884 }
885 serialize(t, out);
886 out.close();
887 if (util::verbose)
888 {
889 std::cerr << "INFO: store_to_file: `" << file << "`" << std::endl;
890 }
891 return true;
892}
893
894template <typename T>
895bool store_to_checked_file(T const & t, std::string const & file)
896{
897 std::string checkfile = file + "_check";
898 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
899 if (!out)
900 {
901 if (util::verbose)
902 {
903 std::cerr << "ERROR: store_to_checked_file not successful for: `" << checkfile << "`" << std::endl;
904 }
905 return false;
906 }
907 add_hash(t, out);
908 out.close();
909 return store_to_file(t, file);
910}
911
912inline bool store_to_checked_file(char const * v, std::string const & file)
913{
914 std::string checkfile = file + "_check";
915 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
916 if (!out)
917 {
918 if (util::verbose)
919 {
920 std::cerr << "ERROR: store_to_checked_file(const char *v, const std::string&)" << std::endl;
921 return false;
922 }
923 }
924 add_hash(v, out);
925 out.close();
926 return store_to_file(v, file);
927}
928
929inline bool store_to_file(std::string const & v, std::string const & file)
930{
931 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
932 if (!out)
933 {
934 if (util::verbose)
935 {
936 std::cerr << "ERROR: store_to_file(const std::string& v, const std::string&)" << std::endl;
937 return false;
938 }
939 }
940 out.write(v.data(), v.size());
941 out.close();
942 return true;
943}
944
945template <uint8_t t_width>
946bool store_to_file(int_vector<t_width> const & v, std::string const & file)
947{
948 osfstream out(file, std::ios::binary | std::ios::trunc | std::ios::out);
949 if (!out)
950 {
951 std::cerr << "ERROR: util::store_to_file:: Could not open file `" << file << "`" << std::endl;
952 return false;
953 }
954 else
955 {
956 if (util::verbose)
957 {
958 std::cerr << "INFO: store_to_file: `" << file << "`" << std::endl;
959 }
960 }
961 v.serialize(out, nullptr, "");
962 out.close();
963 return true;
964}
965
966template <uint8_t t_width>
967bool store_to_checked_file(int_vector<t_width> const & v, std::string const & file)
968{
969 std::string checkfile = file + "_check";
970 osfstream out(checkfile, std::ios::binary | std::ios::trunc | std::ios::out);
971 if (!out)
972 {
973 std::cerr << "ERROR: util::store_to_checked_file: Could not open check file `" << checkfile << "`" << std::endl;
974 return false;
975 }
976 else
977 {
978 if (util::verbose)
979 {
980 std::cerr << "INFO: store_to_checked_file: `" << checkfile << "`" << std::endl;
981 }
982 }
983 add_hash(v, out);
984 out.close();
985 return store_to_file(v, file);
986}
987
988template <typename T>
989bool load_from_file(T & v, std::string const & file)
990{
991 isfstream in(file, std::ios::binary | std::ios::in);
992 if (!in)
993 {
994 if (util::verbose)
995 {
996 std::cerr << "Could not load file `" << file << "`" << std::endl;
997 }
998 return false;
999 }
1000 load(v, in);
1001 in.close();
1002 if (util::verbose)
1003 {
1004 std::cerr << "Load file `" << file << "`" << std::endl;
1005 }
1006 return true;
1007}
1008
1009template <typename T>
1010bool load_from_checked_file(T & v, std::string const & file)
1011{
1012 isfstream in(file + "_check", std::ios::binary | std::ios::in);
1013 if (!in)
1014 {
1015 if (util::verbose)
1016 {
1017 std::cerr << "Could not load check file `" << file << "_check`" << std::endl;
1018 }
1019 return false;
1020 }
1021 uint64_t hash_value;
1022 read_member(hash_value, in);
1023 if (hash_value != util::hashvalue_of_classname(v))
1024 {
1025 if (util::verbose)
1026 {
1027 std::cerr << "File `" << file << "` is not an instance of the class `"
1028 << sdsl::util::demangle2(typeid(T).name()) << "`" << std::endl;
1029 }
1030 return false;
1031 }
1032 return load_from_file(v, file);
1033}
1034
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
1040operator<<(std::ostream & os, t_iv const & v)
1041{
1042 for (auto it = v.begin(), end = v.end(); it != end; ++it)
1043 {
1044 os << *it;
1045 if (it + 1 != end)
1046 os << " ";
1047 }
1048 return os;
1049}
1050
1051template <typename t_iv>
1052inline typename std::enable_if<std::is_same<typename t_iv::index_category, wt_tag>::value, std::ostream &>::type
1053operator<<(std::ostream & os, t_iv const & v)
1054{
1055 for (auto it = v.begin(), end = v.end(); it != end; ++it)
1056 {
1057 os << *it;
1058 if (it + 1 != end and std::is_same<typename t_iv::alphabet_category, int_alphabet_tag>::value)
1059 os << " ";
1060 }
1061 return os;
1062}
1063
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)
1067{
1068 for (auto it = v.begin(), end = v.end(); it != end; ++it)
1069 {
1070 os << *it;
1071 if (it + 1 != end)
1072 os << " ";
1073 }
1074 return os;
1075}
1076
1077template <typename t_iv>
1078inline typename std::enable_if<std::is_same<typename t_iv::category, csa_member_tag>::value, std::ostream &>::type
1079operator<<(std::ostream & os, t_iv const & v)
1080{
1081 for (auto it = v.begin(), end = v.end(); it != end; ++it)
1082 {
1083 os << *it;
1084 if (it + 1 != end and std::is_same<typename t_iv::alphabet_category, int_alphabet_tag>::value)
1085 os << " ";
1086 }
1087 return os;
1088}
1089} // namespace sdsl
1090#endif
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.
Definition sfstream.hpp:264
void close()
Close the stream.
Definition sfstream.hpp:91
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
Definition config.hpp:32
Returns the directory of a file A trailing will be removed std::string dirname(std::string file)
Definition util.hpp:215
uint64_t id()
Get the size of a file in bytes size_t file_size(std::string const &file)
Definition util.hpp:173
uint64_t pid()
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.
Definition io.hpp:565
void read_member< std::string >(std::string &t, std::istream &in)
Definition io.hpp:126
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.
Definition io.hpp:804
std::ostream & operator<<(std::ostream &os, bp_interval< t_int > const &interval)
void write_structure(X const &x, std::ostream &out)
Definition io.hpp:434
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.
Definition io.hpp:733
int remove(std::string const &)
Remove a file.
Definition ram_fs.hpp:221
void load_vector(std::vector< T > &, std::istream &)
Load all elements of a vector from a input stream.
Definition io.hpp:425
double size_in_mega_bytes(T const &t)
Get the size of a data structure in mega bytes (MiB).
Definition io.hpp:861
std::string _idx_lcp_val(t_csa const &, uint64_t, uint64_t, csa_tag)
Internal function used by csXprintf.
Definition io.hpp:507
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.
Definition io.hpp:759
std::string cache_file_name(std::string const &key, cache_config const &config)
Returns the file name of the resource.
Definition io.hpp:688
size_t write_member(T const &t, std::ostream &out, sdsl::structure_tree_node *v=nullptr, std::string name="")
Definition io.hpp:94
bool load_from_file(T &v, std::string const &file)
Load sdsl-object v from a file.
Definition io.hpp:989
void add_hash(T const &t, std::ostream &out)
Definition io.hpp:867
void register_cache_file(std::string const &key, cache_config &config)
Register the existing resource specified by the key to the cache.
Definition io.hpp:717
T::size_type size_in_bytes(T const &t)
Get the size of a data structure in bytes.
Definition io.hpp:854
void read_member(T &t, std::istream &in)
Definition io.hpp:119
std::enable_if< has_load< X >::value, void >::type load(X &x, std::istream &in)
Definition io.hpp:160
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="")
Definition io.hpp:139
bool load_from_cache(T &v, std::string const &key, cache_config const &config, bool add_type_hash=false)
Definition io.hpp:772
bool remove_from_cache(std::string const &key, cache_config &config, bool add_type_hash=false)
Definition io.hpp:828
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.
Definition io.hpp:192
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.
Definition io.hpp:397
bool load_from_checked_file(T &v, std::string const &file)
Definition io.hpp:1010
size_t write_member< std::string >(std::string const &t, std::ostream &out, sdsl::structure_tree_node *v, std::string name)
Definition io.hpp:106
uint64_t _parse_number(std::string::const_iterator &c, std::string::const_iterator const &end)
Internal function used by csXprintf.
Definition io.hpp:476
t_csa const & _idx_csa(t_csa const &t, csa_tag)
Internal function used by csXprintf.
Definition io.hpp:493
bool store_to_file(T const &v, std::string const &file)
Store a data structure to a file.
Definition io.hpp:874
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)
Definition io.hpp:465
format_type
Definition config.hpp:52
bool store_to_checked_file(T const &t, std::string const &file)
Definition io.hpp:895
size_t serialize_empty_object(std::ostream &, structure_tree_node *v=nullptr, std::string name="", T const *t=nullptr)
Definition io.hpp:339
bool store_to_plain_array(t_int_vec &v, std::string const &file)
Store an int_vector as plain int_type array to disk.
Definition io.hpp:319
util.hpp contains platform dependend macros.
#define SDSL_FALLTHROUGH
Definition platform.hpp:24
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.
Definition bits.hpp:652
Helper class for construction process.
Definition config.hpp:66
std::string id
Definition config.hpp:71
std::string dir
Definition config.hpp:70
static char const value
Definition io.hpp:522
decltype(check< X >(nullptr)) type
Definition io.hpp:88
static constexpr std::false_type check(...)
Definition io.hpp:84
static constexpr auto check(T *) -> typename std::is_same< decltype(std::declval< T >().load(std::declval< std::istream & >())), void >::type
Definition io.hpp:78
static constexpr bool value
Definition io.hpp:89
decltype(check< X >(nullptr)) type
Definition io.hpp:68
static constexpr std::false_type check(...)
Definition io.hpp:64
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
Definition io.hpp:55
static constexpr bool value
Definition io.hpp:69
std::streamsize xsputn(char const *, std::streamsize n)
Definition io.hpp:373
int overflow(int c)
Definition io.hpp:365
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.