summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjan.reininghaus <jan.reininghaus@8e3bb3c2-eed4-f18f-5264-0b6c94e6926d>2013-07-16 12:14:42 +0000
committerjan.reininghaus <jan.reininghaus@8e3bb3c2-eed4-f18f-5264-0b6c94e6926d>2013-07-16 12:14:42 +0000
commita46bd27517be340115c34df6c38ceaa25625f0b4 (patch)
tree09421be3abd756c937fb0b013bd705a5e57ccfe0
parent85132716d335a05bb35f8bb48c65d1c6dce934d3 (diff)
new binary format and convert utility
git-svn-id: https://phat.googlecode.com/svn/trunk@135 8e3bb3c2-eed4-f18f-5264-0b6c94e6926d
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/phat/boundary_matrix.h98
-rw-r--r--src/convert.cpp70
3 files changed, 143 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 75ac88f..1d2c34e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -51,6 +51,7 @@ add_executable (self_test src/self_test.cpp ${all_headers})
add_executable (phat src/phat.cpp ${all_headers})
add_executable (info src/info.cpp ${all_headers})
add_executable (benchmark src/benchmark.cpp ${all_headers})
+add_executable (convert src/convert.cpp ${all_headers})
source_group(Header\ Files FILES ${general_includes})
source_group(Header\ Files\\helpers FILES ${helpers_includes})
diff --git a/include/phat/boundary_matrix.h b/include/phat/boundary_matrix.h
index 1bdee61..bcb2541 100644
--- a/include/phat/boundary_matrix.h
+++ b/include/phat/boundary_matrix.h
@@ -205,6 +205,7 @@ namespace phat {
}
}
+
// Loads the boundary_matrix from given file in ascii format
// Format: each line represents a column, first number is dimension, other numbers are the content of the column.
// Ignores empty lines and lines starting with a '#'.
@@ -277,53 +278,98 @@ namespace phat {
return true;
}
+
+
+
// Loads boundary_matrix from given file
- // Format: nr_columns % dim1 % N1 % row1 row2 % ...% rowN1 % dim2 % N2 % ...
+ // Format: version % num_cols (N) % max_dim % 0 % 0 % 0 % 0 % 0 % dim1 % ... % dimN % offset1 % ... % offsetN % num_entries (M) % entry1 % ... % entryM
+ // [ Legacy Format: nr_columns % dim1 % N1 % row1 row2 % ...% rowN1 % dim2 % N2 % ... ]
bool load_binary( std::string filename ) {
std::ifstream input_stream( filename.c_str(), std::ios_base::binary | std::ios_base::in );
if( input_stream.fail() )
return false;
- int64_t nr_columns;
- input_stream.read( (char*)&nr_columns, sizeof( int64_t ) );
- this->set_num_cols( (index)nr_columns );
+ int64_t version;
+ input_stream.read( (char*)&version, sizeof( int64_t ) );
+ if( version >= 0 ) {
+ int64_t nr_columns = version;
+ this->set_num_cols( (index)nr_columns );
+ column temp_col;
+ for( index cur_col = 0; cur_col < nr_columns; cur_col++ ) {
+ int64_t cur_dim;
+ input_stream.read( (char*)&cur_dim, sizeof( int64_t ) );
+ this->set_dim( cur_col, (dimension) cur_dim );
+ int64_t nr_rows;
+ input_stream.read( (char*)&nr_rows, sizeof( int64_t ) );
+ temp_col.resize( (std::size_t)nr_rows );
+ for( index idx = 0; idx < nr_rows; idx++ ) {
+ int64_t cur_row;
+ input_stream.read( (char*)&cur_row, sizeof( int64_t ) );
+ temp_col[ idx ] = (index)cur_row;
+ }
+ this->set_col( cur_col, temp_col );
+ }
+ } else {
+ std::vector< int64_t > preamble( 7, 0 );
+ input_stream.read( (char*)&preamble[ 0 ], sizeof( int64_t ) * preamble.size() );
+ index num_cols = preamble[ 0 ];
+ set_num_cols( num_cols );
- column temp_col;
- for( index cur_col = 0; cur_col < nr_columns; cur_col++ ) {
- int64_t cur_dim;
- input_stream.read( (char*)&cur_dim, sizeof( int64_t ) );
- this->set_dim( cur_col, (dimension) cur_dim );
- int64_t nr_rows;
- input_stream.read( (char*)&nr_rows, sizeof( int64_t ) );
- temp_col.resize( (std::size_t)nr_rows );
- for( index idx = 0; idx < nr_rows; idx++ ) {
- int64_t cur_row;
- input_stream.read( (char*)&cur_row, sizeof( int64_t ) );
- temp_col[ idx ] = (index)cur_row;
+ std::vector< int64_t > buffer( num_cols + 1, -1 );
+
+ input_stream.read( (char*)&buffer[ 0 ], sizeof( int64_t ) * num_cols );
+ for( index cur_col = 0; cur_col < num_cols; cur_col++ )
+ set_dim( cur_col, (dimension)buffer[ cur_col ] );
+
+ input_stream.read( (char*)&buffer[ 0 ], sizeof( int64_t ) * (num_cols + 1) );
+
+ column temp_col;
+ for( index cur_col = 0; cur_col < num_cols; cur_col++ ) {
+ index num_rows = buffer[ cur_col + 1 ] - buffer[ cur_col ];
+ temp_col.resize( num_rows );
+ input_stream.read( (char*)&temp_col[ 0 ], sizeof( int64_t ) * temp_col.size() );
+ set_col( cur_col, temp_col );
}
- this->set_col( cur_col, temp_col );
}
input_stream.close();
return true;
}
- // Saves the boundary_matrix to given file in binary format
- // Format: nr_columns % dim1 % N1 % row1 row2 % ...% rowN1 % dim2 % N2 % ...
+ // Saves the boundary_matrix to given file in binary format -- all symbols are 64 bit wide
+ // Format: version % num_cols (N) % max_dim % 0 % 0 % 0 % 0 % 0 % dim1 % ... % dimN % offset1 % ... % offsetN % num_entries (M) % entry1 % ... % entryM
bool save_binary( std::string filename ) {
std::ofstream output_stream( filename.c_str(), std::ios_base::binary | std::ios_base::out );
if( output_stream.fail() )
return false;
- const int64_t nr_columns = this->get_num_cols();
- output_stream.write( (char*)&nr_columns, sizeof( int64_t ) );
- column tempCol;
- for( index cur_col = 0; cur_col < nr_columns; cur_col++ ) {
- int64_t cur_dim = this->get_dim( cur_col );
+ int64_t version = -1;
+ output_stream.write( (char*)&version, sizeof( int64_t ) );
+
+ const index num_cols = get_num_cols();
+
+ std::vector< int64_t > preamble( 7, 0 );
+ preamble[ 0 ] = num_cols;
+ preamble[ 1 ] = get_max_dim();
+ output_stream.write( (char*)&preamble[ 0 ], sizeof( int64_t ) * preamble.size() );
+
+ for( index cur_col = 0; cur_col < num_cols; cur_col++ ) {
+ int64_t cur_dim = get_dim( cur_col );
output_stream.write( (char*)&cur_dim, sizeof( int64_t ) );
+ }
+
+ index cur_offset = 0;
+ for( index cur_col = 0; cur_col < num_cols; cur_col++ ) {
+ output_stream.write( (char*)&cur_offset, sizeof( int64_t ) );
+ cur_offset += get_num_rows( cur_col );
+ }
+
+ const index num_entries = get_num_entries();
+ output_stream.write( (char*)&num_entries, sizeof( int64_t ) );
+
+ column tempCol;
+ for( index cur_col = 0; cur_col < num_cols; cur_col++ ) {
this->get_col( cur_col, tempCol );
- int64_t cur_nr_rows = tempCol.size();
- output_stream.write( (char*)&cur_nr_rows, sizeof( int64_t ) );
for( index cur_row_idx = 0; cur_row_idx < (index)tempCol.size(); cur_row_idx++ ) {
int64_t cur_row = tempCol[ cur_row_idx ];
output_stream.write( (char*)&cur_row, sizeof( int64_t ) );
diff --git a/src/convert.cpp b/src/convert.cpp
new file mode 100644
index 0000000..ae3304a
--- /dev/null
+++ b/src/convert.cpp
@@ -0,0 +1,70 @@
+/* Copyright 2013 IST Austria
+ Contributed by: Ulrich Bauer, Michael Kerber, Jan Reininghaus
+
+ This file is part of PHAT.
+
+ PHAT is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ PHAT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PHAT. If not, see <http://www.gnu.org/licenses/>. */
+
+
+#include <phat/representations/vector_vector.h>
+#include <phat/boundary_matrix.h>
+
+void print_help() {
+ std::cerr << "Usage: " << "convert " << "[options] input_filename output_filename" << std::endl;
+ std::cerr << std::endl;
+ std::cerr << "Options:" << std::endl;
+ std::cerr << std::endl;
+ std::cerr << "--ascii -- use ascii file format for input_filename" << std::endl;
+ std::cerr << "--binary -- use binary file format for input_filename (default)" << std::endl;
+ std::cerr << "--help -- prints this screen" << std::endl;
+}
+
+void print_help_and_exit() {
+ print_help();
+ exit( EXIT_FAILURE );
+}
+
+void parse_command_line( int argc, char** argv, bool& use_binary, std::string& input_filename, std::string& output_filename) {
+
+ if( argc < 3 ) print_help_and_exit();
+
+ input_filename = argv[ argc - 2 ];
+ output_filename = argv[ argc - 1 ];
+
+ for( int idx = 1; idx < argc - 2; idx++ ) {
+ const std::string option = argv[ idx ];
+
+ if( option == "--ascii" ) use_binary = false;
+ else if( option == "--binary" ) use_binary = true;
+ else if( option == "--help" ) print_help_and_exit();
+ else print_help_and_exit();
+ }
+}
+
+int main( int argc, char** argv )
+{
+ bool use_binary = true; // interpret input as binary or ascii file
+ std::string input_filename; // name of file that contains the boundary matrix
+ std::string output_filename; // name of file that will contain the boundary matrix in the new binary format
+
+ parse_command_line( argc, argv, use_binary, input_filename, output_filename );
+
+ phat::boundary_matrix< phat::bit_tree_pivot_column > matrix;
+ if( use_binary )
+ matrix.load_binary( input_filename );
+ else
+ matrix.load_ascii( input_filename );
+
+ matrix.save_binary( output_filename );
+}