<commit_before>#include "Schema.hpp"
#include <sstream>

static std::string translateType(const DataType& type) {
  std::ostringstream stream;
  if(type.typeTag == DataTypeTag::Integer) {
    stream << "Integer";
  } else if(type.typeTag == DataTypeTag::Numeric) {
    stream << "Numeric<" << (int) type.attributes.numeric.integerPlaces
           << "," << (int) type.attributes.numeric.decimalPlaces << ">";
  } else if(type.typeTag == DataTypeTag::Char) {
    stream << "Char<" << (int) type.attributes.charLen << ">";
  } else if(type.typeTag == DataTypeTag::VarChar) {
    stream << "Varchar<" << (int) type.attributes.charLen << ">";
  } else if(type.typeTag == DataTypeTag::Date) {
    stream << "Date";
  } else if(type.typeTag == DataTypeTag::Timestamp) {
    stream << "Timestamp";
  }
  return stream.str();
}

void Schema::generateCppCode(std::ostream& out) {
  out <<
    "#ifndef H_Schema\n"
    "#define H_Schema\n"
    "#include <vector>\n"
    "#include <map>\n"
    "#include <unordered_map>\n"
    "#include <schema/Types.hpp>\n";
  for(auto& table : tables) {
    out <<
      "//--------------------------------------------------\n"
      "struct " << table.name << "_t {\n";
    // actual columns
    for(auto& column : table.columns) {
      out << "  std::vector<" << translateType(column.type) << "> col_" << column.name << ";\n";
    }
    // primary key index
    bool first;
    if(!table.primaryKey.empty()) {
      out << "  std::unordered_map<std::tuple<";
      first = true;
      for(auto& pkColumn : table.primaryKey) {
        auto& column = table.columns[pkColumn];
        if(!first) out << ",";
        out << translateType(column.type);
        first = false;
      }
      out << ">, size_t> primary_key_idx;\n";
    }
    //insert
    out << "  void insert_tuple(";
    first = true;
    for(auto& column : table.columns) {
      if(!first) out << ",";
      out << translateType(column.type) << " " << column.name;
      first = false;
    }
    out << ") {\n";
    for(auto& column : table.columns) {
      out << "    this->col_" << column.name << ".push_back(" << column.name << ");\n";
    }
    out << "    this->primary_key_idx.insert(std::make_pair(std::make_tuple(";
    first = true;
    for(auto& pkColumn : table.primaryKey) {
      auto& column = table.columns[pkColumn];
      if(!first) out << ",";
      out << column.name;
      first = false;
    }
    out << "), this->col_" << table.columns[0].name << ".size()))\n";
    out << "  }\n";
    //delete
    out << "  void delete_tuple(size_t tid) {\n"
           "    this->primary_key_idx.erase(std::make_tuple(";
    first = true;
    for(auto& pkColumn : table.primaryKey) {
      auto& column = table.columns[pkColumn];
      if(!first) out << ",";
      out << "this->col_" << column.name << "[tid]";
      first = false;
    }
    out << "));\n";
    for(auto& column : table.columns) {
      out << "    this->col_" << column.name << "[tid] = this->col_" << column.name << ".back();\n";
      out << "    this->col_" << column.name << ".pop_back();\n";
    }
    out << "  }\n";
    //TODO: parse
    out << "}\n";
  }
}<commit_msg>schema generation: update primary key idx on delete<commit_after>#include "Schema.hpp"
#include <sstream>

static std::string translateType(const DataType& type) {
  std::ostringstream stream;
  if(type.typeTag == DataTypeTag::Integer) {
    stream << "Integer";
  } else if(type.typeTag == DataTypeTag::Numeric) {
    stream << "Numeric<" << (int) type.attributes.numeric.integerPlaces
           << "," << (int) type.attributes.numeric.decimalPlaces << ">";
  } else if(type.typeTag == DataTypeTag::Char) {
    stream << "Char<" << (int) type.attributes.charLen << ">";
  } else if(type.typeTag == DataTypeTag::VarChar) {
    stream << "Varchar<" << (int) type.attributes.charLen << ">";
  } else if(type.typeTag == DataTypeTag::Date) {
    stream << "Date";
  } else if(type.typeTag == DataTypeTag::Timestamp) {
    stream << "Timestamp";
  }
  return stream.str();
}

void Schema::generateCppCode(std::ostream& out) {
  out <<
    "#ifndef H_Schema\n"
    "#define H_Schema\n"
    "#include <vector>\n"
    "#include <map>\n"
    "#include <unordered_map>\n"
    "#include <schema/Types.hpp>\n";
  for(auto& table : tables) {
    out <<
      "//--------------------------------------------------\n"
      "struct " << table.name << "_t {\n";
    // actual columns
    for(auto& column : table.columns) {
      out << "  std::vector<" << translateType(column.type) << "> col_" << column.name << ";\n";
    }
    // primary key index
    bool first;
    if(!table.primaryKey.empty()) {
      out << "  std::unordered_map<std::tuple<";
      first = true;
      for(auto& pkColumn : table.primaryKey) {
        auto& column = table.columns[pkColumn];
        if(!first) out << ",";
        out << translateType(column.type);
        first = false;
      }
      out << ">, size_t> primary_key_idx;\n";
    }
    //insert
    out << "  void insert_tuple(";
    first = true;
    for(auto& column : table.columns) {
      if(!first) out << ",";
      out << translateType(column.type) << " " << column.name;
      first = false;
    }
    out << ") {\n";
    for(auto& column : table.columns) {
      out << "    this->col_" << column.name << ".push_back(" << column.name << ");\n";
    }
    out << "    this->primary_key_idx.insert(std::make_pair(std::make_tuple(";
    first = true;
    for(auto& pkColumn : table.primaryKey) {
      auto& column = table.columns[pkColumn];
      if(!first) out << ",";
      out << column.name;
      first = false;
    }
    out << "), this->col_" << table.columns[0].name << ".size()))\n";
    out << "  }\n";
    //delete
    out << "  void delete_tuple(size_t tid) {\n"
           "    this->primary_key_idx.erase(std::make_tuple(";
    first = true;
    for(auto& pkColumn : table.primaryKey) {
      auto& column = table.columns[pkColumn];
      if(!first) out << ",";
      out << "this->col_" << column.name << "[tid]";
      first = false;
    }
    out << "));\n";
    for(auto& column : table.columns) {
      out << "    this->col_" << column.name << "[tid] = this->col_" << column.name << ".back();\n";
      out << "    this->col_" << column.name << ".pop_back();\n";
    }
    out << "    this->primary_key_idx[std::make_tuple(";
    first = true;
    for(auto& pkColumn : table.primaryKey) {
      auto& column = table.columns[pkColumn];
      if(!first) out << ",";
      out << "this->col_" << column.name << ".back()";
      first = false;
    }
    out << ")] = tid;\n";
    out << "  }\n";
    //TODO: parse
    out << "}\n";
  }
}
{'fraction_non_alphanumeric': 0.21560946210637336, 'fraction_numerical': 0.00030134096730450503, 'mean_word_length': 2.464509394572025, 'pattern_counts': {'":': 0, '<': 281, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4180593', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2233, 'n_words': 437}