+#include "merge_count.h"
+
+#include <unordered_set>
+
+using namespace std;
+
+Count merge_count(const Count& c0, const Count& c1)
+{
+ Count c;
+
+ c.set_white(c0.white() + c1.white());
+ c.set_draw(c0.draw() + c1.draw());
+ c.set_black(c0.black() + c1.black());
+ c.set_sum_white_elo(c0.sum_white_elo() + c1.sum_white_elo());
+ c.set_sum_black_elo(c0.sum_black_elo() + c1.sum_black_elo());
+ c.set_num_elo(c0.num_elo() + c1.num_elo());
+ if (c0.first_timestamp() <= c1.first_timestamp()) {
+ c.set_opening_num(c0.opening_num());
+ if (c0.has_first_timestamp()) {
+ c.set_first_timestamp(c0.first_timestamp());
+ }
+ c.set_pgn_file_num(c0.pgn_file_num());
+ c.set_pgn_start_position(c0.pgn_start_position());
+ } else {
+ c.set_opening_num(c1.opening_num());
+ if (c1.has_first_timestamp()) {
+ c.set_first_timestamp(c1.first_timestamp());
+ }
+ c.set_pgn_file_num(c1.pgn_file_num());
+ c.set_pgn_start_position(c1.pgn_start_position());
+ }
+
+ // Merge the moves, with deduplication.
+ unordered_set<string> moves;
+ for (int i = 0; i < c0.move_size(); ++i) {
+ moves.insert(c0.move(i));
+ c.add_move(c0.move(i));
+ }
+ for (int i = 0; i < c1.move_size(); ++i) {
+ if (!moves.count(c1.move(i))) {
+ c.add_move(c1.move(i));
+ }
+ }
+
+ return c;
+}