X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDCoupling%2FMEDCouplingRefCountObject.cxx;h=a0c28d56134e6cd9cb0c5f33c1b2db4eddde3a77;hb=8f2a28585bcf231a8f976f36b7fb1d2eed8801c2;hp=e5561245655379e801d0133e61774463499b9c64;hpb=c66a21a11fed90a9536b758a162785908cfe87da;p=tools%2Fmedcoupling.git diff --git a/src/MEDCoupling/MEDCouplingRefCountObject.cxx b/src/MEDCoupling/MEDCouplingRefCountObject.cxx index e55612456..a0c28d561 100644 --- a/src/MEDCoupling/MEDCouplingRefCountObject.cxx +++ b/src/MEDCoupling/MEDCouplingRefCountObject.cxx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2012 CEA/DEN, EDF R&D +// Copyright (C) 2007-2020 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either -// version 2.1 of the License. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,37 +19,261 @@ // Author : Anthony Geay (CEA/DEN) #include "MEDCouplingRefCountObject.hxx" -#include "MED_version.h" +#include "MEDCoupling_version.h" +#include "MCType.hxx" -using namespace ParaMEDMEM; +#include "InterpKernelException.hxx" -const char *ParaMEDMEM::MEDCouplingVersionStr() +#include +#include + +using namespace MEDCoupling; + +GlobalDict *GlobalDict::UNIQUE_INSTANCE=0; + +const char *MEDCoupling::MEDCouplingVersionStr() { - return SALOMEMED_VERSION_STR; + return MEDCOUPLING_VERSION_STR; } -int ParaMEDMEM::MEDCouplingVersion() +int MEDCoupling::MEDCouplingVersion() { - return SALOMEMED_VERSION; + return MEDCOUPLING_VERSION; } -void ParaMEDMEM::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas) +void MEDCoupling::MEDCouplingVersionMajMinRel(int& maj, int& minor, int& releas) { - int ver=SALOMEMED_VERSION; + int ver=MEDCOUPLING_VERSION; maj=(ver & 0xFF0000) >> 16; minor=(ver & 0xFF00) >> 8; releas=(ver & 0xFF); } -RefCountObject::RefCountObject():_cnt(1) +int MEDCoupling::MEDCouplingSizeOfVoidStar() +{ + return 8*sizeof(std::size_t); +} + +std::size_t MEDCoupling::MEDCouplingSizeOfIDs() +{ + return 8*sizeof(mcIdType); +} + +/*! + * If true is returned it is a LittleEndian machine. + * If false it is a BigEndian machine. + * \return the coding mode of integers of the machine. + */ +bool MEDCoupling::MEDCouplingByteOrder() +{ + unsigned int x(1); + unsigned char *xc(reinterpret_cast(&x)); + return xc[0]==1; +} + +const char *MEDCoupling::MEDCouplingByteOrderStr() +{ + static const char LITTLEENDIAN_STR[]="LittleEndian"; + static const char BIGENDIAN_STR[]="BigEndian"; + if(MEDCouplingByteOrder()) + return LITTLEENDIAN_STR; + else + return BIGENDIAN_STR; +} + +bool MEDCoupling::IsCXX11Compiled() +{ + return true; +} + +//= + +std::string BigMemoryObject::debugHeapMemorySize() const +{ + std::size_t ret(getHeapMemorySizeWithoutChildren()),sum(ret); + std::ostringstream oss; + std::vector s2(getDirectChildren()); + std::set s1; + oss << "this (" << this->getClassName() << ") -> " << ret << std::endl; + while(!s2.empty()) + { + std::vector s3; + for(auto it : s2) + { + if(s1.find(it)==s1.end()) + { + ret = it->getHeapMemorySizeWithoutChildren(); sum+=ret; + oss << it->getClassName() << " -> " << ret << std::endl; + s1.insert(it); + std::vector v2(it->getDirectChildren()); + for(auto it2 : v2) + if(s1.find(it2)==s1.end()) + s3.push_back(it2); + } + } + s2=s3; + } + oss << "sum = " << sum << std::endl; + return oss.str(); +} + +std::size_t BigMemoryObject::getHeapMemorySize() const +{ + std::size_t ret(getHeapMemorySizeWithoutChildren()); + std::vector v(getDirectChildren()); + std::set s1,s2(v.begin(),v.end()); + return ret+GetHeapMemoryOfSet(s1,s2); +} + +/*! + * This method returns all the progeny of \a this (this is \b not included in returned vector). + * All the progeny means all the subobjects (children), subsubobjects (little children), ... of \a this. + * The elements in returned array are reported only once even if they appear several times in the progeny of \a this. + */ +std::vector BigMemoryObject::getAllTheProgeny() const +{ + std::vector s1(getDirectChildren()); + std::vector ret; + while(!s1.empty()) + { + ret.insert(ret.end(),s1.begin(),s1.end()); + std::vector s3; + for(std::vector::const_iterator it0=s1.begin();it0!=s1.end();it0++) + { + std::vector s2; + if(*it0) + s2=(*it0)->getDirectChildren(); + for(std::vector::const_iterator it1=s2.begin();it1!=s2.end();it1++) + { + if(*it1) + if(std::find(ret.begin(),ret.end(),*it1)==ret.end()) + s3.push_back(*it1); + } + } + s1=s3; + } + return ret; +} + +/*! + * This method scan all the progeny of \a this (\a this excluded) to see if \a obj is part of it. + * If obj is NULL false is returned. + * \sa BigMemoryObject::getAllTheProgeny + */ +bool BigMemoryObject::isObjectInTheProgeny(const BigMemoryObject *obj) const +{ + if(!obj) + return false; + std::vector objs(getAllTheProgeny()); + return std::find(objs.begin(),objs.end(),obj)!=objs.end(); +} + +std::size_t BigMemoryObject::GetHeapMemorySizeOfObjs(const std::vector& objs) +{ + std::size_t ret(0); + std::set s1,s2; + for(std::vector::const_iterator it0=objs.begin();it0!=objs.end();it0++) + { + if(*it0) + if(s1.find(*it0)==s1.end()) + { + std::vector vTmp((*it0)->getDirectChildren()); + s2.insert(vTmp.begin(),vTmp.end()); + ret+=(*it0)->getHeapMemorySizeWithoutChildren(); + s1.insert(*it0); + } + } + return ret+GetHeapMemoryOfSet(s1,s2); +} + +std::size_t BigMemoryObject::GetHeapMemoryOfSet(std::set& s1, std::set& s2) +{ + std::size_t ret(0); + while(!s2.empty()) + { + std::set s3; + for(std::set::const_iterator it=s2.begin();it!=s2.end();it++) + { + if(s1.find(*it)==s1.end()) + { + ret+=(*it)->getHeapMemorySizeWithoutChildren(); + s1.insert(*it); + std::vector v2((*it)->getDirectChildren()); + for(std::vector::const_iterator it2=v2.begin();it2!=v2.end();it2++) + if(s1.find(*it2)==s1.end()) + s3.insert(*it2); + } + } + s2=s3; + } + return ret; +} + +std::string BigMemoryObject::getHeapMemorySizeStr() const +{ + static const char *UNITS[4]={"B","kB","MB","GB"}; + std::size_t m(getHeapMemorySize()); + std::ostringstream oss; oss.precision(3); + std::size_t remain(0); + int i(0); + for(;i<4;i++) + { + if(m<1024) + { + oss << m; + if(remain!=0) + { + std::ostringstream oss2; oss2 << std::fixed << ((double)remain)/1024.; + std::string s(oss2.str()); + s=s.substr(1,4); + std::size_t pos(s.find_last_not_of('0')); + if(pos==4) + oss << s; + else + oss << s.substr(0,pos+1); + } + oss << " " << UNITS[i]; + break; + } + else + { + if(i!=3) + { + remain=(m%1024); + m/=1024; + } + } + } + if(i==4) + oss << m << " " << UNITS[3]; + return oss.str(); +} + +std::vector BigMemoryObject::getDirectChildren() const +{ + std::vector ret; + std::vector retWithNull(getDirectChildrenWithNull()); + for(std::vector::const_iterator it=retWithNull.begin();it!=retWithNull.end();it++) + if(*it) + ret.push_back(*it); + return ret; +} + +BigMemoryObject::~BigMemoryObject() { } -RefCountObject::RefCountObject(const RefCountObject& other):_cnt(1) +//= + +RefCountObjectOnly::RefCountObjectOnly():_cnt(1) { } -bool RefCountObject::decrRef() const +RefCountObjectOnly::RefCountObjectOnly(const RefCountObjectOnly& other):_cnt(1) +{ +} + +bool RefCountObjectOnly::decrRef() const { bool ret=((--_cnt)==0); if(ret) @@ -57,11 +281,118 @@ bool RefCountObject::decrRef() const return ret; } -void RefCountObject::incrRef() const +void RefCountObjectOnly::incrRef() const { _cnt++; } +int RefCountObjectOnly::getRCValue() const +{ + return _cnt; +} + +RefCountObjectOnly::~RefCountObjectOnly() +{ +} + +/*! + * Do nothing here ! It is not a bug ( I hope :) ) because all subclasses that + * copies using operator= should not copy the ref counter of \a other ! + */ +RefCountObjectOnly& RefCountObjectOnly::operator=(const RefCountObjectOnly& other) +{ + return *this; +} + +//= + +RefCountObject::RefCountObject() +{ +} + +RefCountObject::RefCountObject(const RefCountObject& other):RefCountObjectOnly(other) +{ +} + RefCountObject::~RefCountObject() { } + +//= + +GlobalDict *GlobalDict::GetInstance() +{ + if(!UNIQUE_INSTANCE) + UNIQUE_INSTANCE=new GlobalDict; + return UNIQUE_INSTANCE; +} + +bool GlobalDict::hasKey(const std::string& key) const +{ + std::map::const_iterator it(_my_map.find(key)); + return it!=_my_map.end(); +} + +std::string GlobalDict::value(const std::string& key) const +{ + std::map::const_iterator it(_my_map.find(key)); + if(it==_my_map.end()) + { + std::ostringstream oss; + oss << "GlobalDict::value : key \"" << key << "\" is not in map !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return (*it).second; +} + +std::vector GlobalDict::keys() const +{ + std::vector ret; + for(std::map::const_iterator it=_my_map.begin();it!=_my_map.end();it++) + ret.push_back((*it).first); + return ret; +} + +void GlobalDict::erase(const std::string& key) +{ + std::map::iterator it(_my_map.find(key)); + if(it==_my_map.end()) + { + std::ostringstream oss; + oss << "GlobalDict::erase : key \"" << key << "\" is not in map !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + _my_map.erase(it); +} + +void GlobalDict::clear() +{ + _my_map.clear(); +} + +void GlobalDict::setKeyValue(const std::string& key, const std::string& val) +{ + std::map::const_iterator it(_my_map.find(key)); + if(it!=_my_map.end()) + { + std::ostringstream oss; + oss << "GlobalDict::setKeyValue : key \"" << key << "\" already exists !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + _my_map[key]=val; +} + +void GlobalDict::setKeyValueForce(const std::string& key, const std::string& val) +{ + _my_map[key]=val; +} + +std::string GlobalDict::printSelf() const +{ + std::ostringstream oss; + for(std::map::const_iterator it=_my_map.begin();it!=_my_map.end();it++) + { + oss << "(" << (*it).first << "," << (*it).second << ")" << std::endl; + } + return oss.str(); +}