Commit b77d842b authored by Matthieu Dorier's avatar Matthieu Dorier

using binary format numbers instead of hexadecimal in keys

parent 300b1d0f
......@@ -51,12 +51,8 @@ Event Event::next() const {
if(s == 0) return Event();
size_t i = m_impl->m_container->size()+1;
if(keys[0].size() <= i) return Event();
if(keys[0][i] != '%') return Event();
std::stringstream strEventNumber;
strEventNumber << std::hex << keys[0].substr(i+1);
EventNumber n;
strEventNumber >> n;
EventNumber n = Impl::parseEventNumberFromKeyString(&keys[0][i]);
if(n == InvalidEventNumber) return Event();
return Event(m_impl->m_datastore, m_impl->m_level, m_impl->m_container, n);
}
......
......@@ -53,12 +53,8 @@ Run Run::next() const {
if(s == 0) return Run();
size_t i = m_impl->m_container->size()+1;
if(keys[0].size() <= i) return Run();
if(keys[0][i] != '%') return Run();
std::stringstream strRunNumber;
strRunNumber << std::hex << keys[0].substr(i+1);
RunNumber rn;
strRunNumber >> rn;
RunNumber rn = Impl::parseRunNumberFromKeyString(&keys[0][i]);
if(rn == InvalidRunNumber) return Run();
return Run(m_impl->m_datastore, m_impl->m_level, m_impl->m_container, rn);
}
......
......@@ -56,12 +56,8 @@ SubRun SubRun::next() const {
if(s == 0) return SubRun();
size_t i = m_impl->m_container->size()+1;
if(keys[0].size() <= i) return SubRun();
if(keys[0][i] != '%') return SubRun();
std::stringstream strSubRunNumber;
strSubRunNumber << std::hex << keys[0].substr(i+1);
SubRunNumber rn;
strSubRunNumber >> rn;
SubRunNumber rn = Impl::parseSubRunNumberFromKeyString(&keys[0][i]);
if(rn == InvalidSubRunNumber) return SubRun();
return SubRun(m_impl->m_datastore, m_impl->m_level, m_impl->m_container, rn);
}
......
......@@ -6,6 +6,7 @@
#ifndef __HEPNOS_PRIVATE_EVENT_IMPL_H
#define __HEPNOS_PRIVATE_EVENT_IMPL_H
#include <boost/predef/other/endian.h>
#include <sstream>
#include <iomanip>
#include <memory>
......@@ -29,9 +30,47 @@ class Event::Impl {
, m_event_nr(n) {}
static std::string makeKeyStringFromEventNumber(const EventNumber& n) {
std::string str(1+sizeof(n),'\0');
str[0] = '%';
#ifndef HEPNOS_READABLE_NUMBERS
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&str[1], &n, sizeof(n));
return str;
#else
unsigned i = sizeof(n);
auto n2 = n;
while(n2 != 0) {
str[i] = n2 & 0xff;
n2 = n2 >> 8;
i -= 1;
}
return str;
#endif
#else
std::stringstream strstr;
strstr << "%" << std::setfill('0') << std::setw(16) << std::hex << n;
return strstr.str();
#endif
}
static EventNumber parseEventNumberFromKeyString(const char* str) {
if(str[0] != '%') return InvalidEventNumber;
EventNumber n;
#ifdef HEPNOS_READABLE_NUMBERS
std::stringstream strEventNumber;
strEventNumber << std::hex << std::string(str+1, 16);
strEventNumber >> n;
#else
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&n, &str[1], sizeof(n));
#else
n = 0;
for(unsigned i=0; i < sizeof(n); i++) {
n = 256*n + str[i+1];
}
#endif
#endif
return n;
}
std::string makeKeyStringFromEventNumber() const {
......
......@@ -29,10 +29,48 @@ class Run::Impl {
, m_container(container)
, m_run_nr(rn) {}
static std::string makeKeyStringFromRunNumber(const RunNumber& nr) {
static std::string makeKeyStringFromRunNumber(const RunNumber& n) {
std::string str(1+sizeof(n),'\0');
str[0] = '%';
#ifndef HEPNOS_READABLE_NUMBERS
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&str[1], &n, sizeof(n));
return str;
#else
unsigned i = sizeof(n);
auto n2 = n;
while(n2 != 0) {
str[i] = n2 & 0xff;
n2 = n2 >> 8;
i -= 1;
}
return str;
#endif
#else
std::stringstream strstr;
strstr << "%" << std::setfill('0') << std::setw(16) << std::hex << nr;
strstr << "%" << std::setfill('0') << std::setw(16) << std::hex << n;
return strstr.str();
#endif
}
static RunNumber parseRunNumberFromKeyString(const char* str) {
if(str[0] != '%') return InvalidRunNumber;
RunNumber n;
#ifdef HEPNOS_READABLE_NUMBERS
std::stringstream strRunNumber;
strRunNumber << std::hex << std::string(str+1, 16);
strRunNumber >> n;
#else
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&n, &str[1], sizeof(n));
#else
n = 0;
for(unsigned i=0; i < sizeof(n); i++) {
n = 256*n + str[i+1];
}
#endif
#endif
return n;
}
std::string makeKeyStringFromRunNumber() const {
......
......@@ -30,9 +30,47 @@ class SubRun::Impl {
, m_subrun_nr(rn) {}
static std::string makeKeyStringFromSubRunNumber(const SubRunNumber& n) {
std::string str(1+sizeof(n),'\0');
str[0] = '%';
#ifndef HEPNOS_READABLE_NUMBERS
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&str[1], &n, sizeof(n));
return str;
#else
unsigned i = sizeof(n);
auto n2 = n;
while(n2 != 0) {
str[i] = n2 & 0xff;
n2 = n2 >> 8;
i -= 1;
}
return str;
#endif
#else
std::stringstream strstr;
strstr << "%" << std::setfill('0') << std::setw(16) << std::hex << n;
return strstr.str();
#endif
}
static SubRunNumber parseSubRunNumberFromKeyString(const char* str) {
if(str[0] != '%') return InvalidSubRunNumber;
SubRunNumber n;
#ifdef HEPNOS_READABLE_NUMBERS
std::stringstream strSubRunNumber;
strSubRunNumber << std::hex << std::string(str+1, 16);
strSubRunNumber >> n;
#else
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&n, &str[1], sizeof(n));
#else
n = 0;
for(unsigned i=0; i < sizeof(n); i++) {
n = 256*n + str[i+1];
}
#endif
#endif
return n;
}
std::string makeKeyStringFromSubRunNumber() const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment