// // base_container.h // mc_electron // // Created by Christian Reiling on 10.05.19. // Copyright © 2019 Christian Reiling. All rights reserved. // #ifndef base_container_h #define base_container_h #include #include #include #include #include #include "blitz/tinyvec2.h" #include "blitz/array.h" #include "error_types.h" #include "read_system.h" #include "constants.h" typedef std::deque ddeque; typedef blitz::TinyVector vec3; typedef std::map dim1Map; typedef std::map dim2Map; typedef std::map dim3Map; typedef std::map dim4Map; enum searchStyle {Find, Lower_bound, Lower_bound_Over}; template class Base_Container { protected: T_Map Map; int Dim; public: Base_Container() { Dim = calc_Dim(); } Base_Container(std::string load_path) { Dim = calc_Dim(); importFromCSV(load_path); } protected: template double calc_Dim(); private: void inc_Count(dim1Map *subMap, int *count) ; template void inc_Count(std::map *subMap, int *count); public: void importFromCSV(std::string load_path); private: void addPointFromLine(const std::string &lineFromFile); public: void addPoint(std::deque pointIn); private: void rec_addPoint(std::map *subMap, std::deque pointIn); template void rec_addPoint (std::map *subMap, std::deque pointIn); public: void exportToCSV(std::string export_path) ; private: std::string getExportFileHeader() ; void rec_exportToCSV (const dim1Map &subMap, std::ofstream &exportStream, std::string preset = "") ; template void rec_exportToCSV(const T &subMap, std::ofstream &exportStream, std::string preset = "") ; protected: double get_value(std::deque Points, searchStyle searchMode) ; template T_target* getSubMap(std::deque Points, searchStyle searchMode) ; private: template void rec_getSubMap(T_target *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) ; template void rec_getSubMap(T *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) ; template typename T::iterator searchItrByStyle(T *subMap, double x, searchStyle searchMode) ; protected: void checkEmpty() ; public: T_Map getMap() const {return (Map);} }; template template double Base_Container::calc_Dim() { T dummyMap; int count = 0; inc_Count(&dummyMap, &count); return (count); } template void Base_Container::inc_Count(dim1Map *subMap, int *count) { ++(*count); subMap->insert(std::pair(0,0)); } template template void Base_Container::inc_Count(std::map *subMap, int *count) { ++(*count); T tempMap; inc_Count(&tempMap, count); subMap->insert(std::pair(0,tempMap)); } template void Base_Container::importFromCSV(std::string load_path) { std::ifstream importStream(load_path); if (!importStream.good()) { std::stringstream ss; ss<<"Base_Container::readFromCSV: Could not open file. \n loadFilePath= "< void Base_Container::addPointFromLine(const std::string &lineFromFile) { std::stringstream ss; ss.str(lineFromFile); std::deque dequeTemp; double tempVal; for (int i = 0;i<=Dim;i++) { if (ss>>tempVal) { dequeTemp.push_back(tempVal); } else { std::cout<<"Base_Container::addPointFromFile: Warning could not read line in inputFile. \nloadFilePath\t"< void Base_Container::addPoint(std::deque pointIn) { if (pointIn.size() != Dim+1) { std::stringstream ss; ss<<"Base_Container::addPoint: deque pointIn dimension missmatch.\n"; for (std::deque::iterator Itr = pointIn.begin(); Itr!=pointIn.end(); Itr++) { std::cout<<*Itr<<"\n"; } throw MyException(ss.str()); } rec_addPoint(&Map, pointIn); } template void Base_Container::rec_addPoint(dim1Map *subMap, std::deque pointIn) { //Iteration End if (pointIn.size()!=2) { std::stringstream ss; ss<<"Base_Container::addPoint: Dimension missmatch of pointIn.\nDelta =\t"<find(x) != subMap->end()) { std::stringstream ss; ss<<"Base_Container::addPoint: Dublicate insertion of key.\n"; throw MyException(ss.str()); } subMap->insert(std::pair(x, f)); } template template void Base_Container::rec_addPoint (std::map *subMap, std::deque pointIn) { double x = *pointIn.begin(); pointIn.pop_front(); if (subMap->find(x) == subMap->end()) { // No such element -> generate new Map and insert T tempMap; rec_addPoint(&tempMap, pointIn); subMap->insert(std::pair(x,tempMap)); } else { // Found such element -> Insert to existing map using iteration rec_addPoint(&subMap->find(x)->second, pointIn); } } template void Base_Container::exportToCSV(std::string export_path) { checkEmpty(); std::ofstream exportStream(export_path); if (!exportStream.good()) { std::stringstream ss; ss<<"Base_Container::exportToCSV: Could not open file.\nexportFilePath\t "< std::string Base_Container::getExportFileHeader() { std::stringstream ss; ss<<"# Automatically generated Data of Function2D_Container class by exportToFile().\n"; ss<<"# Generated on "< void Base_Container::rec_exportToCSV(const dim1Map &subMap, std::ofstream &exportStream, std::string preset) { for (dim1Map::const_iterator Itr = subMap.cbegin();Itr!=subMap.cend();Itr++) { exportStream<first<<"\t"<second<<"\n"; } } template template void Base_Container::rec_exportToCSV(const T &subMap, std::ofstream &exportStream, std::string preset) { std::stringstream ss; for (typename T::const_iterator Itr = subMap.cbegin();Itr!=subMap.cend();Itr++) { ss.clear(); ss.str(""); ss<first<<"\t"; rec_exportToCSV(Itr->second, exportStream, ss.str()); } } template double Base_Container::get_value(std::deque Points, searchStyle searchMode) { double x = Points.back(); Points.pop_back(); dim1Map *subMap = getSubMap(Points, searchMode); dim1Map::const_iterator Itr = searchItrByStyle(subMap, x, searchMode); return(Itr->second); } template template T_target* Base_Container::getSubMap(std::deque Points, searchStyle searchMode) { checkEmpty(); double T_Dim = calc_Dim(); if (Points.size() != Dim - T_Dim) { std::stringstream ss; ss<<"Base_Container::getSubMap: Dimension Missmatch of Points\n Missmatch =\t<< "< template void Base_Container::rec_getSubMap(T_target *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) { if (Points.size() != 0) { std::stringstream ss; ss<<"Base_Container::get_subItr: Dimension Missmatch of Points\n"; throw MyException(ss.str()); } *targetMapPtr = subMap; } template template void Base_Container::rec_getSubMap(T *subMap, T_target **targetMapPtr, std::deque Points, searchStyle searchMode) { double x = Points.front(); Points.pop_front(); typename T::iterator Itr = searchItrByStyle(subMap, x, searchMode); rec_getSubMap(&(Itr->second), targetMapPtr, Points, searchMode); } template template typename T::iterator Base_Container::searchItrByStyle(T *subMap, double x, searchStyle searchMode) { typename T::iterator Itr; if (searchMode == Find) { Itr = subMap->find(x); } else if (searchMode == Lower_bound) { Itr = subMap->lower_bound(x); } else if (searchMode == Lower_bound_Over) { Itr = subMap->lower_bound(x); if (Itr == subMap->end()) { --Itr; } } if (Itr == subMap->end()) { std::stringstream ss; ss<<"Base_Container::calcItr: Point is out of bound.\n"; throw MyException(ss.str()); } return (Itr); } template void Base_Container::checkEmpty() { if (Map.empty()) { std::stringstream ss; ss<<"Base_Conainer::checkEmpty: Map is empty.\n"; throw MyException(ss.str()); } } #endif /* base_container_h */