Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. /* core tpie configurations and initializations */
  2. #include <tpie/tpie.h>
  3. #include <tpie/file_stream.h>
  4. #include <string>
  5. #include <ctime>
  6.  
  7. using std::cerr;
  8. using std::endl;
  9.  
  10.  
  11. /* a basic struct storing a 3D segment */
  12. struct segment_t {
  13. double x1, y1, x2, y2;
  14. float z1, z2;
  15. };
  16.  
  17. struct segmentstr_t {
  18. char* str1;
  19. };
  20. struct segmentstr_t make_segment(int x){
  21. struct segmentstr_t s;
  22. s.str1 = (char*)malloc(sizeof(char)*10);
  23. strcpy(s.str1,"Hello");
  24. return s;
  25. }
  26.  
  27.  
  28.  
  29. /* make a dummy segment with given x1,y1, but
  30. * setting other coordinates to 0 */
  31. struct segment_t make_segment(double x1, double y1){
  32. struct segment_t s;
  33. s.x1=x1; s.y1=y1;
  34. s.x2=s.y2=s.z1=s.z2=0.f;
  35. return s;
  36. }
  37.  
  38. /* print x1, y1 coords of segment */
  39. void print_item(const struct segment_t& item){
  40. printf("(%6.2f %6.2f)", item.x1, item.y1);
  41. }
  42.  
  43. void print_item(const struct segmentstr_t& item){
  44. printf("%s", item.str1);
  45. }
  46.  
  47.  
  48. /* write n segment items to tpie::file_stream with given
  49. * output file name fname. Overwrites any previous file */
  50. void writestream(const std::string& fname, size_t n){
  51.  
  52. tpie::file_stream<struct segmentstr_t> out;
  53. out.open(fname, tpie::open::write_only);
  54. for(size_t i=0; i<n; i++){
  55. struct segmentstr_t item = make_segment(i);
  56. //print_item(item); printf("\n");
  57. out.write(item);
  58. }
  59. }
  60.  
  61. /* Read all segments from a tpie::file_stream with
  62. * file name given by fname and prints results */
  63. void readstream(const std::string& fname){
  64.  
  65. tpie::file_stream<struct segmentstr_t> in;
  66. in.open(fname, tpie::open::read_only);
  67. printf("size of stream: %6ld\n\n", in.size());
  68. while (in.can_read()) {
  69. segmentstr_t item = in.read();
  70. //print_item(item); printf("\n");
  71. }
  72. }
  73.  
  74. /* copy infile name to outfile name. Assumes infile
  75. * is a valid tpie::file_stream and overwrites outfile
  76. */
  77. void copystream(const std::string & infile,
  78. const std::string & outfile) {
  79. tpie::file_stream<struct segmentstr_t> in;
  80. tpie::file_stream<struct segmentstr_t> out;
  81.  
  82. in.open(infile);
  83. out.open(outfile, tpie::open::write_only);
  84. while (in.can_read()) {
  85. segmentstr_t item = in.read();
  86. out.write(item);
  87. }
  88. }
  89.  
  90.  
  91. /* core subsystems needed for using file_streams */
  92. #define SUBSYS tpie::MEMORY_MANAGER | tpie::STREAMS
  93.  
  94.  
  95. int main(int /*argc*/, char ** /*argv*/) {
  96. size_t memory_limit_mb = 10000;
  97.  
  98.  
  99.  
  100. /* start up TPIE subsystems, set memory limit */
  101. tpie::tpie_init(SUBSYS);
  102. tpie::get_memory_manager().set_limit(memory_limit_mb*1024*1024);
  103.  
  104. std::string a = "test.in";
  105. std::string b = "test.out";
  106. //cerr << "Writing " << a << endl;
  107. clock_t begin = clock();
  108. writestream(a, 70000000);
  109. clock_t end = clock();
  110. //cerr << "Copying " << a << " to " << b << endl;
  111. copystream(a, b);
  112. //cerr << "Reading " << b << endl;
  113. readstream(b);
  114.  
  115. double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  116. cerr << elapsed_secs << endl;
  117. /* wrap up */
  118. tpie::tpie_finish(SUBSYS);
  119. return EXIT_SUCCESS;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement