#ifndef __TwoDLookup_h_ #define __TwoDLookup_h_ #include #include #include #include #include // This class maps a pair of strings to a string. If the pair of input strings // cannot be found, then we return the empty string. // // This can easily be read from or saved to a CSV file. The top row of the // file has the column headers. The left column of the file has the row // headers. The top left cell is ignored on reading, and blank after writing. // The remainder of the table contains the values which are referenced by the // row and column headers. class TwoDArray { public: typedef std::vector< std::string > StringList; private: std::set< std::string > _allCols, _allRows; StringList _colsInOrder, _rowsInOrder; typedef std::pair< std::string, std::string > Key; std::map< Key, std::string > _values; public: TwoDArray(); void appendFromCSV(std::string const &fileName); void loadFromCSV(std::string const &fileName) { clear(); appendFromCSV(fileName); } void writeToCSV(std::string const &fileName, bool writeHeader = true) const; void writeToCSV(std::ostream &stream, bool writeHeader = true) const; std::string writeToCSV(bool writeHeader = true) const; void writeToCSV(char const *fileName, bool writeHeader = true) const { writeToCSV((std::string)fileName, writeHeader); } std::string get(std::string const &colHeader, std::string const &rowHeader) const; void add(std::string const &colHeader, std::string const &rowHeader, std::string const &value); void clear(); bool containsRow(std::string rowHeader) const; // Keep the column headers, nothing else. void eraseAllRows(); StringList const &getRowHeaders() const { return _rowsInOrder; } StringList const &getColHeaders() const { return _colsInOrder; } }; #endif