コード例 #1
0
ファイル: main.cpp プロジェクト: khiemnv/algorithms
element* table_remove(BlockInfo* pRoot, BlockInfo* pBlock) {
  MY_PRINTF("  table remove\n");
  element* pCur = pRoot;
  MY_ASSERT(table_search(pRoot, element_size(pBlock)) != NULL);
  if (pCur == pBlock) {
    //pCur->child
    //pSib
    if (element_child(pCur)) {
      element* pSib = element_sibling(pCur);
      pCur = element_child(pCur);
      element_sibling(pCur) = pSib;
    }
    else {
      pCur = element_sibling(pCur);
    }
  }
  else if (element_size(pCur) == element_size(pBlock)) {
    MY_ASSERT(pCur != pBlock);
    MY_ASSERT(element_child(pCur) != NULL);
    element_child(pCur) = child_remove(element_child(pCur), pBlock);
  }
  else if (element_size(pCur) < element_size(pBlock)) {
    MY_ASSERT(element_sibling(pCur) != NULL);
    element_sibling(pCur) = table_remove(element_sibling(pCur), pBlock);
  }
  else {
    MY_ASSERT(element_size(pCur) > element_size(pBlock));
    MY_ASSERT(0);
  }
  return pCur;
};
コード例 #2
0
ファイル: simple.c プロジェクト: CODECOMMUNITY/nofib
/* This function  yields and sums up thecoefficients of the polynomials
   used in the energy and pressure calculations.*/
inline double polynomial
(double *G[4][5], int degree, double rho_table[], double theta_table[],
 double rho_value, double theta_value)
{
  int rho_index;
  int theta_index;
  double *A;
  double A_sum = 0.0;
  int i,j;
  
  rho_index = table_search(3, rho_table, rho_value);
  theta_index = table_search(4, theta_table, theta_value);
  A = G[rho_index-1][theta_index-1];
  for (i=0;i<=degree;i++)
    for (j=0;j<=degree;j++)
      {
	double A_val;
	
        A_val = *(A+3*i+j);
	A_sum += (A_val * power(rho_value, i) * power(theta_value, j));
      }
  return(A_sum);
}
コード例 #3
0
ファイル: layout.c プロジェクト: JohnPJenkins/swift-t
static adlb_code
build_worker2host(const struct xlb_hostnames *hostnames,
      const xlb_layout *layout, int my_workers,
      int **worker2host, int *host_count)
{
  *worker2host = malloc(sizeof((*worker2host)[0]) *
                              (size_t)my_workers);
  ADLB_MALLOC_CHECK(*worker2host);

  struct table host_name_idx_map;
  bool ok = table_init(&host_name_idx_map, 128);
  CHECK_MSG(ok, "Table init failed");

  *host_count = 0;
  for (int i = 0; i < my_workers; i++)
  {
    int rank = xlb_rank_from_my_worker_idx(layout, i);
    const char *host_name = xlb_hostnames_lookup(hostnames, rank);
    CHECK_MSG(host_name != NULL, "Unexpected error looking up host for "
              "rank %i", rank);

    unsigned long host_idx;
    if (!table_search(&host_name_idx_map, host_name, (void**)&host_idx))
    {
      host_idx = (unsigned long)(*host_count)++;
      ok = table_add(&host_name_idx_map, host_name, (void*)host_idx);
      CHECK_MSG(ok, "Table add failed");
    }
    (*worker2host)[i] = (int)host_idx;
    DEBUG("host_name_idx_map: my worker %i (rank %i) -> host %i (%s)",
          i, xlb_rank_from_my_worker_idx(layout, i), (int)host_idx,
          host_name);
  }

  table_free_callback(&host_name_idx_map, false, NULL);

  return ADLB_SUCCESS;
}
コード例 #4
0
ファイル: main.c プロジェクト: paswd/kp9
int main(void)
{
	Table *table = table_create();
	printf("-------------------------------------------\n");
	printf("Commands:\n");
	printf("a <key> <value> - push to table\n");
	printf("d <key> - delete from table\n");
	printf("s - sort table\n");
	printf("p - print table\n");
	printf("f <key> - find element by key\n");
	printf("c - clear table\n");
	printf("q - exit\n");
	printf("-------------------------------------------\n");
	size_t cnt = 0;
	char last_cmd = '\n';
	while (true) {
		if (!cnt || last_cmd == '\n')
			printf("> ");
		char cmd;
		bool is_finished = false;
		scanf("%c", &cmd);
		size_t pos;
		//LinearlistElement *this = NULL;
		Key *key = key_create();
		switch (cmd) {
			case 'q':
				is_finished = true;
				break;
			case 'a':
				table_push(table);
				break;
			case 'd':
				//scanf("%d", &pos);
				key_set_value_from_input(key);
				table_note_delete(table, key);
				break;
			case 's':
				table_sort(table);
				break;
			case 'p':
				table_print(table);
				break;
			case 'c':
				table_clear(table);
				break;
			case 'f':
				key_set_value_from_input(key);
				Note *found = table_search(table, key, &pos);
				if (found == NULL) {
					printf("There are no notes with this key in the table\n");
					break;
				}
				note_print(table, found);
				break;
			case '\n':
				break;
			default:
				printf("Invalid command `%c`\n", cmd);
				break;
		}
		last_cmd = cmd;
		key_destroy(&key);
		if (is_finished) break;

		cnt++;
		cnt %= 2;

		//printf("First: %lld\n", (long long) linearlist_get_value(linearlist_get_first(linearlist)));
		//printf("Last: %lld\n", (long long) linearlist_get_value(linearlist_get_last(linearlist)));
	}
	printf("Goodbye!\n");

	table_destroy(&table);

	return 0;
}
コード例 #5
0
ファイル: callbacks.c プロジェクト: SystemGarden/habitat
/* callback to edit the current job table used by clockwork */
G_MODULE_EXPORT void 
on_edit_jobs (GtkObject *object, gpointer user_data)
{
     TABLE clockcf=NULL;
     char *clockpurl, *jobpurl, jobpurl_t[1000];
     int r;

     /* Check clockwork is running to see if we can read its configuration */
     if (uicollect_is_clockwork_running(NULL, NULL, NULL, NULL, 1)) {

          /* Get the latest configuration from the local running clockwork */
          clockpurl = util_strjoin("http://localhost:", HTTPD_PORT_HTTP_STR,
				   "/cftsv", NULL);
	  clockcf = route_tread(clockpurl, NULL);
	  if ( ! clockcf ) {
	       elog_printf(DIAG, "Unable to read clockwork configuration "
			   "(%s), although it is running; possibly security "
			   "is an issue", clockpurl);
	       elog_printf(FATAL, "<big><b>Unable to Load Collector "
			   "Configuration</b></big>\n"
			   "The collector is running but the configuration "
			   "can't be read. Check Habitat's security "
			   "configuration");
	       nfree(clockpurl);
	       return;
	  }
	  nfree(clockpurl);

	  r = table_search(clockcf, "name", "jobs");
	  if (r == -1) {
	       elog_printf(DIAG, "Clockwork configuration read but 'jobs' "
			   "declaration is missing");
	       elog_printf(FATAL, "<big><b>Unable to Load Collector "
			   "Configuration</b></big>\n"
			   "The collector does not have a configured job "
			   "table. Please check your configuration");
	       table_destroy(clockcf);
	       return;
	  }

	  jobpurl = table_getcurrentcell(clockcf, "value");
     } else {

          /* Clockwork is not running, so find the job table from the 
	   * current configuration (the jobs directive). This has a flaw as 
	   * clockwork may be started manually with a job switch (-j or -J) */
          jobpurl = cf_getstr(iiab_cf, "jobs");
	  if ( ! jobpurl) {
	       elog_printf(FATAL, "Unable to load collection jobs, as there "
			   "was no configuration directive.\n\n"
			   "Please specify -j, -J or set the directive `jobs' "
			   "in the configuration file to the route containing "
			   "a job table. \n\n"
			   "For example, `jobs=file:/etc/jobs.norm' "
			   "will look for the file /etc/jobs.norm");
	       return;
	  }
     }

     /* read the job table */
     r = route_expand(jobpurl_t, jobpurl, "NOJOB", 0);
     if (r == -1 || jobpurl_t[0] == '\0') {
          elog_printf(FATAL, "Unable to load collection jobs, as there are "
		      "no valid configuration directives in the table %s/%s. "
		      "Please specify -j, -J or set the directive `jobs' in "
		      "the configuration file to the route containing a job "
		      "table. For example, `jobs=file:/etc/jobs.norm' "
		      "will look for the file /etc/jobs.norm", 
		      jobpurl, jobpurl_t);
	  return;
     }

     uiedit_load_route(jobpurl_t, "Collection Jobs");

     if (clockcf)
          table_destroy(clockcf);
}
コード例 #6
0
ファイル: table_test.c プロジェクト: cernekm/VUT-FIT-IFJ-2014
int main(void)
{
    t_table *table = table_init(128);
    if (table == NULL) {
        fprintf(stderr, "Table could not be initialized!\n");
        return EXIT_FAILURE;
    }
    
    print_table_info(table);
    print_table(table);

    int data_pole[NUM] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    t_string key_pole[NUM];

    int i;
    for (i = 0; i < NUM; i++) {
        stringInit(&key_pole[i]);
    }

    for (i = 0; i < NUM; i++) {
        stringAddChar(&key_pole[i], 'a' + i);
    }

    int ret = 0;
    t_tab_data * data_ptr;

    for (i = 0; i < NUM; i++) {
        ret = table_insert(table, &key_pole[i], &data_ptr);
        switch (ret) {
        case TAB_INSERT_OK:
            data_ptr->a = data_pole[i];
            break;
        case TAB_INSERT_FOUND:
            fprintf(stderr, "Item already inserted\n");
            break;
        case TAB_INSERT_ALL_ERR:
            fprintf(stderr, "Allocation error\n");
            break;
        }
    }

    print_table_info(table);
    print_table(table);
    
    /*
    //1. raz
    printf("INSERT\n\n");
    for (i = 0; i < NUM; i++) {
        ret = table_insert(table, key_pole[i], 7, (t_tab_data **) &data);
        switch (ret) {
        case TAB_INSERT_FAIL:
            fprintf(stderr, "Item already inserted.\n");
            break;
        case TAB_INSERT_FULL:
            while (ret == TAB_INSERT_FULL) {
                if (table_resize(table) == TAB_RESIZE_FAIL) {
                    fprintf(stderr, "RESIZE FAIL\n");
                    table_destroy(table);
                    return EXIT_FAILURE;
                }
                ret = table_insert(table, key_pole[i], 7, (t_tab_data **)&data);
            }
            if (ret == TAB_INSERT_FAIL) {
                fprintf(stderr, "Item already inserted.\n");
                break;
            }
            if (ret == TAB_INSERT_ALL_ERR) {
                fprintf(stderr, "ALLOC ERR\n");
                table_destroy(table);
                return EXIT_FAILURE;
            }
            *(int *)data = data_pole[i];
            break;
        case TAB_INSERT_OK:
            *(int *)data = data_pole[i];
            break;
        case TAB_INSERT_ALL_ERR:
            fprintf(stderr, "ALLOC ERROR\n");
            table_destroy(table);
            return EXIT_FAILURE;
        }
    }

    print_table(table);
    */
    
    printf("-----------------------------------------------------------------------------------------\n\n");

    printf("GET DATA\n\n");
    for (i = 0; i < NUM; i++) {
        ret = table_search(table, &key_pole[i], (t_tab_data **) &data_ptr);
        if (ret == TAB_SEARCH_OK) {
            printf("Key:    %s\n", key_pole[i].string);
            printf("Data:  %d\n\n", data_ptr == NULL ? -1 : ((t_tab_data *)data_ptr)->a);
        }
    }

    /*
    print_table(table);
    */

    for (i = 0; i < NUM; i++) {
        stringFree(&key_pole[i]);
    }
    table_destroy(table);
    return EXIT_SUCCESS;
}
コード例 #7
0
ファイル: serverOLD.c プロジェクト: cykamancer/workspace
int handle_command(int sock, char *cmd, Table* database, char* username, char* password)
{

	n = sprintf(message, "Processing command '%s'\n", cmd);
	fp = fopen(buffer2, "a+");
	logger(LOGGING, fp, message); // replace LOG commands with logger() calls
	fclose(fp);

	// For now, just send back the command to the client.
	int isReadingCMD=0; //boolean that indicated whether it is reading the command indicator (AUTH, SET, GET)

	char* split, arg1, arg2, arg3;
	split=strtok(cmd, " ");
    if(split=="AUTH")
    {
    	arg1=strtok(NULL, " ");//Get username
    	arg2=strtok(NULL, " ");//Get password
    	if(username==arg1&&password==arg2){
    		sendall(sock, arg1, strlen(arg1));
    		return 0;
    	}
    	else return -1;

    }
    else if(split=="GET")
    {
    	arg1=strtok(NULL, " ");//Get name of table
    	arg2=strtok(NULL, " ");//Get key of entry
        Table* table=table_search(arg1, database);
    	if(table){
    		Entry* entry=entry_search(arg2, table->entry);
    	    if(entry){
    			sendall(sock, entry->value, strlen(entry->value));
    	    }
    	}
    }
    else if(split=="SET")
    {
    	arg1=strtok(NULL, " ");//Get name of table
    	arg2=strtok(NULL, " ");//Get key of entry
    	arg3=strtok(NULL, " ");//Get value to set

    	Table* table=table_search(arg1, database);
    	if(!table){
    		int i;
    		for(i=0; i<MAX_DATABASE_LEN; i++){
    		    if(database[i].name==""){
    		    	table=&database[i];
                    break;
    	    	}
    		}
    	}
    	table->name=arg1;
    	Entry* entry=entry_search(arg2, table->entry);
    	if(!entry){
    		int i;
    		for(i=0; i<MAX_DATABASE_LEN; i++){
    		    if(table->entry[i].key==""){
    		    	entry=&(table->entry[i]);
                    break;
    	    	}
    		}
    	}
    	entry->key=arg2;
        entry->value=arg3;
        sendall(sock, entry->value, strlen(entry->value));
    }
	/*
	sendall(sock, cmd, strlen(cmd));
	sendall(sock, "\n", 1);*/

	return 0;
}