RunSet.cpp 7.92 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6 7 8 9 10 11 12 13 14 15 16 17
#include <iomanip>
#include <sstream>
#include <string>
#include "hepnos/DataSet.hpp"
#include "hepnos/RunSet.hpp"
#include "private/DataSetImpl.hpp"
#include "private/DataStoreImpl.hpp"
#include "private/RunImpl.hpp"
#include "private/RunSetImpl.hpp"

namespace hepnos {

Matthieu Dorier's avatar
Matthieu Dorier committed
18 19 20 21
////////////////////////////////////////////////////////////////////////////////////////////
// RunSet implementation
////////////////////////////////////////////////////////////////////////////////////////////

22
RunSet::RunSet(DataSet* ds)
23 24
: m_impl(std::make_unique<RunSet::Impl>(ds)) {
}
25 26 27

RunSet::~RunSet() {}

28
Run RunSet::operator[](const RunNumber& runNumber) {
29
    auto it = find(runNumber);
30 31
    if(!it->valid())
        throw Exception("Requested Run does not exist");
32 33 34
    return std::move(*it);
}

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
RunSet::iterator RunSet::find(const RunNumber& runNumber) {
    int ret;
    std::vector<char> data;
    std::string parent = m_impl->m_dataset->fullname();
    std::string strNum = Run::Impl::makeKeyStringFromRunNumber(runNumber);
    auto datastore = m_impl->m_dataset->m_impl->m_datastore;
    auto level = m_impl->m_dataset->m_impl->m_level;
    bool b = datastore->m_impl->load(level+1, parent, strNum, data);
    if(!b) return end();
    return iterator(Run(datastore, level+1, parent, runNumber));
}

RunSet::const_iterator RunSet::find(const RunNumber& runNumber) const {
    iterator it = const_cast<RunSet*>(this)->find(runNumber);
    return it;
}

RunSet::iterator RunSet::begin() {
    auto it = find(0);
    if(it != end()) return *it;

    auto ds_level = m_impl->m_dataset->m_impl->m_level;
    auto datastore = m_impl->m_dataset->m_impl->m_datastore;
    std::string container = m_impl->m_dataset->fullname();
    Run run(datastore, ds_level+1, container, 0);
    run = run.next();

    if(run.valid()) return iterator(run);
    else return end();
}

RunSet::iterator RunSet::end() {
    return m_impl->m_end;
}

RunSet::const_iterator RunSet::cbegin() const {
    return const_iterator(const_cast<RunSet*>(this)->begin());
}

RunSet::const_iterator RunSet::cend() const {
    return m_impl->m_end;
}

RunSet::const_iterator RunSet::begin() const {
    return const_iterator(const_cast<RunSet*>(this)->begin());
}

RunSet::const_iterator RunSet::end() const {
    return m_impl->m_end;
}

RunSet::iterator RunSet::lower_bound(const RunNumber& lb) {
    if(lb == 0) {
        auto it = find(0);
        if(it != end()) {
            return it;
        } else {
            Run run(m_impl->m_dataset->m_impl->m_datastore, 
                    m_impl->m_dataset->m_impl->m_level+1,
                    m_impl->m_dataset->fullname(), 0);
            run = run.next();
            if(!run.valid()) return end();
            else return iterator(run);
        }
    } else {
        auto it = find(lb-1);
        if(it != end()) {
            ++it;
            return it;
        }
        Run run(m_impl->m_dataset->m_impl->m_datastore,
                m_impl->m_dataset->m_impl->m_level+1,
                m_impl->m_dataset->fullname(), lb-1);
        run = run.next();
        if(!run.valid()) return end();
        else return iterator(run);
    }
}

RunSet::const_iterator RunSet::lower_bound(const RunNumber& lb) const {
    iterator it = const_cast<RunSet*>(this)->lower_bound(lb);
    return it;
}

RunSet::iterator RunSet::upper_bound(const RunNumber& ub) {
    Run run(m_impl->m_dataset->m_impl->m_datastore, 
            m_impl->m_dataset->m_impl->m_level+1, 
            m_impl->m_dataset->fullname(), ub);
    run = run.next();
    if(!run.valid()) return end();
    else return iterator(run);
}

RunSet::const_iterator RunSet::upper_bound(const RunNumber& ub) const {
    iterator it = const_cast<RunSet*>(this)->upper_bound(ub);
    return it;
}

Matthieu Dorier's avatar
Matthieu Dorier committed
133 134 135 136
////////////////////////////////////////////////////////////////////////////////////////////
// RunSet::const_iterator::Impl implementation
////////////////////////////////////////////////////////////////////////////////////////////

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
class RunSet::const_iterator::Impl {
    public:
        Run m_current_run;

        Impl()
        : m_current_run()
        {}

        Impl(const Run& run)
        : m_current_run(run)
        {}

        Impl(Run&& run)
            : m_current_run(std::move(run))
        {}

        Impl(const Impl& other)
            : m_current_run(other.m_current_run)
        {}

        bool operator==(const Impl& other) const {
            return m_current_run == other.m_current_run;
        }
};

Matthieu Dorier's avatar
Matthieu Dorier committed
162
////////////////////////////////////////////////////////////////////////////////////////////
163
// RunSet::const_iterator implementation
Matthieu Dorier's avatar
Matthieu Dorier committed
164 165
////////////////////////////////////////////////////////////////////////////////////////////

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
RunSet::const_iterator::const_iterator()
: m_impl(std::make_unique<Impl>()) {}

RunSet::const_iterator::const_iterator(const Run& run)
: m_impl(std::make_unique<Impl>(run)) {}

RunSet::const_iterator::const_iterator(Run&& run)
: m_impl(std::make_unique<Impl>(std::move(run))) {}

RunSet::const_iterator::~const_iterator() {}

RunSet::const_iterator::const_iterator(const RunSet::const_iterator& other)
: m_impl(std::make_unique<Impl>(*other.m_impl)) {}

RunSet::const_iterator::const_iterator(RunSet::const_iterator&& other)
: m_impl(std::move(other.m_impl)) {}

RunSet::const_iterator& RunSet::const_iterator::operator=(const RunSet::const_iterator& other) {
    if(&other == this) return *this;
    m_impl = std::make_unique<Impl>(*other.m_impl);
    return *this;
}

RunSet::const_iterator& RunSet::const_iterator::operator=(RunSet::const_iterator&& other) {
    if(&other == this) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
}

RunSet::const_iterator::self_type RunSet::const_iterator::operator++() {
    if(!m_impl) {
        throw Exception("Trying to increment an invalid iterator");
    }
    m_impl->m_current_run = m_impl->m_current_run.next();
    return *this;
}

RunSet::const_iterator::self_type RunSet::const_iterator::operator++(int) {
    const_iterator copy = *this;
    ++(*this);
    return copy;
}

const RunSet::const_iterator::reference RunSet::const_iterator::operator*() {
    if(!m_impl) {
        throw Exception("Trying to dereference an invalid iterator");
    }
    return m_impl->m_current_run;
}

const RunSet::const_iterator::pointer RunSet::const_iterator::operator->() {
    if(!m_impl) return nullptr;
    return &(m_impl->m_current_run);
}

bool RunSet::const_iterator::operator==(const self_type& rhs) const {
    if(!m_impl && !rhs.m_impl)  return true;
    if(m_impl  && !rhs.m_impl)  return false;
    if(!m_impl && rhs.m_impl)   return false;
    return *m_impl == *(rhs.m_impl);
}

bool RunSet::const_iterator::operator!=(const self_type& rhs) const {
        return !(*this == rhs);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
232
////////////////////////////////////////////////////////////////////////////////////////////
233
// RunSet::iterator implementation
Matthieu Dorier's avatar
Matthieu Dorier committed
234 235
////////////////////////////////////////////////////////////////////////////////////////////

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
RunSet::iterator::iterator(const Run& current)
: const_iterator(current) {}

RunSet::iterator::iterator(Run&& current)
: const_iterator(std::move(current)) {}

RunSet::iterator::iterator()
: const_iterator() {}

RunSet::iterator::~iterator() {}

RunSet::iterator::iterator(const RunSet::iterator& other)
: const_iterator(other) {}

RunSet::iterator::iterator(RunSet::iterator&& other)
: const_iterator(std::move(other)) {}

RunSet::iterator& RunSet::iterator::operator=(const RunSet::iterator& other) {
    if(this == &other) return *this;
    m_impl = std::make_unique<Impl>(*other.m_impl);
    return *this;
}

RunSet::iterator& RunSet::iterator::operator=(RunSet::iterator&& other) {
    if(this == &other) return *this;
    m_impl = std::move(other.m_impl);
    return *this;
}

RunSet::iterator::reference RunSet::iterator::operator*() {
    return const_cast<reference>(const_iterator::operator*());
}

RunSet::iterator::pointer RunSet::iterator::operator->() {
    return const_cast<pointer>(const_iterator::operator->());
}

}