Exemple #1
0
// test vector's functions
int main(int argc, char **argv) {
  size_t i;
  vector_t v = VectorCreate(INITIAL_SIZE);

  if (v == NULL)
    return EXIT_FAILURE;

  for (i = 0; i < FINAL_SIZE; ++i) {
    int *x = (int*)malloc(sizeof(int));
    if (x == NULL)
      return EXIT_FAILURE;

    *x = FINAL_SIZE - i;
    element_t old;

    // set the value to the position i in the vector
    // terminate the program if VectorSet failed
    if (!(VectorSet(v, i, x, &old)))
      return EXIT_FAILURE;
  }

  PrintIntVector(v);

  // free the values stored in the vector
  for (i = 0; i < VectorLength(v); ++i)
    free(VectorGet(v, i));
  // free the vector
  VectorFree(v);

  return EXIT_SUCCESS;
}
Exemple #2
0
int main(int argc, const char * argv[]) {
  VectorRef vector = VectorCreate(3.0, 4.0);
  VectorMessage(vector, "Add", 4.0);
  printf("{%g, %g}\n", VectorGetX(vector), VectorGetY(vector));
  VectorMessage(vector, "Multiply", 3.0);
  printf("{%g, %g}\n", VectorGetX(vector), VectorGetY(vector));
  return 0;
}
Exemple #3
0
void GenericSortEmptyVector(ADTErr(*SortFunc)(Vector* _vec))
{
	Vector* vec = NULL; 
	
	vec = VectorCreate(100, 100);
	
	SortFunc(vec);
	printf("SortEmptyVector..................OK\n");
	
	VectorDestroy(vec);
}
Exemple #4
0
void CountingSortEmptyVector()
{
	Vector* vec = NULL; 
	
	vec = VectorCreate(100, 100);
	
	CountingSort(vec, 100);
	
	printf("SortEmptyVector..................OK\n");
	
	VectorDestroy(vec);
}
Exemple #5
0
void RadixSortEmptyVector()
{
	Vector* vec = NULL; 
	
	vec = VectorCreate(100, 100);
	
	RadixSort(vec, 1);
	
	printf("SortEmptyVector..................OK\n");
	
	VectorDestroy(vec);
}
PtVector VectorCopy (PtVector pvec)
{
  PtVector Copy; int I;

  /* verifica se o vector existe */
  if (pvec == NULL) { Error = NO_VECTOR; return NULL; }

  /* criação do vector copia nulo */
  if ((Copy = VectorCreate (pvec->NElem)) == NULL) return NULL;

  /* fazer a copia do vector */
  for (I = 0; I < pvec->NElem; I++) Copy->Vector[I] = pvec->Vector[I];

  return Copy;  /* devolve o vector copia */
}
Exemple #7
0
int *PM1_INSERT(int log2size){
	int *hdVector = VectorCreate();
	int iter = (1 << log2size) - 2;

	int i;
//	print("\n");
	for(i = 0; i < iter; i++){
	//	putnum(i);
	//	print(" ");
		hdVector = VectorPushBack(hdVector, i);		
	//	putnum(hdVector);
	//	print("\n");
	}
	return hdVector;
}
Exemple #8
0
/**
 * <p>
 * The given string contains a list of substrings separated by the
 * specified delimiter characters. The substrings may contain quoted
 * strings and/or contain backslash-escaped characters. The common
 * backslash escape sequences are supported and return their ASCII
 * values.
 * </p>
 *
 * @param string
 *	A list represented as a string.
 *
 * @param delims
 *	A set of delimiter characters.
 *
 * @param flags
 *
 *	TOKEN_KEEP_EMPTY
 *
 *	If false, then a run of one or more delimeters is treated as a
 *	single delimeter separating tokens. Otherwise each delimeter
 *	separates a token that may be empty.
 *
 *	string		true		false
 *	-------------------------------------------
 *	[a,b,c]		[a] [b] [c]	[a] [b] [c]
 *	[a,,c]		[a] [] [c]	[a] [c]
 *	[a,,]		[a] [] [] 	[a]
 *	[,,]		[] [] []	(null)
 *	[]		[]		(null)
 *
 *	TOKEN_KEEP_BACKSLASH
 *
 *	The token might have backslash escapes that are suppose to be
 *	part of the token, like a regex string /RE/ where you need to
 *	keep any "\/" between the open and closing slashes. We still
 *	need to recognise escapes and not convert them to a literal.
 *
 *	TOKEN_IGNORE_QUOTES
 *
 *	Disable any special processing of quoted substrings; quotes
 *	are treated as literals.
 *
 *	TOKEN_KEEP_ASIS
 *
 *	Shorthand for TOKEN_KEEP_BACKSLASH | TOKEN_IGNORE_QUOTES.
 *
 *	TOKEN_KEEP_BRACKETS
 *
 *	Split strings with brackets, keeping the open and close:
 *	parenthesis, "(" and ")"; angle brackets, "<" and ">"; square
 *	brackets, "[" and "]"; and/or braces, "{" and "}" grouped
 *	together. Both open and close brackets must in the set of
 *	delimiters. For example:
 *
 *	string		delims	vector
 *	-------------------------------------------
 *	"a{b}c"		"{}"	"a", "{b}", "c"
 *	"a{{b}}c"	"{}"	"a", "{{b}}", "c"
 *	"a{{b\{c}}d"	"{}"	"a", "{{b{c}}", "d"
 *	"a{{b[(<c}}d"	"{}"	"a", "{{b[(<c}}", "d"
 *	"a{b{c}{d}e}f"	"{}"	"a", "{b{c}{d}e}", "f"
 *	"<>a{b<c>d}<e>"	"{}<>"	"<>", "a", "{b<c>d}", "<e>", ""
 *
 * @return
 *	A vector of C strings.
 */
Vector
TextSplit(const char *string, const char *delims, int flags)
{
	char *token;
	Vector list;

	if ((list = VectorCreate(5)) == NULL)
		return NULL;

	VectorSetDestroyEntry(list, free);

	while ((token = TokenNext(string, &string, delims, flags)) != NULL)
		(void) VectorAdd(list, token);

	return list;
}
Exemple #9
0
void PETScLinearSolver::Init(const int *sparse_index)
{
   if(sparse_index)
   {
      d_nz = sparse_index[0];
      o_nz = sparse_index[1];
      nz = sparse_index[2]; 
      m_size_loc = sparse_index[3];
   }   
    
   VectorCreate(m_size);   
   MatrixCreate(m_size, m_size);

   global_x = new PetscScalar[m_size];

}
PtVector  VectorMultiplication (PtVector pvec1, PtVector pvec2)
{
  PtVector Mult; int I;

  /* validação dos vectores */
  if (!ValidVectors (pvec1, pvec2)) return NULL;

  /* criação do vector produto nulo */
  if ((Mult = VectorCreate (pvec1->NElem)) == NULL) return NULL;

  /* produto dos dois vectores */
  for (I = 0; I < pvec1->NElem; I++)
    Mult->Vector[I] = pvec1->Vector[I] * pvec2->Vector[I];

  return Mult;  /* devolve o vector produto */
}
PtVector  VectorSubtraction (PtVector pvec1, PtVector pvec2)
{
  PtVector Sub; int I;

  /* validação dos vectores */
  if (!ValidVectors (pvec1, pvec2)) return NULL;

  /* criação do vector diferença nulo */
  if ((Sub = VectorCreate (pvec1->NElem)) == NULL) return NULL;

  /* diferença dos dois vectores */
  for (I = 0; I < pvec1->NElem; I++)
    Sub->Vector[I] = pvec1->Vector[I] - pvec2->Vector[I];

  return Sub;  /* devolve o vector diferença */
}
PtVector  VectorAddition (PtVector pvec1, PtVector pvec2)
{
  PtVector Add; int I;

  /* validação dos vectores */
  if (!ValidVectors (pvec1, pvec2)) return NULL;

  /* criação do vector soma nulo */
  if ((Add = VectorCreate (pvec1->NElem)) == NULL) return NULL;

  /* soma dos dois vectores */
  for (I = 0; I < pvec1->NElem; I++)
    Add->Vector[I] = pvec1->Vector[I] + pvec2->Vector[I];

  return Add;  /* devolve o vector soma */
}
PtVector VectorCreateFile (char *pnomef)
{
  PtVector Vector; FILE *PtF; unsigned int NElem, I;

  /* abertura com validacao do ficheiro para leitura */
  if ( (PtF = fopen (pnomef, "r")) == NULL) { Error = NO_FILE; return NULL; }

  /* leitura da dimensão do vector do ficheiro e criação do vector */
  fscanf (PtF, "%u", &NElem);
  if (NElem < 1) { Error = BAD_SIZE; fclose (PtF); return NULL; }

  if ((Vector = VectorCreate (NElem)) == NULL) { fclose (PtF); return NULL; }

  /* leitura das componentes do vector do ficheiro */
  for (I = 0; I < NElem; I++) fscanf (PtF, "%lf", Vector->Vector+I);

  fclose (PtF);  /* fecho do ficheiro */

  return Vector;  /* devolve o vector criado */
}
Exemple #14
0
int main(int argc, char *argv[]) {
  uint32_t i;
  vector_t v = VectorCreate(4);

  if (v == NULL)
    return EXIT_FAILURE;

  for (i = 0; i < N; ++i) { // Place some elements in the vector.
    int *x = (int*)malloc(sizeof(int));
    element_t old;
    VectorSet(v, i, x, &old);
  }

  PrintIntVector(v);


  

  return EXIT_SUCCESS;
}
Exemple #15
0
void MassiveGenericSortTestWithNegatives(ADTErr(*SortFunc)(Vector* _vec))
{
	int vectorSize, i, j;
	int num1, num2;
	Vector* vec;
	srand(time(NULL));
	
	for(j = 0; j < RUNS; j++)
	{
		
		vectorSize = rand() % MAX_VECTOR_SIZE;

		vec = VectorCreate(vectorSize, 100);
		

		for(i = 0; i < vectorSize; i++)
		{
			i % 2 == 0 ? VectorAdd(vec, rand() % MAX_NUMBER) : VectorAdd(vec, - rand() % MAX_NUMBER);
		}

		SortFunc(vec);

		VectorDelete(vec, &num1);

		for(i = 0; i < vectorSize - 1; i++)
		{
			VectorDelete(vec, &num2);
			if(num2 > num1)
			{
				printf("MassiveSortTestWithNegatives..................FAIL\n");
				VectorDestroy(vec);
				return;
			}
			num1 = num2;
		}

		VectorDestroy(vec);
	}

	printf("MassiveSortTestWithNegatives..................OK\n");
}
Exemple #16
0
void MassiveRadixSortTest()
{
	int vectorSize, i, j;
	int num1, num2;
	Vector* vec;
	srand(time(NULL));
	
	for(j = 0; j < RUNS; j++)
	{
		
		vectorSize = rand() % MAX_VECTOR_SIZE;

		vec = VectorCreate(vectorSize, 100);

		for(i = 0; i < vectorSize; i++)
		{
			VectorAdd(vec, rand() % MAX_NUMBER);
		}

		RadixSort(vec, 3);

		
		VectorDelete(vec, &num1);

		for(i = 0; i < vectorSize - 1; i++)
		{
			VectorDelete(vec, &num2);
			if(num2 > num1)
			{
				printf("MassiveSortTest..................FAIL\n");
				VectorDestroy(vec);
				return;
			}
			num1 = num2;
		}

		VectorDestroy(vec);
	}

	printf("MassiveSortTest..................OK\n");
}
Exemple #17
0
static
ISTATUS
SphereComputeBounds(
    _In_ const void *context,
    _In_opt_ PCMATRIX model_to_world,
    _Out_ PBOUNDING_BOX world_bounds
    )
{
    PCSPHERE sphere = (PCSPHERE)context;

    float_t radius = sqrt(sphere->radius_squared);
    VECTOR3 to_bound = VectorCreate(radius, radius, radius);

    POINT3 lower_bound = PointVectorSubtract(sphere->center, to_bound);
    POINT3 upper_bound = PointVectorAdd(sphere->center, to_bound);

    BOUNDING_BOX object_bounds = BoundingBoxCreate(lower_bound, upper_bound);
    *world_bounds = BoundingBoxTransform(model_to_world, object_bounds);

    return ISTATUS_SUCCESS;
}
Exemple #18
0
void SimpleGenericSort(ADTErr(*SortFunc)(Vector* _vec))
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 46);
	VectorAdd(vec, 2);
	VectorAdd(vec, 83);
	VectorAdd(vec, 41);
	VectorAdd(vec, 102);
	VectorAdd(vec, 5);
	VectorAdd(vec, 17);
	VectorAdd(vec, 31);
	VectorAdd(vec, 64);
	VectorAdd(vec, 49);
	VectorAdd(vec, 18);

	/*VectorPrint(vec);*/

	SortFunc(vec);
	
	/*VectorPrint(vec);*/
	
	VectorDelete(vec, &a);
	
	if(a == 102)
	{
		printf("SimpleSort..................OK\n");
	}	
	else
	{
		printf("SimpleSort..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Exemple #19
0
void SimpleRadixSort()
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 46);
	VectorAdd(vec, 2);
	VectorAdd(vec, 83);
	VectorAdd(vec, 41);
	VectorAdd(vec, 102);
	VectorAdd(vec, 5);
	VectorAdd(vec, 17);
	VectorAdd(vec, 31);
	VectorAdd(vec, 64);
	VectorAdd(vec, 49);
	VectorAdd(vec, 18);

	RadixSort(vec, 3);
	


	VectorDelete(vec, &a);
	VectorDelete(vec, &a);
	VectorDelete(vec, &a);
	
	if(a == 64)
	{
		printf("SimpleSort..................OK\n");
	}	
	else
	{
		printf("SimpleSort..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Exemple #20
0
Vector
TextSplitOnC(Text self, const char *delims, int returnEmptyTokens)
{
	Text token;
	Vector list;

	if ((list = VectorCreate(5)) == NULL)
		return NULL;

	TextResetTokens(self);

	while ((token = TextNextTokenC(self, delims, returnEmptyTokens)) != NULL) {
		if (VectorAdd(list, token)) {
			VectorDestroy(list);
			/*@-kepttrans@*/
			TextDestroy(token);
			/*@=kepttrans@*/
			return NULL;
		}
	}

	return list;
}
Exemple #21
0
void SimpleRadixSortForSingleMember()
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 5);

	RadixSort(vec, 1);
	
	VectorDelete(vec, &a);
	
	if(a == 5)
	{
		printf("SimpleSortForSingleMember..................OK\n");
	}	
	else
	{
		printf("SimpleSortForSingleMember..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Exemple #22
0
void SimpleGenericSortForSingleMember(ADTErr(*SortFunc)(Vector* _vec))
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 5);

	SortFunc(vec);
	
	VectorDelete(vec, &a);
	
	if(a == 5)
	{
		printf("SimpleSortForSingleMember..................OK\n");
	}	
	else
	{
		printf("SimpleSortForSingleMember..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Exemple #23
0
void TimeCompare()
{
	Vector* vec;
	int i, functionsRun, sizes;
	clock_t start, end; 
	float seconds;
	ADTErr(*functions[9])(Vector* _vec) = {BubbleSort, ShakeSort, QuickSort, QuickSort_Iter, InsertionSort, ShellSort, MergeSort, MergeSort_Iter, SelectionSort};
	int vectorSizes[4] = {100, 100, 100, 100};/*TODO check for 10000*/
	
	printf("\nRunning time compare.\n");

	for(sizes = 0; sizes < 4; sizes++)
	{
		printf("\nFor vector of %d integers:\n\n", vectorSizes[sizes]);	
	
		for(functionsRun = 0; functionsRun < 11; functionsRun++)
		{
			vec = VectorCreate(100, 100);

			for(i = 0; i < vectorSizes[sizes]; i++)
			{
				VectorAdd(vec, rand() % 1000);
			}

			if(functionsRun == 9)
			{
				start = clock();
				CountingSort(vec, MAX_NUMBER);
				end = clock();
				seconds = (float)(end - start) / CLOCKS_PER_SEC;
			}
			else if(functionsRun == 10)
			{
				start = clock();
				CountingSort(vec, 3);
				end = clock();
				seconds = (float)(end - start) / CLOCKS_PER_SEC;
			}
			else 
			{
				start = clock();
				functions[functionsRun](vec);
				end = clock();
				seconds = (float)(end - start) / CLOCKS_PER_SEC;
			}

			switch(functionsRun)
			{
				case 0:
					printf("BubbleSort: %f\n", seconds);
					break;
				case 1:
					printf("ShakeSort: %f\n", seconds);
					break;
				case 2:
					printf("QuickSort: %f\n", seconds);
					break;
				case 3:
					printf("QuickSort_Iter: %f\n", seconds);
					break;
				case 4:
					printf("InsertionSort: %f\n", seconds);
					break;
				case 5:
					printf("ShellSort: %f\n", seconds);
					break;
				case 6:
					printf("MergeSort: %f\n", seconds);
					break;
				case 7:
					printf("MergeSort - Iterative: %f\n", seconds);
					break;
				case 8:
					printf("SelectionSort: %f\n", seconds);
					break;
				case 9:
					printf("Counting: %f\n", seconds);
					break;
				case 10:
					printf("Radix: %f\n", seconds);
					break;
			}

			VectorDestroy(vec);
		}
	}
}
Exemple #24
0
static void init()
{
    symbols = VectorCreate();
    blocks = VectorCreate();
}