8#ifndef model_IDREADER_H_
9#define model_IDREADER_H_
25 template <
int keySize>
28 static_assert(keySize>0,
"keySize must be >0");
30 typedef std::array<SingleKeyType, keySize>
KeyType;
53 template <
int keySize,
int valueSize,
typename T>
54 class IDreader :
public std::map<typename IDreaderKeySelector<keySize>::KeyType, std::array<T, valueSize> >
56 static_assert(valueSize>=0,
"valueSize must be >0");
60 typedef std::array<T, valueSize> ValueType;
61 typedef std::pair<KeyType, ValueType > BinType;
63 long long int currentFrame;
64 std::map<std::string,size_t> labelsMap;
67 bool readTXT(
const std::string& filename)
70 std::ifstream ifs ( filename.c_str() , std::ifstream::in );
76 std::cout<<
"Reading: "<<filename;
82 while (std::getline(ifs, line))
84 std::stringstream
ss(line);
97 throw std::runtime_error(
"IDreader: key "+std::to_string(
temp)+
" must be an integer.");
103 value[col-keySize]=
temp;
108 this->emplace(
key,value);
118 std::cout<<
" ("<<this->size()<<
" entries) ["<<(clock()-
t0)/CLOCKS_PER_SEC<<
" sec]"<<std::endl;
122 std::cout<<
"Unable to open:"<<filename<<std::endl;
131 bool readBIN(
const std::string& filename)
135 std::cout<<
"Reading: "<<filename<<std::flush<<
" (";
136 const auto t0=std::chrono::system_clock::now();
138 for (
const auto& pair : rE)
141 const bool success=this->emplace(pair.first,pair.second).second;
144 throw std::runtime_error(
"IDreader: cannot insert key="+std::to_string(pair.first)+
".");
147 std::cout<<this->size()<<
"elements in "<<(std::chrono::duration<double>(std::chrono::system_clock::now()-
t0)).
count()<<
" sec)"<<std::endl;
176 std::ifstream labelsFile(labelsFileName.c_str(), std::ifstream::in);
177 if (labelsFile.is_open())
179 std::cout<<
"reading labels file: "<<labelsFileName;
183 std::getline(labelsFile, line);
186 while (std::getline(labelsFile, line))
189 const bool success=labelsMap.emplace(line,row).second;
192 std::cout<<
"Unable to insert label "<<line<<std::endl;
201 std::cout<<
" ("<<labelsMap.size()<<
" labels) ["<<(clock()-
t0)/CLOCKS_PER_SEC<<
" sec]"<<std::endl;
205 std::cout<<
"Unable to read labels file:"<<labelsFileName<<std::endl;
212 const auto rowIter(this->find(
key));
213 assert(rowIter!=this->
end() &&
"ID not found.");
214 const auto colIter(labelsMap.find(label));
215 assert(colIter!=labelsMap.end() &&
"label not found.");
216 return rowIter->second.operator[](colIter->second);
220 const T&
first(
const std::string& label)
222 const auto rowIter(this->begin());
223 const auto colIter(labelsMap.find(label));
224 assert(colIter!=labelsMap.end() &&
"label not found.");
225 return rowIter->second.operator[](colIter->second);
229 const T&
last(
const std::string& label)
231 const auto rowIter(this->rbegin());
232 const auto colIter(labelsMap.find(label));
233 assert(colIter!=labelsMap.end() &&
"label not found.");
234 return rowIter->second.operator[](colIter->second);
238 std::string
getFilename(
const long long int& frameN,
const bool& useTXT)
const
255 bool isGood(
const long long int& frameN,
const bool& useTXT)
const
259 std::ifstream ifs (
getFilename(frameN,useTXT).c_str() , std::ifstream::in );
264 bool read(
const long long int& frameN,
const bool& useTXT)
270 assert(frameN>=0 &&
"frameN MUST BE >= 0");
272 if (frameN!=currentFrame || this->empty())
285 assert(0 &&
"NOT IMEPLEMETNTED YET");
292 std::cout<<
getFilename(frameN,useTXT)<<
" already read."<<std::endl;
A class template that reads generic binary files containing arrays of DataType elements.
const bool & success() const
void readLabelsFile(const std::string &labelsFileName)
bool read(const long long int &frameN, const bool &useTXT)
IDreader(const std::string &fileNamePrefix_in)
const T & first(const std::string &label)
std::string getFilename(const long long int &frameN, const bool &useTXT) const
const T & operator()(const KeyType &key, const std::string &label)
bool isGood(const long long int &frameN, const bool &useTXT) const
const T & last(const std::string &label)
const std::string fileNamePrefix
size_t InstanceCount< Derived >::count
long long int SingleKeyType
static SingleKeyType & keyElement(KeyType &key, const size_t &)
static SingleKeyType & keyElement(KeyType &key, const size_t &k)
std::array< SingleKeyType, keySize > KeyType
long long int SingleKeyType
enum insertvertexresult success