<commit_before>/* Copyright 2016 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "split_algo.h"

#include <algorithm>
#include <glog/logging.h>
#include <numeric>
#include <unordered_set>

#include "src/base/base.h"
#include "src/data_store/column.h"
#include "src/proto/config.pb.h"
#include "src/proto/tree.pb.h"
#include "src/utils/vector_slice.h"

using namespace std::placeholders;

namespace gbdt {

const double kFloatTolerance = 1e-6;

pair<VectorSlice<uint>, VectorSlice<uint>>
Partition(const BucketizedFloatColumn* feature, const Split& split, VectorSlice<uint> samples) {
  CHECK(split.has_float_split()) << "Split and feature type mismatch for " << feature->name();
  uint left_size = 0;
  bool missing_to_right = split.float_split().missing_to_right_child();
  float threshold = split.float_split().threshold();
  const auto& col = feature->col();
  for (uint i = 0; i < samples.size(); ++i) {
    if ((col.missing(samples[i]) && !missing_to_right) ||
        (!col.missing(samples[i]) && feature->get_row_max(samples[i]) < threshold)) {
      swap(samples[i], samples[left_size++]);
    }
  }
  return make_pair(VectorSlice<uint>(samples, 0, left_size),
                   VectorSlice<uint>(samples, left_size, samples.size() - left_size));
}

pair<VectorSlice<uint>, VectorSlice<uint>>
Partition(const StringColumn* feature, const Split& split, VectorSlice<uint> samples) {
  CHECK(split.has_cat_split()) << "Split and feature type mismatch for " << feature->name();
  // TODO(criver): converting repeated fields everytime seems wasteful. Rewrite this if protobuf
  // has better support for set. However, this is arguably not so bad, since this is done at
  // per-node level not the per-instance level.
  unordered_set<uint> categories;
  if (split.cat_split().internal_categorical_index_size() > 0) {
    categories.insert(split.cat_split().internal_categorical_index().begin(),
                      split.cat_split().internal_categorical_index().end());
  } else {
    for (const auto& cat: split.cat_split().category()) {
      uint cat_index;
      if (feature->get_cat_index(cat, &cat_index)) {
        categories.insert(cat_index);
      }
    }
  }

  const auto& col = feature->col();
  uint left_size = 0;
  for (uint i = 0; i < samples.size(); ++i) {
    if (categories.find(col[samples[i]]) != categories.end())
      swap(samples[i], samples[left_size++]);
  }
  return make_pair(VectorSlice<uint>(samples, 0, left_size),
                   VectorSlice<uint>(samples, left_size, samples.size() - left_size));
}

pair<VectorSlice<uint>, VectorSlice<uint>>
Partition(const Column* feature, const Split& split, VectorSlice<uint> samples) {
  if (feature->type() == Column::kStringColumn) {
    return Partition(static_cast<const StringColumn*>(feature), split, samples);
  } else if (feature->type() == Column::kBucketizedFloatColumn) {
    return Partition(static_cast<const BucketizedFloatColumn*>(feature), split, samples);
  } else {
    return make_pair(VectorSlice<uint>(samples, 0, 0), VectorSlice<uint>(samples));
  }<commit_msg>Fixed namespace conflict of swap<commit_after>/* Copyright 2016 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "split_algo.h"

#include <algorithm>
#include <glog/logging.h>
#include <numeric>
#include <unordered_set>

#include "src/base/base.h"
#include "src/data_store/column.h"
#include "src/proto/config.pb.h"
#include "src/proto/tree.pb.h"
#include "src/utils/vector_slice.h"

using namespace std::placeholders;

namespace gbdt {

const double kFloatTolerance = 1e-6;

pair<VectorSlice<uint>, VectorSlice<uint>>
Partition(const BucketizedFloatColumn* feature, const Split& split, VectorSlice<uint> samples) {
  CHECK(split.has_float_split()) << "Split and feature type mismatch for " << feature->name();
  uint left_size = 0;
  bool missing_to_right = split.float_split().missing_to_right_child();
  float threshold = split.float_split().threshold();
  const auto& col = feature->col();
  for (uint i = 0; i < samples.size(); ++i) {
    if ((col.missing(samples[i]) && !missing_to_right) ||
        (!col.missing(samples[i]) && feature->get_row_max(samples[i]) < threshold)) {
      std::swap(samples[i], samples[left_size++]);
    }
  }
  return make_pair(VectorSlice<uint>(samples, 0, left_size),
                   VectorSlice<uint>(samples, left_size, samples.size() - left_size));
}

pair<VectorSlice<uint>, VectorSlice<uint>>
Partition(const StringColumn* feature, const Split& split, VectorSlice<uint> samples) {
  CHECK(split.has_cat_split()) << "Split and feature type mismatch for " << feature->name();
  // TODO(criver): converting repeated fields everytime seems wasteful. Rewrite this if protobuf
  // has better support for set. However, this is arguably not so bad, since this is done at
  // per-node level not the per-instance level.
  unordered_set<uint> categories;
  if (split.cat_split().internal_categorical_index_size() > 0) {
    categories.insert(split.cat_split().internal_categorical_index().begin(),
                      split.cat_split().internal_categorical_index().end());
  } else {
    for (const auto& cat: split.cat_split().category()) {
      uint cat_index;
      if (feature->get_cat_index(cat, &cat_index)) {
        categories.insert(cat_index);
      }
    }
  }

  const auto& col = feature->col();
  uint left_size = 0;
  for (uint i = 0; i < samples.size(); ++i) {
    if (categories.find(col[samples[i]]) != categories.end())
      std::swap(samples[i], samples[left_size++]);
  }
  return make_pair(VectorSlice<uint>(samples, 0, left_size),
                   VectorSlice<uint>(samples, left_size, samples.size() - left_size));
}

pair<VectorSlice<uint>, VectorSlice<uint>>
Partition(const Column* feature, const Split& split, VectorSlice<uint> samples) {
  if (feature->type() == Column::kStringColumn) {
    return Partition(static_cast<const StringColumn*>(feature), split, samples);
  } else if (feature->type() == Column::kBucketizedFloatColumn) {
    return Partition(static_cast<const BucketizedFloatColumn*>(feature), split, samples);
  } else {
    return make_pair(VectorSlice<uint>(samples, 0, 0), VectorSlice<uint>(samples));
  }
{'fraction_non_alphanumeric': 0.13790244576481966, 'fraction_numerical': 0.005250794528119386, 'mean_word_length': 4.4298574643660915, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4411596', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2173, 'n_words': 598}