<commit_before>/* Copyright (C) 2017 INRA
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublipnse, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "itm.hpp"

#include <set>

#include <cassert>

namespace bx = baryonyx;

struct merged_constraint_hash
{
    inline size_t operator()(
      const std::vector<bx::function_element>& fct) const noexcept
    {
        std::size_t seed{ fct.size() };

        for (auto& elem : fct)
            seed ^=
              elem.variable_index + 0x9e3779b9 + (seed << 6) + (seed >> 2);

        return seed;
    }
};

//
// Fills the cache and uses it to search same function and merge equal, less
// and greater equal constraints. To be use in any order, the fill operation
// depends of the operator type passed in parameter.
//
template<typename cacheT, typename retT>
static void
fill_merged_constraints(std::shared_ptr<bx::context> ctx,
                        cacheT& cache,
                        bx::operator_type op,
                        const bx::problem& pb,
                        retT& ret)
{
    switch (op) {
    case bx::operator_type::equal:
        for (const auto& elem : pb.equal_constraints) {
            auto it = cache.find(elem.elements);
            if (it == cache.end()) {
                cache.emplace(elem.elements, ret.size());
                ret.emplace_back(
                  elem.elements,
                  bx::numeric_cast<int>(std::lround(elem.value)),
                  bx::numeric_cast<int>(std::lround(elem.value)),

                  elem.id);
            } else {
                if (ret[it->second].min <= elem.value and
                    elem.value <= ret[it->second].max) {
                    ret[it->second].min = elem.value;
                    ret[it->second].max = elem.value;
                } else {
                    ctx->error(
                      "Constraint %d: is inconsistent with previous %d.\n",
                      elem.id,
                      ret[it->second].id);
                }
            }
        }
        break;
    case bx::operator_type::less:
        for (const auto& elem : pb.less_constraints) {
            auto it = cache.find(elem.elements);
            if (it == cache.end()) {
                cache.emplace(elem.elements, ret.size());
                ret.emplace_back(
                  elem.elements,
                  std::numeric_limits<int>::min(),
                  bx::numeric_cast<int>(std::lround(elem.value)),
                  elem.id);
            } else {
                ret[it->second].max =
                  std::min(ret[it->second].max,
                           bx::numeric_cast<int>(std::lround(elem.value)));
            }
        }
        break;
    case bx::operator_type::greater:
        for (const auto& elem : pb.greater_constraints) {
            auto it = cache.find(elem.elements);
            if (it == cache.end()) {
                cache.emplace(elem.elements, ret.size());
                ret.emplace_back(elem.elements,
                                 elem.value,
                                 std::numeric_limits<int>::max(),
                                 elem.id);
            } else {
                ret[it->second].min =
                  std::max(ret[it->second].min,
                           bx::numeric_cast<int>(std::lround(elem.value)));
            }
        }
        break;
    default:
        break;
    }
}

static inline bx::operator_type
operator_from_string(const std::string& op) noexcept
{
    if (op == "equal")
        return bx::operator_type::equal;
    if (op == "less")
        return bx::operator_type::less;
    if (op == "greater")
        return bx::operator_type::greater;

    return bx::operator_type::undefined;
}

//
// Restore original order: reorder merged constraints according to the
// merged_constraint::id (initially initialized in the lp format reading
// process).
//
static std::vector<bx::itm::merged_constraint>
make_unsorted_merged_constraints(std::shared_ptr<bx::context> ctx,
                                 const bx::problem& pb)<commit_msg>itm: fix compilation issue<commit_after>/* Copyright (C) 2017 INRA
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublipnse, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "itm.hpp"

#include <set>

#include <cassert>

namespace bx = baryonyx;

struct merged_constraint_hash
{
    inline size_t operator()(
      const std::vector<bx::function_element>& fct) const noexcept
    {
        std::size_t seed{ fct.size() };

        for (auto& elem : fct)
            seed ^=
              elem.variable_index + 0x9e3779b9 + (seed << 6) + (seed >> 2);

        return seed;
    }
};

//
// Fills the cache and uses it to search same function and merge equal, less
// and greater equal constraints. To be use in any order, the fill operation
// depends of the operator type passed in parameter.
//
template<typename cacheT, typename retT>
static void
fill_merged_constraints(std::shared_ptr<bx::context> ctx,
                        cacheT& cache,
                        bx::operator_type op,
                        const bx::problem& pb,
                        retT& ret)
{
    switch (op) {
    case bx::operator_type::equal:
        for (const auto& elem : pb.equal_constraints) {
            auto it = cache.find(elem.elements);
            if (it == cache.end()) {
                cache.emplace(elem.elements, ret.size());
                ret.emplace_back(elem.elements,
                                 bx::numeric_cast<int>(elem.value),
                                 bx::numeric_cast<int>(elem.value),

                                 elem.id);
            } else {
                if (ret[it->second].min <= elem.value and
                    elem.value <= ret[it->second].max) {
                    ret[it->second].min = elem.value;
                    ret[it->second].max = elem.value;
                } else {
                    ctx->error(
                      "Constraint %d: is inconsistent with previous %d.\n",
                      elem.id,
                      ret[it->second].id);
                }
            }
        }
        break;
    case bx::operator_type::less:
        for (const auto& elem : pb.less_constraints) {
            auto it = cache.find(elem.elements);
            if (it == cache.end()) {
                cache.emplace(elem.elements, ret.size());
                ret.emplace_back(elem.elements,
                                 std::numeric_limits<int>::min(),
                                 bx::numeric_cast<int>(elem.value),
                                 elem.id);
            } else {
                ret[it->second].max = std::min(
                  ret[it->second].max, bx::numeric_cast<int>(elem.value));
            }
        }
        break;
    case bx::operator_type::greater:
        for (const auto& elem : pb.greater_constraints) {
            auto it = cache.find(elem.elements);
            if (it == cache.end()) {
                cache.emplace(elem.elements, ret.size());
                ret.emplace_back(elem.elements,
                                 elem.value,
                                 std::numeric_limits<int>::max(),
                                 elem.id);
            } else {
                ret[it->second].min = std::max(
                  ret[it->second].min, bx::numeric_cast<int>(elem.value));
            }
        }
        break;
    default:
        break;
    }
}

static inline bx::operator_type
operator_from_string(const std::string& op) noexcept
{
    if (op == "equal")
        return bx::operator_type::equal;
    if (op == "less")
        return bx::operator_type::less;
    if (op == "greater")
        return bx::operator_type::greater;

    return bx::operator_type::undefined;
}

//
// Restore original order: reorder merged constraints according to the
// merged_constraint::id (initially initialized in the lp format reading
// process).
//
static std::vector<bx::itm::merged_constraint>
make_unsorted_merged_constraints(std::shared_ptr<bx::context> ctx,
                                 const bx::problem& pb)
{'fraction_non_alphanumeric': 0.10776718315454907, 'fraction_numerical': 0.002582439411998411, 'mean_word_length': 1.8686609686609688, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2797057', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2647, 'n_words': 800}