machine_range.hpp 7.82 KB
Newer Older
Millian Poquet's avatar
Millian Poquet committed
1 2 3 4 5
/**
 * @file machine_range.hpp
 * @brief Contains the class which handles set of machines
 */

6 7 8 9 10 11 12
#pragma once

#include <string>

#include <boost/icl/interval_set.hpp>
#include <boost/icl/closed_interval.hpp>

Millian Poquet's avatar
Millian Poquet committed
13 14 15
/**
 * @brief Handles a set of machines
 */
16 17 18
struct MachineRange
{
public:
Millian Poquet's avatar
Millian Poquet committed
19 20 21
    /**
     * @brief Represents a closed interval between two bounds.
     */
22
    typedef boost::icl::closed_interval<int, std::less> ClosedInterval;
Millian Poquet's avatar
Millian Poquet committed
23 24 25
    /**
     * @brief Represents a Set of ClosedInterval
     */
26 27 28
    typedef boost::icl::interval_set<int,
        ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, int),
        ClosedInterval> Set;
29 30

public:
Millian Poquet's avatar
Millian Poquet committed
31 32 33 34
    /**
     * @brief Returns an element_iterator corresponding to the beginning of the MachineRange
     * @return An element_iterator corresponding to the beginning of the MachineRange
     */
35
    Set::element_iterator elements_begin();
Millian Poquet's avatar
Millian Poquet committed
36 37 38 39 40

    /**
     * @brief Returns an element_const_iterator corresponding to the beginning of the MachineRange
     * @return An element_const_iterator corresponding to the beginning of the MachineRange
     */
41 42
    Set::element_const_iterator elements_begin() const;

Millian Poquet's avatar
Millian Poquet committed
43 44 45 46
    /**
     * @brief Returns an element_iterator corresponding to the ending of the MachineRange
     * @return An element_iterator corresponding to the ending of the MachineRange
     */
47
    Set::element_iterator elements_end();
Millian Poquet's avatar
Millian Poquet committed
48 49 50 51 52

    /**
     * @brief Returns an element_const_iterator corresponding to the ending of the MachineRange
     * @return An element_const_iterator corresponding to the ending of the MachineRange
     */
53 54
    Set::element_const_iterator elements_end() const;

Millian Poquet's avatar
Millian Poquet committed
55 56 57 58
    /**
     * @brief Returns an interval iterator corresponding to the beginning of the MachineRange
     * @return An interval iterator corresponding to the beginning of the MachineRange
     */
59
    Set::iterator intervals_begin();
Millian Poquet's avatar
Millian Poquet committed
60 61 62 63 64

    /**
     * @brief Returns an interval const_iterator corresponding to the beginning of the MachineRange
     * @return An interval const_iterator corresponding to the beginning of the MachineRange
     */
65 66
    Set::const_iterator intervals_begin() const;

Millian Poquet's avatar
Millian Poquet committed
67 68 69 70
    /**
     * @brief Returns an interval iterator corresponding to the ending of the MachineRange
     * @return An interval iterator corresponding to the ending of the MachineRange
     */
71
    Set::iterator intervals_end();
Millian Poquet's avatar
Millian Poquet committed
72 73 74 75 76

    /**
     * @brief Returns an interval const_iterator corresponding to the ending of the MachineRange
     * @return An interval const_iterator corresponding to the ending of the MachineRange
     */
77 78
    Set::const_iterator intervals_end() const;

Millian Poquet's avatar
Millian Poquet committed
79 80 81
    /**
     * @brief Clears the MachineRange: After this call, it will be empty
     */
82
    void clear();
Millian Poquet's avatar
Millian Poquet committed
83 84 85 86 87

    /**
     * @brief Inserts a MachineRange into another MachineRange
     * @param[in] range The MachineRange to insert
     */
88
    void insert(const MachineRange & range);
Millian Poquet's avatar
Millian Poquet committed
89 90 91 92 93

    /**
     * @brief Inserts a ClosedInterval into a MachineRange
     * @param[in] interval The interval to insert
     */
94
    void insert(ClosedInterval interval);
Millian Poquet's avatar
Millian Poquet committed
95 96 97 98 99

    /**
     * @brief Inserts a value (a single machine) into a MachineRange
     * @param[in] value The value (the single machine) to insert
     */
100 101
    void insert(int value);

Millian Poquet's avatar
Millian Poquet committed
102 103 104 105
    /**
     * @brief Removes a MachineRange from another MachineRange
     * @param[in] range The MachineRange to remove
     */
106
    void remove(const MachineRange & range);
Millian Poquet's avatar
Millian Poquet committed
107 108 109 110 111

    /**
     * @brief Removes a ClosedInterval from a MachineRange
     * @param[in] interval The ClosedInterval to remove
     */
112
    void remove(ClosedInterval interval);
113

Millian Poquet's avatar
Millian Poquet committed
114 115 116 117 118
    /**
     * @brief Removes a value (a single machine) from a MachineRange
     * @param value The value (the single machine) to remove
     */
    void remove(int value);
119

Millian Poquet's avatar
Millian Poquet committed
120 121 122 123
    /**
     * @brief Returns the first element of the MachineRange
     * @return The first element of the MachineRange
     */
124
    int first_element() const;
Millian Poquet's avatar
Millian Poquet committed
125 126 127 128 129

    /**
     * @brief Returns the number of machines in the MachineRange
     * @return The number of machines in the MachineRange
     */
130
    unsigned int size() const;
Millian Poquet's avatar
Millian Poquet committed
131 132 133 134 135 136

    /**
     * @brief Returns whether a machine is in the MachineRange
     * @param[in] machine_id The machine
     * @return True if and only if the the given machine is in the MachineRange
     */
137 138
    bool contains(int machine_id) const;

Millian Poquet's avatar
Millian Poquet committed
139 140 141 142 143 144 145 146 147
    /**
     * @brief Returns a std::string corresponding to the MachineRange
     * @details For example, with default values, the MachineRange {1,2,3,7} would be outputted as "[1,3]∪[7]"
     * @param[in] union_str The union string
     * @param[in] opening_bracket The opening_bracket string
     * @param[in] closing_bracket The closing_bracket string
     * @param[in] sep The separator string
     * @return A std::string corresponding to the MachineRange
     */
148
    std::string to_string_brackets(const std::string & union_str = "∪", const std::string & opening_bracket = "[", const std::string & closing_bracket = "]", const std::string & sep = ",") const;
Millian Poquet's avatar
Millian Poquet committed
149 150 151 152 153 154 155
    /**
     * @brief Returns a std::string corresponding to the MachineRange
     * @details For example, with default values, the MachineRange {1,2,3,7} would be outputted as "1-3,7"
     * @param[in] sep The separator string
     * @param[in] joiner The joiner string
     * @return A std::string corresponding to the MachineRange
     */
156
    std::string to_string_hyphen(const std::string & sep = ",", const std::string & joiner = "-") const;
Millian Poquet's avatar
Millian Poquet committed
157 158 159 160 161 162 163

    /**
     * @brief Returns a std::string corresponding to the MachineRange
     * @details For example, with default values, the MachineRange {1,2,3,7} would be outputted as "1,2,3,7"
     * @param sep The separator string
     * @return A std::string corresponding to the MachineRange
     */
164 165
    std::string to_string_elements(const std::string & sep = ",") const;

Millian Poquet's avatar
Millian Poquet committed
166 167 168 169 170
    /**
     * @brief Sets the set of a MachineRange such that it is equal to the set of another MachineRange
     * @param[in] other The other MachineRange
     * @return The current MachineRange after the operation
     */
171
    MachineRange & operator=(const MachineRange & other);
Millian Poquet's avatar
Millian Poquet committed
172 173 174 175 176 177

    /**
     * @brief Sets the set of a MachineRange such that it is equal to another ClosedInterval
     * @param[in] interval The ClosedInterval
     * @return The current MachineRange after the operation
     */
178
    MachineRange & operator=(const MachineRange::ClosedInterval & interval);
179

Millian Poquet's avatar
Millian Poquet committed
180 181 182 183 184
    /**
     * @brief Returns whether a MachineRange is equal to another MachineRange
     * @param[in] other The other MachineRange
     * @return True if and only if the two MachineRange contain the same machines
     */
185
    bool operator==(const MachineRange & other);
Millian Poquet's avatar
Millian Poquet committed
186 187 188 189 190 191

    /**
     * @brief Sets the set as the intersection between itself and another MachineRange
     * @param[in] other The other MachineRange
     * @return The current MachineRange after the operation
     */
192
    MachineRange & operator&=(const MachineRange & other);
Millian Poquet's avatar
Millian Poquet committed
193 194 195 196 197 198

    /**
     * @brief Sets the set as the difference between itself and another MachineRange
     * @param[in] other The other MachineRange
     * @return The current MachineRange after the operation
     */
199
    MachineRange &operator-=(const MachineRange & other);
200 201

public:
Millian Poquet's avatar
Millian Poquet committed
202 203 204 205 206 207 208 209 210
    /**
     * @brief Creates a MachineRange from a hyphenized string
     * @details Hyphenized strings are a kind of representation of sets of machines. For example, with default values of sep and joiner, "1-3,7" represents the machines {1,2,3,7}.
     * @param[in] str The hyphenized string
     * @param[in] sep The separator string
     * @param[in] joiner The joiner string
     * @param[in] error_prefix The error prefix (used to output errors)
     * @return The MachineRange which corresponds to a hyphenized string
     */
211
    static MachineRange from_string_hyphen(const std::string & str, const std::string & sep = ",", const std::string & joiner = "-", const std::string & error_prefix = "Invalid machine range string");
212 213

private:
Millian Poquet's avatar
Millian Poquet committed
214
    Set set; //!< The internal set of machines
215
};