<commit_before>#include "bilgamesh_internal.hh"
#include <tuple>
#include <utility>
#include <vector>
#include <cstdlib>

template <class T>
_bgm_board<T>::_bgm_board () : black_move (true)
{
  hboard[0] = 0x0000000000555555ULL;
  hboard[1] = 0x0000000000555555ULL;
}

template <class T>
bool
_bgm_board<T>::is_black_move () const
{
  return black_move;
}

template <class T>
bool
_bgm_board<T>::swap_turn ()
{
  black_move = !black_move;
  return black_move;
}

template <class T>
void
_bgm_board<T>::count_black (int& men, int& kings) const
{
  men = __builtin_popcountll(men_mask & hboard[0]);
  kings = __builtin_popcountll(kings_mask & hboard[0]);
}

template <class T>
void
_bgm_board<T>::count_white (int& men, int& kings) const
{
  men = __builtin_popcountll(men_mask & hboard[1]);
  kings = __builtin_popcountll(kings_mask & hboard[1]);
}

template <class T>
void
_bgm_board<T>::set (const std::vector<int>& men_black, const std::vector<int>& kings_black, const std::vector<int>& men_white, const std::vector<int>& kings_white, bool bm)
{
  hboard[0] = 0x0ULL; hboard[1] = 0x0ULL;

  for (auto i : men_black) {
    hboard[0] |= (0x1ULL << 2*(i - 1));
  }

  for (auto i : kings_black) {
    hboard[0] |= (0x2ULL << 2*(i - 1));
  }

  for (auto i : men_white) {
    hboard[1] |= (0x1ULL << 2*(i - 1));
  }

  for (auto i : kings_white) {
    hboard[1] |= (0x2ULL << 2*(i - 1));
  }

  hboard[1] = _bgm_get_reverse_bitpairs (hboard[1]);
  black_move = bm;
}

template <class T>
void
_bgm_board<T>::set_raw (uint64_t& b, uint64_t& w)
{
  hboard[0] = b; hboard[1] = w;
}

template <class T>
void
_bgm_board<T>::apply (const _bgm_action<T>& in)
{
  in.apply (hboard);

  black_move = !black_move;
}

template <class T>
void
_bgm_board<T>::get_actions (std::vector<_bgm_action<T>>& actions)
{
  int num_men, num_kings;
  uint64_t chboard, ohboard;
  std::vector<T> men_pos, kings_pos;

  actions.clear();

  if (black_move) {
    chboard = hboard[0];
    ohboard = hboard[1];
    count_black (num_men, num_kings);
  } else {
    chboard = hboard[1];
    ohboard = hboard[0];
    count_white (num_men, num_kings);
  }

  _bgm_get_occupied_positions (chboard & men_mask, men_pos, num_men);
  _bgm_get_occupied_positions (chboard & kings_mask, kings_pos, num_kings);

  std::vector<_bgm_action<T>> captures;
  std::vector<_bgm_action<T>> chains;
  uint64_t tmp_board[2];

  for (auto pos : men_pos) {
    get_step_man_capture (chboard, ohboard, pos, captures);

    for (auto cap : captures) {
      if (cap.is_man_to_king()) {
	actions.push_back (cap);
      } else {
	chains.push_back (cap);
      }
    }

    while (!chains.empty()) {
      auto chain = chains.back ();
      chains.pop_back ();

      tmp_board[0] = chboard;
      tmp_board[1] = ohboard;
      chain.apply (tmp_board);

      get_step_man_capture (tmp_board[0], tmp_board[1], chain.last (), captures);

      if (captures.empty ()) {
	actions.push_back (chain);
      } else {
	for (auto cap : captures) {
	  if (cap.is_man_to_king()) {
	    const bool promotion_ends_turn = false;
	    if (!promotion_ends_turn) {
	      chains.push_back (chain.clone().join(cap));
	    } else {
	      actions.push_back (chain.clone().join(cap));
	    }
	  } else {
	    chains.push_back (chain.clone().join(cap));
	  }
	}
      }
    }
  }

  for (auto pos : kings_pos) {
    get_step_king_capture (chboard, ohboard, pos, captures);

    for (auto cap : captures) {
      chains.push_back (cap);
    }

    while (!chains.empty()) {
      auto chain = chains.back ();
      chains.pop_back ();

      tmp_board[0] = chboard;
      tmp_board[1] = ohboard;
      chain.apply (tmp_board);

      get_step_king_capture (tmp_board[0], tmp_board[1], chain.last(), captures);

      if (captures.empty ()) {<commit_msg>Account for white multiple capture. Promotion ends turn in second location.<commit_after>#include "bilgamesh_internal.hh"
#include <tuple>
#include <utility>
#include <vector>
#include <cstdlib>

template <class T>
_bgm_board<T>::_bgm_board () : black_move (true)
{
  hboard[0] = 0x0000000000555555ULL;
  hboard[1] = 0x0000000000555555ULL;
}

template <class T>
bool
_bgm_board<T>::is_black_move () const
{
  return black_move;
}

template <class T>
bool
_bgm_board<T>::swap_turn ()
{
  black_move = !black_move;
  return black_move;
}

template <class T>
void
_bgm_board<T>::count_black (int& men, int& kings) const
{
  men = __builtin_popcountll(men_mask & hboard[0]);
  kings = __builtin_popcountll(kings_mask & hboard[0]);
}

template <class T>
void
_bgm_board<T>::count_white (int& men, int& kings) const
{
  men = __builtin_popcountll(men_mask & hboard[1]);
  kings = __builtin_popcountll(kings_mask & hboard[1]);
}

template <class T>
void
_bgm_board<T>::set (const std::vector<int>& men_black, const std::vector<int>& kings_black, const std::vector<int>& men_white, const std::vector<int>& kings_white, bool bm)
{
  hboard[0] = 0x0ULL; hboard[1] = 0x0ULL;

  for (auto i : men_black) {
    hboard[0] |= (0x1ULL << 2*(i - 1));
  }

  for (auto i : kings_black) {
    hboard[0] |= (0x2ULL << 2*(i - 1));
  }

  for (auto i : men_white) {
    hboard[1] |= (0x1ULL << 2*(i - 1));
  }

  for (auto i : kings_white) {
    hboard[1] |= (0x2ULL << 2*(i - 1));
  }

  hboard[1] = _bgm_get_reverse_bitpairs (hboard[1]);
  black_move = bm;
}

template <class T>
void
_bgm_board<T>::set_raw (uint64_t& b, uint64_t& w)
{
  hboard[0] = b; hboard[1] = w;
}

template <class T>
void
_bgm_board<T>::apply (const _bgm_action<T>& in)
{
  in.apply (hboard);

  black_move = !black_move;
}

template <class T>
void
_bgm_board<T>::get_actions (std::vector<_bgm_action<T>>& actions)
{
  int num_men, num_kings;
  uint64_t chboard, ohboard;
  std::vector<T> men_pos, kings_pos;

  actions.clear();

  if (black_move) {
    chboard = hboard[0];
    ohboard = hboard[1];
    count_black (num_men, num_kings);
  } else {
    chboard = hboard[1];
    ohboard = hboard[0];
    count_white (num_men, num_kings);
  }

  _bgm_get_occupied_positions (chboard & men_mask, men_pos, num_men);
  _bgm_get_occupied_positions (chboard & kings_mask, kings_pos, num_kings);

  std::vector<_bgm_action<T>> captures;
  std::vector<_bgm_action<T>> chains;
  uint64_t tmp_board[2];

  const bool promotion_ends_turn = false;

  for (auto pos : men_pos) {
    get_step_man_capture (chboard, ohboard, pos, captures);

    for (auto cap : captures) {
      if (cap.is_man_to_king()) {
	if (!promotion_ends_turn) {
	  chains.push_back (cap);
	} else {
	  actions.push_back (cap);
	}
      } else {
	chains.push_back (cap);
      }
    }

    while (!chains.empty()) {
      auto chain = chains.back ();
      chains.pop_back ();

      tmp_board[0] = chboard;
      tmp_board[1] = ohboard;
      chain.apply (tmp_board);

      if (black_move) {
	get_step_man_capture (tmp_board[0], tmp_board[1], chain.last (), captures);
      } else {
	get_step_man_capture (tmp_board[0], tmp_board[1], 33 - chain.last (), captures);
      }

      if (captures.empty ()) {
	actions.push_back (chain);
      } else {
	for (auto cap : captures) {
	  if (cap.is_man_to_king()) {
	    if (!promotion_ends_turn) {
	      chains.push_back (chain.clone().join(cap));
	    } else {
	      actions.push_back (chain.clone().join(cap));
	    }
	  } else {
	    chains.push_back (chain.clone().join(cap));
	  }
	}
      }
    }
  }

  for (auto pos : kings_pos) {
    get_step_king_capture (chboard, ohboard, pos, captures);

    for (auto cap : captures) {
      chains.push_back (cap);
    }

    while (!chains.empty()) {
      auto chain = chains.back ();
      chains.pop_back ();

      tmp_board[0] = chboard;
      tmp_board[1] = ohboard;
      chain.apply (tmp_board);

      if (black_move) {
	get_step_king_capture (tmp_board[0], tmp_board[1], chain.last (), captures);
      } else {
	get_step_king_capture (tmp_board[0], tmp_board[1], 33 - chain.last (), captures);
      }

      if (captures.empty ()) {
{'fraction_non_alphanumeric': 0.1530985737725609, 'fraction_numerical': 0.023980815347721823, 'mean_word_length': 3.628504672897196, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '2922385', 'n_tokens_mistral': 3286, 'n_tokens_neox': 3092, 'n_words': 633}