text:
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2021 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "StdAfx.h"
#include "PasswordGenerator.h"
#include "CharSetBasedGenerator.h"
#include "PatternBasedGenerator.h"

#include <algorithm>
#include <boost/scoped_array.hpp>

#include "../Util/Base64.h"
#include "../Util/PwUtil.h"
#include "../Util/StrUtil.h"
#include "../Util/MemUtil.h"

using boost::scoped_array;

PWG_ERROR PwgGenerateEx(std::vector<TCHAR>& vOutPassword,
	const PW_GEN_SETTINGS_EX* pSettings, CNewRandom* pRandomSource)
{
	if(pSettings == NULL) { ASSERT(FALSE); return PWGE_NULL_PTR; }

	EraseTCharVector(vOutPassword, true);

	CNewRandom* pAllocatedRandom = NULL;
	CNewRandom* pRandom = pRandomSource;
	if(pRandom == NULL)
	{
		pAllocatedRandom = new CNewRandom();
		pRandom = pAllocatedRandom;
	}

	std::vector<WCHAR> vOutBuffer;

	PWG_ERROR e = PWGE_UNKNOWN_GENERATOR;
	if(pSettings->btGeneratorType == PWGT_CHARSET)
		e = CsbgGenerate(vOutBuffer, pSettings, pRandom);
	else if(pSettings->btGeneratorType == PWGT_PATTERN)
		e = PbgGenerate(vOutBuffer, pSettings, pRandom);
	else { ASSERT(FALSE); }

	if((vOutBuffer.size() == 0) || (std::find(vOutBuffer.begin(),
		vOutBuffer.end(), L'\0') == vOutBuffer.end()))
		vOutBuffer.push_back(L'\0'); // Terminate string

	ASSERT(vOutBuffer.size() > 0);
#ifdef _UNICODE
	vOutPassword.resize(vOutBuffer.size());
	memcpy(&vOutPassword[0], &vOutBuffer[0], vOutPassword.size() * sizeof(WCHAR));
#else
	char* pszAnsi = _StringToAnsi(&vOutBuffer[0]);
	vOutPassword.resize(szlen(pszAnsi) + 1);
	memcpy(&vOutPassword[0], pszAnsi, vOutPassword.size() * sizeof(char));
	mem_erase(pszAnsi, vOutPassword.size() * sizeof(char));
	SAFE_DELETE_ARRAY(pszAnsi);
#endif

	EraseWCharVector(vOutBuffer, false);
	SAFE_DELETE(pAllocatedRandom);
	return e;
}

WCHAR PwgGenerateCharacter(const PwCharSet& pcs, CNewRandom* pRandom)
{
	if(pRandom == NULL) { ASSERT(FALSE); return L'\0'; }
	if(pcs.Size() == 0) return L'\0';

	const UINT64 i = pRandom->GetRandomUInt64(pcs.Size());
	return pcs.GetAt(static_cast<unsigned int>(i));
}

bool PwgPrepareCharSet(PwCharSet& pcs, const PW_GEN_SETTINGS_EX* pSettings)
{
	if(pSettings == NULL) { ASSERT(FALSE); return false; }

	const unsigned int cc = pcs.Size();
	for(unsigned int i = 0; i < cc; ++i)
	{
		const WCHAR ch = pcs.GetAt(i);
		if((ch == L'\0') || (ch == L'\t') || (ch == L'\r') || (ch == L'\n') ||
			((ch >= L'\xD800') && (ch <= L'\xDFFF'))) // Surrogate
			return false;
	}

	if(pSettings->bNoConfusing != FALSE) pcs.Remove(PDCS_CONFUSING);

	if(pSettings->strExcludeChars.size() > 0)
		pcs.Remove(pSettings->strExcludeChars.c_str());

	return true;
}

void PwgShufflePassword(std::vector<WCHAR>& vBuffer, CNewRandom* pRandom)
{
	if(pRandom == NULL) { ASSERT(FALSE); return; }

	size_t cc = vBuffer.size();
	for(size_t i = 0; i < vBuffer.size(); ++i)
	{
		if(vBuffer[i] == L'\0')
		{
			cc = i;
			break;
		}
	}
	if(cc <= 1) return; // Nothing to shuffle

	for(size_t i = cc - 1; i >= 1; --i)
	{
		const size_t j = static_cast<size_t>(pRandom->GetRandomUInt64(i + 1));

		const WCHAR t = vBuffer[i];
		vBuffer[i] = vBuffer[j];
		vBuffer[j] = t;
	}

	ASSERT(wcslen(&vBuffer[0]) == cc);
}

LPCTSTR PwgErrorToString(PWG_ERROR uError)
{
	if(uError == PWGE_SUCCESS) return _T("Success");
	if(uError == PWGE_NULL_PTR) return _T("Internal error");
	if(uError == PWGE_UNKNOWN_GENERATOR) return _T("Internal error");
	if(uError == PWGE_TOO_FEW_CHARACTERS)
		return _T("There are too few characters in the character set to build up a password matching the specified rules");
	if(uError == PWGE_INVALID_CHARSET) return _T("The character set is invalid");
	if(uError == PWGE_INVALID_PATTERN) return _T("The pattern is invalid");

	return _T("Unknown error");
}

/* std::basic_string<WCHAR> HexStrToWCharStr(LPCTSTR lpString)
{
	std::basic_string<WCHAR> str;

	ASSERT(lpString != NULL); if(lpString == NULL) return str;

	DWORD dwLength = _tcslen(lpString), i = 0;
	if((dwLength & 3) != 0) { ASSERT(FALSE); return str; }

	BYTE bt1, bt2;

	while(true)
	{
		TCHAR ch1 = lpString[i], ch2 = lpString[i + 1];
		TCHAR ch3 = lpString[i + 2], ch4 = lpString[i + 3];

		ConvertStrToHex(ch1, ch2, bt1);
		ConvertStrToHex(ch3, ch4, bt2);

		str += (WCHAR)((((WCHAR)bt1) << 8) | ((WCHAR)bt2));

		i += 4;
		if(lpString[i] == 0) break;
	}

	return str;
}

std::basic_string<TCHAR> WCharVecToHexStr(const std::vector<WCHAR>& vec)
{
	std::basic_string<TCHAR> strOut;
	TCHAR ch1, ch2;

	for(DWORD i = 0; i < vec.size(); ++i)
	{
		ConvertHexToStr((BYTE)(vec[i] >> 8), ch1, ch2);
		strOut += ch1;
		strOut += ch2;

		ConvertHexToStr((BYTE)(vec[i] & 0xFF), ch1, ch2);
		strOut += ch1;
		strOut += ch2;
	}

	return strOut;
} */

std::basic_string<TCHAR> PwgProfileToString(const PW_GEN_SETTINGS_EX* pSettings)
{
	std::basic_string<TCHAR> strEmpty;

	ASSERT(pSettings != NULL);
	if(pSettings == NULL) return strEmpty;

	std::vector<BYTE> s;

	s.push_back(PWGD_VERSION_BYTE);

	UTF8_BYTE *pbName = _StringToUTF8(pSettings->strName.c_str());
	UTF8_BYTE *pb = pbName;
	while(*pb != 0) { s.push_back(*pb); ++pb; }
	s.push_back(0);
	SAFE_DELETE_ARRAY(pbName);

	s.push_back(pSettings->btGeneratorType);
	s.push_back((pSettings->bCollectUserEntropy == TRUE) ? (BYTE)'U' : (BYTE)'N');
	s.push_back((BYTE)((pSettings->dwLength >> 24) & 0xFF));
	s.push_back((BYTE)((pSettings->dwLength >> 16) & 0xFF));
	s.push_back((BYTE)((pSettings->dwLength >> 8) & 0xFF));
	s.push_back((BYTE)(pSettings->dwLength & 0xFF));

	PwCharSet pcs(pSettings->strCharSet.c_str());
	USHORT usFlags = pcs.PackAndRemoveCharRanges();
	s.push_back((BYTE)((usFlags >> 8) & 0xFF));
	s.push_back((BYTE)(usFlags & 0xFF));

	std::basic_string<WCHAR> strRemChars = pcs.ToString();
	for(unsigned int uCS = 0; uCS < strRemChars.size(); ++uCS)
	{
		s.push_back((BYTE)(strRemChars[uCS] >> 8));
		s.push_back((BYTE)(strRemChars[uCS] & 0xFF));
	}
	s.push_back(0);
	s.push_back(0);

	for(unsigned int uPat = 0; uPat < pSettings->strPattern.size(); ++uPat)
	{
		s.push_back((BYTE)(pSettings->strPattern[uPat] >> 8));
		s.push_back((BYTE)(pSettings->strPattern[uPat] & 0xFF));
	}
	s.push_back(0);
	s.push_back(0);

	s.push_back((BYTE)((pSettings->bNoConfusing == TRUE) ? 'N' : 'A'));
	s.push_back((BYTE)((pSettings->bPatternPermute == TRUE) ? 'P' : 'N'));
	s.push_back((BYTE)((pSettings->bNoRepeat == TRUE) ? 'N' : 'R'));

	for(unsigned int uExc = 0; uExc < pSettings->strExcludeChars.size(); ++uExc)
	{
		s.push_back((BYTE)(pSettings->strExcludeChars[uExc] >> 8));
		s.push_back((BYTE)(pSettings->strExcludeChars[uExc] & 0xFF));
	}
	s.push_back(0);
	s.push_back(0);

	DWORD dwOutSize = static_cast<DWORD>(s.size() * 4 + 12);
	BYTE *pBase64 = new BYTE[dwOutSize];
	if(CBase64Codec::Encode(&s[0], (DWORD)s.size(), pBase64, &dwOutSize) == false)
	{
		ASSERT(FALSE);
		return strEmpty;
	}

#ifdef _UNICODE
	TCHAR *lpFinal = _StringToUnicode((char *)pBase64);
#else
	TCHAR *lpFinal = (TCHAR *)pBase64;
#endif

	std::basic_string<TCHAR> strFinal = lpFinal;

	SAFE_DELETE_ARRAY(pBase64);
#ifdef _UNICODE
	SAFE_DELETE_ARRAY(lpFinal);
#endif

	return strFinal;
}

void PwgStringToProfile(const std::basic_string<TCHAR>& strProfile,
	PW_GEN_SETTINGS_EX* s)
{
	ASSERT(s != NULL); if(s == NULL) return;

	PwgGetDefaultProfile(s);

#ifdef _UNICODE
	const char *lpEncoded = _StringToAnsi(strProfile.c_str());
	std::basic_string<char> strEncoded = lpEncoded;
	SAFE_DELETE_ARRAY(lpEncoded);
#else
	std::basic_string<char> strEncoded = strProfile.c_str();
#endif

	DWORD dwDecodedSize = static_cast<DWORD>(strProfile.size() + 130);

	scoped_array<BYTE> pDecoded(new BYTE[dwDecodedSize]);
	memset(pDecoded.get(), 0, dwDecodedSize);

	if(CBase64Codec::Decode((BYTE *)strEncoded.c_str(),
		static_cast<DWORD>(strEncoded.size()), pDecoded.get(),
		&dwDecodedSize) == false) { ASSERT(FALSE); return; }

	ASSERT(pDecoded.get()[0] <= PWGD_VERSION_BYTE);

	TCHAR* lpName = _UTF8ToString(&pDecoded.get()[1]);
	s->strName = lpName;

	BYTE *pb = (BYTE *)memchr(pDecoded.get(), 0, dwDecodedSize);
	if(pb == NULL) { ASSERT(FALSE); return; }

	++pb;
	s->btGeneratorType = *pb; ++pb;
	s->bCollectUserEntropy = ((*pb == (BYTE)'U') ? TRUE : FALSE); ++pb;

	s->dwLength = (static_cast<DWORD>(*pb) << 24); ++pb;
	s->dwLength |= (static_cast<DWORD>(*pb) << 16); ++pb;
	s->dwLength |= (static_cast<DWORD>(*pb) << 8); ++pb;
	s->dwLength |= static_cast<DWORD>(*pb); ++pb;

	USHORT usFlags = (USHORT)(*pb) << 8; ++pb;
	usFlags |= (USHORT)(*pb); ++pb;

	PwCharSet pcs;
	pcs.UnpackCharRanges(usFlags);

	while(true)
	{
		BYTE bt1 = *pb; ++pb;
		BYTE bt2 = *pb; ++pb;
		if((bt1 == 0) && (bt2 == 0)) break;
		pcs.Add(((WCHAR)bt1 << 8) | (WCHAR)bt2);
	}
	s->strCharSet = pcs.ToString();

	while(true)
	{
		BYTE bt1 = *pb; ++pb;
		BYTE bt2 = *pb; ++pb;
		if((bt1 == 0) && (bt2 == 0)) break;
		s->strPattern += (WCHAR)(((WCHAR)bt1 << 8) | (WCHAR)bt2);
	}

	ASSERT((*pb == (BYTE)'N') || (*pb == (BYTE)'A'));
	s->bNoConfusing = ((*pb == (BYTE)'N') ? TRUE : FALSE); ++pb;
	ASSERT((*pb == (BYTE)'P') || (*pb == (BYTE)'N') || (*pb == 0));
	s->bPatternPermute = ((*pb == (BYTE)'P') ? TRUE : FALSE); ++pb;
	ASSERT((*pb == (BYTE)'N') || (*pb == (BYTE)'R') || (*pb == 0));
	s->bNoRepeat = ((*pb == (BYTE)'N') ? TRUE : FALSE); ++pb;

	while(true)
	{
		BYTE bt1 = *pb; ++pb;
		BYTE bt2 = *pb; ++pb;
		if((bt1 == 0) && (bt2 == 0)) break;
		s->strExcludeChars += (WCHAR)(((WCHAR)bt1 << 8) | (WCHAR)bt2);
	}

	SAFE_DELETE_ARRAY(lpName);
}

void PwgGetDefaultProfile(PW_GEN_SETTINGS_EX* s)
{
	ASSERT(s != NULL); if(s == NULL) return;

	s->strName.clear();

	s->btGeneratorType = PWGT_CHARSET;
	s->bCollectUserEntropy = FALSE;

	s->dwLength = 20;

	PwCharSet pcs;
	pcs.Add(PDCS_UPPER_CASE, PDCS_LOWER_CASE, PDCS_NUMERIC);
	s->strCharSet = pcs.ToString();

	s->strPattern.clear();
	s->bPatternPermute = FALSE;

	s->bNoConfusing = FALSE;
	s->bNoRepeat = FALSE;

	s->strExcludeChars.clear();
}

BOOL PwgHasSecurityReducingOption(const PW_GEN_SETTINGS_EX* pSettings)
{
	ASSERT(pSettings != NULL); if(pSettings == NULL) return FALSE;

	if(pSettings->bNoConfusing != FALSE) return TRUE;
	if(pSettings->bNoRepeat != FALSE) return TRUE;
	if(pSettings->strExcludeChars.size() > 0) return TRUE;

	return FALSE;
}

max_stars_repo_name: rrvt/KeePassLastPass

max_issues_repo_name: rrvt/KeePassLastPass

max_forks_repo_name: rrvt/KeePassLastPass


text:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <numeric>
#include <chrono>
#include <ctime>
#include <stdexcept>

#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <gdal_priv.h>
#include <ogrsf_frmts.h>

#include <CloudTools.Common/IO/IO.h>
#include <CloudTools.Common/IO/Reporter.h>
#include <CloudTools.DEM/Metadata.h>

namespace po = boost::program_options;
namespace fs = boost::filesystem;

using namespace CloudTools::DEM;
using namespace CloudTools::IO;

struct Tree
{
	OGRPoint location;
	int year;
	int radius;
};

void writeResultsToFile(
	const std::vector<Tree>& matched,
	const std::vector<Tree>& missed,
	VectorMetadata metadata,
	const std::string& outPath);

int main(int argc, char* argv[]) try
{
	std::string inputFile;
	std::string referenceFile;

	std::string yearField = "Plantjaar";
	std::string radiusField = "RADIUS";

	unsigned int minYear = 0;
	unsigned int maxYear = 9999;
	unsigned int minRadius = 0;
	unsigned int minTolerance = 3;

	// Read console arguments
	po::options_description desc("Allowed options");
	desc.add_options()
		("input,i", po::value<std::string>(&inputFile),
		 "cluster map file to verify")
		("reference,r", po::value<std::string>(&referenceFile),
		 "reference file to verify against")
		("reference-year", po::value<std::string>(&yearField)->default_value(yearField),
		 "year field in reference file")
		("reference-radius", po::value<std::string>(&radiusField)->default_value(radiusField),
		 "radius field in reference file")
		("min-year", po::value<unsigned int>(&minYear)->default_value(minYear),
		 "minimum plant year")
		("max-year", po::value<unsigned int>(&maxYear)->default_value(maxYear),
		 "maximum plant year")
		("min-radius", po::value<unsigned int>(&minRadius)->default_value(minRadius),
		 "minimum tree radius")
		("min-tolerance", po::value<unsigned int>(&minTolerance)->default_value(minTolerance),
		 "minimum distance tolerance for matching")
		("verbose,v", "verbose output")
		("help,h", "produce help message");

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, desc), vm);
	po::notify(vm);

	// Argument validation
	if (vm.count("help"))
	{
		std::cout << "Verifies detected trees against reference file." << std::endl;
		std::cout << desc << std::endl;
		return Success;
	}

	bool argumentError = false;
	if (!vm.count("input"))
	{
		std::cerr << "The input file is mandatory." << std::endl;
		argumentError = true;
	}
	if (!fs::is_regular_file(inputFile))
	{
		std::cerr << "The input file does not exist." << std::endl;
		argumentError = true;
	}

	if (!vm.count("reference"))
	{
		std::cerr << "The reference file is mandatory." << std::endl;
		argumentError = true;
	}
	if (!fs::is_regular_file(referenceFile))
	{
		std::cerr << "The reference file does not exist." << std::endl;
		argumentError = true;
	}

	if (argumentError)
	{
		std::cerr << "Use the --help option for description." << std::endl;
		return InvalidInput;
	}

	// Program
	std::cout << "=== DEM Vegetation Filter Verifier ===" << std::endl;
	std::clock_t clockStart = std::clock();
	auto timeStart = std::chrono::high_resolution_clock::now();

	Reporter* reporter = vm.count("verbose")
	                     ? static_cast<Reporter*>(new TextReporter())
	                     : static_cast<Reporter*>(new BarReporter());

	GDALAllRegister();

	// Open datasets
	GDALDataset* inputDataset = static_cast<GDALDataset*>(GDALOpenEx(inputFile.c_str(),
	                                                                 GDAL_OF_VECTOR | GDAL_OF_READONLY,
	                                                                 nullptr, nullptr, nullptr));
	if (inputDataset == nullptr)
		throw std::runtime_error("Error at opening the input file.");

	GDALDataset* referenceDataset = static_cast<GDALDataset*>(GDALOpenEx(referenceFile.c_str(),
	                                                                     GDAL_OF_VECTOR | GDAL_OF_READONLY,
	                                                                     nullptr, nullptr, nullptr));
	if (referenceDataset == nullptr)
		throw std::runtime_error("Error at opening the reference file.");

	OGRLayer* inputLayer, * referenceLayer;
	if (inputDataset->GetLayerCount() == 1)
		inputLayer = inputDataset->GetLayer(0);
	else
		throw std::runtime_error("There are more than 1 input layers.");

	if (referenceDataset->GetLayerCount() == 1)
		referenceLayer = referenceDataset->GetLayer(0);
	else
		throw std::runtime_error("There are more than 1 reference layers.");

	VectorMetadata inputMetadata(std::vector<OGRLayer*>{inputLayer});
	VectorMetadata referenceMetadata(std::vector<OGRLayer*>{referenceLayer});

	// Create bounding box for input dataset
	OGRGeometry* inputBoundingBox;
	{
		OGRMultiPoint inputBoundingPoints;
		OGRPoint inputBoundingPoint;

		inputBoundingPoint = OGRPoint(inputMetadata.originX(), inputMetadata.originY());
		inputBoundingPoints.addGeometry(&inputBoundingPoint);

		inputBoundingPoint = OGRPoint(inputMetadata.originX(), inputMetadata.originY() - inputMetadata.extentY());
		inputBoundingPoints.addGeometry(&inputBoundingPoint);

		inputBoundingPoint = OGRPoint(inputMetadata.originX() + inputMetadata.extentX(), inputMetadata.originY());
		inputBoundingPoints.addGeometry(&inputBoundingPoint);

		inputBoundingPoint = OGRPoint(inputMetadata.originX() + inputMetadata.extentX(),
		                              inputMetadata.originY() - inputMetadata.extentY());
		inputBoundingPoints.addGeometry(&inputBoundingPoint);

		inputBoundingBox = inputBoundingPoints.ConvexHull();
	}

	// Display metadata
	if (vm.count("verbose"))
	{
		std::cout << std::endl << "--- Input file ---" << std::endl;
		std::cout << "File path: \t" << inputFile << std::endl;
		std::cout << "Tree count: \t" << inputLayer->GetFeatureCount() << std::endl;
		std::cout << inputMetadata << std::endl;

		std::cout << std::endl << "--- Reference file ---" << std::endl;
		std::cout << "File path: \t" << referenceFile << std::endl;
		std::cout << "Tree count: \t" << referenceLayer->GetFeatureCount() << std::endl;
		std::cout << referenceMetadata << std::endl;

		if (!readBoolean("Would you like to continue?"))
		{
			std::cerr << "Operation aborted." << std::endl;
			return UserAbort;
		}
	}

	// Read input trees
	OGRFeature* feature;
	OGRGeometry* geometry;
	OGRPoint* point;
	OGRCoordinateTransformation* transformation;

	std::vector<OGRPoint> inputTrees;
	inputTrees.reserve(inputLayer->GetFeatureCount());
	while ((feature = inputLayer->GetNextFeature()) != nullptr)
	{
		geometry = feature->GetGeometryRef();
		if (geometry->getGeometryType() != wkbPoint && geometry->getGeometryType() != wkbPoint25D)
			throw std::runtime_error("A geometry is not a point.");

		point = (OGRPoint*) geometry;
		inputTrees.push_back(*point);
		OGRFeature::DestroyFeature(feature);
	}

	// Create CRS transformation (if required)
	if (!referenceMetadata.reference().IsSame(&inputMetadata.reference()))
	{
		if (vm.count("verbose"))
			std::cout << "Reprojection between input and reference dataset required." << std::endl;

		transformation = OGRCreateCoordinateTransformation(
			&referenceMetadata.reference(), &inputMetadata.reference());

		if (transformation == nullptr)
			throw std::runtime_error("Coordinate reference transformation failure.");
	}

	// Read reference trees
	std::vector<Tree> referenceTrees;
	while ((feature = referenceLayer->GetNextFeature()) != nullptr)
	{
		int year = feature->GetFieldAsInteger(yearField.c_str());
		int radius = feature->GetFieldAsInteger(radiusField.c_str());

		geometry = feature->GetGeometryRef();
		if (geometry->getGeometryType() != wkbPoint)
			throw std::runtime_error("A geometry is not a point.");
		point = (OGRPoint*) geometry;

		if (year >= minYear && year <= maxYear && radius >= minRadius)
		{
			double x = point->getX();
			double y = point->getY();

			if (transformation != nullptr)
			{
				if (!transformation->Transform(1, &x, &y))
					throw std::runtime_error("Coordinate reference transformation failure.");
			}

			OGRPoint transformedPoint(x, y);
			if (transformedPoint.Within(inputBoundingBox))
				referenceTrees.push_back(Tree{transformedPoint, year, radius});

		}

		OGRFeature::DestroyFeature(feature);
	}

	if (transformation != nullptr)
		delete transformation;

	if (vm.count("verbose"))
		std::cout << "Reference tree count (considered): " << referenceTrees.size() << std::endl;

	// Close datasets
	GDALClose(inputDataset);
	GDALClose(referenceDataset);
	delete inputBoundingBox;

	reporter->reset();
	reporter->report(0.f, "Verification");

	// Verification: matching input and reference trees
	bool found;
	unsigned int counter = 0;
	std::vector<Tree> matched, missed;
	for (const auto& referenceTree : referenceTrees)
	{
		found = false;
		for (const auto& inputTreeLocation : inputTrees)
		{
			if (referenceTree.location.Distance(&inputTreeLocation) <= std::max(referenceTree.radius, (int)minTolerance))
			{
				found = true;
				break;
			}
		}

		if (found)
			matched.push_back(referenceTree);
		else
			missed.push_back(referenceTree);

		++counter;
		if ((counter) % 100 == 0)
			reporter->report(counter * 1.f / referenceTrees.size(), "Verification");
	}
	reporter->report(1.f, "Verification");
	delete reporter;

	// Write result GeoJSON output file
	writeResultsToFile(matched, missed, inputMetadata, "result.json");

	// Write results to standard output
	std::cout << std::endl
	          << "Verification completed!" << std::endl
	          << std::endl << std::fixed << std::setprecision(2)
	          << "[Basic statistic]" << std::endl
	          << "Reference trees matched: " << matched.size() << std::endl
	          << "Reference trees failed: " << missed.size() << std::endl
	          << "Match ratio: " << ((100.f * matched.size()) / (referenceTrees.size())) << "%" << std::endl
	          << std::endl
	          << "[Miss statistic]" << std::endl
	          << "Average radius: " << ((std::accumulate(missed.begin(), missed.end(),
	                                                     0.0, [](double sum, const Tree& tree)
	                                                     {
		                                                     return sum + tree.radius;
	                                                     })) / missed.size()) << std::endl
	          << std::endl
	          << "[Advanced statistic]" << std::endl
	          << "Extraction rate: " << ((100.f * inputTrees.size()) / referenceTrees.size()) << "%" << std::endl
	          << "Matching rate: " << ((100.f * matched.size()) / (referenceTrees.size())) << "%" << std::endl
	          << "Commission rate: " << ((100.f * (inputTrees.size() - matched.size())) / inputTrees.size()) << "%" << std:: endl
	          << "Omission rate: " << ((100.f * missed.size()) / (referenceTrees.size())) << "%" << std::endl;

	// Execution time measurement
	std::clock_t clockEnd = std::clock();
	auto timeEnd = std::chrono::high_resolution_clock::now();

	std::cout << std::endl
	          << "CPU time used: "
	          << 1.f * (clockEnd - clockStart) / CLOCKS_PER_SEC / 60 << " min" << std::endl
	          << "Wall clock time passed: "
	          << std::chrono::duration<float>(timeEnd - timeStart).count() / 60 << " min" << std::endl;

	return Success;
}
catch (std::exception &ex)
{
	std::cerr << "ERROR: " << ex.what() << std::endl;
	return UnexcpectedError;
}

void writeResultsToFile(
	const std::vector<Tree>& matched,
	const std::vector<Tree>& missed,
	VectorMetadata metadata,
	const std::string& outPath)
{
	GDALDriver* driver = GetGDALDriverManager()->GetDriverByName("GeoJSON");
	if (driver == nullptr)
		throw std::invalid_argument("Target output format unrecognized.");

	if (fs::exists(outPath) &&
	    driver->Delete(outPath.c_str()) == CE_Failure &&
	    !fs::remove(outPath))
		throw std::runtime_error("Cannot overwrite previously created target file.");

	GDALDataset* ds = driver->Create(outPath.c_str(), 0, 0, 0, GDT_Unknown, nullptr);
	if (ds == nullptr)
		throw std::runtime_error("Target file creation failed.");

	OGRLayer* layer = ds->CreateLayer("points", &metadata.reference(), wkbPoint, nullptr);
	{
		OGRFieldDefn field("Category", OFTString);
		field.SetWidth(10);
		if (layer->CreateField(&field) != OGRERR_NONE)
			throw std::runtime_error("Category field creation failed.");
	}
	{
		OGRFieldDefn field("Radius", OFTInteger);
		if (layer->CreateField(&field) != OGRERR_NONE)
			throw std::runtime_error("Category field creation failed.");
	}
	{
		OGRFieldDefn field("Year", OFTInteger);
		if (layer->CreateField(&field) != OGRERR_NONE)
			throw std::runtime_error("Category field creation failed.");
	}

	OGRFeature *feature;
	for(auto& tree : matched)
	{
		feature = OGRFeature::CreateFeature(layer->GetLayerDefn());
		feature->SetField("Category", "matched");
		feature->SetField("Year", tree.year);
		feature->SetField("Radius", tree.radius);
		feature->SetGeometry(&tree.location);
		if (layer->CreateFeature(feature) != OGRERR_NONE)
			throw std::runtime_error("Feature creation failed.");
		OGRFeature::DestroyFeature(feature);
	}

	for(auto& tree : missed)
	{
		feature = OGRFeature::CreateFeature(layer->GetLayerDefn());
		feature->SetField("Category", "missed");
		feature->SetField("Year", tree.year);
		feature->SetField("Radius", tree.radius);
		feature->SetGeometry(&tree.location);
		if (layer->CreateFeature(feature) != OGRERR_NONE)
			throw std::runtime_error("Feature creation failed.");
		OGRFeature::DestroyFeature(feature);
	}

	GDALClose(ds);
}

ext: cpp

max_stars_repo_name: mcserep/PointCloudTools

max_issues_repo_name: mcserep/PointCloudTools

max_forks_repo_name: mcserep/PointCloudTools


text:
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

    This example program shows how to use dlib's implementation of the paper:
        One Millisecond Face Alignment with an Ensemble of Regression Trees by
        Vahid Kazemi and Josephine Sullivan, CVPR 2014

    In particular, we will train a face landmarking model based on a small dataset
    and then evaluate it.  If you want to visualize the output of the trained
    model on some images then you can run the face_landmark_detection_ex.cpp
    example program with sp.dat as the input model.

    It should also be noted that this kind of model, while often used for face
    landmarking, is quite general and can be used for a variety of shape
    prediction tasks.  But here we demonstrate it only on a simple face
    landmarking task.
*/

// program na vytvaranie modeluhladania bodov na tvari
// upraveny subor - povodny je mozne najst na https://github.com/davisking/dlib/blob/master/examples/train_shape_predictor_ex.cpp

#include <dlib/image_processing.h>
#include <dlib/data_io.h>
#include <iostream>

using namespace dlib;
using namespace std;

// ----------------------------------------------------------------------------------------

std::vector<std::vector<double> > get_interocular_distances(
        const std::vector<std::vector<full_object_detection> > &objects
);
/*!
    ensures
        - returns an object D such that:
            - D[i][j] == the distance, in pixels, between the eyes for the face represented
              by objects[i][j].
!*/

// ----------------------------------------------------------------------------------------

int main(int argc, char **argv) {
    try {

        dlib::array<array2d<unsigned char> > images_train, images_test;
        std::vector<std::vector<full_object_detection> > faces_train, faces_test;

        load_image_dataset(images_train, faces_train,
                           "../../../ibug_300W_large_face_landmark_dataset/trainUsta+7.xml");

//        pre otestovanie by bolo potrebne upravit testovaciu metodu viac info na https://github.com/davisking/dlib/blob/master/examples/train_shape_predictor_ex.cpp
//        load_image_dataset(images_test, faces_test, "../../ibug_300W_large_face_landmark_dataset/testUsta+7.xml");

        shape_predictor_trainer trainer;

        trainer.set_cascade_depth(12);
        trainer.set_num_trees_per_cascade_level(600);
        trainer.set_tree_depth(4);
        trainer.set_nu(0.1); //shrinkage faktor
        trainer.set_oversampling_amount(550);
        trainer.set_feature_pool_size(750);
        trainer.set_lambda(0.2);
        trainer.set_num_test_splits(300);

        trainer.set_num_threads(8);
        trainer.be_verbose();

        shape_predictor sp = trainer.train(images_train, faces_train);

        serialize("../data/models/27BodovyModel.dat") << sp;

    }
    catch (exception &e) {
        cout << "\nexception thrown!" << endl;
        cout << e.what() << endl;
    }
}

// ----------------------------------------------------------------------------------------

double interocular_distance(
        const full_object_detection &det
) {
    dlib::vector<double, 2> l, r;
    double cnt = 0;
    // Find the center of the left eye by averaging the points around
    // the eye.
    for (unsigned long i = 36; i <= 41; ++i) {
        l += det.part(i);
        ++cnt;
    }
    l /= cnt;

    // Find the center of the right eye by averaging the points around
    // the eye.
    cnt = 0;
    for (unsigned long i = 42; i <= 47; ++i) {
        r += det.part(i);
        ++cnt;
    }
    r /= cnt;

    // Now return the distance between the centers of the eyes
    return length(l - r);
}

std::vector<std::vector<double> > get_interocular_distances(
        const std::vector<std::vector<full_object_detection> > &objects
) {
    std::vector<std::vector<double> > temp(objects.size());
    for (unsigned long i = 0; i < objects.size(); ++i) {
        for (unsigned long j = 0; j < objects[i].size(); ++j) {
            temp[i].push_back(interocular_distance(objects[i][j]));
        }
    }
    return temp;
}

// ----------------------------------------------------------------------------------------


text:
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
 */

#include <boost/hana/assert.hpp>
#include <boost/hana/back.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/front.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/tail.hpp>
namespace hana = boost::hana;

BOOST_HANA_CONSTANT_CHECK(hana::front(hana::range_c<int, 0, 5>) == hana::int_c<0>);
BOOST_HANA_CONSTANT_CHECK(hana::back(hana::range_c<unsigned long, 0, 5>) == hana::ulong_c<4>);
BOOST_HANA_CONSTANT_CHECK(hana::tail(hana::range_c<int, 0, 5>) == hana::make_range(hana::int_c<1>, hana::int_c<5>));

int main() { }

max_stars_repo_name: qicosmos/hana

max_issues_repo_name: qicosmos/hana

max_forks_repo_name: qicosmos/hana


text:
//
// Copyright (c) 2016-2019 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#define BOOST_MPL_LIMIT_MAP_SIZE 128
#include <boost/preprocessor/iterate.hpp>
#include <boost/mpl/map/map50.hpp>

namespace boost {
namespace mpl {
// clang-format off
#define BOOST_PP_ITERATION_PARAMS_1                                            \
  (3, (51, BOOST_MPL_LIMIT_MAP_SIZE, <boost/mpl/map/aux_/numbered.hpp>))
#include BOOST_PP_ITERATE()
// clang-format on
} // namespace mpl
} // namespace boost

#define BOOST_MPL_PREPROCESSING_MODE
#include <boost/mpl/map.hpp>
#undef BOOST_MPL_PREPROCESSING_MODE

#include <boost/mpl/map.hpp>
#include <boost/mpl/at.hpp>

namespace mpl = boost::mpl;

using m = mpl::map<mpl::pair<mpl::int_<1>, mpl::int_<1>>,
                   mpl::pair<mpl::int_<2>, mpl::int_<2>>,
                   mpl::pair<mpl::int_<3>, mpl::int_<3>>,
                   mpl::pair<mpl::int_<4>, mpl::int_<4>>,
                   mpl::pair<mpl::int_<5>, mpl::int_<5>>,
                   mpl::pair<mpl::int_<6>, mpl::int_<6>>,
                   mpl::pair<mpl::int_<7>, mpl::int_<7>>,
                   mpl::pair<mpl::int_<8>, mpl::int_<8>>,
                   mpl::pair<mpl::int_<9>, mpl::int_<9>>,
                   mpl::pair<mpl::int_<10>, mpl::int_<10>>,
                   mpl::pair<mpl::int_<11>, mpl::int_<11>>,
                   mpl::pair<mpl::int_<12>, mpl::int_<12>>,
                   mpl::pair<mpl::int_<13>, mpl::int_<13>>,
                   mpl::pair<mpl::int_<14>, mpl::int_<14>>,
                   mpl::pair<mpl::int_<15>, mpl::int_<15>>,
                   mpl::pair<mpl::int_<16>, mpl::int_<16>>,
                   mpl::pair<mpl::int_<17>, mpl::int_<17>>,
                   mpl::pair<mpl::int_<18>, mpl::int_<18>>,
                   mpl::pair<mpl::int_<19>, mpl::int_<19>>,
                   mpl::pair<mpl::int_<20>, mpl::int_<20>>,
                   mpl::pair<mpl::int_<21>, mpl::int_<21>>,
                   mpl::pair<mpl::int_<22>, mpl::int_<22>>,
                   mpl::pair<mpl::int_<23>, mpl::int_<23>>,
                   mpl::pair<mpl::int_<24>, mpl::int_<24>>,
                   mpl::pair<mpl::int_<25>, mpl::int_<25>>,
                   mpl::pair<mpl::int_<26>, mpl::int_<26>>,
                   mpl::pair<mpl::int_<27>, mpl::int_<27>>,
                   mpl::pair<mpl::int_<28>, mpl::int_<28>>,
                   mpl::pair<mpl::int_<29>, mpl::int_<29>>,
                   mpl::pair<mpl::int_<30>, mpl::int_<30>>,
                   mpl::pair<mpl::int_<31>, mpl::int_<31>>,
                   mpl::pair<mpl::int_<32>, mpl::int_<32>>,
                   mpl::pair<mpl::int_<33>, mpl::int_<33>>,
                   mpl::pair<mpl::int_<34>, mpl::int_<34>>,
                   mpl::pair<mpl::int_<35>, mpl::int_<35>>,
                   mpl::pair<mpl::int_<36>, mpl::int_<36>>,
                   mpl::pair<mpl::int_<37>, mpl::int_<37>>,
                   mpl::pair<mpl::int_<38>, mpl::int_<38>>,
                   mpl::pair<mpl::int_<39>, mpl::int_<39>>,
                   mpl::pair<mpl::int_<40>, mpl::int_<40>>,
                   mpl::pair<mpl::int_<41>, mpl::int_<41>>,
                   mpl::pair<mpl::int_<42>, mpl::int_<42>>,
                   mpl::pair<mpl::int_<43>, mpl::int_<43>>,
                   mpl::pair<mpl::int_<44>, mpl::int_<44>>,
                   mpl::pair<mpl::int_<45>, mpl::int_<45>>,
                   mpl::pair<mpl::int_<46>, mpl::int_<46>>,
                   mpl::pair<mpl::int_<47>, mpl::int_<47>>,
                   mpl::pair<mpl::int_<48>, mpl::int_<48>>,
                   mpl::pair<mpl::int_<49>, mpl::int_<49>>,
                   mpl::pair<mpl::int_<50>, mpl::int_<50>>,
                   mpl::pair<mpl::int_<51>, mpl::int_<51>>,
                   mpl::pair<mpl::int_<52>, mpl::int_<52>>,
                   mpl::pair<mpl::int_<53>, mpl::int_<53>>,
                   mpl::pair<mpl::int_<54>, mpl::int_<54>>,
                   mpl::pair<mpl::int_<55>, mpl::int_<55>>,
                   mpl::pair<mpl::int_<56>, mpl::int_<56>>,
                   mpl::pair<mpl::int_<57>, mpl::int_<57>>,
                   mpl::pair<mpl::int_<58>, mpl::int_<58>>,
                   mpl::pair<mpl::int_<59>, mpl::int_<59>>,
                   mpl::pair<mpl::int_<60>, mpl::int_<60>>,
                   mpl::pair<mpl::int_<61>, mpl::int_<61>>,
                   mpl::pair<mpl::int_<62>, mpl::int_<62>>,
                   mpl::pair<mpl::int_<63>, mpl::int_<63>>,
                   mpl::pair<mpl::int_<64>, mpl::int_<64>>,
                   mpl::pair<mpl::int_<65>, mpl::int_<65>>,
                   mpl::pair<mpl::int_<66>, mpl::int_<66>>,
                   mpl::pair<mpl::int_<67>, mpl::int_<67>>,
                   mpl::pair<mpl::int_<68>, mpl::int_<68>>,
                   mpl::pair<mpl::int_<69>, mpl::int_<69>>,
                   mpl::pair<mpl::int_<70>, mpl::int_<70>>,
                   mpl::pair<mpl::int_<71>, mpl::int_<71>>,
                   mpl::pair<mpl::int_<72>, mpl::int_<72>>,
                   mpl::pair<mpl::int_<73>, mpl::int_<73>>,
                   mpl::pair<mpl::int_<74>, mpl::int_<74>>,
                   mpl::pair<mpl::int_<75>, mpl::int_<75>>,
                   mpl::pair<mpl::int_<76>, mpl::int_<76>>,
                   mpl::pair<mpl::int_<77>, mpl::int_<77>>,
                   mpl::pair<mpl::int_<78>, mpl::int_<78>>,
                   mpl::pair<mpl::int_<79>, mpl::int_<79>>,
                   mpl::pair<mpl::int_<80>, mpl::int_<80>>,
                   mpl::pair<mpl::int_<81>, mpl::int_<81>>,
                   mpl::pair<mpl::int_<82>, mpl::int_<82>>,
                   mpl::pair<mpl::int_<83>, mpl::int_<83>>,
                   mpl::pair<mpl::int_<84>, mpl::int_<84>>,
                   mpl::pair<mpl::int_<85>, mpl::int_<85>>,
                   mpl::pair<mpl::int_<86>, mpl::int_<86>>,
                   mpl::pair<mpl::int_<87>, mpl::int_<87>>,
                   mpl::pair<mpl::int_<88>, mpl::int_<88>>,
                   mpl::pair<mpl::int_<89>, mpl::int_<89>>,
                   mpl::pair<mpl::int_<90>, mpl::int_<90>>,
                   mpl::pair<mpl::int_<91>, mpl::int_<91>>,
                   mpl::pair<mpl::int_<92>, mpl::int_<92>>,
                   mpl::pair<mpl::int_<93>, mpl::int_<93>>,
                   mpl::pair<mpl::int_<94>, mpl::int_<94>>,
                   mpl::pair<mpl::int_<95>, mpl::int_<95>>,
                   mpl::pair<mpl::int_<96>, mpl::int_<96>>,
                   mpl::pair<mpl::int_<97>, mpl::int_<97>>,
                   mpl::pair<mpl::int_<98>, mpl::int_<98>>,
                   mpl::pair<mpl::int_<99>, mpl::int_<99>>,
                   mpl::pair<mpl::int_<100>, mpl::int_<100>>,
                   mpl::pair<mpl::int_<101>, mpl::int_<101>>,
                   mpl::pair<mpl::int_<102>, mpl::int_<102>>,
                   mpl::pair<mpl::int_<103>, mpl::int_<103>>,
                   mpl::pair<mpl::int_<104>, mpl::int_<104>>,
                   mpl::pair<mpl::int_<105>, mpl::int_<105>>,
                   mpl::pair<mpl::int_<106>, mpl::int_<106>>,
                   mpl::pair<mpl::int_<107>, mpl::int_<107>>,
                   mpl::pair<mpl::int_<108>, mpl::int_<108>>,
                   mpl::pair<mpl::int_<109>, mpl::int_<109>>,
                   mpl::pair<mpl::int_<110>, mpl::int_<110>>,
                   mpl::pair<mpl::int_<111>, mpl::int_<111>>,
                   mpl::pair<mpl::int_<112>, mpl::int_<112>>,
                   mpl::pair<mpl::int_<113>, mpl::int_<113>>,
                   mpl::pair<mpl::int_<114>, mpl::int_<114>>,
                   mpl::pair<mpl::int_<115>, mpl::int_<115>>,
                   mpl::pair<mpl::int_<116>, mpl::int_<116>>,
                   mpl::pair<mpl::int_<117>, mpl::int_<117>>,
                   mpl::pair<mpl::int_<118>, mpl::int_<118>>,
                   mpl::pair<mpl::int_<119>, mpl::int_<119>>,
                   mpl::pair<mpl::int_<120>, mpl::int_<120>>,
                   mpl::pair<mpl::int_<121>, mpl::int_<121>>,
                   mpl::pair<mpl::int_<122>, mpl::int_<122>>,
                   mpl::pair<mpl::int_<123>, mpl::int_<123>>,
                   mpl::pair<mpl::int_<124>, mpl::int_<124>>,
                   mpl::pair<mpl::int_<125>, mpl::int_<125>>,
                   mpl::pair<mpl::int_<126>, mpl::int_<126>>,
                   mpl::pair<mpl::int_<127>, mpl::int_<127>>,
                   mpl::pair<mpl::int_<128>, mpl::int_<128>>>;

template <class> struct q;
int main() {
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<1>>::type,
                               mpl::int_<1>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<2>>::type,
                               mpl::int_<2>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<3>>::type,
                               mpl::int_<3>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<4>>::type,
                               mpl::int_<4>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<5>>::type,
                               mpl::int_<5>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<6>>::type,
                               mpl::int_<6>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<7>>::type,
                               mpl::int_<7>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<8>>::type,
                               mpl::int_<8>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<9>>::type,
                               mpl::int_<9>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<10>>::type,
                               mpl::int_<10>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<11>>::type,
                               mpl::int_<11>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<12>>::type,
                               mpl::int_<12>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<13>>::type,
                               mpl::int_<13>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<14>>::type,
                               mpl::int_<14>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<15>>::type,
                               mpl::int_<15>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<16>>::type,
                               mpl::int_<16>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<17>>::type,
                               mpl::int_<17>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<18>>::type,
                               mpl::int_<18>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<19>>::type,
                               mpl::int_<19>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<20>>::type,
                               mpl::int_<20>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<21>>::type,
                               mpl::int_<21>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<22>>::type,
                               mpl::int_<22>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<23>>::type,
                               mpl::int_<23>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<24>>::type,
                               mpl::int_<24>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<25>>::type,
                               mpl::int_<25>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<26>>::type,
                               mpl::int_<26>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<27>>::type,
                               mpl::int_<27>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<28>>::type,
                               mpl::int_<28>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<29>>::type,
                               mpl::int_<29>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<30>>::type,
                               mpl::int_<30>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<31>>::type,
                               mpl::int_<31>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<32>>::type,
                               mpl::int_<32>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<33>>::type,
                               mpl::int_<33>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<34>>::type,
                               mpl::int_<34>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<35>>::type,
                               mpl::int_<35>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<36>>::type,
                               mpl::int_<36>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<37>>::type,
                               mpl::int_<37>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<38>>::type,
                               mpl::int_<38>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<39>>::type,
                               mpl::int_<39>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<40>>::type,
                               mpl::int_<40>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<41>>::type,
                               mpl::int_<41>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<42>>::type,
                               mpl::int_<42>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<43>>::type,
                               mpl::int_<43>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<44>>::type,
                               mpl::int_<44>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<45>>::type,
                               mpl::int_<45>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<46>>::type,
                               mpl::int_<46>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<47>>::type,
                               mpl::int_<47>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<48>>::type,
                               mpl::int_<48>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<49>>::type,
                               mpl::int_<49>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<50>>::type,
                               mpl::int_<50>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<51>>::type,
                               mpl::int_<51>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<52>>::type,
                               mpl::int_<52>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<53>>::type,
                               mpl::int_<53>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<54>>::type,
                               mpl::int_<54>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<55>>::type,
                               mpl::int_<55>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<56>>::type,
                               mpl::int_<56>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<57>>::type,
                               mpl::int_<57>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<58>>::type,
                               mpl::int_<58>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<59>>::type,
                               mpl::int_<59>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<60>>::type,
                               mpl::int_<60>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<61>>::type,
                               mpl::int_<61>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<62>>::type,
                               mpl::int_<62>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<63>>::type,
                               mpl::int_<63>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<64>>::type,
                               mpl::int_<64>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<65>>::type,
                               mpl::int_<65>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<66>>::type,
                               mpl::int_<66>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<67>>::type,
                               mpl::int_<67>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<68>>::type,
                               mpl::int_<68>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<69>>::type,
                               mpl::int_<69>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<70>>::type,
                               mpl::int_<70>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<71>>::type,
                               mpl::int_<71>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<72>>::type,
                               mpl::int_<72>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<73>>::type,
                               mpl::int_<73>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<74>>::type,
                               mpl::int_<74>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<75>>::type,
                               mpl::int_<75>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<76>>::type,
                               mpl::int_<76>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<77>>::type,
                               mpl::int_<77>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<78>>::type,
                               mpl::int_<78>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<79>>::type,
                               mpl::int_<79>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<80>>::type,
                               mpl::int_<80>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<81>>::type,
                               mpl::int_<81>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<82>>::type,
                               mpl::int_<82>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<83>>::type,
                               mpl::int_<83>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<84>>::type,
                               mpl::int_<84>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<85>>::type,
                               mpl::int_<85>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<86>>::type,
                               mpl::int_<86>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<87>>::type,
                               mpl::int_<87>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<88>>::type,
                               mpl::int_<88>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<89>>::type,
                               mpl::int_<89>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<90>>::type,
                               mpl::int_<90>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<91>>::type,
                               mpl::int_<91>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<92>>::type,
                               mpl::int_<92>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<93>>::type,
                               mpl::int_<93>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<94>>::type,
                               mpl::int_<94>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<95>>::type,
                               mpl::int_<95>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<96>>::type,
                               mpl::int_<96>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<97>>::type,
                               mpl::int_<97>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<98>>::type,
                               mpl::int_<98>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<99>>::type,
                               mpl::int_<99>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<100>>::type,
                               mpl::int_<100>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<101>>::type,
                               mpl::int_<101>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<102>>::type,
                               mpl::int_<102>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<103>>::type,
                               mpl::int_<103>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<104>>::type,
                               mpl::int_<104>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<105>>::type,
                               mpl::int_<105>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<106>>::type,
                               mpl::int_<106>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<107>>::type,
                               mpl::int_<107>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<108>>::type,
                               mpl::int_<108>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<109>>::type,
                               mpl::int_<109>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<110>>::type,
                               mpl::int_<110>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<111>>::type,
                               mpl::int_<111>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<112>>::type,
                               mpl::int_<112>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<113>>::type,
                               mpl::int_<113>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<114>>::type,
                               mpl::int_<114>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<115>>::type,
                               mpl::int_<115>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<116>>::type,
                               mpl::int_<116>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<117>>::type,
                               mpl::int_<117>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<118>>::type,
                               mpl::int_<118>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<119>>::type,
                               mpl::int_<119>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<120>>::type,
                               mpl::int_<120>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<121>>::type,
                               mpl::int_<121>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<122>>::type,
                               mpl::int_<122>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<123>>::type,
                               mpl::int_<123>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<124>>::type,
                               mpl::int_<124>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<125>>::type,
                               mpl::int_<125>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<126>>::type,
                               mpl::int_<126>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<127>>::type,
                               mpl::int_<127>>::value,
                "");
  static_assert(boost::is_same<typename mpl::at<m, mpl::int_<128>>::type,
                               mpl::int_<128>>::value,
                "");
}

max_stars_repo_name: GuiCodron/sml

max_issues_repo_name: GuiCodron/sml

max_forks_repo_name: GuiCodron/sml


text:
/////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2021 Andreas Milton Maniotis.
//
// Email: andreas.maniotis@gmail.com
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
/////////////////////////////////////////////////////////////////////////////////////////////

#include "aml/conslist.hpp"
#include "aml/string.hpp"

#include <type_traits>

#include <boost/core/demangle.hpp>
#include <iostream>

namespace test::conslist
{
    template<int n>
    struct type {};

    template<typename...>
    class Template {};

    void test_empty_list()
    {
        using aml::conslist;

        using empty = conslist<>;
        static_assert(empty::size() == 0, "");

        using list_one = conslist<>::cons<void>;
        using list_two = conslist<>::rcons<void>;

        static_assert(std::is_same< list_one, conslist<void> >::value, "");
        static_assert(std::is_same< list_two, conslist<void> >::value, "");
        static_assert(std::is_same< conslist<>::template apply<Template>, Template<> >::value, "");

        static_assert(std::is_same< empty, empty::reverse >::value, "");

        static_assert(std::is_same<conslist<>::lfold_with<Template, void***>, void***>::value, "");
        static_assert(std::is_same<conslist<>::rfold_with<Template, void***>, void***>::value, "");

        static_assert(std::is_same<aml::lfold<conslist<>, aml::function<Template>, void***>, void***>::value, "");
        static_assert(std::is_same<aml::rfold<conslist<>, aml::function<Template>, void***>, void***>::value, "");
    }

    void test_non_empty_list()
    {
        using aml::conslist;

        using list_t = conslist< type<1>, type<2>, type<3> >;

        static_assert(list_t::size() == 3, "");

        static_assert(std::is_same<list_t::cons<type<0> >,
                                   conslist< type<0>, type<1>, type<2>, type<3> > >::value, "");

        static_assert(std::is_same<list_t::rcons<type<0> >,
                                   conslist< type<1>, type<2>, type<3>, type<0> > >::value, "");

        static_assert(std::is_same<list_t::head, type<1> >::value, "");

        static_assert(std::is_same<list_t::tail, conslist<type<2>, type<3> > >::value, "");

        static_assert(std::is_same<list_t::reverse, conslist<type<3>, type<2>, type<1> > >::value, "");

        static_assert(std::is_same<list_t::apply<Template>, Template<type<1>, type<2>, type<3> > >::value, "");

        static_assert(std::is_same
                      <
                          list_t::lfold_with<Template, void***>,
                          Template<void***, Template< type<1>, Template<type<2>, type<3> > > >
                      >::value, "");

        static_assert(std::is_same
                      <
                          list_t::rfold_with<Template, void***>,
                          Template< type<1>, Template< type<2>, Template< type<3>, void***> > >
                      >::value, "");

        static_assert(std::is_same
                      <
                          list_t::lfold_with<Template, void***>,
                          aml::lfold< list_t, aml::function<Template>, void*** >
                      >::value, "");

        static_assert(std::is_same
                      <
                          list_t::rfold_with<Template, void***>,
                          aml::rfold< list_t, aml::function<Template>, void*** >
                      >::value, "");

    }

    template<auto >
    struct idx;

    void test_join()
    {
        using list_0 = aml::conslist<>;
        using list_1 = aml::conslist<idx<1> >;
        using list_2 = aml::conslist<idx<2>, idx<3> >;
        using list_3 = aml::conslist<idx<4>, idx<5>, idx<6> >;

        using j_0 = aml::conslist<>::join<>;
        using j_1 = aml::conslist<>::join<list_0>;
        using j_2 = aml::conslist<>::join<list_0, list_0>;
        using j_3 = aml::conslist<>::join<list_0, list_1>;
        using j_4 = aml::conslist<>::join<list_1, list_2, list_3>;
        using j_5 = aml::conslist<>::join<list_0, list_1, list_2, list_3>;

        static_assert(std::is_same<j_0, aml::conslist<>>::value, "");
        static_assert(std::is_same<j_1, aml::conslist<>>::value, "");
        static_assert(std::is_same<j_2, aml::conslist<>>::value, "");
        static_assert(std::is_same<j_3, aml::conslist<idx<1> > >::value, "");
        static_assert(std::is_same<j_4, aml::conslist<idx<1>, idx<2>, idx<3>, idx<4>, idx<5>, idx<6> > >::value, "");
        static_assert(std::is_same<j_5, j_4>::value, "");

        static_assert(std::is_same< aml::join<list_0, list_1, list_2, list_3>, j_4 >::value, "");
    }

}

#include <iostream>
#include <string>

int main()
{
    void (*test_set[])() =
    {
        test::conslist::test_empty_list,
        test::conslist::test_non_empty_list,
        test::conslist::test_join

    };

    for ( auto test : test_set )
        test();

    std::cout << __FILE__ << ": " << sizeof(test_set)/sizeof(test_set[0])  << " tests passed." << std::endl;

}

max_stars_repo_name: aandriko/libaml

max_issues_repo_name: aandriko/libaml

max_forks_repo_name: aandriko/libaml


text:
// Copyright (c) 2011-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "chainparams.h"
#include "consensus/validation.h"
#include "main.h"
#include "miner.h"
#include "pubkey.h"
#include "random.h"
#include "uint256.h"
#include "util.h"

#include "test/test_bitcoin.h"

#include <boost/atomic.hpp>
#include <boost/test/unit_test.hpp>

extern boost::atomic<uint32_t> sizeForkTime;

BOOST_FIXTURE_TEST_SUITE(block_size_tests, TestingSetup)

// Fill block with dummy transactions until it's serialized size is exactly nSize
static void
FillBlock(CBlock& block, unsigned int nSize)
{
    assert(block.vtx.size() > 0); // Start with at least a coinbase

    unsigned int nBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
    if (nBlockSize > nSize) {
        block.vtx.resize(1); // passed in block is too big, start with just coinbase
        nBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
    }

    CMutableTransaction tx;
    tx.vin.resize(1);
    tx.vin[0].scriptSig = CScript() << OP_11;
    tx.vin[0].prevout.hash = block.vtx[0].GetHash(); // passes CheckBlock, would fail if we checked inputs.
    tx.vin[0].prevout.n = 0;
    tx.vout.resize(1);
    tx.vout[0].nValue = 1LL;
    tx.vout[0].scriptPubKey = block.vtx[0].vout[0].scriptPubKey;

    unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
    block.vtx.reserve(1+nSize/nTxSize);

    // ... add copies of tx to the block to get close to nSize:
    while (nBlockSize+nTxSize < nSize) {
        block.vtx.push_back(tx);
        nBlockSize += nTxSize;
        tx.vin[0].prevout.hash = GetRandHash(); // Just to make each tx unique
    }
    // Make the last transaction exactly the right size by making the scriptSig bigger.
    block.vtx.pop_back();
    nBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
    unsigned int nFill = nSize - nBlockSize - nTxSize;
    for (unsigned int i = 0; i < nFill; i++)
        tx.vin[0].scriptSig << OP_11;
    block.vtx.push_back(tx);
    nBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION);
    assert(nBlockSize == nSize);
}

static bool TestCheckBlock(CBlock& block, uint64_t nTime, unsigned int nSize)
{
    SetMockTime(nTime);
    block.nTime = nTime;
    FillBlock(block, nSize);
    CValidationState validationState;
    bool fResult = CheckBlock(block, validationState, false, false) && validationState.IsValid();
    SetMockTime(0);
    return fResult;
}

//
// Unit test CheckBlock() for conditions around the block size hard fork
//
BOOST_AUTO_TEST_CASE(BigBlockFork_Time1)
{
    CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
    CBlockTemplate *pblocktemplate;

    uint64_t t = GetTime();
    uint64_t preforkSize = OLD_MAX_BLOCK_SIZE;
    uint64_t postforkSize = MAX_BLOCK_SIZE;
    uint64_t tActivate = t;

    sizeForkTime.store(tActivate);

    LOCK(cs_main);

    BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
    CBlock *pblock = &pblocktemplate->block;

    // Before fork time...
    BOOST_CHECK(TestCheckBlock(*pblock, t-1LL, preforkSize)); // 1MB : valid
    BOOST_CHECK(!TestCheckBlock(*pblock, t-1LL, preforkSize+1)); // >1MB : invalid
    BOOST_CHECK(!TestCheckBlock(*pblock, t-1LL, postforkSize)); // big : invalid

    // Exactly at fork time...
    BOOST_CHECK(TestCheckBlock(*pblock, t, preforkSize)); // 1MB : valid
    BOOST_CHECK(TestCheckBlock(*pblock, t, postforkSize)); // big : valid
    BOOST_CHECK(!TestCheckBlock(*pblock, t,  postforkSize+1)); // big+1 : invalid

    // After fork time...
    BOOST_CHECK(TestCheckBlock(*pblock, t+11000, preforkSize)); // 1MB : valid
    BOOST_CHECK(TestCheckBlock(*pblock, t+11000, postforkSize)); // big : valid
    BOOST_CHECK(!TestCheckBlock(*pblock, t+11000,  postforkSize+1)); // big+1 : invalid

    sizeForkTime.store(std::numeric_limits<uint32_t>::max());
}

// Test activation time 30 days after earliest possible:
BOOST_AUTO_TEST_CASE(BigBlockFork_Time2)
{
    CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
    CBlockTemplate *pblocktemplate;

    uint64_t t = GetTime();
    uint64_t preforkSize = OLD_MAX_BLOCK_SIZE;
    uint64_t postforkSize = MAX_BLOCK_SIZE;

    uint64_t tActivate = t+60*60*24*30;
    sizeForkTime.store(tActivate);

    LOCK(cs_main);

    BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
    CBlock *pblock = &pblocktemplate->block;

    // Exactly at fork time...
    BOOST_CHECK(TestCheckBlock(*pblock, t, preforkSize)); // 1MB : valid
    BOOST_CHECK(!TestCheckBlock(*pblock, t, postforkSize)); // big : invalid

    // Exactly at activation time....
    BOOST_CHECK(TestCheckBlock(*pblock, tActivate, preforkSize)); // 1MB : valid
    BOOST_CHECK(TestCheckBlock(*pblock, tActivate, postforkSize)); // big : valid

    sizeForkTime.store(std::numeric_limits<uint32_t>::max());
}

// Test: no miner consensus, no big blocks:
BOOST_AUTO_TEST_CASE(BigBlockFork_NoActivation)
{
    CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
    CBlockTemplate *pblocktemplate;

    uint64_t t = GetTime();
    uint64_t preforkSize = OLD_MAX_BLOCK_SIZE;
    uint64_t postforkSize = MAX_BLOCK_SIZE;

    LOCK(cs_main);

    BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
    CBlock *pblock = &pblocktemplate->block;

    // Exactly at fork time...
    BOOST_CHECK(TestCheckBlock(*pblock, t, preforkSize)); // 1MB : valid
    BOOST_CHECK(!TestCheckBlock(*pblock, t, postforkSize)); // big : invalid

    uint64_t tAfter = t+11000;
    BOOST_CHECK(!TestCheckBlock(*pblock, tAfter, postforkSize));
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: BitcoinNext/BitcoinNext

max_issues_repo_name: notbitcoinclassic/notbitcoinclassic

max_forks_repo_name: notbitcoinclassic/notbitcoinclassic


text:
//
// Created by Davide  Caroselli on 07/09/16.
//

#include <cstddef>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
#include <db/NGramStorage.h>
#include <sys/time.h>
#include <corpus/CorpusReader.h>
#ifdef _OPENMP
#include <thread>
#include <omp.h>
#endif

using namespace std;
using namespace mmt;
using namespace mmt::ilm;

namespace {
    const size_t ERROR_IN_COMMAND_LINE = 1;
    const size_t GENERIC_ERROR = 2;
    const size_t SUCCESS = 0;

    struct args_t {
        string model_path;
        string input_path;
        uint8_t order = 0x5;

        size_t buffer_size = 100000;
    };
} // namespace

namespace po = boost::program_options;
namespace fs = boost::filesystem;

bool ParseArgs(int argc, const char *argv[], args_t *args) {
    po::options_description desc("Train an adaptive language model from scratch");
    desc.add_options()
            ("help,h", "print this help message")
            ("model,m", po::value<string>()->required(), "output model path")
            ("input,i", po::value<string>()->required(), "input folder with input corpora")
            ("order,o", po::value<size_t>(), "the language model order (default is 5)")
            ("buffer,b", po::value<size_t>(), "size of the buffer expressed in number of n-grams");

    po::variables_map vm;
    try {
        po::store(po::parse_command_line(argc, argv, desc), vm);

        if (vm.count("help")) {
            std::cout << desc << std::endl;
            return false;
        }

        po::notify(vm);

        args->model_path = vm["model"].as<string>();
        args->input_path = vm["input"].as<string>();

        if (vm.count("buffer"))
            args->buffer_size = vm["buffer"].as<size_t>();

        if (vm.count("order"))
            args->order = (uint8_t) vm["order"].as<size_t>();

    } catch (po::error &e) {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        std::cerr << desc << std::endl;
        return false;
    }

    return true;
}

void ListCorpora(const string &root, vector<string> &outCorpora) {
    fs::recursive_directory_iterator endit;

    for (fs::recursive_directory_iterator it(root); it != endit; ++it) {
        if (fs::is_regular_file(*it))
            outCorpora.push_back(fs::absolute(it->path()).string());
    }
}

double GetTime() {
    struct timeval time;

    if (gettimeofday(&time, NULL)) {
        //  Handle error
        return 0;
    }

    return (double) time.tv_sec + ((double) time.tv_usec / 1000000.);
}

void LoadCorpus(const string &corpus, NGramStorage &storage, uint8_t order, size_t buffer_size) {
    domain_t domain = (domain_t) stoi(fs::path(corpus).stem().string());

    CorpusReader reader(corpus);
    NGramBatch batch(order, buffer_size);
    size_t batches=1;
    vector<wid_t> sentence;
    while(reader.Read(sentence)) {
        if (!batch.Add(domain, sentence)) {
            storage.PutBatch(batch);

            ++batches;
            batch.Clear();
            batch.Add(domain, sentence);
        }
    }

    if (!batch.IsEmpty()) {
        storage.PutBatch(batch);
        batch.Clear();
    }
    cerr << "loading domain:" << domain << " requires " << batches << " batches" << endl;
}

int main(int argc, const char *argv[]) {
#ifdef _OPENMP
    int threads = std::min((thread::hardware_concurrency() * 2) / 3, 8U);

    omp_set_dynamic(0);
    omp_set_num_threads(threads);
#endif

    args_t args;

    if (!ParseArgs(argc, argv, &args))
        return ERROR_IN_COMMAND_LINE;

    if (!fs::exists(args.input_path) || !fs::is_directory(args.input_path)) {
        cerr << "ERROR: input path is not a valid directory" << endl;
        return GENERIC_ERROR;
    }

    if (!fs::is_directory(args.model_path))
        fs::create_directories(args.model_path);

    vector<string> corpora;
    ListCorpora(args.input_path, corpora);

    NGramStorage storage(args.model_path, args.order, true);

#pragma omp parallel for schedule(dynamic)
    for (size_t i = 0; i < corpora.size(); ++i) {
        string &corpus = corpora[i];
        double begin = GetTime();
        LoadCorpus(corpus, storage, args.order, args.buffer_size);
        double elapsed = GetTime() - begin;
        cout << "Corpus " << corpus << " DONE in " << elapsed << "s" << endl;
    }

    storage.ForceCompaction();
    return SUCCESS;
}

max_stars_repo_name: ugermann/MMT

max_issues_repo_name: ugermann/MMT

max_forks_repo_name: ugermann/MMT