dna/SurjectiveMapping.h dna/TypeDefs.h algorithm functional dna::SurjectiveMapping dna::SurjectiveMapping::Pair dna //CopyrightEpicGames,Inc.AllRightsReserved. #pragmaonce #include"dna/TypeDefs.h" #ifdef_MSC_VER #pragmawarning(push) #pragmawarning(disable:43654987) #endif #include<algorithm> #include<functional> #ifdef_MSC_VER #pragmawarning(pop) #endif namespacedna{ template<typenameTFrom,typenameTTo=TFrom> structSurjectiveMapping{ public: structPair{ TFromfrom; TToto; }; public: explicitSurjectiveMapping(MemoryResource*memRes): from{memRes}, to{memRes}{ } Pairget(std::size_tindex)const{ assert(index<size()); return{from[index],to[index]}; } voidadd(TFromfrom_,TToto_){ from.push_back(from_); to.push_back(to_); } voidset(std::size_tindex,TFromfrom_,TToto_){ if(index>=size()){ from.resize(index+1ul); to.resize(index+1ul); } from[index]=from_; to[index]=to_; } voidremoveIf(std::function<bool(constTFrom&,constTTo&)>predicate){ assert(from.size()==to.size()); autoitFrom=from.begin(); autoitTo=to.begin(); while(itFrom!=from.end()){ if(predicate(*itFrom,*itTo)){ itFrom=from.erase(itFrom); itTo=to.erase(itTo); }else{ ++itFrom; ++itTo; } } } voidupdateFrom(constUnorderedMap<TFrom, TFrom>&mapping){ update(from,mapping); } voidupdateTo(constUnorderedMap<TTo, TTo>&mapping){ update(to,mapping); } std::size_tsize()const{ assert(from.size()==to.size()); returnfrom.size(); } voidclear(){ from.clear(); to.clear(); } private: template<typenameU> voidupdate(Vector<U>&target,constUnorderedMap<U, U>&mapping){ std::transform(target.begin(),target.end(),target.begin(),[&mapping](UoldValue){ returnmapping.at(oldValue); }); } protected: Vector<TFrom>from; Vector<TTo>to; }; }//namespacedna