Beispiel #1
0
std::pair< CvMat *, CvMat * > readkeys( const char * filename )
{
	int buffer_length = 2048;
	char buffer[buffer_length];

	std::ifstream in_file( filename );

	in_file.getline( buffer, buffer_length );
	std::vector< std::string > tokens = tokenize_str( buffer, " " );

	// parse header information
	int number_of_keypoints = lexical_cast< std::string, int >( tokens[0] );
	int length = lexical_cast< std::string, int >( tokens[1] );

	CvMat * key_vectors = cvCreateMat( number_of_keypoints, KEY_VECTORS_WIDTH, CV_32FC1 );
	CvMat * key_descriptors = cvCreateMat( number_of_keypoints, KEY_DESCRIPTORS_WIDTH, CV_32FC1 );

	// parse rest of file
	for( int j = 0; j < number_of_keypoints; j++ )
	{
		// parse vectors
		in_file.getline( buffer, buffer_length );
		tokens = tokenize_str( buffer, " " );
		for( int i = 0; i < tokens.size(); i++ )
		{
			cvmSet( key_vectors, j, i, lexical_cast< std::string, float >( tokens[i] ) );
		}

		// parse descriptors
		float sum = 0;
		for( int k = 0; k < ceil( (float)length / DESCRIPTORS_PER_LINE ); k ++ )
		{
			in_file.getline( buffer, buffer_length );
			tokens = tokenize_str( buffer, " " );
			
			for( int i = 0; i < tokens.size(); i++ )
				sum += lexical_cast< std::string, float >( tokens[i] ) ;

		}
		float normalize = cvSqrt( pow( sum, 2 ) );
		for( int i = 0; i < tokens.size(); i++ )
			cvmSet( key_descriptors, j, i,  lexical_cast< std::string, float >( tokens[i] ) / normalize );
	}

	return std::make_pair( key_vectors, key_descriptors );
}
Beispiel #2
0
TEAM *input_dat(FILE *fp, int *num_team)
{
	char line[SIZE_STR], **token;
	int i, num_token;
	int cmp_pts();
	TEAM team_home, team_away, *team_ary;
	struct list *team_list, *p;

	*num_team = 0;
	team_list = init_list();
	while (fgets(line, SIZE_STR, fp) != NULL) {
		if (line[0] != '#') {
			token = tokenize_str(line, "-\t\n", &num_token);
			if (num_token >= 4) {
				set_home_team(&team_home, token);
				set_away_team(&team_away, token);

				if ((p = search_team(team_list, team_home.name)) == NULL) {
					push_queue(team_list, &team_home, sizeof(team_home));
					(*num_team)++;
				}
				else {
					add_team_result(p, team_home);
				}

				if ((p = search_team(team_list, team_away.name)) == NULL) {
					push_queue(team_list, &team_away, sizeof(team_away));
					(*num_team)++;
				}
				else {
					add_team_result(p, team_away);
				}
			}
			free_token_ary(token, num_token);
		}
	}

	team_ary = (TEAM *) malloc(sizeof(TEAM) * (*num_team));
	if (team_ary == NULL) {
		fprintf(stderr, "ERROR: Unable to allocate memory.\n");
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < *num_team; i++) {
		pop_queue(team_list, &team_ary[i], sizeof(TEAM));
	}
	delete_list(team_list);

	/* 安定ソートで被ゴール数→ゴール数→得失点差→勝ち点の順で整列すると順位で並ぶ */
	/* リーグ戦の規程によってはこのやり方ではダメ */
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_gag);
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_gfo);
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_gdi);
	bubblesort(team_ary, *num_team, sizeof(TEAM), cmp_pts);

	return team_ary;
}
void multiprogram(FILE *ptr_file, queue *q_ready, queue *q_process)
{
	process *curr_proc = NULL;		/* the current process removed from the front of the ready queue */
	char **token_arr = NULL;		/* array of tokens used to store the current instruction */
	char buffer[256];			/* buffer used to store the string read from the input file */
	queue *q_wait = init_queue();

	while (!(empty_queue(q_ready))) {	/* continue executing processes until no processes remain in the ready state */
		curr_proc = (process *) dequeue(q_ready);

		fsetpos(ptr_file, &(curr_proc->process_fpos));	/* update the file read position curr_proc's next instruction */
		fgets(buffer, 256, ptr_file);

		token_arr = tokenize_str(buffer);
		int continue_processing = TRUE;
		int ret_val = -999;
		while (continue_processing) {
			ret_val = process_instruction(curr_proc, q_ready, q_process, q_wait, token_arr);
			free_token(token_arr);

			if (ret_val == 0) {		/* alloc/dealloc was successful continue processing at next instruction */
				fgetpos(ptr_file, &(curr_proc->process_fpos));
				fgets(buffer, 256, ptr_file);

				token_arr = tokenize_str(buffer);
				continue_processing = TRUE;
			} else if (ret_val == 1) { 	/* a "SL #" instruction was encountered continue processing later */
				fgetpos(ptr_file, &(curr_proc->process_fpos)); /* update the processes fpos */
				continue_processing = FALSE;
			} else if (ret_val == -1) {	/* alloc/dealloc failed continue processing later at same instruction */
				continue_processing = FALSE;
			}
		}
	}
	free_queue(q_wait);

	return;
}
static void init_ready_queue(FILE *ptr_file, queue *q_ready)
{
	char buffer[256];
	char **token_arr;
	int pid = -1;
	int new_proc_found = FALSE;
	while (fgets(buffer, 256, ptr_file) != NULL) {		/* read each string in the input file pointed to by ptr_file */
		token_arr = tokenize_str(buffer);		/* break the input string in the buffer into tokens */
		if (new_proc_found) {				/* check if an "ID #" command has been scanned */
			init_process(ptr_file, token_arr, q_ready, pid);	/* introduce a new process to the system */
			new_proc_found = FALSE;
		}
		if (is_new_proc(token_arr)) {			/* check if the token_arr contains an "ID #" command */
			new_proc_found = TRUE;
			pid = atoi(token_arr[1]);		/* record the PID parsed from the "ID #" command */
		}
		free_token(token_arr);
	}
	return;
}
vector<string> Configuration::GetStrArray(string const & field) const
{
    string const param_str = GetStr(field);
    return tokenize_str(param_str);
}
Beispiel #6
0
TrafficPattern * TrafficPattern::New(string const & pattern, int nodes, 
				     Configuration const * const config)
{
  string pattern_name;
  string param_str;
  size_t left = pattern.find_first_of('(');
  if(left == string::npos) {
    pattern_name = pattern;
  } else {
    pattern_name = pattern.substr(0, left);
    size_t right = pattern.find_last_of(')');
    if(right == string::npos) {
      param_str = pattern.substr(left+1);
    } else {
      param_str = pattern.substr(left+1, right-left-1);
    }
  }
  vector<string> params = tokenize_str(param_str);
  
  TrafficPattern * result = NULL;
  if(pattern_name == "bitcomp") {
    result = new BitCompTrafficPattern(nodes);
  } else if(pattern_name == "transpose") {
    result = new TransposeTrafficPattern(nodes);
  } else if(pattern_name == "bitrev") {
    result = new BitRevTrafficPattern(nodes);
  } else if(pattern_name == "shuffle") {
    result = new ShuffleTrafficPattern(nodes);
  } else if(pattern_name == "randperm") {
    int perm_seed = -1;
    if(params.empty()) {
      if(config) {
	if(config->GetStr("perm_seed") == "time") {
	  perm_seed = int(time(NULL));
	  cout << "SEED: perm_seed=" << perm_seed << endl;
	} else {
	  perm_seed = config->GetInt("perm_seed");
	}
      } else {
	cout << "Error: Missing parameter for random permutation traffic pattern: " << pattern << endl;
	exit(-1);
      }
    } else {
      perm_seed = atoi(params[0].c_str());
    }
    result = new RandomPermutationTrafficPattern(nodes, perm_seed);
  } else if(pattern_name == "uniform") {
    result = new UniformRandomTrafficPattern(nodes);
  } else if(pattern_name == "background") {
    vector<int> excludes = tokenize_int(params[0]);
    result = new UniformBackgroundTrafficPattern(nodes, excludes);
  } else if(pattern_name == "diagonal") {
    result = new DiagonalTrafficPattern(nodes);
  } else if(pattern_name == "asymmetric") {
    result = new AsymmetricTrafficPattern(nodes);
  } else if(pattern_name == "taper64") {
    result = new Taper64TrafficPattern(nodes);
  } else if(pattern_name == "bad_dragon") {
    bool missing_params = false;
    int k = -1;
    if(params.size() < 1) {
      if(config) {
	k = config->GetInt("k");
      } else {
	missing_params = true;
      }
    } else {
      k = atoi(params[0].c_str());
    }
    int n = -1;
    if(params.size() < 2) {
      if(config) {
	n = config->GetInt("n");
      } else {
	missing_params = true;
      }
    } else {
      n = atoi(params[1].c_str());
    }
    if(missing_params) {
      cout << "Error: Missing parameters for dragonfly bad permutation traffic pattern: " << pattern << endl;
      exit(-1);
    }
    result = new BadPermDFlyTrafficPattern(nodes, k, n);
  } else if((pattern_name == "tornado") || (pattern_name == "neighbor") ||
	    (pattern_name == "badperm_yarc")) {
    bool missing_params = false;
    int k = -1;
    if(params.size() < 1) {
      if(config) {
	k = config->GetInt("k");
      } else {
	missing_params = true;
      }
    } else {
      k = atoi(params[0].c_str());
    }
    int n = -1;
    if(params.size() < 2) {
      if(config) {
	n = config->GetInt("n");
      } else {
	missing_params = true;
      }
    } else {
      n = atoi(params[1].c_str());
    }
    int xr = -1;
    if(params.size() < 3) {
      if(config) {
	xr = config->GetInt("xr");
      } else {
	missing_params = true;
      }
    } else {
      xr = atoi(params[2].c_str());
    }
    if(missing_params) {
      cout << "Error: Missing parameters for digit permutation traffic pattern: " << pattern << endl;
      exit(-1);
    }
    if(pattern_name == "tornado") {
      result = new TornadoTrafficPattern(nodes, k, n, xr);
    } else if(pattern_name == "neighbor") {
      result = new NeighborTrafficPattern(nodes, k, n, xr);
    } else if(pattern_name == "badperm_yarc") {
      result = new BadPermYarcTrafficPattern(nodes, k, n, xr);
    }
  } else if(pattern_name == "hotspot") {
    if(params.empty()) {
      params.push_back("-1");
    } 
    vector<int> hotspots = tokenize_int(params[0]);
    for(size_t i = 0; i < hotspots.size(); ++i) {
      if(hotspots[i] < 0) {
	hotspots[i] = RandomInt(nodes - 1);
      }
    }
    vector<int> rates;
    if(params.size() >= 2) {
      rates = tokenize_int(params[1]);
      rates.resize(hotspots.size(), rates.back());
    } else {
      rates.resize(hotspots.size(), 1);
    }
    result = new HotSpotTrafficPattern(nodes, hotspots, rates);
  } else {
    cout << "Error: Unknown traffic pattern: " << pattern << endl;
    exit(-1);
  }
  return result;
}