All pastes #1733121 Raw Edit

codegen.pwn

public cpp v1 · immutable
#1733121 ·published 2009-12-31 15:40 UTC
rendered paste body
#include <a_samp>new	gValues[] = {166, 165, 164, 163, 280, 281, 282, 283, 288, 284, 285, 287, 57, 265, 266, 267};#define NL "\r\n"#define TAB "\t"#define TYPE "stock bool:" NL TAB// The number of skipped complete cells before adding a lower limit.#define SMALL_LIMIT (5)main(){	// This program generates code to check if a number is in an array VERY	// quickly, for example for a list of vehicle models which are cop cars.	// The parameters are:	// filename, function name, values to check for, default return value.	GenerateList("codeout.pwn", "IsACivillianSkin", gValues, false);}/*----------------------------------------------------------------------------*-Function:	QuickSortParams:	numbers[] - Array of values you want sorting.	arraySize - Number of items.Return:	-Notes:	Entry point for the recursive QSort function-*----------------------------------------------------------------------------*/#define QuickSort(%1,%2) \	QSort((%1), 0, (%2) - 1)GenerateList(const fname[], const func[], values[], const bool:ret, const size = sizeof (values)){	new		File:fhnd = fopen(fname, io_write);	if (fhnd)	{		// First, sort the array.		QuickSort(values, size);		new			numstr[11],			// Then find the largest value.			largest = values[size - 1],			// Now find the smallest value - this MAY not be used.			smallest = values[0];		// Write the function header		fwrite(fhnd, TYPE);		fwrite(fhnd, func);		fwrite(fhnd, "(val)" NL "{" NL TAB "static" NL TAB TAB "s_iValArray[] = " NL TAB TAB "{" NL TAB TAB TAB);		// Shift the array if required.		if (smallest / cellbits > SMALL_LIMIT)		{			for (new i = 0; i != size; ++i)			{				values[i] -= smallest;			}		}		// Write the array of values.		WriteNumbers(fhnd, values, size);		// Do some basic pre-processing.		fwrite(fhnd, NL TAB TAB "};" NL TAB "if (val > ");		valstr(numstr, largest);		fwrite(fhnd, numstr);		fwrite(fhnd, ")" NL TAB "{" NL TAB TAB "return ");		fwrite(fhnd, ret ? ("false") : ("true"));		fwrite(fhnd, ";" NL TAB "}" NL TAB);		// Do some advanced pre-processing.		if (smallest / cellbits > SMALL_LIMIT)		{			fwrite(fhnd, "val -= ");			valstr(numstr, smallest);			fwrite(fhnd, numstr);			fwrite(fhnd, ";" NL TAB "if (val < 0)" NL TAB "{" NL TAB TAB "return ");			fwrite(fhnd, ret ? ("false") : ("true"));			fwrite(fhnd, ";" NL TAB "}" NL TAB);		}		fwrite(fhnd, "return ");		fwrite(fhnd, ret ? ("!") : (""));		fwrite(fhnd, "!(s_iValArray[val >> 5] & (1 << (val & 0x1F)));" NL "}" NL);		fclose(fhnd);	}	else	{		printf("Codegen error: Failed to open file \"%s\".", fname);	}}WriteNumbers(const File:fhnd, const values[], const size){	new		numstr[11],		linenums = 0,		curidx = 0,		curval,		write = 0;	for (new i = 0; i != size; ++i)	{		if (curidx != values[i] >> 5)		{			format(numstr, sizeof (numstr), "%10d", curval);			fwrite(fhnd, numstr);			curidx = values[i] >> 5;			curval = 0;			write = 0;			if (++linenums == 5)			{				fwrite(fhnd, "," NL TAB TAB TAB);				linenums = 0;			}			else			{				fwrite(fhnd, ", ");			}		}		curval |= 1 << (values[i] & 0x1F);		++write;	}	if (write)	{		format(numstr, sizeof (numstr), "%10d", curval);		fwrite(fhnd, numstr);	}}/*----------------------------------------------------------------------------*-Function:	QSortParams:	numbers[] - Array of values to be sorted.	left - Start index in the array.	right - End index in the array.Return:	-Notes:	Adapted from: http://linux.wku.edu/~lamonml/algor/sort/quick.html	Implementation of the quicksort routine.-*----------------------------------------------------------------------------*/stock QSort(numbers[], left, right){	new		pivot = numbers[left],		l_hold = left,		r_hold = right;	while (left < right)	{		while ((numbers[right] >= pivot) && (left < right)) right--;		if (left != right)		{			numbers[left] = numbers[right];			left++;		}		while ((numbers[left] <= pivot) && (left < right)) left++;		if (left != right)		{			numbers[right] = numbers[left];			right--;		}	}	numbers[left] = pivot;	pivot = left;	left = l_hold;	right = r_hold;	if (left < pivot) QSort(numbers, left, pivot - 1);	if (right > pivot) QSort(numbers, pivot + 1, right);}