Reworked Container structure and wrote template for n-dim Containers.
This commit is contained in:
+119
-23
@@ -8,38 +8,134 @@
|
||||
|
||||
|
||||
#include <iostream>
|
||||
//#include <mpi.h>
|
||||
#include "hdf5.h"
|
||||
|
||||
#include <map>
|
||||
#include <deque>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
//Sum Generic Container
|
||||
/*template <typename T>
|
||||
double Sum(const T& cont) {
|
||||
double sum = 0;
|
||||
|
||||
const std::string filePath = "/Users/ChristianReiling/Desktop/hdf5_example/examplefile.h5";
|
||||
typename T::const_iterator iter = cont.begin();
|
||||
for (iter; iter != cont.end(); iter++) {
|
||||
sum += *iter;
|
||||
}
|
||||
|
||||
hid_t file_id, dataset_id, dataspace_id; /* identifiers */
|
||||
hsize_t dims[2];
|
||||
herr_t status;
|
||||
return sum;
|
||||
}
|
||||
*/
|
||||
//Sum Generic Map
|
||||
//template <typename T1, typename T2>
|
||||
|
||||
template <typename T1, typename T2>
|
||||
double Sum(const map<T1, T2>& mp) {
|
||||
double sum = 0;
|
||||
|
||||
/* Create a new file using default properties. */
|
||||
file_id = H5Fcreate(filePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
||||
typename map<T1,T2>::const_iterator iter = mp.begin();
|
||||
for (iter; iter != mp.end(); iter++) {
|
||||
sum += iter->second;
|
||||
}
|
||||
|
||||
/* Create the data space for the dataset. */
|
||||
dims[0] = 4;
|
||||
dims[1] = 6;
|
||||
dataspace_id = H5Screate_simple(2, dims, NULL);
|
||||
return sum;
|
||||
}
|
||||
|
||||
template <typename T2>
|
||||
typename map<double,T2>::const_iterator test(const map<double,T2>&mp) {
|
||||
return(mp.begin());
|
||||
}
|
||||
/*
|
||||
//Sum Generic Container Iterators
|
||||
template <typename T>
|
||||
double Sum(T& begin, const T& end) {
|
||||
double sum = 0;
|
||||
|
||||
/* Create the dataset. */
|
||||
dataset_id = H5Dcreate2(file_id, "/dset", H5T_STD_I32BE, dataspace_id,
|
||||
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
||||
for (begin; begin != end; begin++) {
|
||||
sum += *begin;
|
||||
}
|
||||
|
||||
/* End access to the dataset and release resources used by it. */
|
||||
status = H5Dclose(dataset_id);
|
||||
return sum;
|
||||
}
|
||||
|
||||
//Sum Generic Map Iterators
|
||||
template <typename T1, typename T2>
|
||||
double Sum(map<T1, T2>::iterator& begin, map<T1, T2>::iterator& end) {
|
||||
double sum = 0;
|
||||
|
||||
/* Terminate access to the data space. */
|
||||
status = H5Sclose(dataspace_id);
|
||||
for (begin; begin != end; begin++) {
|
||||
sum += begin->second;
|
||||
}
|
||||
|
||||
/* Close the file. */
|
||||
status = H5Fclose(file_id);
|
||||
return sum;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
void add_To_Map(map<double,double> *Map, deque<double> queIn) {
|
||||
double x1 = *queIn.begin();
|
||||
double f = *queIn.rbegin();
|
||||
if ( Map->find(x1)!=Map->end() ) {
|
||||
stringstream ss;
|
||||
ss<<"HIT: Key x="<<x1<<" already exists.";
|
||||
cout<<ss.str()<<endl;
|
||||
//throw MyException(ss.str());
|
||||
}
|
||||
Map->insert(pair<double,double>(x1, f));
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void add_To_Map (map<double,T> *Map, deque<double> queIn) {
|
||||
int dim = (int)queIn.size()-1;
|
||||
if (dim<1) {
|
||||
stringstream ss;
|
||||
ss<<"addTo_Map: vecIn not big enough.";
|
||||
cout<<ss.str()<<endl;
|
||||
throw MyException(ss.str());
|
||||
}
|
||||
double xN = *queIn.begin();
|
||||
queIn.pop_front();
|
||||
T tempMap;
|
||||
if ( Map->find(xN)==Map->end() ) { // No such element -> generate new Map and insert
|
||||
T tempMap;
|
||||
add_To_Map(&tempMap, queIn);
|
||||
Map->insert(pair<double,T>(xN,tempMap));
|
||||
} else { // Found such element -> Insert new pair
|
||||
add_To_Map(&Map->find(xN)->second, queIn); //Add element in element, using pointer to subMap.
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
//Map
|
||||
cout << "map" << endl;
|
||||
map<double, double> mp;
|
||||
/*
|
||||
mp[3] = 5.5;
|
||||
mp[4] = 6.7;
|
||||
mp[5] = 8.4;
|
||||
cout<<test(mp)->first<<endl;
|
||||
|
||||
|
||||
add_To_Map(&mp,queIn);
|
||||
*/
|
||||
|
||||
deque<double> queIn;
|
||||
map<double,map<double,double>> d2mp;
|
||||
queIn.push_back(3);
|
||||
queIn.push_back(4);
|
||||
queIn.push_back(5);
|
||||
add_To_Map(&d2mp, queIn);
|
||||
|
||||
queIn.pop_back();
|
||||
queIn.pop_back();
|
||||
queIn.push_back(6);
|
||||
queIn.push_back(7);
|
||||
add_To_Map(&d2mp, queIn);
|
||||
//cout << Sum(mp) <<endl;
|
||||
//map<string, double>::iterator mp_iter_begin = mp.begin();
|
||||
//map<string, double>::iterator mp_iter_end = mp.end();
|
||||
//cout << Sum(mp_iter_begin, mp_iter_end) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user