📄 cluto_ifstream_matrix.hpp
字号:
_dense = false;
_graph = false;
return;
}
_f.clear();
_f.seekg(0, std::ios_base::beg);
index_type maybe_nrows;
size_type maybe_ncols;
_f >> maybe_nrows;
_f >> maybe_ncols;
getline( _f, line );
while (_f)
{
getline( _f, line );
_line.clear(); _line.str(line);
if (detect_graph_and_dense_check_line(maybe_nrows, maybe_ncols))
{
return;
}
}
// if we've gotten all the way through the matrix and
// there still isn't anything left...
_dense = true;
_graph = false;
}
void detect_dense()
{
std::string line;
getline( _f, line );
_line.clear(); _line.str(line);
int tok_count=0;
while (_line)
{
size_type st;
_line >> st;
tok_count++;
}
if (tok_count == 1)
{
_dense = true;
}
else
{
_dense = false;
}
_f.clear();
_f.seekg(0, std::ios_base::beg);
return;
}
/**
* Checks the current _line to determine if the
* input is dense or sparse.
*
* @return true if detection occured, false otherwise
*/
bool detect_graph_and_dense_check_line(index_type maybe_nrows, index_type maybe_ncols)
{
using boost::lexical_cast;
using boost::bad_lexical_cast;
int tok_count = 0;
std::string tok;
while (!_line.eof())
{
/*index_type i;
_line >> i;
if (_line.eof())
{
// the correct behavior will happen if we
// just wrap around...
}
else if (!_line.fail())
{
if (i < 1 || i > maybe_ncols)
{
_dense = true;
_graph = false;
return (true);
}
// we always have to be
// able to parse a value...
value_type v;
_line >> v;
tok_count+=2;
}
else
{
_dense = true;
_graph = false;
return (true);
}*/
try
{
_line >> tok;
index_type i = lexical_cast<index_type>(tok);
if (i < 1 || i > maybe_ncols)
{
_dense = true;
_graph = false;
return (true);
}
_line >> tok;
value_type v = lexical_cast<value_type>(tok);
tok_count += 2;
}
catch (bad_lexical_cast e)
{
_dense = true;
_graph = false;
return (true);
}
}
if (tok_count == maybe_ncols)
{
return (false);
}
else
{
_dense = false;
_graph = true;
return (true);
}
}
};
template <class i_index_type, class i_value_type, class i_size_type>
struct smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> >
{
typedef i_size_type size_type;
typedef i_index_type index_type;
typedef i_value_type value_type;
typedef boost::tuple<index_type, index_type, value_type> nonzero_descriptor;
typedef impl::cluto_ifstream_matrix_const_iterator<i_index_type, i_value_type> nonzero_iterator;
typedef size_type nz_index_type;
typedef void row_iterator;
typedef void row_nonzero_descriptor;
typedef void row_nonzero_iterator;
typedef void column_iterator;
};
template <class i_index_type, class i_value_type, class i_size_type>
inline std::pair<typename smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::size_type,
typename smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::size_type >
dimensions(cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type>& m)
{
typedef smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
typename traits::size_type nrows,ncols;
m._f.clear();
m._f.seekg(0, std::ios_base::beg);
if (m._graph)
{
m._f >> nrows;
ncols = nrows;
}
else
{
m._f >> nrows >> ncols;
}
return (std::make_pair(nrows, ncols));
}
template <class i_index_type, class i_value_type, class i_size_type>
typename smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::size_type
nnz(cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type>& m)
{
typedef smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
typename traits::index_type d1, d2;
typename traits::size_type nnz;
typedef typename traits::size_type size_type;
// clear any error bits
m._f.clear();
m._f.seekg(0, std::ios_base::beg);
if (m._dense)
{
if (m._graph)
{
m._f >> nnz;
return (nnz*nnz);
}
else
{
m._f >> d1 >> d2;
return (((size_type)d1)*((size_type)d2));
}
}
else
{
if (m._graph)
{
m._f >> d1 >> nnz;
return (nnz);
}
else
{
m._f >> d1 >> d2 >> nnz;
return (nnz);
}
}
}
template <class i_index_type, class i_value_type, class i_size_type>
std::pair<typename smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::nonzero_iterator,
typename smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> >::nonzero_iterator>
nonzeros(cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type>& m)
{
typedef smatrix_traits<cluto_ifstream_matrix<i_index_type, i_value_type, i_size_type> > traits;
//typename traits::size_type ncols;
m._f.clear();
m._f.seekg(0, std::ios_base::beg);
//getline(m._f, header_line);
//m._f >> d1 >> d2 >> d3;
// seek after nrows,ncols,nnz
//m._f.seekg(2*sizeof(i_index_type)+sizeof(i_size_type), std::ios_base::beg);
typedef typename traits::nonzero_iterator nz_iter;
return (std::make_pair(nz_iter(m._f, m._line, m._dense), nz_iter()));
}
}
#ifdef BOOST_MSVC
#if _MSC_VER >= 1400
// restore the warning for ifstream::read
#pragma warning( pop )
#endif // _MSC_VER >= 1400
#endif // BOOST_MSVC
#endif //YASMIC_CLUTO_IFSTREAM_MATRIX
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -