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

6 7 8 9
#include "machine_range.hpp"

#include <vector>

10
#include <boost/algorithm/string.hpp>
11 12 13

#include <simgrid/msg.h>

Millian Poquet's avatar
Millian Poquet committed
14

Millian Poquet's avatar
Millian Poquet committed
15
XBT_LOG_NEW_DEFAULT_CATEGORY(machine_range, "machine_range"); //!< Logging
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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

using namespace std;

MachineRange::Set::element_iterator MachineRange::elements_begin()
{
    return boost::icl::elements_begin(set);
}

MachineRange::Set::element_const_iterator MachineRange::elements_begin() const
{
    return boost::icl::elements_begin(set);
}

MachineRange::Set::element_iterator MachineRange::elements_end()
{
    return boost::icl::elements_end(set);
}

MachineRange::Set::element_const_iterator MachineRange::elements_end() const
{
    return boost::icl::elements_end(set);
}

MachineRange::Set::iterator MachineRange::intervals_begin()
{
    return set.begin();
}

MachineRange::Set::const_iterator MachineRange::intervals_begin() const
{
    return set.begin();
}

MachineRange::Set::iterator MachineRange::intervals_end()
{
    return set.end();
}

MachineRange::Set::const_iterator MachineRange::intervals_end() const
{
    return set.end();
}


void MachineRange::clear()
{
    set.clear();
Millian Poquet's avatar
Millian Poquet committed
63
    xbt_assert(size() == 0);
64 65
}

66 67 68 69 70 71
void MachineRange::insert(const MachineRange &range)
{
    for (auto it = range.intervals_begin(); it != range.intervals_end(); ++it)
        set.insert(*it);
}

72
void MachineRange::insert(ClosedInterval interval)
73 74 75 76 77 78 79 80 81
{
    set.insert(interval);
}

void MachineRange::insert(int value)
{
    set.insert(value);
}

82 83 84 85 86
void MachineRange::remove(const MachineRange &range)
{
    set -= range.set;
}

87
void MachineRange::remove(ClosedInterval interval)
88 89 90 91 92 93 94 95 96
{
    set -= interval;
}

void MachineRange::remove(int value)
{
    set -= value;
}

97 98 99 100 101 102 103 104 105 106 107
int MachineRange::first_element() const
{
    xbt_assert(size() > 0);
    return *elements_begin();
}

unsigned int MachineRange::size() const
{
    return set.size();
}

108 109 110 111 112
bool MachineRange::contains(int machine_id) const
{
    return boost::icl::contains(set, machine_id);
}

113 114 115
std::string MachineRange::to_string_brackets(const std::string & union_str,
                                             const std::string & opening_bracket,
                                             const std::string & closing_bracket,
116
                                             const std::string & sep) const
117 118
{
    vector<string> machine_id_strings;
119
    for (auto it = intervals_begin(); it != intervals_end(); ++it)
120 121 122 123
        if (it->lower() == it->upper())
            machine_id_strings.push_back(opening_bracket + to_string(it->lower()) + closing_bracket);
        else
            machine_id_strings.push_back(opening_bracket + to_string(it->lower()) + sep + to_string(it->upper()) + closing_bracket);
124 125 126 127

    return boost::algorithm::join(machine_id_strings, union_str);
}

128
std::string MachineRange::to_string_hyphen(const std::string &sep, const std::string &joiner) const
129 130
{
    vector<string> machine_id_strings;
131
    for (auto it = intervals_begin(); it != intervals_end(); ++it)
132 133 134 135
        if (it->lower() == it->upper())
            machine_id_strings.push_back(to_string(it->lower()));
        else
            machine_id_strings.push_back(to_string(it->lower()) + joiner + to_string(it->upper()));
136 137 138

    return boost::algorithm::join(machine_id_strings, sep);
}
139

140
string MachineRange::to_string_elements(const string &sep) const
141 142 143 144 145 146 147
{
    vector<string> machine_id_strings;
    for (auto it = elements_begin(); it != elements_end(); ++it)
        machine_id_strings.push_back(to_string(*it));

    return boost::algorithm::join(machine_id_strings, sep);
}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

MachineRange MachineRange::from_string_hyphen(const string &str, const string &sep, const string &joiner, const string & error_prefix)
{
    MachineRange res;

    // Let us do a split by sep to get all the parts
    vector<string> parts;
    boost::split(parts, str, boost::is_any_of(sep), boost::token_compress_on);

    for (const string & part : parts)
    {
        // Since each machineIDk can either be a single machine or a closed interval, let's try to split on joiner
        vector<string> interval_parts;
        boost::split(interval_parts, part, boost::is_any_of(joiner), boost::token_compress_on);
        xbt_assert(interval_parts.size() >= 1 && interval_parts.size() <= 2,
                   "%s: the MIDk '%s' should either be a single machine ID"
                   " (syntax: MID to represent the machine ID MID) or a closed interval (syntax: MIDa-MIDb to represent"
                   " the machine interval [MIDA,MIDb])", error_prefix.c_str(), part.c_str());

        if (interval_parts.size() == 1)
        {
            int machine_id = std::stoi(interval_parts[0]);
            res.insert(machine_id);
        }
        else
        {
            int machineIDa = std::stoi(interval_parts[0]);
            int machineIDb = std::stoi(interval_parts[1]);

            xbt_assert(machineIDa <= machineIDb, "%s: the MIDk '%s' is composed of two"
                      " parts (1:%d and 2:%d) but the first value must be lesser than or equal to the second one",
                      error_prefix.c_str(), part.c_str(), machineIDa, machineIDb);

181
            res.insert(MachineRange::ClosedInterval(machineIDa, machineIDb));
182 183 184 185 186
        }
    }

    return res;
}
187 188 189 190 191 192 193

MachineRange &MachineRange::operator=(const MachineRange &other)
{
    set = other.set;
    return *this;
}

194
MachineRange &MachineRange::operator=(const MachineRange::ClosedInterval &interval)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
{
    set.clear();
    xbt_assert(set.size() == 0);
    set.insert(interval);
    return *this;
}

bool MachineRange::operator==(const MachineRange &other)
{
    return set == other.set;
}

MachineRange & MachineRange::operator&=(const MachineRange &other)
{
    set &= other.set;
    return *this;
}

MachineRange &MachineRange::operator-=(const MachineRange &other)
{
    set -= other.set;
    return *this;
}