Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_Feature_cpp)
00056 #define ALIZE_Feature_cpp
00057
00058 #include <new>
00059 #include <memory.h>
00060 #include "Feature.h"
00061 #include "alizeString.h"
00062 #include "Exception.h"
00063 #include "Config.h"
00064 #include "ULongVector.h"
00065
00066 using namespace alize;
00067
00068
00069 Feature::Feature(unsigned long vectSize)
00070 :Object(), _vectSize(vectSize), _isValid(false), _labelCode(0),
00071 _dataVector(createVector())
00072 {
00073
00074
00075 }
00076
00077 Feature::Feature(const Config& config)
00078 :Object(), _vectSize(config.getParam_vectSize()), _isValid(false),
00079 _labelCode(0), _dataVector(createVector())
00080 {
00081
00082
00083 }
00084
00085 Feature& Feature::create(unsigned long vectSize)
00086 {
00087 Feature* p = new (std::nothrow) Feature(vectSize);
00088 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00089 return *p;
00090 }
00091
00092 Feature::Feature(const Feature& f)
00093 :Object(), _vectSize(f._vectSize), _isValid(f._isValid),
00094 _labelCode(f._labelCode), _dataVector(createVector())
00095 {
00096 memcpy(_dataVector, f._dataVector, _vectSize*sizeof(_dataVector[0]));
00097 }
00098
00099 const Feature& Feature::operator=(const Feature& f)
00100 {
00101 if (_vectSize != f._vectSize)
00102 throw Exception("Feature copy : source vectSize ("
00103 + String::valueOf(f._vectSize)
00104 + ") does not match target vectSize ("
00105 + String::valueOf(_vectSize) + ")", __FILE__, __LINE__);
00106 memcpy(_dataVector, f._dataVector, _vectSize*sizeof(_dataVector[0]));
00107 _isValid = f._isValid;
00108 _labelCode = f._labelCode;
00109 return *this;
00110 }
00111
00112 bool Feature::operator==(const Feature& f) const
00113 {
00114 if (_vectSize != f._vectSize || _isValid != f._isValid)
00115 return false;
00116 for (unsigned long i=0; i<_vectSize; i++)
00117 if (_dataVector[i] != f._dataVector[i])
00118 return false;
00119 return true;
00120 }
00121
00122 bool Feature::operator!=(const Feature& c) const { return !(*this == c); }
00123
00124 Feature::data_t* Feature::createVector() const
00125 {
00126 data_t* p = new (std::nothrow) data_t[_vectSize==0?1:_vectSize];
00127 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00128 return p;
00129 }
00130
00131 void Feature::reset()
00132 {
00133 for (unsigned long i=0; i<_vectSize; i++)
00134 {
00135 _dataVector[i] = 0.0;
00136 }
00137 _labelCode = 0;
00138 _isValid = false;
00139 }
00140
00141 bool Feature::isValid() const { return _isValid; }
00142
00143 void Feature::setValidity(bool v) { _isValid = v; }
00144
00145 unsigned long Feature::getLabelCode() const { return _labelCode; }
00146
00147 void Feature::setLabelCode(unsigned long v) { _labelCode = v; }
00148
00149 Feature::data_t& Feature::operator[](unsigned long i)
00150 {
00151 assertIsInBounds(__FILE__, __LINE__, i, _vectSize);
00152 assert(_dataVector != NULL);
00153 return _dataVector[i];
00154 }
00155
00156 Feature::data_t Feature::operator[](unsigned long i) const
00157 {
00158 assertIsInBounds(__FILE__, __LINE__, i, _vectSize);
00159 assert(_dataVector != NULL);
00160 return _dataVector[i];
00161 }
00162
00163 unsigned long Feature::getVectSize() const { return _vectSize; }
00164
00165 void Feature::setVectSize(const K&, unsigned long s)
00166 {
00167 if (s != _vectSize)
00168 {
00169
00170 assert(_dataVector != NULL);
00171 delete [] _dataVector;
00172 _vectSize = s;
00173 _dataVector = createVector();
00174 }
00175 }
00176
00177 void Feature::setData(const FloatVector& v, unsigned long start)
00178 {
00179 if (_vectSize + start> v.size())
00180 throw Exception("Lack of data to fill the feature", __FILE__, __LINE__);
00181 const float* source = v.getArray();
00182 for (unsigned long i=0; i<_vectSize; i++)
00183 _dataVector[i] = (data_t)source[i+start];
00184 }
00185
00186 void Feature::setData(const DoubleVector& v, unsigned long start)
00187 {
00188 if (_vectSize + start> v.size())
00189 throw Exception("Lack of data to fill the feature", __FILE__, __LINE__);
00190 const double* source = v.getArray();
00191 for (unsigned long i=0; i<_vectSize; i++)
00192 _dataVector[i] = (data_t)source[i+start];
00193 }
00194
00195 void Feature::copySelectedData(const Feature& f, const ULongVector& selection)
00196 {
00197 const unsigned long selectionSize = selection.size();
00198 if (selectionSize > _vectSize)
00199 throw Exception("", __FILE__, __LINE__);
00200 const unsigned long sourceSize = f._vectSize;
00201 unsigned long* selectionVect = selection.getArray();
00202 double* sourceVect = f._dataVector;
00203 unsigned long sourceIdx;
00204 for (unsigned long i=0; i<selectionSize; i++)
00205 {
00206 if ( (sourceIdx = selectionVect[i]) >= sourceSize)
00207 throw Exception("", __FILE__, __LINE__);
00208 _dataVector[i] = sourceVect[sourceIdx];
00209 }
00210
00211 _labelCode = f._labelCode;
00212 _isValid = f._isValid;
00213 }
00214
00215 Feature::data_t* Feature::getDataVector() const { return _dataVector; }
00216
00217 String Feature::getClassName() const { return "Feature"; }
00218
00219 String Feature::toString() const
00220 {
00221 String s = Object::toString()
00222 + "\n vectSize = " + String::valueOf(_vectSize)
00223 + "\n label code = " + String::valueOf(_labelCode)
00224 + "\n is valid = " + String::valueOf(_isValid);
00225 for (unsigned long i=0; i<_vectSize; i++)
00226 s += "\n data[" + String::valueOf(i) + "] = "
00227 + String::valueOf(operator[](i));
00228 return s;
00229 }
00230
00231 Feature::~Feature()
00232 {
00233 assert(_dataVector != NULL);
00234 delete[] _dataVector;
00235 }
00236
00237
00238 #endif // !defined(ALIZE_Feature_cpp)
00239