T& operator[](int id) { return _pointer.getPointer()[id]; }
bool isEqual(const MemArray<T>& 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;
{
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<std::string>& 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;
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<std::string>& vst, char defaultChar) throw(INTERP_KERNEL::Exception);
};
class DataArrayAsciiCharTuple;
}
return true;
}
-
+
/*!
* \param [in] sl is typically the number of components
+ * \return True if a not null pointer is present, False if not.
*/
template<class T>
- void MemArray<T>::repr(int sl, std::ostream& stream) const
+ bool MemArray<T>::reprHeader(int sl, std::ostream& stream) const
{
stream << "Number of tuples : ";
if(!_pointer.isNull())
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<class T>
+ void MemArray<T>::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;
else
stream << "Empty Data\n";
}
- else
- stream << "No data !\n";
}
/*!
{
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();
}
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<std::string>& 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<std::string>& 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<std::string>::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<nbTuples;i++,pt+=nbCompo)
+ {
+ const std::string& tmp=vst[i];
+ std::size_t sz=tmp.length();
+ std::copy(tmp.begin(),tmp.begin()+sz,pt);
+ std::fill(pt+sz,pt+nbCompo,defaultChar);
+ }
+}
+
DataArrayAsciiCharIterator *DataArrayAsciiChar::iterator()
{
return new DataArrayAsciiCharIterator(this);
void DataArrayAsciiChar::reprWithoutNameStream(std::ostream& stream) const throw(INTERP_KERNEL::Exception)
{
DataArray::reprWithoutNameStream(stream);
- _mem.repr(getNumberOfComponents(),stream);
+ if(_mem.reprHeader(getNumberOfComponents(),stream))
+ {
+ const char *data=begin();
+ int nbOfTuples=getNumberOfTuples();
+ int nbCompo=getNumberOfComponents();
+ for(int i=0;i<nbOfTuples;i++,data+=nbCompo)
+ {
+ stream << "Tuple #" << i << " : \"";
+ std::copy(data,data+nbCompo,std::ostream_iterator<char>(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)
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<char>(oss));
return oss.str();
}