IrregularityFile.cpp 2.34 KB
Newer Older
Matteo's avatar
update  
Matteo committed
1
#include "IrregularityFile.h"
Matteo's avatar
Matteo committed
2

Matteo's avatar
Matteo committed
3
#include <algorithm>
Matteo's avatar
Matteo committed
4
#include <exception>
Matteo's avatar
Matteo committed
5
6
#include <iterator>
#include <memory>
Matteo's avatar
update  
Matteo committed
7

Matteo's avatar
Matteo committed
8
IrregularityFile::IrregularityFile(std::optional<uint16_t> offset) : offset_(offset) {}
Matteo's avatar
update  
Matteo committed
9

Matteo's avatar
Matteo committed
10
11
12
IrregularityFile::IrregularityFile(const IrregularityFile& rhs) {
    std::transform(rhs.irregularities_.begin(), rhs.irregularities_.end(), std::back_inserter(irregularities_),
                   [](const std::unique_ptr<Irregularity>& ptr) { return std::make_unique<Irregularity>(*ptr); });
Matteo's avatar
Matteo committed
13
}
Matteo's avatar
update  
Matteo committed
14

Matteo's avatar
Matteo committed
15
16
17
18
IrregularityFile& IrregularityFile::add(std::unique_ptr<Irregularity> irregularity) {
    irregularities_.push_back(std::move(irregularity));
    return *this;
}
Matteo's avatar
update  
Matteo committed
19

Matteo's avatar
Matteo committed
20
IrregularityFile& IrregularityFile::remove_by_id(const boost::uuids::uuid id) {
Matteo's avatar
Matteo committed
21
22
23
    auto it =
        std::find_if(irregularities_.begin(), irregularities_.end(),
                     [&id](const std::unique_ptr<Irregularity>& irregularity) { return irregularity->get_id() == id; });
Matteo's avatar
Matteo committed
24
25
26
27
    if (it != irregularities_.end()) {
        irregularities_.erase(it);
    }
    return *this;
Matteo's avatar
update  
Matteo committed
28
29
}

Matteo's avatar
Matteo committed
30
IrregularityFile& IrregularityFile::sort() {
Matteo's avatar
Matteo committed
31
32
33
34
    std::sort(irregularities_.begin(), irregularities_.end(),
              [](const std::unique_ptr<Irregularity>& a, const std::unique_ptr<Irregularity>& b) {
                  return a->get_time_label() < b->get_time_label();
              });
Matteo's avatar
Matteo committed
35
    return *this;
Matteo's avatar
update  
Matteo committed
36
37
}

Matteo's avatar
Matteo committed
38
std::optional<uint16_t> IrregularityFile::get_offset() const { return offset_; }
Matteo's avatar
update  
Matteo committed
39

Matteo's avatar
Matteo committed
40
std::vector<std::unique_ptr<Irregularity>>::iterator IrregularityFile::begin() { return irregularities_.begin(); }
Matteo's avatar
update  
Matteo committed
41

Matteo's avatar
Matteo committed
42
std::vector<std::unique_ptr<Irregularity>>::iterator IrregularityFile::end() { return irregularities_.end(); }
Matteo's avatar
update  
Matteo committed
43

Matteo's avatar
Matteo committed
44
45
46
47
48
49
json IrregularityFile::toJSON() const {
    json j;
    j["Offset"] = offset_.value_or(0);
    j["Irregularities"] = json::array();
    for (const auto& irregularity : irregularities_) {
        j["Irregularities"].push_back(irregularity->to_JSON());
Matteo's avatar
update  
Matteo committed
50
    }
Matteo's avatar
Matteo committed
51
    return j;
Matteo's avatar
update  
Matteo committed
52
53
}

Matteo's avatar
Matteo committed
54
55
56
57
58
59
60
61
62
63
IrregularityFile IrregularityFile::fromJSON(const json j) {
    if (!j.contains("Offset") || !j.contains("Irregularities")) {
        throw std::invalid_argument("Invalid JSON");
    }
    IrregularityFile irregularity_file(j["Offset"].get<uint16_t>());
    for (const auto& irregularity : j["Irregularities"]) {
        irregularity_file.add(std::make_unique<Irregularity>(Irregularity::from_JSON(irregularity)));
    }
    return irregularity_file;
}