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

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

Matteo's avatar
Matteo committed
5
6
7
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
8
}
Matteo's avatar
update  
Matteo committed
9

Matteo's avatar
Matteo committed
10
11
12
13
IrregularityFile& IrregularityFile::add(std::unique_ptr<Irregularity> irregularity) {
    irregularities_.push_back(std::move(irregularity));
    return *this;
}
Matteo's avatar
update  
Matteo committed
14

Matteo's avatar
Matteo committed
15
IrregularityFile& IrregularityFile::remove_by_id(const boost::uuids::uuid id) {
Matteo's avatar
Matteo committed
16
17
18
    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
19
20
21
22
    if (it != irregularities_.end()) {
        irregularities_.erase(it);
    }
    return *this;
Matteo's avatar
update  
Matteo committed
23
24
}

Matteo's avatar
Matteo committed
25
IrregularityFile& IrregularityFile::sort() {
Matteo's avatar
Matteo committed
26
27
28
29
    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
30
    return *this;
Matteo's avatar
update  
Matteo committed
31
32
}

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

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

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

Matteo's avatar
Matteo committed
39
40
41
42
43
44
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
45
    }
Matteo's avatar
Matteo committed
46
    return j;
Matteo's avatar
update  
Matteo committed
47
48
}

Matteo's avatar
Matteo committed
49
50
51
52
53
54
55
56
57
58
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;
}