AttributeTypes.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // AttributeTypes.cpp
  2. #include "AttributeTypes.h"
  3. #include <iomanip>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <clocale>
  7. using namespace std;
  8. // CLASS ATTRIBUTE_TYPE
  9. AttributeType::~AttributeType()
  10. {
  11. }
  12. void AttributeType::PrintDescription(ofstream &ofs)
  13. {
  14. ofs << "@ATTRIBUTE " << m_attName << " ";
  15. }
  16. vector<string> AttributeType::GetMapping()
  17. {
  18. return vector<string>(0);
  19. }
  20. string AttributeType::GetName()
  21. {
  22. return m_attName;
  23. }
  24. // CLASS ATTRIBUTE_TYPE_INT
  25. AttributeTypeInt::AttributeTypeInt(string attName) : AttributeType(attName)
  26. {
  27. }
  28. AttributeTypeInt::~AttributeTypeInt()
  29. {
  30. }
  31. double AttributeTypeInt::operator()(string attValue)
  32. {
  33. return stoi(attValue);
  34. }
  35. void AttributeTypeInt::Print(ofstream& ofs, double const &attValue)
  36. {
  37. ofs << (int) attValue;
  38. }
  39. void AttributeTypeInt::PrintDescription(ofstream& ofs)
  40. {
  41. AttributeType::PrintDescription(ofs);
  42. ofs << "INTEGER\n";
  43. }
  44. // CLASS ATTRIBUTE_TYPE_NUM
  45. AttributeTypeNum::AttributeTypeNum(string attName, int precision) : AttributeType(attName), m_precision(precision)
  46. {
  47. // *** Use the following locale in order to get "." as decimal-point separator
  48. std::setlocale(LC_NUMERIC, "C");
  49. }
  50. AttributeTypeNum::~AttributeTypeNum()
  51. {
  52. }
  53. double AttributeTypeNum::operator()(string attValue)
  54. {
  55. return stod(attValue);
  56. }
  57. void AttributeTypeNum::Print(ofstream& ofs, double const &attValue)
  58. {
  59. ofs << fixed << setprecision(m_precision) << attValue;
  60. }
  61. void AttributeTypeNum::PrintDescription(ofstream& ofs)
  62. {
  63. AttributeType::PrintDescription(ofs);
  64. ofs << "NUMERIC\n";
  65. }
  66. // CLASS ATTRIBUTE_TYPE_NOM
  67. AttributeTypeNom::AttributeTypeNom(string attName, string attDescription) : AttributeType(attName)
  68. {
  69. // find content between curly braces
  70. size_t start = attDescription.find("{");
  71. size_t end = attDescription.find("}");
  72. if (start == string::npos || end == string::npos)
  73. throw range_error("Could not find one or both curly braces");
  74. attDescription = attDescription.substr(start+1, end-start-1);
  75. stringstream ss(attDescription);
  76. int id = 0;
  77. while (ss)
  78. {
  79. string value;
  80. if (!getline(ss, value, ','))
  81. break;
  82. m_nominalMap[value] = id;
  83. id++;
  84. }
  85. //create inverse map
  86. for (auto p: m_nominalMap)
  87. m_inversedMap[p.second] = p.first;
  88. }
  89. AttributeTypeNom::~AttributeTypeNom()
  90. {
  91. }
  92. double AttributeTypeNom::operator()(string attValue)
  93. {
  94. return m_nominalMap[attValue];
  95. }
  96. void AttributeTypeNom::Print(ofstream& ofs, double const &attValue)
  97. {
  98. ofs << m_inversedMap[attValue];
  99. }
  100. void AttributeTypeNom::PrintDescription(ofstream& ofs)
  101. {
  102. AttributeType::PrintDescription(ofs);
  103. ofs << "{";
  104. unsigned int i;
  105. for (i=0; i<m_inversedMap.size()-1; i++)
  106. ofs << m_inversedMap[i] << ',';
  107. ofs << m_inversedMap[i] << "}" << endl;
  108. }
  109. vector<string> AttributeTypeNom::GetMapping()
  110. {
  111. vector<string> res;
  112. for (unsigned int i=0; i<m_inversedMap.size(); i++)
  113. res.push_back(m_inversedMap[i]);
  114. return res;
  115. }