00001 #ifndef INDII_ML_DATA_READER_HPP 00002 #define INDII_ML_DATA_READER_HPP 00003 00004 #include "../aux/vector.hpp" 00005 #include "../aux/matrix.hpp" 00006 00007 #include <iostream> 00008 #include <string> 00009 #include <vector> 00010 00011 namespace indii { 00012 namespace ml { 00013 namespace data { 00014 00015 /** 00016 * Abstract reader for %data files. 00017 * 00018 * @author Lawrence Murray <lawrence@indii.org> 00019 * @version $Rev: 582 $ 00020 * @date $Date: 2008-12-15 17:03:50 +0000 (Mon, 15 Dec 2008) $ 00021 */ 00022 class Reader { 00023 public: 00024 /** 00025 * Construct new reader from an input stream, where all columns are of 00026 * interest. 00027 * 00028 * @param in Stream from which to read. 00029 */ 00030 Reader(std::istream* in); 00031 00032 /** 00033 * Construct new reader from a file, where all columns are of interest. 00034 * 00035 * @param file Name of file from which to read. 00036 */ 00037 Reader(const std::string& file); 00038 00039 /** 00040 * Construct new reader from an input stream, where only a 00041 * single column is of interest. 00042 * 00043 * @param in Stream from which to read. 00044 * @param col Index of the column of interest. 00045 */ 00046 Reader(std::istream* in, unsigned int col); 00047 00048 /** 00049 * Construct new reader from a file, where only a single column 00050 * is of interest. 00051 * 00052 * @param file Name of file from which to read. 00053 * @param col Index of the column of interest . 00054 */ 00055 Reader(const std::string& file, unsigned int col); 00056 00057 /** 00058 * Construct new reader from an input stream, where only a 00059 * subset of columns are of interest. 00060 * 00061 * @param in Stream from which to read. 00062 * @param cols Indices of the columns of interest from the input 00063 * stream, in the order of interest. 00064 */ 00065 Reader(std::istream* in, const std::vector<unsigned int>& cols); 00066 00067 /** 00068 * Construct new reader from a file, where only a subset of 00069 * columns are of interest. 00070 * 00071 * @param file Name of file from which to read. 00072 * @param cols Indices of the columns of interest from the input 00073 * stream, in the order of interest. 00074 */ 00075 Reader(const std::string& file, const std::vector<unsigned int>& cols); 00076 00077 /** 00078 * Destructor. The input stream is closed if the object was created 00079 * using a file name, but left open otherwise. 00080 */ 00081 virtual ~Reader(); 00082 00083 /** 00084 * Read next value. 00085 * 00086 * @param into Double into which to read the value. 00087 * 00088 * @return Number of values actually read. Will be 1 if a value is 00089 * successfully read, and 0 if the end of the stream is reached 00090 * during reading. 00091 * 00092 * One value is read from the input stream into @c into. If a 00093 * particular column or subset of columns of interest have been 00094 * specified, all others are skipped during the reading. 00095 */ 00096 virtual unsigned int read(double* into) = 0; 00097 00098 /** 00099 * Read next values into vector. 00100 * 00101 * @param into Vector into which to read the values. 00102 * 00103 * @return Number of values actually read. Will be less than the 00104 * size of the given vector if the end of the stream is reached 00105 * during reading. 00106 * 00107 * <tt>into.size()</tt> values are read from the input stream into 00108 * @c into. If a particular column or subset of columns of interest 00109 * have been specified, all others are skipped during the reading. 00110 */ 00111 virtual unsigned int read(indii::ml::aux::vector* into) = 0; 00112 00113 /** 00114 * Read next values into matrix. 00115 * 00116 * @param into Matrix into which to read the values. 00117 * 00118 * @return Number of values actually read. Will be less than the 00119 * size of the given matrix if the end of the stream is reached 00120 * during reading. 00121 * 00122 * <tt>into.size1() * into.size2()</tt> values are read from the 00123 * input stream into @c into in column-wise fashion. If a particular 00124 * column or subset of columns of interest have been specified, all 00125 * others are skipped during the reading. 00126 */ 00127 virtual unsigned int read(indii::ml::aux::matrix* into) = 0; 00128 00129 /** 00130 * Read next values into symmetric matrix. 00131 * 00132 * @param into Matrix into which to read the values. 00133 * 00134 * @return Number of values actually read. Will be less than the 00135 * size of the lower triangular portion of the given matrix if the 00136 * end of the stream is reached during reading. 00137 * 00138 * <tt>0.5 * (into.size1() * into.size1() + into.size1())</tt> 00139 * values are read from the input stream into the lower triangle of 00140 * @c into in column-wise fashion. If a particular column or subset of 00141 * columns of interest have been specified, all others are skipped 00142 * during the reading. 00143 */ 00144 virtual unsigned int read(indii::ml::aux::symmetric_matrix* into) = 0; 00145 00146 protected: 00147 /** 00148 * The input stream. 00149 */ 00150 std::istream* in; 00151 00152 /** 00153 * Columns of interest. Empty if all columns are of interest. 00154 */ 00155 std::vector<unsigned int> cols; 00156 00157 private: 00158 /** 00159 * Does object own the input stream? 00160 */ 00161 const bool ownStream; 00162 00163 }; 00164 00165 } 00166 } 00167 } 00168 00169 #endif
1.5.3