<commit_before>#include "BRDBoard.h"

#include "FileFormats/BRDFile.h"

#include <cerrno>
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using namespace std::placeholders;

const string BRDBoard::kNetUnconnectedPrefix = "UNCONNECTED";
const string BRDBoard::kComponentDummyName   = "...";

BRDBoard::BRDBoard(const BRDFile *const boardFile)
    : m_file(boardFile) {
	// TODO: strip / trim all strings, especially those used as keys
	// TODO: just loop through original arrays?
	vector<BRDPart> m_parts(m_file->num_parts);
	vector<BRDPin> m_pins(m_file->num_pins);
	vector<BRDNail> m_nails(m_file->num_nails);
	vector<BRDPoint> m_points(m_file->num_format);

	m_parts  = m_file->parts;
	m_pins   = m_file->pins;
	m_nails  = m_file->nails;
	m_points = m_file->format;

	// Set outline
	{
		for (auto &brdPoint : m_points) {
			auto point = make_shared<Point>(brdPoint.x, brdPoint.y);
			outline_.push_back(point);
		}
	}

	// Populate map of unique nets
	SharedStringMap<Net> net_map;
	{
		// adding special net 'UNCONNECTED'
		auto net_nc           = make_shared<Net>();
		net_nc->name          = kNetUnconnectedPrefix;
		net_nc->is_ground     = false;
		net_map[net_nc->name] = net_nc;

		// handle all the others
		for (auto &brd_nail : m_nails) {
			auto net = make_shared<Net>();

			// copy NET name and number (probe)
			net->name = string(brd_nail.net);

			// avoid having multiple UNCONNECTED<XXX> references
			if (is_prefix(kNetUnconnectedPrefix, net->name)) continue;

			// check whether the pin represents ground
			net->is_ground = (net->name == "GND");
			net->number    = brd_nail.probe;

			if (brd_nail.side == 1) {
				net->board_side = kBoardSideTop;
			} else {
				net->board_side = kBoardSideBottom;
			}

			// so we can find nets later by name (making unique by name)
			net_map[net->name] = net;
		}
	}

	// Populate parts
	{
		for (auto &brd_part : m_parts) {
			auto comp = make_shared<Component>();

			comp->name    = string(brd_part.name);
			comp->mfgcode = std::move(brd_part.mfgcode);

			comp->p1 = {brd_part.p1.x, brd_part.p1.y};
			comp->p2 = {brd_part.p2.x, brd_part.p2.y};

			// is it some dummy component to indicate test pads?
			if (is_prefix(kComponentDummyName, comp->name)) comp->component_type = Component::kComponentTypeDummy;

			// check what side the board is on (sorcery?)
			if (brd_part.type < 8 && brd_part.type >= 4) {
				comp->board_side = kBoardSideTop;
			} else if (brd_part.type >= 8) {
				comp->board_side = kBoardSideBottom;
			} else {
				comp->board_side = kBoardSideBoth; // ???
			}

			comp->mount_type = (brd_part.type & 0xc) ? Component::kMountTypeSMD : Component::kMountTypeDIP;

			components_.push_back(comp);
		}
	}

	// Populate pins
	{
		// generate dummy component as reference
		auto comp_dummy            = make_shared<Component>();
		comp_dummy->name           = kComponentDummyName;
		comp_dummy->component_type = Component::kComponentTypeDummy;

		// NOTE: originally the pin diameter depended on part.name[0] == 'U' ?
		unsigned int pin_idx  = 0;
		unsigned int part_idx = 1;
		auto pins             = m_pins;
		auto parts            = m_parts;

		for (size_t i = 0; i < pins.size(); i++) {
			// (originally from BoardView::DrawPins)
			const BRDPin &brd_pin = pins[i];
			std::shared_ptr<Component> comp       = components_[brd_pin.part - 1];

			if (!comp) continue;<commit_msg>Attempt to fix situation where parts appear on both sides<commit_after>#include "BRDBoard.h"

#include "FileFormats/BRDFile.h"

#include <cerrno>
#include <fstream>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using namespace std::placeholders;

const string BRDBoard::kNetUnconnectedPrefix = "UNCONNECTED";
const string BRDBoard::kComponentDummyName   = "...";

BRDBoard::BRDBoard(const BRDFile *const boardFile)
    : m_file(boardFile) {
	// TODO: strip / trim all strings, especially those used as keys
	// TODO: just loop through original arrays?
	vector<BRDPart> m_parts(m_file->num_parts);
	vector<BRDPin> m_pins(m_file->num_pins);
	vector<BRDNail> m_nails(m_file->num_nails);
	vector<BRDPoint> m_points(m_file->num_format);

	m_parts  = m_file->parts;
	m_pins   = m_file->pins;
	m_nails  = m_file->nails;
	m_points = m_file->format;

	// Set outline
	{
		for (auto &brdPoint : m_points) {
			auto point = make_shared<Point>(brdPoint.x, brdPoint.y);
			outline_.push_back(point);
		}
	}

	// Populate map of unique nets
	SharedStringMap<Net> net_map;
	{
		// adding special net 'UNCONNECTED'
		auto net_nc           = make_shared<Net>();
		net_nc->name          = kNetUnconnectedPrefix;
		net_nc->is_ground     = false;
		net_map[net_nc->name] = net_nc;

		// handle all the others
		for (auto &brd_nail : m_nails) {
			auto net = make_shared<Net>();

			// copy NET name and number (probe)
			net->name = string(brd_nail.net);

			// avoid having multiple UNCONNECTED<XXX> references
			if (is_prefix(kNetUnconnectedPrefix, net->name)) continue;

			// check whether the pin represents ground
			net->is_ground = (net->name == "GND");
			net->number    = brd_nail.probe;

			if (brd_nail.side == 1) {
				net->board_side = kBoardSideTop;
			} else {
				net->board_side = kBoardSideBottom;
			}

			// so we can find nets later by name (making unique by name)
			net_map[net->name] = net;
		}
	}

	// Populate parts
	{
		for (auto &brd_part : m_parts) {
			auto comp = make_shared<Component>();

			comp->name    = string(brd_part.name);
			comp->mfgcode = std::move(brd_part.mfgcode);

			comp->p1 = {brd_part.p1.x, brd_part.p1.y};
			comp->p2 = {brd_part.p2.x, brd_part.p2.y};

			// is it some dummy component to indicate test pads?
			if (is_prefix(kComponentDummyName, comp->name)) comp->component_type = Component::kComponentTypeDummy;

			// check what side the board is on (sorcery?)
			if (
					(brd_part.type == 1)
					||(brd_part.type >= 4 && brd_part.type < 8)) {
				comp->board_side = kBoardSideTop;
			} else if (
					(brd_part.type == 2)
					||(brd_part.type >= 8)
					) {
				comp->board_side = kBoardSideBottom;
			} else {
				comp->board_side = kBoardSideBoth;
			}
					 
			comp->mount_type = (brd_part.type & 0xc) ? Component::kMountTypeSMD : Component::kMountTypeDIP;

			components_.push_back(comp);
		}
	}

	// Populate pins
	{
		// generate dummy component as reference
		auto comp_dummy            = make_shared<Component>();
		comp_dummy->name           = kComponentDummyName;
		comp_dummy->component_type = Component::kComponentTypeDummy;

		// NOTE: originally the pin diameter depended on part.name[0] == 'U' ?
		unsigned int pin_idx  = 0;
		unsigned int part_idx = 1;
		auto pins             = m_pins;
		auto parts            = m_parts;

		for (size_t i = 0; i < pins.size(); i++) {
			// (originally from BoardView::DrawPins)
			const BRDPin &brd_pin = pins[i];
			std::shared_ptr<Component> comp       = components_[brd_pin.part - 1];

			if (!comp) continue;
{'fraction_non_alphanumeric': 0.13197897428612018, 'fraction_numerical': 0.004830231566983947, 'mean_word_length': 3.3781094527363185, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 107, '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': '4488139', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2516, 'n_words': 574}