Alps  2.0.2
AlpsParameterBase.h
Go to the documentation of this file.
1 /*===========================================================================*
2  * This file is part of the Abstract Library for Parallel Search (ALPS). *
3  * *
4  * ALPS is distributed under the Eclipse Public License as part of the *
5  * COIN-OR repository (http://www.coin-or.org). *
6  * *
7  * Authors: *
8  * *
9  * Yan Xu, Lehigh University *
10  * Aykut Bulut, Lehigh University *
11  * Ted Ralphs, Lehigh University *
12  * *
13  * Conceptual Design: *
14  * *
15  * Yan Xu, Lehigh University *
16  * Ted Ralphs, Lehigh University *
17  * Laszlo Ladanyi, IBM T.J. Watson Research Center *
18  * Matthew Saltzman, Clemson University *
19  * *
20  * *
21  * Copyright (C) 2001-2023, Lehigh University, Yan Xu, Aykut Bulut, and *
22  * Ted Ralphs. *
23  * All Rights Reserved. *
24  *===========================================================================*/
25 
26 
27 #ifndef AlpsParameterBase_h_
28 #define AlpsParameterBase_h_
29 
30 //----------------------------------------------------------
31 // AlpsParameterBase.h is modified from BCP_parameters.hpp
32 //----------------------------------------------------------
33 
34 #include <algorithm>
35 #include <cctype>
36 #include <cstdio>
37 #include <fstream>
38 #include <iostream>
39 #include <string>
40 #include <utility> // for 'pair'
41 #include <vector>
42 
43 #include "CoinError.hpp"
44 
45 #if defined(__GNUC__) && (__GNUC__ >= 3)
46 # include <sstream>
47 # include <locale>
48 # define ALPS_STRINGSTREAM std::istringstream
49 #else
50 # include <strstream>
51 # include <cctype>
52 # define ALPS_STRINGSTREAM std::istrstream
53 #endif
54 
55 class AlpsEncoded;
56 
57 //##############################################################################
58 
75 };
76 
77 //##############################################################################
78 
82 
83  private:
89  int index_;
92  public:
93  // default copy constructor and assignment operator are fine
99  AlpsParameter(const AlpsParameterT t, const int i) :
100  type_(t), index_(i) {}
108  AlpsParameterT type() const { return type_; }
111  int index() const { return index_; }
113 };
114 
115 //##############################################################################
116 
139  protected:
144  std::vector< std::pair<std::string, AlpsParameter> > keys_;
145 
148  std::string prefix_;
149 
152  std::vector<std::string> obsoleteKeys_;
153 
155  bool* bpar_;
156 
158  int* ipar_;
159 
161  double* dpar_;
162 
164  std::string* spar_;
165 
167  int numSa_;
168  std::vector<std::string>* sapar_;
169 
171  //---------------------------------------------------------------------------
172 
173  public:
180  virtual void createKeywordList() = 0;
181 
183  virtual void setDefaultEntries() = 0;
190  virtual void pack(AlpsEncoded& buf) {
191  throw CoinError("can't call pack()", "pack", " AlpsParameterSet");
192  }
193 
195  virtual void unpack(AlpsEncoded& buf){
196  throw CoinError("can't call unpack()", "unpack", " AlpsParameterSet");
197  }
199 
200  //---------------------------------------------------------------------------
201 
202  public:
203 
212  // This the one used in readFromStream()
213  void setEntry(const AlpsParameter key, const char * val) {
214  switch (key.type()){
215  case AlpsNoPar: break;
216  case AlpsBoolPar: bpar_ [key.index()] = atoi(val) ? true : false; break;
217  case AlpsIntPar: ipar_ [key.index()] = atoi(val); break;
218  case AlpsDoublePar: dpar_ [key.index()] = atof(val); break;
219  case AlpsStringPar: spar_ [key.index()] = val; break;
220  case AlpsStringArrayPar: sapar_[key.index()].push_back(val); break;
221  }
222  }
223 
237  void readFromStream(std::istream& parstream);
238 
240  void readFromFile(const char * paramfile);
241 
243  void readFromArglist(const int argnum, const char * const * arglist);
244 
248  void writeToStream(std::ostream& outstream) const;
249 
251  AlpsParameterSet(int c, int i, int d, int s, int sa) :
252  keys_(),
253  prefix_("Alps"),
254  bpar_(new bool[c]),
255  ipar_(new int[i]),
256  dpar_(new double[d]),
257  spar_(new std::string[s]),
258  sapar_(new std::vector<std::string>[sa])
259  {}
260 
262  virtual ~AlpsParameterSet() {
263  keys_.clear();
264  obsoleteKeys_.clear();
265  delete[] bpar_; bpar_ = 0;
266  delete[] ipar_; ipar_ = 0;
267  delete[] dpar_; dpar_ = 0;
268  delete[] spar_; spar_ = 0;
269  delete[] sapar_; sapar_ = 0;
270  }
271 };
272 
273 #endif
AlpsParameterSet::numSa_
int numSa_
The "vector of string" parameters.
Definition: AlpsParameterBase.h:167
AlpsParameterSet::readFromFile
void readFromFile(const char *paramfile)
Read parameters from a file.
AlpsStringPar
@ AlpsStringPar
String parameter (E.g., data file name.).
Definition: AlpsParameterBase.h:71
AlpsBoolPar
@ AlpsBoolPar
Bool parameter.
Definition: AlpsParameterBase.h:65
AlpsParameterT
AlpsParameterT
This enumerative constant describes the possible parameter types.
Definition: AlpsParameterBase.h:60
AlpsParameterSet::writeToStream
void writeToStream(std::ostream &outstream) const
Write keyword-value pairs to the stream specified in the argument.
AlpsParameterSet::dpar_
double * dpar_
The double parameters.
Definition: AlpsParameterBase.h:161
AlpsParameterSet
This is the class serves as a holder for a set of parameters.
Definition: AlpsParameterBase.h:138
AlpsParameterSet::readFromArglist
void readFromArglist(const int argnum, const char *const *arglist)
Read parameters from the command line.
AlpsParameter::AlpsParameter
AlpsParameter(const AlpsParameterT t, const int i)
Constructor where members are specified.
Definition: AlpsParameterBase.h:99
AlpsNoPar
@ AlpsNoPar
The type is not yet specified.
Definition: AlpsParameterBase.h:63
AlpsParameterSet::spar_
std::string * spar_
The string (actually, std::string) parameters.
Definition: AlpsParameterBase.h:164
AlpsIntPar
@ AlpsIntPar
Integer parameter.
Definition: AlpsParameterBase.h:67
AlpsEncoded
Definition: AlpsEncoded.h:64
AlpsParameter::type
AlpsParameterT type() const
Return the type of the parameter.
Definition: AlpsParameterBase.h:108
AlpsParameterSet::pack
virtual void pack(AlpsEncoded &buf)
Pack the parameter set into the buffer.
Definition: AlpsParameterBase.h:190
AlpsParameter::type_
AlpsParameterT type_
The type of the parameter (e.g., AlpsIntPar).
Definition: AlpsParameterBase.h:87
AlpsParameterSet::readFromStream
void readFromStream(std::istream &parstream)
Read the parameters from the stream specified in the argument.
AlpsParameter::index
int index() const
Return the index of the parameter within all parameters of the same type.
Definition: AlpsParameterBase.h:111
AlpsParameterSet::ipar_
int * ipar_
The integer parameters.
Definition: AlpsParameterBase.h:158
AlpsParameterSet::keys_
std::vector< std::pair< std::string, AlpsParameter > > keys_
The keyword, parameter pairs.
Definition: AlpsParameterBase.h:144
AlpsParameter::AlpsParameter
AlpsParameter()
The default constructor creates a phony parameter.
Definition: AlpsParameterBase.h:97
AlpsParameterSet::~AlpsParameterSet
virtual ~AlpsParameterSet()
The destructor deletes all data members.
Definition: AlpsParameterBase.h:262
AlpsParameterSet::createKeywordList
virtual void createKeywordList()=0
Method for creating the list of keyword looked for in the parameter file.
AlpsDoublePar
@ AlpsDoublePar
Double parameter.
Definition: AlpsParameterBase.h:69
AlpsParameterSet::setEntry
void setEntry(const AlpsParameter key, const char *val)
First, there is the assignment operator that sets the whole parameter set at once.
Definition: AlpsParameterBase.h:213
AlpsParameterSet::bpar_
bool * bpar_
The bool parameters.
Definition: AlpsParameterBase.h:155
AlpsParameterSet::setDefaultEntries
virtual void setDefaultEntries()=0
Method for setting the default values for the parameters.
AlpsParameter::index_
int index_
The index of this parameter within all parameters of the same type.
Definition: AlpsParameterBase.h:89
AlpsParameterSet::unpack
virtual void unpack(AlpsEncoded &buf)
Unpack the parameter set from the buffer.
Definition: AlpsParameterBase.h:195
AlpsParameterSet::AlpsParameterSet
AlpsParameterSet(int c, int i, int d, int s, int sa)
The constructor allocate memory for parameters.
Definition: AlpsParameterBase.h:251
AlpsParameterSet::prefix_
std::string prefix_
Prefix to be used for looking up parameters.
Definition: AlpsParameterBase.h:148
AlpsParameterSet::obsoleteKeys_
std::vector< std::string > obsoleteKeys_
list of obsolete keywords.
Definition: AlpsParameterBase.h:152
AlpsParameter
This parameter indeintifies a single parameter entry.
Definition: AlpsParameterBase.h:81
AlpsParameter::~AlpsParameter
~AlpsParameter()
The destructor.
Definition: AlpsParameterBase.h:102
AlpsStringArrayPar
@ AlpsStringArrayPar
The parameter is an array of strings.
Definition: AlpsParameterBase.h:74
AlpsParameterSet::sapar_
std::vector< std::string > * sapar_
Definition: AlpsParameterBase.h:168