예제 #1
0
//Optimizes the Append function by resizing the array by the power of 2. This reduces the number of resizing tasks.
int64_t* optimized_append(int64_t* op_1, size_t* op_1_size, int64_t* op_2, size_t* op_2_size, size_t* ret_size) {
	int64_t *ret;
	size_t allocated_size;
	//Assign the res with op. That means both of them address to same memory location. In other word, copy the array.
	ret = op_1;
	//Check if the size is a power of 2. If so, then reallocate the array size.
	if (isPowerof2(*op_1_size)) {
		allocated_size = 2;
		//Realloc the array size of 'res'. Allocate the array size by power of 2.
		while (allocated_size < (*op_1_size + *op_2_size)) {
			allocated_size *= 2;
		}
		ret = (int64_t*) realloc(ret, allocated_size * sizeof(int64_t));
		if (ret == NULL) {
			fputs("fail to realloc at optimized_append functon in Util.c\n", stderr);
			exit(-2);
		}
	}
	//Update the item in the appended list.
	for (size_t i = 0; i < *op_2_size; i++) {
		ret[*op_1_size + i] = op_2[i];
	}
	*ret_size = *op_1_size + *op_2_size;
	return ret;
}
예제 #2
0
int main()
{
	const char* string = (char *)malloc(50);
	const char* sub = (char *)malloc(19);
	int a = 8; float b = 9.4444; int c = 2;
	int d;
	printf("Before swapping: a = %d c = %d\n", a , c);
	a = a + c;
	c = a - c;
	a = a - c;
	printf("After swapping: a = %d c = %d\n", a , c);
	string = "This is the company namded apple inc";
	sub = "is";
	printf("Substring: %d\n", isSubstring(string, sub));
	printf("Position of the MSB is: %d\n", msbPos(3));
	printf("Is bit set: %d\n", isBitset(8,4));
	printf("%x to little endian %x\n", a, BigToLittle(a));
	printf("%d is a power of 2: %d\n", a ,isPowerof2(a));
	printf("%s after reversed: %s\n", string, reverse(string));
	printf("%s is palindrome: %d\n", string, isPalindrome(string));
	printf("%d is a prime number: %d\n",a, isPrimeNumber(a));
	primeNumber(20);
	printf("\nFibonacci Series: ");
	fibonacci(15);
	printf("\n");
	printf("Factorial of %d: %d\n", a, factorial(a));
	printf("%s after reversing: %s\n", string, reverseString(string));
	printf("Nearest int of %f is: %d\n", b, nearestInt(b));
	printf("%d", (-1%8));
	return 0;
}