| 1 |
// (C) Copyright Carnegie Mellon University 2005 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, Carnegie Mellon University, |
| 7 |
// |
| 8 |
// Date : 26/05/2005 |
| 9 |
|
| 10 |
#ifndef NameReader_HPP |
| 11 |
#define NameReader_HPP |
| 12 |
#include <string> |
| 13 |
#include <vector> |
| 14 |
#include <list> |
| 15 |
#include <fstream> |
| 16 |
#include <iostream> |
| 17 |
#include <CoinHelperFunctions.hpp> |
| 18 |
#include "OsiSolverInterface.hpp" |
| 19 |
//#include <tr1/unordered_map> |
| 20 |
#include <map> |
| 21 |
|
| 22 |
namespace Bonmin{ |
| 23 |
/** A class for reading a .col or .row file containing name for variables and constraints (usually ampl generated file). |
| 24 |
*/ |
| 25 |
class NamesReader |
| 26 |
{ |
| 27 |
public: |
| 28 |
/** Constructor with a file name given by a const char * */ |
| 29 |
NamesReader(const char * fileName, const char * suffix); |
| 30 |
/** Constructor with a file name given by a string and also default (empty string) */ |
| 31 |
NamesReader(const std::string & fileName="", const std::string& suffix=".col"); |
| 32 |
/** Reads the .col file*/ |
| 33 |
bool readFile(); |
| 34 |
/** Reads the .col file fileName*/ |
| 35 |
bool readFile(const std::string &file) |
| 36 |
{ |
| 37 |
file_=file; |
| 38 |
return readFile(); |
| 39 |
} |
| 40 |
|
| 41 |
/** Copy the names to Names. */ |
| 42 |
void copyNames(OsiSolverInterface::OsiNameVec& Names); |
| 43 |
|
| 44 |
/** Access Names of indexed by i. */ |
| 45 |
const std::string& name(int i){ |
| 46 |
return names_[i]; |
| 47 |
} |
| 48 |
|
| 49 |
/** Access index of variable str */ |
| 50 |
int index(const char * str){ |
| 51 |
return indices_[str]; |
| 52 |
} |
| 53 |
private: |
| 54 |
/// Name of the file to read. |
| 55 |
std::string file_; |
| 56 |
|
| 57 |
/// Suffix of the file (".col", ".row") |
| 58 |
std::string suffix_; |
| 59 |
|
| 60 |
/// String comparison strucutre. |
| 61 |
struct ltstr |
| 62 |
{ |
| 63 |
bool operator()(const char* s1, const char* s2) const |
| 64 |
{ |
| 65 |
return strcmp(s1, s2) < 0; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
/// Hash type. |
| 70 |
typedef std::map<const char *, int, ltstr> namesHash; |
| 71 |
///Hash map used to store the indices. |
| 72 |
namesHash indices_; |
| 73 |
///Variable names. |
| 74 |
std::vector<std::string> names_; |
| 75 |
}; |
| 76 |
} |
| 77 |
#endif |