diff --git a/src/pfaedle/gtfs/ShapeContainer.h b/src/pfaedle/gtfs/ShapeContainer.h index 1128031..be8a9b5 100644 --- a/src/pfaedle/gtfs/ShapeContainer.h +++ b/src/pfaedle/gtfs/ShapeContainer.h @@ -61,6 +61,7 @@ class ShapeContainer { size_t _max; std::string _curId; std::stringstream _writeBuffer; + std::fpos _lastBuff; }; #include "ShapeContainer.tpp" diff --git a/src/pfaedle/gtfs/ShapeContainer.tpp b/src/pfaedle/gtfs/ShapeContainer.tpp index b55d646..1b5c196 100644 --- a/src/pfaedle/gtfs/ShapeContainer.tpp +++ b/src/pfaedle/gtfs/ShapeContainer.tpp @@ -3,10 +3,11 @@ // Authors: Patrick Brosi #include +#include // ____________________________________________________________________________ template -ShapeContainer::ShapeContainer() { +ShapeContainer::ShapeContainer() : _lastBuff(0) { std::string f = pfaedle::getTmpFName("", ""); _storage.open(f, std::fstream::in | std::fstream::out | std::fstream::trunc); @@ -23,6 +24,8 @@ ShapeContainer::ShapeContainer() { // ____________________________________________________________________________ template ShapeContainer::~ShapeContainer() { + _storage << _writeBuffer.rdbuf(); + _storage.flush(); _storage.close(); } @@ -70,18 +73,22 @@ size_t ShapeContainer::size() const { template std::string ShapeContainer::add(const ad::cppgtfs::gtfs::Shape& s) { if (has(s.getId())) return s.getId(); + size_t size = s.getPoints().size(); _ids.insert(s.getId()); - _writeBuffer << s.getId() << '\t' << s.getPoints().size(); - _writeBuffer << std::setprecision(11); - for (auto p : s.getPoints()) { - _writeBuffer << " " << p.lat << " " << p.lng << " " << p.travelDist; - } - // entries are newline separated - _writeBuffer << '\n'; + _writeBuffer << s.getId(); + _writeBuffer.put(' '); + _writeBuffer.write(reinterpret_cast(&size), sizeof(size)); - if (_writeBuffer.tellp() > 1000 * 5000) { - _storage << _writeBuffer.rdbuf(); + for (const auto& p : s.getPoints()) { + _writeBuffer.write(reinterpret_cast(&p.lat), sizeof(p.lat)); + _writeBuffer.write(reinterpret_cast(&p.lng), sizeof(p.lng)); + _writeBuffer.write(reinterpret_cast(&p.travelDist), sizeof(p.travelDist)); + } + + if (_writeBuffer.tellp() - _lastBuff > 1000 * 5000) { + _lastBuff = _writeBuffer.tellp(); + _storage << _writeBuffer.rdbuf(); _writeBuffer.clear(); } @@ -92,6 +99,7 @@ std::string ShapeContainer::add(const ad::cppgtfs::gtfs::Shape& s) { template void ShapeContainer::open() { _storage << _writeBuffer.rdbuf(); + _storage.flush(); _writeBuffer.clear(); _ptr = 0; @@ -107,14 +115,17 @@ bool ShapeContainer::nextStoragePt( while (_storage.good() && !_storage.fail()) { if (!_ptr) { _storage >> _curId; - _storage >> _max; + _storage.ignore(); + + _storage.read(reinterpret_cast(&_max), sizeof(_max)); } if (!_storage.good() || _storage.fail()) return false; - _storage >> ret->lat; - _storage >> ret->lng; - _storage >> ret->travelDist; + _storage.read(reinterpret_cast(&ret->lat), sizeof(ret->lat)); + _storage.read(reinterpret_cast(&ret->lng), sizeof(ret->lng)); + _storage.read(reinterpret_cast(&ret->travelDist), sizeof(ret->travelDist)); + ret->seq = _ptr + 1; ret->id = _curId;