From: ageay Date: Tue, 19 Mar 2013 15:59:22 +0000 (+0000) Subject: Some useful constructors in DataArrayAsciiChar X-Git-Tag: V6_main_FINAL~267 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=b2f2047b60073b6b546bb3d3c55ef22c0ed8d78a;p=tools%2Fmedcoupling.git Some useful constructors in DataArrayAsciiChar --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 4a65a62c6..00c5d4e43 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -68,6 +68,7 @@ namespace ParaMEDMEM T& operator[](int id) { return _pointer.getPointer()[id]; } bool isEqual(const MemArray& other, T prec, std::string& reason) const; void repr(int sl, std::ostream& stream) const; + bool reprHeader(int sl, std::ostream& stream) const; void reprZip(int sl, std::ostream& stream) const; void fillWithValue(const T& val); T *fromNoInterlace(int nbOfComp) const; @@ -726,6 +727,8 @@ namespace ParaMEDMEM { public: MEDCOUPLING_EXPORT static DataArrayAsciiChar *New(); + MEDCOUPLING_EXPORT static DataArrayAsciiChar *New(const std::string& st) throw(INTERP_KERNEL::Exception); + MEDCOUPLING_EXPORT static DataArrayAsciiChar *New(const std::vector& vst, char defaultChar) throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayChar *buildEmptySpecializedDAChar() const throw(INTERP_KERNEL::Exception); MEDCOUPLING_EXPORT DataArrayAsciiCharIterator *iterator(); MEDCOUPLING_EXPORT DataArrayAsciiChar *deepCpy() const; @@ -739,6 +742,8 @@ namespace ParaMEDMEM MEDCOUPLING_EXPORT bool isEqualIfNotWhy(const DataArrayChar& other, std::string& reason) const throw(INTERP_KERNEL::Exception); private: DataArrayAsciiChar() { } + DataArrayAsciiChar(const std::string& st) throw(INTERP_KERNEL::Exception); + DataArrayAsciiChar(const std::vector& vst, char defaultChar) throw(INTERP_KERNEL::Exception); }; class DataArrayAsciiCharTuple; diff --git a/src/MEDCoupling/MEDCouplingMemArray.txx b/src/MEDCoupling/MEDCouplingMemArray.txx index 35404431b..5dddb27b6 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.txx +++ b/src/MEDCoupling/MEDCouplingMemArray.txx @@ -167,12 +167,13 @@ namespace ParaMEDMEM } return true; } - + /*! * \param [in] sl is typically the number of components + * \return True if a not null pointer is present, False if not. */ template - void MemArray::repr(int sl, std::ostream& stream) const + bool MemArray::reprHeader(int sl, std::ostream& stream) const { stream << "Number of tuples : "; if(!_pointer.isNull()) @@ -186,9 +187,21 @@ namespace ParaMEDMEM stream << "No data"; stream << "\n"; stream << "Data content :\n"; - const T *data=getConstPointer(); - if(!_pointer.isNull()) + bool ret=!_pointer.isNull(); + if(!ret) + stream << "No data !\n"; + return ret; + } + + /*! + * \param [in] sl is typically the number of components + */ + template + void MemArray::repr(int sl, std::ostream& stream) const + { + if(reprHeader(sl,stream)) { + const T *data=getConstPointer(); if(_nb_of_elem!=0 && sl!=0) { int nbOfTuples=_nb_of_elem/sl; @@ -203,8 +216,6 @@ namespace ParaMEDMEM else stream << "Empty Data\n"; } - else - stream << "No data !\n"; } /*! diff --git a/src/MEDCoupling/MEDCouplingMemArrayChar.cxx b/src/MEDCoupling/MEDCouplingMemArrayChar.cxx index 40265f5d7..e52d0cafa 100644 --- a/src/MEDCoupling/MEDCouplingMemArrayChar.cxx +++ b/src/MEDCoupling/MEDCouplingMemArrayChar.cxx @@ -1865,7 +1865,7 @@ std::string DataArrayByteTuple::repr() const throw(INTERP_KERNEL::Exception) { std::ostringstream oss; oss << "("; for(int i=0;i<_nb_of_compo-1;i++) - oss << _pt[i] << ", "; + oss << (int)_pt[i] << ", "; oss << _pt[_nb_of_compo-1] << ")"; return oss.str(); } @@ -1908,6 +1908,78 @@ DataArrayAsciiChar *DataArrayAsciiChar::New() return new DataArrayAsciiChar; } +/*! + * Returns a new instance of DataArrayAsciiChar. The caller is to delete this array + * using decrRef() as it is no more needed. + * \param [in] st the string. This input string should have a length greater than 0. If not an excpetion will be thrown. + */ +DataArrayAsciiChar *DataArrayAsciiChar::New(const std::string& st) throw(INTERP_KERNEL::Exception) +{ + return new DataArrayAsciiChar(st); +} + +/*! + * \param [in] st the string. This input string should have a length greater than 0. If not an excpetion will be thrown. + */ +DataArrayAsciiChar::DataArrayAsciiChar(const std::string& st) throw(INTERP_KERNEL::Exception) +{ + std::size_t lgth=st.length(); + if(lgth==0) + throw INTERP_KERNEL::Exception("DataArrayAsciiChar contructor with string ! Size of input string is null !"); + alloc(1,lgth); + std::copy(st.begin(),st.begin()+lgth,getPointer()); +} + +/*! + * Returns a new instance of DataArrayAsciiChar. The caller is to delete this array + * using decrRef() as it is no more needed. + * This constructor uses \a vst input vector of strings to initialize itself. For all strings whose length is lower than max length of strings in + * \a vst the remaining locations in memory will be set to character \a defaultChar. + * + * \param [in] defaultChar the default character used to fill not defined locations in \a this + * \param [in] vst vector of strings. This input vector must be non empty. \a this will have its component size set to the max lgth of strings contained + * in \a vst. If all strings are empty an INTERP_KERNEL::Exception will be thrown. + * + * \throw If input \a vst is empty. + * \throw If all strings in \a vst are empty. + */ +DataArrayAsciiChar *DataArrayAsciiChar::New(const std::vector& vst, char defaultChar) throw(INTERP_KERNEL::Exception) +{ + return new DataArrayAsciiChar(vst,defaultChar); +} + +/*! + * This constructor uses \a vst input vector of strings to initialize itself. For all strings whose length is lower than max length of strings in + * \a vst the remaining locations in memory will be set to character \a defaultChar. + * + * \param [in] defaultChar the default character used to fill not defined locations in \a this + * \param [in] vst vector of strings. This input vector must be non empty. \a this will have its component size set to the max lgth of strings contained + * in \a vst. If all strings are empty an INTERP_KERNEL::Exception will be thrown. + * + * \throw If input \a vst is empty. + * \throw If all strings in \a vst are empty. + */ +DataArrayAsciiChar::DataArrayAsciiChar(const std::vector& vst, char defaultChar) throw(INTERP_KERNEL::Exception) +{ + if(vst.empty()) + throw INTERP_KERNEL::Exception("DataArrayAsciiChar contructor with vector of strings ! Empty array !"); + std::size_t nbCompo=0; + for(std::vector::const_iterator it=vst.begin();it!=vst.end();it++) + nbCompo=std::max(nbCompo,(*it).length()); + if(nbCompo==0) + throw INTERP_KERNEL::Exception("DataArrayAsciiChar contructor with vector of strings ! All strings in not empty vector are empty !"); + int nbTuples=(int)vst.size(); + alloc(nbTuples,(int)nbCompo); + char *pt=getPointer(); + for(int i=0;i(stream)); + stream << "\"\n"; + } + } } void DataArrayAsciiChar::reprZipWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception) { - DataArray::reprWithoutNameStream(stream); - _mem.reprZip(getNumberOfComponents(),stream); + reprWithoutNameStream(stream); } void DataArrayAsciiChar::reprCppStream(const char *varName, std::ostream& stream) const throw(INTERP_KERNEL::Exception) @@ -2055,10 +2137,8 @@ DataArrayAsciiCharTuple::DataArrayAsciiCharTuple(char *pt, int nbOfComp):_pt(pt) std::string DataArrayAsciiCharTuple::repr() const throw(INTERP_KERNEL::Exception) { - std::ostringstream oss; oss << "("; - for(int i=0;i<_nb_of_compo-1;i++) - oss << _pt[i] << ", "; - oss << _pt[_nb_of_compo-1] << ")"; + std::ostringstream oss; + std::copy(_pt,_pt+_nb_of_compo,std::ostream_iterator(oss)); return oss.str(); }