예제 #1
0
int main(void){

  matrix* A = create_matrix(3, 3);
  value temp_a[9] = { 0, 0, 1,
		      0, 1, 0,
		      1, 0, 0};
  insert_array(temp_a, A);

  matrix* B = create_matrix(3, 1);
  value temp_b[3] = { 0,
		      4,
		      3};
  insert_array(temp_b, B);

  matrix* X = create_matrix(3, 1);

  gauss_jordan_solver(A, X, B);

  /* X should be */
  matrix* solution = create_matrix(3, 1);
  value temp_solution[3] = {3,
			    4,
			    0};
  insert_array(temp_solution, solution);

  assert(compare_matrices(X, solution));

  free_matrix(A);
  free_matrix(B);
  free_matrix(X);
  free_matrix(solution);

  return 0;
}
예제 #2
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_block(block_node * block, scope * scope, str_list * lines, int add_braces) {
	int i;
	node_list * statements = block->stmts;
	if (add_braces)
		insert_array(lines, "{");
	for (i = 0 ; i < statements->used ; i++) {
		compile_statement(statements->array[i], scope, lines);
	}
	if (add_braces)
		insert_array(lines, "}");
	return new_expr_val("(block)", NO_TYPE);
}
예제 #3
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_function(function_node * func, scope * current_scope, str_list * lines) {
	char * orig_name = func->name;
	char * name;

	// Declaring new scope
	scope * n_scope = new_scope(current_scope);
	expr_val * args = compile_statement(func->args, n_scope, lines);
	expr_val * return_type = compile_statement(func->return_type, n_scope, lines);

	// Does func with that name already exist
	if (get_variable_from_scope(current_scope, orig_name)) {
		int len = strlen(orig_name) + 46;
		char * msg = (char *) malloc(len);
		snprintf(msg, len, "Functions name already exists %s", orig_name);
		report_error(msg, 0);
	} else if(strcmp(orig_name, "main") == 0) { // @todo: remove this hack
 		create_variable_in_scope(current_scope, orig_name, return_type->type, FUNC, &var_num);
 		name = orig_name;
	} else {
		name = create_variable_in_scope(current_scope, orig_name, return_type->type, FUNC, &var_num);
	}

	int len = strlen(args->val) + strlen(return_type->val) + strlen(name) + 5;
	char * output = (char *) malloc(len);

	snprintf(output, len, "%s %s%s", return_type->val, name, args->val);

	insert_array(lines, output);
	compile_block(func->block, n_scope, lines, 1);
	return new_expr_val("(function)", NO_TYPE);
}
예제 #4
0
int tabtoh::convert(){
    bool err = 1;
    QString instr_mnemonic;
    QString num_of_instr_str;
    QString temp_line;
    QStringList instr_description;
    QStringList converted_instr_description;
    QStringList instr_names_list;
    int num_of_instr = 0;
    int instr_num_of_lines = 0;

    qDebug()<<"(tabtoh) Counting instructions ...";
    num_of_instr = count_instructions();
    num_of_instr_str = QString::number(num_of_instr, 10);
    qDebug()<<"(tabtoh) Instructions: "<<num_of_instr;

    qDebug()<<"(tabtoh) Extracting instructions ...";
    for(int i = 0; i < input_file.size(); i++){
        if(input_file.at(i).contains("INSTR")){
            err = 0;

            instr_mnemonic = input_file.at(i).section('\t', 1, 1);
            instr_mnemonic.prepend("&");
            instr_names_list << instr_mnemonic;

            for(int j = i; j < input_file.size(); j++){
                instr_description << input_file.at(j);
                instr_num_of_lines++;
                if(is_empty_line(input_file.at(j))){
                    break;
                }
            }

            if(instr_num_of_lines){
                converted_instr_description << parse_and_convert_instr(instr_description);
                instr_num_of_lines = 0;
                instr_description.clear();
            }
        }
    }


    insert_comment("/* Automatically generated by tabtoh */", output_file);
    insert_newline(output_file);
    insert_ifndef("ISA_H_", output_file);
    insert_include("common.h", output_file);
    insert_newline(output_file);
    insert_define("NUM_OF_INSTRUCTIONS", num_of_instr_str.toLatin1().data(), output_file);
    insert_newline(output_file);
    output_file += converted_instr_description;
    insert_newline(output_file);
    insert_array("const instruction_t*", model_name_str.toLatin1().data(), "NUM_OF_INSTRUCTIONS", instr_names_list, output_file);
    insert_endif(output_file);

    if(!err){
        write_entire_file(output_file_str, output_file);
    }

    return err;
}
예제 #5
0
int main(){
  clock_t begin, end;
  double time_spent;
  begin = clock();

  matrix* Q = create_matrix(4, 4);
  value Q_arr[16] = {1, 0, 1, 0,
		     0, 2, 0, 1,
		     1, 0, 2, 0,
		     0, 1, 0, 1};
  insert_array(Q_arr, Q);

  sparse_matrix* s_Q = create_sparse_matrix(Q, 8);

  matrix* q = create_matrix(4, 1);
  value q_arr[4] = {3,
		    20,
		    5,
		    15};
  insert_array(q_arr, q);

  matrix* expected = create_matrix(4, 1);
  value e_arr[4] = {1,
                    5,
                    2,
                    10};
  insert_array(e_arr, expected);

  matrix* x = create_zero_matrix(4, 1);


  conjugate_gradient(s_Q, x, q);

  assert(compare_matrices(x, expected));

  free_matrix(Q);
  free_matrix(q);
  free_matrix(x);
  free_matrix(expected);
  free_sparse_matrix(s_Q);


  end = clock(); 
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("time taken was: %f \n", time_spent);

}
예제 #6
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_return(return_node * ret, scope * scope, str_list * lines) {
	expr_val * expression = compile_statement(ret->expr, scope, lines);

	int len = strlen(expression->val) + 9;
	char * output = (char *) malloc(len);
	snprintf(output, len, "return %s;", expression->val);
	insert_array(lines, output);
	return expression;
}
예제 #7
0
/* Helper for scan_disk.  */
static int
scan_disk_partition_iter (grub_disk_t disk, grub_partition_t p, void *data)
{
  const char *name = data;
  struct grub_diskfilter_vg *arr;
  grub_disk_addr_t start_sector;
  struct grub_diskfilter_pv_id id;
  grub_diskfilter_t diskfilter;

  grub_dprintf ("diskfilter", "Scanning for DISKFILTER devices on disk %s\n",
		name);
#ifdef GRUB_UTIL
  grub_util_info ("Scanning for DISKFILTER devices on disk %s", name);
#endif

  disk->partition = p;
  
  for (arr = array_list; arr != NULL; arr = arr->next)
    {
      struct grub_diskfilter_pv *m;
      for (m = arr->pvs; m; m = m->next)
	if (m->disk && m->disk->id == disk->id
	    && m->disk->dev->id == disk->dev->id
	    && m->part_start == grub_partition_get_start (disk->partition)
	    && m->part_size == grub_disk_get_size (disk))
	  return 0;
    }

  for (diskfilter = grub_diskfilter_list; diskfilter; diskfilter = diskfilter->next)
    {
#ifdef GRUB_UTIL
      grub_util_info ("Scanning for %s devices on disk %s", 
		      diskfilter->name, name);
#endif
      id.uuid = 0;
      id.uuidlen = 0;
      arr = diskfilter->detect (disk, &id, &start_sector);
      if (arr &&
	  (! insert_array (disk, &id, arr, start_sector, diskfilter)))
	{
	  if (id.uuidlen)
	    grub_free (id.uuid);
	  return 0;
	}
      if (arr && id.uuidlen)
	grub_free (id.uuid);

      /* This error usually means it's not diskfilter, no need to display
	 it.  */
      if (grub_errno != GRUB_ERR_OUT_OF_RANGE)
	grub_print_error ();

      grub_errno = GRUB_ERR_NONE;
    }

  return 0;
}
예제 #8
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_if(if_node * stmt, scope * scope, str_list * lines) {
	expr_val * expr = compile_statement(stmt->expr, scope, lines);

	// Random val to generate afterif
	int val = var_num++;
	int var_len = sizeof(char)*(int)log10(val) + 1;

	// Before block
	int before_len = strlen(expr->val) + 21 + var_len;
	char * before = (char *)malloc(before_len);

	snprintf(before, before_len, "if(!%s) goto afterif%d;", expr->val, val);
	insert_array(lines, before);

	expr_val * block = compile_block(stmt->block, scope, lines, 0);

	int after_len = var_len + 10;
	char * after = (char *)malloc(after_len);
	snprintf(after, after_len, "afterif%d:;", val);

	insert_array(lines, after);

	return NULL;
}
예제 #9
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_assignment(binary_op_node * ass, scope * scope, str_list * lines) {
	expr_val * name = compile_statement(ass->left, scope, lines);
	expr_val * val = compile_statement(ass->right, scope, lines);


	if (!determine_type(ass->op, name->type, val->type)) return NULL;

	char * name_value = name->val;
	char * right_value = val->val;

	int len = strlen(name_value) + strlen(right_value) + 5;
	char * output = (char *) malloc(len);

	snprintf(output, len, "%s = %s;", name_value, right_value);
	insert_array(lines, output);
	return new_expr_val(name_value, val->type);
}
예제 #10
0
파일: mem.cpp 프로젝트: koba-e964/ktest
uint memmng_free(MemMng* mem,void* addr,int size)
{
	//freeはaddr順
	//まづ いるべきばしょを みつく
	int ind=0;
	char* start=(char*)addr;
	char* end=start+size;
	
	for(;ind<mem->nFrees;ind++)
	{
		if(mem->free[ind].end < start)//[ind].start [ind].end start end
			continue;
		if(mem->free[ind].end==start)
		{
			mem->free[ind].end=end;
			if(ind<MEMMNG_FREES-1 && end==mem->free[ind+1].start)
			{
				mem->free[ind].end=mem->free[ind+1].end;
				//erase
				erase_array(mem->free,ind+1,ind+2,mem->nFrees,sizeof(FreeInfo));
			}
			return 0;//success
		}
		if(mem->free[ind].start < end)//not start end [ind].start [ind].end
		{
			mem->lostsize+=size;
			mem->losts+=1;
			return (uint)-1;//failure
		}
		if(end==mem->free[ind].start)
		{
			mem->free[ind].start=start;
			return 0;//success
		}
		break;
	}
	FreeInfo insertee={start,end};
	if(0<=insert_array(mem->free,ind,&insertee,1,mem->nFrees,MEMMNG_FREES,sizeof(FreeInfo)))
	{
		mem->nFrees++;
		make_max(mem->maxfrees,mem->nFrees);
		return 0;//success
	}
	return (uint)-1;//failure
}
예제 #11
0
파일: 2019.c 프로젝트: isszombie/Trainning
int main(int argc,char** argv)
{
	int i,m,n;
	int* data;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(m==0&&n==0)
		{
			break;
		}
		data=(int*)malloc((n+1)*sizeof(int));
		for(i=0;i<n;i++)
		{
			scanf("%d",data+i);
		}
		insert_array(data,n,m);
		print_array(data,n+1);
		free(data);
	}
	return 0;
}
예제 #12
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_call(call_node * call, scope * current_scope, str_list * lines) {
	expr_val * name = compile_statement(call->expr, current_scope, lines);
	expr_val * args = compile_statement(call->args, current_scope, lines);

	// Determine function type
	variable * func = get_variable_from_scope_gen_name(current_scope, name->val);

	expr_val * var = get_new_var(func->type, current_scope, lines);

	// Extracting values
	char * name_val = name->val;
	char * args_val = args->val;
	char * var_val = var->val;

	int len = strlen(var_val) + strlen(name_val) + strlen(args_val) + 8;
	char * output = (char *) malloc(len);

	snprintf(output, len, "%s = %s%s;", var_val, name_val, args_val);
	insert_array(lines, output);

	return new_expr_val(var_val, var->type);
}
예제 #13
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_comparison_assignment(binary_op_node * node, scope * scope, str_list * lines) {
	types op = node->op;
	expr_val * left = compile_statement(node->left, scope, lines);
	expr_val * right = compile_statement(node->right, scope, lines);

	char * left_val = left->val;
	char * right_val = right->val;

	expr_val * var = get_new_var(INT_TYPE, scope, lines);
	char * op_str;

	switch (op) {
		case EQUAL:
			op_str = "==";
			break;
		case LESS:
			op_str = "<";
			break;
		case LESS_EQUAL:
			op_str = "<=";
			break;
		case GREATER:
			op_str = ">";
			break;
		case GREATER_EQUAL:
			op_str = ">=";
			break;
		default:
			break;
	}

	int out_len = strlen(left_val) + strlen(right_val) + strlen(op_str) + 11;
	char * output = (char *) malloc(out_len);

	snprintf(output, out_len, "%s = %s %s %s;", var->val, left_val, op_str, right_val);
	insert_array(lines, output);

	return var;
}
예제 #14
0
// The main function.
int main(void)
{
	int i=0, idx=0, tmp;
	int num[100];		/* The numbers to input */

	/* Creating a heap, and initializing it */
	struct _HeapStruct heap;
	HeapInit(&heap);

	while(1)
	{
		scanf("%d", &tmp);
		if(tmp == -1)
			break;
		num[idx++] = tmp;
		insert_array(&heap, tmp);
	}

	buildHeap(&heap);
	print(&heap);

	return 0;
}
예제 #15
0
파일: c_generator.c 프로젝트: tokeloke/tok
expr_val * compile_addition(binary_op_node * node, scope * scope, str_list * lines) {
	int op = node->op;

	expr_val * left = compile_statement(node->left, scope, lines);
	expr_val * right = compile_statement(node->right, scope, lines);

	// Determine if types are compatible
	expr_type type;
	if(!(type = determine_type(op, left->type, right->type))) return NULL;

	expr_val * var = get_new_var(type, scope, lines);

	// Extracting values
	char * var_val = var->val;
	char * left_val = left->val;
	char * right_val = right->val;

	int len = strlen(var_val) + strlen(left_val) + strlen(right_val) + 8;
	char * output = (char *) malloc(len);

	snprintf(output, len, "%s = %s + %s;", var_val, left_val, right_val);
	insert_array(lines, output);
	return new_expr_val(var_val, INT_TYPE);
}
예제 #16
0
파일: c_generator.c 프로젝트: tokeloke/tok
// Instansiate var if it does not exist
expr_val * get_variable(char * var, expr_type type, scope * scope, str_list * lines) {
	// If var does not exist, declare it
	if (!get_variable_from_scope(scope, var)) {
		char * name = create_variable_in_scope(scope, var, type, LOCAL_VAR, &var_num);
		char * type_str;
		switch(type) {
			case INT_TYPE:
				type_str = "int";
				break;
			case CHAR_TYPE:
				type_str = "char";
				break;
			case NO_TYPE:
				break;
			default:
				break;
		}
		char * init_stmt = (char *) malloc(strlen(name) + strlen(type_str) + 2);
		sprintf(init_stmt, "%s %s;", type_str, name);
		insert_array(lines, init_stmt);
		return new_expr_val(name, type);
	}
	return new_expr_val(var, type);
}
예제 #17
0
파일: bot.c 프로젝트: HackerCow/hackerb0t
void process_line(SOCKET socket, char* msg)
{
    // V3:
    //@color=#0000FF;emotes=;subscriber=0;turbo=0;user_type= :bomb_mask!bomb_mask@bomb_mask.tmi.twitch.tv PRIVMSG #bomb_mask :Yo thanks
    //:[email protected] PRIVMSG #channel :message that was sent

    if(strncmp(msg, "PING", 4) == 0)
    {
        printf("\n\n\n###########PING###########\n\n\n");
        int len = strlen("PONG");
        send_msg(socket, "PONG tmi.twitch.tv", &len);
        return;
    }

    /* see if message contains tags */
    char* tags = strtok(msg, " ");

    array_t tags_arr;
    int r = parse_tags(tags, &tags_arr);
    char* prefix = strtok(NULL, " ");

    char* cmd = strtok(NULL, " ");
    //printf("prefix and cmd read\n");
    //printf("stuff parsed\n");
    if(strcmp(cmd, "PRIVMSG") == 0)
    {

        //printf("private message\n");
        char* username = calloc(32, sizeof(char));
        char* channel = strtok(NULL, " ")+1;
        char* bot_command = strtok(NULL, " ");
        sscanf(bot_command, ":%s", bot_command);
        if(bot_command[0] != '!')
        {
            free(username);
            return;
        }

        array_t params;
        init_array(&params, 1);
        char* temp_tok = NULL;
        while(temp_tok = strtok(NULL, " \r\n"))
        {
            insert_array(&params, temp_tok);
        }
        int res = sscanf(prefix, "%*[^!]!%[^@]@%*", username);

        char* usertype = get_value(&tags_arr, "user_type");
        //printf("usertype: %s\n", usertype);

        for(int i = 0; i < command_count; i++)
        {
            if(strcmp(registered_commands[i]->name, bot_command+1) == 0)
            {
                char* response = NULL;
                hackerbot_command_args args = {username, usertype, channel, NULL, socket};
                if(registered_commands[i]->argcount == 0)
                {
                    response = registered_commands[i]->function(&args);
                }
                else
                {
                    if(params.used < registered_commands[i]->argcount)
                        response = registered_commands[i]->usage;
                    /* 
                        if the user supplies too many arguments, we will just merge all the 
                        "unnecessarry" ones into one string to make the last argument 
                    */
                    else if(params.used > registered_commands[i]->argcount)
                    {
                        /* ...-1 is perfectly fine because we never hit this code path if argcount == 0 */
                        char* merged = strdup(params.array[registered_commands[i]->argcount-1]);
                        int msg_len = 0;
                        for(int j = registered_commands[i]->argcount; j < params.used; j++)
                        {
                            msg_len = strlen(merged);
                            char* temp_buf = calloc(msg_len + 1 + strlen(params.array[j]) + 1, sizeof(char));
                            memcpy(temp_buf, merged, msg_len);
                            memcpy(temp_buf + msg_len, " ", 1);
                            memcpy(temp_buf + msg_len + 1, params.array[j], strlen(params.array[j]) + 1);

                            free(merged);
                            merged = calloc(msg_len + 1 + strlen(params.array[j]) + 1, sizeof(char));
                            memcpy(merged, temp_buf, msg_len + 1 + strlen(params.array[j]) + 1);
                            free(temp_buf);
                        }

                        int used = params.used;
                        for(int j = registered_commands[i]->argcount; j < used; j++)
                            remove_from_array(&params);

                        params.array[registered_commands[i]->argcount-1] = merged;

                        args.params = &params;
                        response = registered_commands[i]->function(&args);
                        free(merged);
                    }
                    else
                    {
                        args.params = &params;
                        response = registered_commands[i]->function(&args);
                    }
                }
                if(!response)
                    response = "There was an error while processing your request BibleThump";

                send_irc_message(socket, response, username, args.channel);
                goto cleanup;
            }
        }

        send_irc_message(socket, "I don't know that command", username, channel);

        cleanup:
        free_array(&tags_arr);
        free_array(&params);
        free(username);
    }
}
예제 #18
0
파일: test_int.c 프로젝트: marso329/courses
int main(void) {
  clock_t begin, end;
  double time_spent;
  begin = clock();

  matrix* a = create_matrix(4, 4);
  value temp_a[16] = { 18, 60, 57, 96,
		       41, 24, 99, 58,
		       14, 30, 97, 66,
		       51, 13, 19, 85 };
  insert_array(temp_a, a);

  matrix* b = create_matrix(4, 4);
  assert(insert_array(temp_a, b));


  //tests check_boundaries
  assert(check_boundaries(1,1,a));
  assert(check_boundaries(4,4,a));
  assert(!check_boundaries(4,5,a));
  assert(!check_boundaries(5,4,a));
  assert(!check_boundaries(0,1,a));
  assert(!check_boundaries(1,0,a));
  assert(!check_boundaries(-1,1,a));
  assert(!check_boundaries(1,-1,a));


  //tests compare_matrices,insert_value and get_value
  assert(compare_matrices(a,b));
  assert(insert_value(10,1,1,b));
  assert(!compare_matrices(a,b));
  assert(get_value(1,1,b)==10);
  assert(insert_value(18,1,1,b));
  assert(compare_matrices(a,b));


  //tests is_matrix
  matrix* c=a;
  assert(compare_matrices(a,c));
  assert(!is_matrix(a,b));
  assert(is_matrix(a,c));


  //tests insert_value by trying to go outside the matrix
  assert(insert_value(1,1,1,c));
  assert(insert_value(2,2,2,c));
  assert(insert_value(3,3,3,c));
  assert(insert_value(4,4,4,c));
  assert(!insert_value(5,5,5,c));
  assert(!insert_value(-1,-1,-1,c));
  assert(!insert_value(-1,-1,1,c));
  assert(!insert_value(-1,1,-1,c));

  //test get_value
  assert(get_value(1,1,c)==1);
  assert(get_value(2,2,c)==2);
  assert(get_value(3,3,c)==3);
  assert(get_value(4,4,c)==4);
  assert(get_value(0,0,c)==0);
  assert(get_value(1,-1,c)==0);
  assert(get_value(-1,1,c)==0);
  assert(get_value(5,5,c)==0);

  //tests insert and get without boundary checks
  insert_value_without_check(4,1,1,c);
  insert_value_without_check(3,2,2,c);
  insert_value_without_check(2,3,3,c);
  insert_value_without_check(1,4,4,c);
  assert(get_value_without_check(1,1,c)==4);
  assert(get_value_without_check(2,2,c)==3);
  assert(get_value_without_check(3,3,c)==2);
  assert(get_value_without_check(4,4,c)==1);

  //tests add_matrices
  value temp_b[16]={
    36,120,114,192,
    82,48,198,116,
    28, 60, 194,132,
    102,26,38,170};
  assert(insert_array(temp_b,a));
  matrix* d = create_matrix(4, 4);
  assert(add_matrices(b,b,d));
  assert(compare_matrices(d,a));

  //tests subtract_matrices
  value temp_c[16]={
    0,0,0,0,
    0,0,0,0,
    0, 0, 0,0,
    0,0,0,0};
  assert(insert_array(temp_c,a));
  assert(subtract_matrices(b,b,d));
  assert(compare_matrices(d,a));

  //tests sum_of_row
  assert(insert_array(temp_a,a));
  assert(sum_of_row(1,a)==231);
  assert(sum_of_row(4,a)==168);
  assert(sum_of_row(0,a)==0);
  assert(sum_of_row(5,a)==0);

  //tests sum_of_column
  assert(sum_of_column(1,a)==124);
  assert(sum_of_column(4,a)==305);
  assert(sum_of_column(0,a)==0);
  assert(sum_of_column(5,a)==0);

  //tests get_row_vector
  matrix* e = create_matrix(1, 4);
  value temp_d[4] = { 18, 60, 57, 96};
  assert(insert_array(temp_d,e));
  matrix* f = create_matrix(1, 4);
  assert(!get_row_vector(0,a,f));
  assert(!get_row_vector(5,a,f));
  assert(get_row_vector(1,a,f));
  assert(compare_matrices(e,f));

  //tests get_column_vector
  matrix* g = create_matrix(4, 1);
  assert(insert_array(temp_d,e));
  matrix* h = create_matrix(1, 4);
  assert(!get_row_vector(0,a,h));
  assert(!get_row_vector(5,a,h));
  assert(get_row_vector(1,a,h));
  assert(compare_matrices(e,h));

  //tests mulitply_matrices
  assert(multiply_matrices(a,a,b));
  value temp_f[16]={8478,5478,14319,17130,
		    6066,6760,15418,16792,
		    6206,5328,14431,15096,
		    6052,5047,7652,14129.00};
  assert(insert_array(temp_f,d));
  assert(compare_matrices(b,d));
  assert(!multiply_matrices(a,h,b));
  assert(!multiply_matrices(a,a,h));

  //tests transpose_matrix
  value temp_g[16]={18,41,14,51,
		    60,24,30,13,
		    57,99,97,19,
		    96,58,66,85};
  assert(insert_array(temp_g,d));
  assert(transpose_matrix(a,b));
  assert(compare_matrices(b,d));
  assert(!transpose_matrix(e,b));
  assert(!transpose_matrix(a,e));

  //tests multiply_matrix_with_scalar
  value temp_h[16] = { 36, 120, 114, 192,
		       82, 48, 198, 116,
		       28, 60, 194, 132,
		       102, 26, 38, 170 };
  assert(insert_array(temp_h,b));
  multiply_matrix_with_scalar(2,a);
  assert(compare_matrices(a,b));

  //test get_sub_matrix
  matrix* i=create_matrix(2,2);
  assert(insert_array(temp_a,a));
  assert(get_sub_matrix(1,2,1,2,a,i));
  matrix* j=create_matrix(2,2);
  value temp_i[4] = { 18, 60, 41, 24};
  assert(insert_array(temp_i,j));
  assert(compare_matrices(j,i));
  value temp_j[4] = { 97, 66, 19, 85};
  assert(insert_array(temp_j,j));
  assert(get_sub_matrix(3,4,3,4,a,i));
  assert(compare_matrices(j,i));
  assert(!get_sub_matrix(2,4,3,4,a,i));
  assert(!get_sub_matrix(3,4,2,4,a,i));
  assert(!get_sub_matrix(4,5,4,5,a,i));
  assert(!get_sub_matrix(0,1,0,1,a,i));

  //test insert_row_vector
  assert(insert_array(temp_a,a));
  value temp_k[16] = { 18, 60, 57, 96,
		       18, 60, 57, 96,
		       14, 30, 97, 66,
		       51, 13, 19, 85 };
  assert(insert_array(temp_k,b));
  assert(insert_array(temp_d,e));
  assert(insert_row_vector(2,e,a));
  assert(compare_matrices(a,b));

  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("time taken was: %f \n",time_spent);
  free_matrix(a);
  free_matrix(b);
  free_matrix(d);
  free_matrix(e);
  free_matrix(f);
  free_matrix(g);
  free_matrix(h);
  free_matrix(i);
  free_matrix(j);

  return 0;
}
예제 #19
0
/* array_recov reads in  an array struct saved to disk and inserts it into
   the servers list of arrays */
int array_recov(

  char *path, 
  job_array **new_pa)

  {
  job_array *pa;
  array_request_node *rn;
  char  log_buf[LOCAL_LOG_BUF_SIZE];
  int   fd;
  int   old_version;
  int   num_tokens;
  int   i;
  int   len;
  int   rc;

  old_version = ARRAY_QS_STRUCT_VERSION;

  /* allocate the storage for the struct */
  pa = (job_array*)calloc(1,sizeof(job_array));

  if (pa == NULL)
  {
    return PBSE_SYSTEM;
  }

  /* initialize the linked list nodes */

  CLEAR_HEAD(pa->request_tokens);

  fd = open(path, O_RDONLY, 0);

  if (array_259_upgrade)
    {
    rc = read_and_convert_259_array(fd, pa, path);
    if(rc != PBSE_NONE)
      {
      free(pa);
      close(fd);
      return rc;
      }
    }
  else
    {

    /* read the file into the struct previously allocated.
     */

    len = read(fd, &(pa->ai_qs), sizeof(pa->ai_qs));
    if ((len < 0) || ((len < (int)sizeof(pa->ai_qs)) && (pa->ai_qs.struct_version == ARRAY_QS_STRUCT_VERSION)))
      {
      sprintf(log_buf, "error reading %s", path);
      log_err(errno, __func__, log_buf);
      free(pa);
      close(fd);
      return PBSE_SYSTEM;
      }

    if (pa->ai_qs.struct_version != ARRAY_QS_STRUCT_VERSION)
      {
      rc = array_upgrade(pa, fd, pa->ai_qs.struct_version, &old_version);
      if(rc)
        {
        sprintf(log_buf, "Cannot upgrade array version %d to %d", pa->ai_qs.struct_version, ARRAY_QS_STRUCT_VERSION);
        log_err(errno, __func__, log_buf);
        free(pa);
        close(fd);
        return rc;
        }
      }
    }

  pa->job_ids = calloc(pa->ai_qs.array_size, sizeof(char *));

  /* check to see if there is any additional info saved in the array file */
  /* check if there are any array request tokens that haven't been fully
     processed */

  if (old_version > 1)
    {
    if (read(fd, &num_tokens, sizeof(int)) != sizeof(int))
      {
      sprintf(log_buf, "error reading token count from %s", path);
      log_err(errno, __func__, log_buf);

      free(pa);
      close(fd);
      return PBSE_SYSTEM;
      }

    for (i = 0; i < num_tokens; i++)
      {
      rn = (array_request_node *)calloc(1, sizeof(array_request_node));

      if (read(fd, rn, sizeof(array_request_node)) != sizeof(array_request_node))
        {

        sprintf(log_buf, "error reading array_request_node from %s", path);
        log_err(errno, __func__, log_buf);

        free(rn);

        for (rn = (array_request_node*)GET_NEXT(pa->request_tokens);
            rn != NULL;
            rn = (array_request_node*)GET_NEXT(pa->request_tokens))
          {
          delete_link(&rn->request_tokens_link);
          free(rn);
          }

        free(pa);

        close(fd);
        return PBSE_SYSTEM;
        }

      CLEAR_LINK(rn->request_tokens_link);

      append_link(&pa->request_tokens, &rn->request_tokens_link, (void*)rn);

      }

    }

  close(fd);

  CLEAR_HEAD(pa->ai_qs.deps);

  if (old_version != ARRAY_QS_STRUCT_VERSION)
    {
    /* resave the array struct if the version on disk is older than the current */
    array_save(pa);
    }

  pa->ai_mutex = calloc(1, sizeof(pthread_mutex_t));
  pthread_mutex_init(pa->ai_mutex,NULL);

  lock_ai_mutex(pa, __func__, NULL, LOGLEVEL);

  /* link the struct into the servers list of job arrays */
  insert_array(pa);

  *new_pa = pa;

  return PBSE_NONE;

  }
예제 #20
0
int setup_array_struct(
    
  job *pjob)

  {
  job_array          *pa;
  array_request_node *rn;

  int                 bad_token_count;
  int                 array_size;
  int                 rc;
  char                log_buf[LOCAL_LOG_BUF_SIZE];
  long                max_array_size;

    pa = (job_array *)calloc(1,sizeof(job_array));

  pa->ai_qs.struct_version = ARRAY_QS_STRUCT_VERSION;
  
  strcpy(pa->ai_qs.parent_id, pjob->ji_qs.ji_jobid);
  strcpy(pa->ai_qs.fileprefix, pjob->ji_qs.ji_fileprefix);
  snprintf(pa->ai_qs.owner, sizeof(pa->ai_qs.owner), "%s", pjob->ji_wattr[JOB_ATR_job_owner].at_val.at_str);
  snprintf(pa->ai_qs.submit_host, sizeof(pa->ai_qs.submit_host), "%s", get_variable(pjob, pbs_o_host));

  pa->ai_qs.num_cloned = 0;
  CLEAR_HEAD(pa->request_tokens);

  pa->ai_mutex = calloc(1, sizeof(pthread_mutex_t));
  pthread_mutex_init(pa->ai_mutex,NULL);
  lock_ai_mutex(pa, __func__, NULL, LOGLEVEL);

  if (job_save(pjob, SAVEJOB_FULL, 0) != 0)
    {
    /* the array is deleted in svr_job_purge */
    unlock_ai_mutex(pa, __func__, "1", LOGLEVEL);
    svr_job_purge(pjob);
    /* Does job array need to be removed? */

    if (LOGLEVEL >= 6)
      {
      log_record(
        PBSEVENT_JOB,
        PBS_EVENTCLASS_JOB,
        (pjob != NULL) ? pjob->ji_qs.ji_jobid : "NULL",
        "cannot save job");
      }

    return(1);
    }

  if ((rc = set_slot_limit(pjob->ji_wattr[JOB_ATR_job_array_request].at_val.at_str, pa)))
    {
    long max_limit = 0;
    get_svr_attr_l(SRV_ATR_MaxSlotLimit, &max_limit);
    array_delete(pa);

    snprintf(log_buf,sizeof(log_buf),
      "Array %s requested a slot limit above the max limit %ld, rejecting\n",
      pa->ai_qs.parent_id,
      max_limit);

    log_event(PBSEVENT_SYSTEM,PBS_EVENTCLASS_JOB,pa->ai_qs.parent_id,log_buf);

    return(INVALID_SLOT_LIMIT);
    }

  pa->ai_qs.jobs_running = 0;
  pa->ai_qs.num_started = 0;
  pa->ai_qs.num_failed = 0;
  pa->ai_qs.num_successful = 0;
  
  bad_token_count = parse_array_request(
                      pjob->ji_wattr[JOB_ATR_job_array_request].at_val.at_str,
                      &(pa->request_tokens));

  /* get the number of elements that should be allocated in the array */
  rn = (array_request_node *)GET_NEXT(pa->request_tokens);
  array_size = 0;
  pa->ai_qs.num_jobs = 0;
  while (rn != NULL) 
    {
    if (rn->end > array_size)
      array_size = rn->end;
    /* calculate the actual number of jobs (different from array size) */
    pa->ai_qs.num_jobs += rn->end - rn->start + 1;

    rn = (array_request_node *)GET_NEXT(rn->request_tokens_link);
    }

  /* size of array is the biggest index + 1 */
  array_size++; 

  if (get_svr_attr_l(SRV_ATR_MaxArraySize, &max_array_size) == PBSE_NONE)
    {
    if (max_array_size < pa->ai_qs.num_jobs)
      {
      array_delete(pa);

      return(ARRAY_TOO_LARGE);
      }
    }

  /* initialize the array */
  pa->job_ids = calloc(array_size, sizeof(char *));
  if (pa->job_ids == NULL)
    {
    sprintf(log_buf, "Failed to alloc job_ids: job %s", pjob->ji_qs.ji_jobid);
    log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, log_buf);
    return(PBSE_MEM_MALLOC);
    }


  /* remember array_size */
  pa->ai_qs.array_size = array_size;

  CLEAR_HEAD(pa->ai_qs.deps);

  array_save(pa);

  if (bad_token_count > 0)
    {
    array_delete(pa);
    return 2;
    }

  pjob->ji_arraystruct = pa;

  insert_array(pa);

  unlock_ai_mutex(pa, __func__, "1", LOGLEVEL);

  return(PBSE_NONE);
  } /* END setup_array_struct() */
예제 #21
0
int main()
{

    //int size_array = 17;
    //int array_file[17] = {0,1,2,3,5,23,53,33,46,23,77,23,75,23,64,22,63};
    // int size_array  = 15;
    //int array_file[15] = {23,53,234,23,121,2,13,63,98,234, 6, 2,11,2,63};

   // int size_array = 5;
   // int array_file[5] = {0,1,2,3,5};

    int size_array = 32;
    int array_file[32];//
    // = {0,1,2,3,5,2};
    //
    int i;
    for(i = 0; i < size_array; i++)
		{
					array_file[i] = i * (rand()%100);
		}

   	//int size_array = 4;
    // int array_file[4] = {0,1,2,6};



    int c_array = 0;
    printf("---------------------------------------------------------\n");

    for(c_array = 0; c_array < size_array; c_array++)
        printf("%d, ", array_file[c_array]);

    printf("\n---------------------End count Array -------------------\n");

    int *endState[size_array];
    int *startState[size_array];

    int *stateSize = (int *)malloc(sizeof(int *));

    insert_array(endState, stateSize, size_array);

    int countFile;
    printf("State size : %d \n", *stateSize);

    for(countFile =  0; countFile < *stateSize; countFile++) {
        printf("-- Print all index ( not size ) = [ %d ] : %d \n",countFile,  endState[countFile]);
    }

    getfile(endState, startState, stateSize, size_array);


    countFile = 0;
    int countPerFile;

    for(; countFile  < *stateSize; countFile++) {
        printf("--- Start state : %d - %d \n", startState[countFile], endState[countFile]);
        int end = endState[countFile];
        int start = startState[countFile];
				
				if((end - start) == 0)
				{	
						break;
				}

        for(countPerFile = start; countPerFile <= end; ++countPerFile) {
            printf("--- Count Per file : %d , array_file : [ %d ] \n", countPerFile, array_file[countPerFile]);
        }

        countPerFile = 0;
    }

}
예제 #22
0
/***********************************************************************************************************************************************
*	FUNCTION NAME : main.c
*
*	DESCRIPTION : contains main function swhich call other functions for implementing the given problem statement
*
*	RETURN VALUE : SUCCESS
*
**************************************************************************************************************************************************/
int main(int argc, char *argv[])
{
	/*error handling for command line arguments*/
	if(NULL == argv[1])
	{
		printf("Please provide first input file name\n");
		exit(FAILURE);
	}

	if(NULL == argv[2])
	{
		printf("Please provide second input file name\n");
		exit(FAILURE);
	}

	if(NULL == argv[3])
	{
		printf("Please provide output file name\n");
		exit(FAILURE);
	}

	FILE *fp1; //file pointer for first input file
	FILE *fp2; //file pointer for second input file
	FILE *fp3; //file pointer for output file

	int index; //array index

	char string[MAX];//for storing strings
	memset(string,0,MAX*sizeof(char));//initializing input to null

	char *arr[MAX]; //array for storing names
	/*initializing array of pointers to null*/
	for (index=0; index<MAX-1; index++)
	{
		arr[index] = NULL;
	}

	file_open(&fp1, argv[1], "r");//opening first input file in read only mode
	file_open(&fp2, argv[2], "r");//opening second input file in read only mode
	file_open(&fp3,argv[3], "w");//opening output file in write only mode

	index = 0;
	/*reading from first input file & storing it in array*/
	while(1)
	{
		fgets(string, (MAX-1)*sizeof(char), fp1); //reading from the first input file
		/*error handling for end of file*/
		if(feof(fp1))
		{
			break;
		}
		insert_array(arr, string, index);
		++index;
	}

	/*reading from second input file and storing it in array*/
	while(1)
	{
		fgets(string, (MAX-1)*sizeof(char), fp2); //reading from the second input file
		/*error handling for end of file*/
		if(feof(fp2))
		{
			break;
		}
		insert_array(arr, string, index);
		++index;
	}

	printf("\n");
	printf("***************array before sorting*************************\n");
	display_array(arr, index);
	printf("\n\n");

	
	quicksort(arr, 0, index-1);//sorting the array
	printf("*******************array after sorting****************************\n");
	display_array(arr, index);
	printf("\n\n");


	insert_file(arr, index, &fp3);//read from the ds and store it in output file

	file_close(&fp1); //closing first input file
	file_close(&fp2); //closing second input file
	file_close(&fp3); //closing output file

	free_array(arr, index); //free the link list

	return SUCCESS;
}
예제 #23
0
int main(){
  clock_t begin, end;
  double time_spent;
  begin = clock();

  matrix* Q = create_matrix(2,2);
  value Q_arr[4] = {	2, 0,
			0, 2};
  insert_array(Q_arr, Q);


  matrix* q = create_matrix(2, 1);
  value q_arr[2] = {  -2, 
		      -5};
  insert_array(q_arr, q);


  matrix* F = create_matrix(5, 2);
  value F_arr[10] = {  1, -2, 
		       -1, -2,
		       -1,  2, 
		       1,  0,
		       0,  1};
  insert_array(F_arr, F);


  matrix* g = create_matrix(5, 1);
  value g_arr[5] = { -2, 
		     -6, 
		     -2, 
		     0,
		     0};
  insert_array(g_arr, g);

  /* starting point */
  matrix* z = create_matrix(2,1);
  value z_arr[2] = {1, 
		    1};
  insert_array(z_arr, z);


  problem* problem = create_problem(Q, q, NULL, NULL, F, g, z, 0, 0);

  quadopt_solver(problem);

  matrix* expected = create_matrix(2, 1);
  value e_arr[2] = {1.4,
		    1.7};
  insert_array(e_arr, expected);

  assert(compare_matrices(problem->solution, expected));

  free_matrix(expected);
  free_problem(problem);

  end = clock(); 
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("time taken was: %f \n", time_spent);

  return 0;
}