Example #1
0
/*only works with values that are numbers.
This function needs to be expanded when I have more time.
*/
void dsort(Dict dict)
{
	int change =1;
	int i=0;
	while(change)
	{
		change = 0;
		
		while(i<getLength(dict)-1)
		{
			if(gget(dict->values,i)<gget(dict->values,i+1))
			{
				moveKeyToEnd(dict,gget(dict->keys,i));
				change =1;
			}
			else
			{
				i++;
			}	
		}
		i=0;
		
	
	}
	//dprint(dict,printChar,printInt);
}
Example #2
0
File: sexpr.C Project: bougyman/sfs
void
gread_string (gread_state *state)
{
  int c = gget ();
  switch (c) {
    case EOF:
      gerror ();
    case '"':
      {
        gstring *string = New gstring (*(state->chars));
        delete state->chars;
        delete state;
        greturn (string);
      }
    case '\\':
      c = gget ();
      switch (c) {
        case EOF:
          gerror ();
        case 'n':
          state->chars->append ('\n');
          ggoto (gread_string, state);
      }
      // else fall through
    default:
      state->chars->append (c);
      ggoto (gread_string, state);
  }
}
Example #3
0
/*This function prints out a Dict, it takes as its arguments to print(void* item)
functions, one to print the keys, and one to print the values.
*/
void dprint(Dict dictionary,void (*printkey)(void* item),void (*printvalue)(void* item))
{
	int i;
	printf("Length: %d\n",getLength(dictionary));
	printf("Capacity: %d\n",dictionary->capacity);
	for(i=0;i<getLength(dictionary);i++)
	{
		printf("Key %d: <",i);
		printkey(gget(dictionary->keys,i));
		printf(",");
		printvalue(gget(dictionary->values,i));
		printf(">\n");
	}
}
Example #4
0
Point( )
{
double x, y, r;
char code[20];

gget( Buf, "Mark" );
strcpy( code, Buf );
gget( Buf, "Position" );
sscanf( Buf, "%lf %lf", &x, &y );
gget( Buf, "Size" );
sscanf( Buf, "%lf", &r );
NTptsize( r );
r = Chh * 0.4;
point( x, y, code, r );
}
Example #5
0
void rest_pitchers(Db_Object db, int league_id){

	char query[4*DEFAULT_QUERY_LENGTH];
	int num_chars = sprintf(query,SELECT_BL_END,league_id);
	native_result* result = db->execute_query_result(db->conn,query,num_chars);
	int i;
	native_row row;
	Array_List list = Array_List_create(sizeof(Key_Value));
	while(row = db->fetch_row(result)){
		int player_id = db->get_column_int(row,0);
		int batters_left = db->get_column_int(row,1);
		int endurance = db->get_column_int(row,2);
		int max_batters = calc_max_batters(endurance);
		int new_batters_left = batters_left + divide_ceiling(max_batters,4);
		if(new_batters_left> max_batters){
			new_batters_left = max_batters;
		}
		Key_Value key_value = malloc(sizeof(struct key_value));
		key_value->key = (void*)player_id;
		key_value->value = (void*)new_batters_left;
		Array_List_add(list,key_value);
	}
	db->free_result(result);
	for(i=0;i<list->length;i++){
		Key_Value key_value = gget(list,i);
		num_chars = sprintf(query,UPDATE_BATTERS_LEFT,key_value->value,key_value->key);
		db->execute_query(db->conn,query,num_chars);
		free(key_value);
	}
}
Example #6
0
File: sexpr.C Project: bougyman/sfs
void
consume_whitespace ()
{
  while (1) {
    int c = gget ();
    if (isspace (c))
      continue;
    else if (c == ';') {
      for (; c != EOF && c != '\n' && c != '\r'; c = gget ()) ;
    }
    else {
      gunget (c);
      break;
    }
  }
}
Example #7
0
Arrow( )
{
double x1, y1, x2, y2;
gget( Buf, "Points" );
sscanf( Buf, "%lf %lf %lf %lf", &x1, &y1, &x2, &y2 );
arr( x1, y1, x2, y2, 0.3, 0.2, 0.0 );
}
Example #8
0
void update_rotation(Db_Object db, int league_id){
	
	char query[DEFAULT_QUERY_LENGTH];
	const char* SELECT_CURRENT_STARTERS = "select t_rotation.team_id,position from t_rotation join t_team on t_rotation.team_id = t_team.team_id where next_starter = 1 AND league_id = %d and t_team.active = 1;";
	int query_len = sprintf(query,SELECT_CURRENT_STARTERS,league_id);
	native_result* result = db->execute_query_result(db->conn,query,query_len);
	native_row row;
	Array_List list = Array_List_create(sizeof(Key_Value));
	while(row = db->fetch_row(result)){
		int team_id = db->get_column_int(row,0);
		int position = db->get_column_int(row,1);
		PRINT_MESSAGE(1,"team_id: %d starter: %d\n",team_id,position);
		Key_Value starter = malloc(sizeof(struct key_value));
		starter->key = (void*)team_id;
		starter->value = (void*)position;
		PRINT_MESSAGE(1,"starter,key: %d, starter.value: %d\n",starter->key,starter->value);
		Array_List_add(list,starter);
	}
	if(list->length>8){
		exit(1);
	}
	db->free_result(result);
	const char* RESET_NEXT_STARTER = "Update t_rotation set next_starter = 0 where team_id = %d; update t_lineup set bat_order=0, position=0 where team_id=%d and position=1;";
	const char* SELECT_NEW_STARTER = "select player_id from t_rotation where team_id = %d and position = %d;";
	const char* UPDATE_ROTATION = "update t_rotation set next_starter = 1 where team_id = %d and position = %d; update t_lineup set position =1, bat_order=9 where player_id=%d;";
	int i;
	for(i=0;i<list->length;i++){
		int team_id = (int)((Key_Value)gget(list,i))->key;
		int position = (int)((Key_Value)gget(list,i))->value;
		position = (position%5) + 1;
		query_len = sprintf(query,RESET_NEXT_STARTER,team_id,team_id);
		db->execute_query(db->conn,query,query_len);
		int query_len = sprintf(query,SELECT_NEW_STARTER,team_id,position);
		result = db->execute_query_result(db->conn,query,query_len);
		row = db->fetch_row(result);
		int new_starter_id = db->get_column_int(row,0);
		db->free_result(result);
		query_len = sprintf(query,UPDATE_ROTATION,team_id,position,new_starter_id);
		db->execute_query(db->conn,query,query_len);		
	}
	Array_List_free(list,1);
}
Example #9
0
File: proc.c Project: machinaut/go
// Get the next goroutine that m should run.
// Sched must be locked on entry, is unlocked on exit.
// Makes sure that at most $GOMAXPROCS gs are
// running on cpus (not in system calls) at any given time.
static G*
nextgandunlock(void)
{
	G *gp;

	if(runtime·sched.mcpu < 0)
		runtime·throw("negative runtime·sched.mcpu");

	// If there is a g waiting as m->nextg,
	// mnextg took care of the runtime·sched.mcpu++.
	if(m->nextg != nil) {
		gp = m->nextg;
		m->nextg = nil;
		schedunlock();
		return gp;
	}

	if(m->lockedg != nil) {
		// We can only run one g, and it's not available.
		// Make sure some other cpu is running to handle
		// the ordinary run queue.
		if(runtime·sched.gwait != 0)
			matchmg();
	} else {
		// Look for work on global queue.
		while(runtime·sched.mcpu < runtime·sched.mcpumax && (gp=gget()) != nil) {
			if(gp->lockedm) {
				mnextg(gp->lockedm, gp);
				continue;
			}
			runtime·sched.mcpu++;		// this m will run gp
			schedunlock();
			return gp;
		}
		// Otherwise, wait on global m queue.
		mput(m);
	}
	if(runtime·sched.mcpu == 0 && runtime·sched.msyscall == 0)
		runtime·throw("all goroutines are asleep - deadlock!");
	m->nextg = nil;
	m->waitnextg = 1;
	runtime·noteclear(&m->havenextg);
	if(runtime·sched.waitstop && runtime·sched.mcpu <= runtime·sched.mcpumax) {
		runtime·sched.waitstop = 0;
		runtime·notewakeup(&runtime·sched.stopped);
	}
	schedunlock();

	runtime·notesleep(&m->havenextg);
	if((gp = m->nextg) == nil)
		runtime·throw("bad m->nextg in nextgoroutine");
	m->nextg = nil;
	return gp;
}
Example #10
0
void* dgetValue(Dict dict,void* key)
{
	int index = gindexOf(dict->keys,key);
	if(index!=-1)
	{	
		return gget(dict->values,index);
	}
	else
	{
//		fprintf(stderr,"Key not in dictionary.\n");
		return NULL;
	}
}	
Example #11
0
File: sexpr.C Project: bougyman/sfs
void
gread_symbol (gread_state *state)
{
  int c = gget ();
  if (issymbolchar (c)) {
    state->chars->append (tolower (c));
    ggoto (gread_symbol, state);
  }
  else {
    gunget (c);
    gsymbol *sym = g_symbol (*(state->chars));
    delete state->chars;
    delete state;
    greturn (sym);
  }
}
Example #12
0
/*
NOTE: This function only works if the values are all integers.

This function will add a new item to the dictionary if
the item is not in the dictionary.  If the item is in the dictionary,
it will incriment its value.
*/		
void addOrIncriment(Dict dictionary,void* item)
{
	int value;
	int index = gindexOf(dictionary->keys,item);
	if(index>-1)
	{
//		printf("incrimenting value\n");
		value = (int)gget(dictionary->values,index);
		gset(dictionary->values,index,(void*)value+1);
	}
	else
	{
		daddKey(dictionary,item,(void*)1);
	}

}
Example #13
0
File: sexpr.C Project: bougyman/sfs
void
gread_integer (gread_state *state)
{
  int c = gget ();
  if (isdigit (c)) {
    state->chars->append (c);
    ggoto (gread_integer, state);
  }
  else {
    gunget (c);
    // XX: strtoul?
    ginteger *num = New ginteger (atol (state->chars->cstr ()));
    delete state->chars;
    delete state;
    greturn (num);
  }
}
Example #14
0
File: proc.c Project: machinaut/go
// Kick off new ms as needed (up to mcpumax).
// There are already `other' other cpus that will
// start looking for goroutines shortly.
// Sched is locked.
static void
matchmg(void)
{
	G *g;

	if(m->mallocing || m->gcing)
		return;
	while(runtime·sched.mcpu < runtime·sched.mcpumax && (g = gget()) != nil){
		M *m;

		// Find the m that will run g.
		if((m = mget(g)) == nil){
			m = runtime·malloc(sizeof(M));
			// Add to runtime·allm so garbage collector doesn't free m
			// when it is just in a register or thread-local storage.
			m->alllink = runtime·allm;
			runtime·allm = m;
			m->id = runtime·sched.mcount++;

			if(runtime·iscgo) {
				CgoThreadStart ts;

				if(libcgo_thread_start == nil)
					runtime·throw("libcgo_thread_start missing");
				// pthread_create will make us a stack.
				m->g0 = runtime·malg(-1);
				ts.m = m;
				ts.g = m->g0;
				ts.fn = runtime·mstart;
				runtime·asmcgocall(libcgo_thread_start, &ts);
			} else {
				if(Windows)
					// windows will layout sched stack on os stack
					m->g0 = runtime·malg(-1);
				else
					m->g0 = runtime·malg(8192);
				runtime·newosproc(m, m->g0, m->g0->stackbase, runtime·mstart);
			}
		}
		mnextg(m, g);
	}
}
Example #15
0
// Kick off new m's as needed (up to mcpumax).
// Sched is locked.
static void
matchmg(void)
{
	G *gp;
	M *mp;

	if(m->mallocing || m->gcing)
		return;

	while(haveg() && canaddmcpu()) {
		gp = gget();
		if(gp == nil)
			runtime·throw("gget inconsistency");

		// Find the m that will run gp.
		if((mp = mget(gp)) == nil)
			mp = runtime·newm();
		mnextg(mp, gp);
	}
}
Example #16
0
File: sexpr.C Project: bougyman/sfs
void
gread_gob (gread_state *ignore)
{
  consume_whitespace ();
  int c = gget ();

  switch (c) {
    case EOF:
      gerror (); // unexpected end of input
    case '(':
      {
      gread_res = NULL;
      gread_state *state = New gread_state;
      state->list.elms = state->list.elmstail = NULL;
      ggoto (gread_list, state);
      }
    case ')':
      gerror (); // unbalanced parentheses
    case '"':
      {
      gread_state *state = New gread_state;
      state->chars = New str;
      ggoto (gread_string, state);
      }
    default:
      if (isdigit (c)) {
        gread_state *state = New gread_state;
        state->chars = New str;
        state->chars->append (c);
        ggoto (gread_integer, state);
      }
      else if (issymbolchar (c)) {
        gread_state *state = New gread_state;
        state->chars = New str;
        state->chars->append (tolower (c));
        ggoto (gread_symbol, state);
      }
      else
        gerror (); // unexpected character
  }
}
Example #17
0
File: sexpr.C Project: bougyman/sfs
void
gread_list (gread_state *state)
{
  if (gread_res) {
    gpair *newpair = New gpair (gread_res, bottom);
    gpair *head = state->list.elms;
    gpair *tail = state->list.elmstail;
    if (tail) {
      tail->cdr = newpair;
      tail = newpair;
    }
    else
      head = tail = newpair;
    state->list.elms = head;
    state->list.elmstail = tail;
  }

  consume_whitespace ();
  int c = gget ();
  switch (c) {
    case EOF:
      gerror ();
    case ')':
      {
        gpair *elms = state->list.elms;
        delete state;
        if (elms)
          greturn (elms);
        else
          greturn (bottom);
      }
    default:
      gunget (c);
      gpush (gread_list, state);
      ggoto (gread_gob, NULL);
  }
}
Example #18
0
int main(int argc, char** argv){
	
	int seed = getpid();
	int c;
	int option_index = 0;
	int league_id;
	int num_players;
	
	struct argp_option options[] = {
		{"seed",'s', "int", 0, 0},
		{"league-id", 'l', "int", 0, 0},
		{"players", 'n', "int", 0, 0},
		{"hitter-mean",'h', "double", 0, 0},
		{"hitter-sd", 'i', "double", 0, 0},
		{"pitcher-mean", 'p', "double", 0, 0},
		{"pitcher-sd", 'r', "double", 0, 0},
		{0}
	};

	struct arguments args = {&seed, &league_id, &num_players, &HITTER_MEAN,
					&HITTER_SD, &PITCHER_MEAN, &PITCHER_SD};
	struct argp argp = {options, arg_parser, 0, 0, global_child}; 
	argp_parse(&argp, argc, argv, 0, 0, &args);

#ifdef _USEGSL_
	gsl_rng_env_setup();
	const gsl_rng_type *type = gsl_rng_default;
	rng = gsl_rng_alloc(type);
	gsl_rng_set(rng,seed);
#else
	srand(seed);
#endif
	Db_Object db = db_connect(CONFIG_FILE);
	db->begin_transaction(db->conn);
	fprintf(stderr,"Seed: %d\n",seed);

	Array_List first_names = Array_List_create(sizeof(char*));
	Array_List last_names = Array_List_create(sizeof(char*));

	select_names(db,first_names,last_names);
	int i;

	struct player new_player;
	struct hitter_skill hitter_skill;
	struct pitcher_skill pitcher_skill;
	struct fielder_skill fielder_skill;

	for(i=0;i<num_players;i++){
		
		int power= random_normal(HITTER_MEAN,HITTER_SD,MIN_SKILL,MAX_SKILL);
		int contact = random_normal(HITTER_MEAN,HITTER_SD,MIN_SKILL,MAX_SKILL);
		int fielding = random_normal(HITTER_MEAN,HITTER_SD,MIN_SKILL,MAX_SKILL);
		int speed = random_normal(HITTER_MEAN,HITTER_SD,MIN_SKILL,MAX_SKILL);
		int intelligence = random_normal(HITTER_MEAN,HITTER_SD,MIN_SKILL,MAX_SKILL);
		int control = random_normal(PITCHER_MEAN,PITCHER_SD,MIN_SKILL,MAX_SKILL);
		int movement = random_normal(PITCHER_MEAN,PITCHER_SD,MIN_SKILL,MAX_SKILL);
		int velocity = random_normal(PITCHER_MEAN,PITCHER_SD,MIN_SKILL,MAX_SKILL);
		int endurance = 0;
		int bats = (rand()%100)+1;
		int throws = (rand()%100)+1;
		int position = rand()%18;

/*TODO: Make the choosing of names random.*/
		int first_name = 8 * (power + contact + fielding + speed + bats) % first_names->length;
		int last_name = 8 * (first_name + speed + intelligence + control + movement + velocity + bats + throws + control + movement + position) % last_names->length;
		
		if(throws <= PERCENT_THROW_RIGHT){
			new_player.throws = RIGHT;
		}
		else{
			new_player.throws = LEFT;
		}
		
		if(bats<= PERCENT_HIT_SAME){
			new_player.bats = new_player.throws;
		}
		else if(bats<PERCENT_HIT_SAME+PERCENT_HIT_DIFFERENT){
			if(new_player.throws == RIGHT){
				new_player.bats = LEFT;
			}
			else{
				new_player.bats = RIGHT;
			}
		}
		else{
			new_player.bats = SWITCH;
		}
		if(position <= 9 && position > 1){
			new_player. position = position;
			pitcher_skill.end = random_normal(30.0,PITCHER_SD*SD_TWEAK,0,49);
		}
		else if(position>=15 || position == 1){
			new_player.position =1;
			pitcher_skill.end = random_normal(80.0,PITCHER_SD*SD_TWEAK,50,100);
		}
		else{
			new_player.position = 0;
			pitcher_skill.end = random_normal(30.0,PITCHER_SD*SD_TWEAK,0,49);
		}
		int vs_right_mod = 5;
		int vs_left_mod = 5;
		if(new_player.bats == RIGHT){
			vs_right_mod = -5;
		}
		if(new_player.bats ==LEFT){
			vs_left_mod = -5;
		}
		
		new_player.first_name = gget(first_names,first_name);
		new_player.last_name = gget(last_names,last_name);
		
		hitter_skill.cvr = calc_skill(contact+vs_right_mod,HITTER_SD);
		hitter_skill.pvr = calc_skill(power+vs_right_mod,HITTER_SD);
		hitter_skill.cvl = calc_skill(contact+vs_left_mod,HITTER_SD);
		hitter_skill.pvl = calc_skill(power+vs_left_mod,HITTER_SD);
		hitter_skill.spd = speed;	
		
		fielder_skill.range = calc_skill(fielding,HITTER_SD);
		fielder_skill.arm = calc_skill(fielding,HITTER_SD);
		fielder_skill.field = calc_skill(fielding,HITTER_SD);
		
		new_player.intelligence = intelligence;
		
		pitcher_skill.mov = calc_skill(movement,PITCHER_SD);
		pitcher_skill.vel = calc_skill(velocity,PITCHER_SD);
		pitcher_skill.ctrl = calc_skill(control,PITCHER_SD);
		
		insert_player(db,&new_player,&hitter_skill,&pitcher_skill,&fielder_skill,league_id);
		
	}
	db->commit(db->conn);
	db->close_connection(db->conn);	
#ifdef _USEGSL_
	gsl_rng_free(rng);
#endif

	return 0;	
}
Example #19
0
// Get the next goroutine that m should run.
// Sched must be locked on entry, is unlocked on exit.
// Makes sure that at most $GOMAXPROCS g's are
// running on cpus (not in system calls) at any given time.
static G*
nextgandunlock(void)
{
	G *gp;
	uint32 v;

top:
	if(atomic_mcpu(runtime·sched.atomic) >= maxgomaxprocs)
		runtime·throw("negative mcpu");

	// If there is a g waiting as m->nextg, the mcpu++
	// happened before it was passed to mnextg.
	if(m->nextg != nil) {
		gp = m->nextg;
		m->nextg = nil;
		schedunlock();
		return gp;
	}

	if(m->lockedg != nil) {
		// We can only run one g, and it's not available.
		// Make sure some other cpu is running to handle
		// the ordinary run queue.
		if(runtime·sched.gwait != 0) {
			matchmg();
			// m->lockedg might have been on the queue.
			if(m->nextg != nil) {
				gp = m->nextg;
				m->nextg = nil;
				schedunlock();
				return gp;
			}
		}
	} else {
		// Look for work on global queue.
		while(haveg() && canaddmcpu()) {
			gp = gget();
			if(gp == nil)
				runtime·throw("gget inconsistency");

			if(gp->lockedm) {
				mnextg(gp->lockedm, gp);
				continue;
			}
			runtime·sched.grunning++;
			schedunlock();
			return gp;
		}

		// The while loop ended either because the g queue is empty
		// or because we have maxed out our m procs running go
		// code (mcpu >= mcpumax).  We need to check that
		// concurrent actions by entersyscall/exitsyscall cannot
		// invalidate the decision to end the loop.
		//
		// We hold the sched lock, so no one else is manipulating the
		// g queue or changing mcpumax.  Entersyscall can decrement
		// mcpu, but if does so when there is something on the g queue,
		// the gwait bit will be set, so entersyscall will take the slow path
		// and use the sched lock.  So it cannot invalidate our decision.
		//
		// Wait on global m queue.
		mput(m);
	}

	// Look for deadlock situation.
	// There is a race with the scavenger that causes false negatives:
	// if the scavenger is just starting, then we have
	//	scvg != nil && grunning == 0 && gwait == 0
	// and we do not detect a deadlock.  It is possible that we should
	// add that case to the if statement here, but it is too close to Go 1
	// to make such a subtle change.  Instead, we work around the
	// false negative in trivial programs by calling runtime.gosched
	// from the main goroutine just before main.main.
	// See runtime·main above.
	//
	// On a related note, it is also possible that the scvg == nil case is
	// wrong and should include gwait, but that does not happen in
	// standard Go programs, which all start the scavenger.
	//
	if((scvg == nil && runtime·sched.grunning == 0) ||
	   (scvg != nil && runtime·sched.grunning == 1 && runtime·sched.gwait == 0 &&
	    (scvg->status == Grunning || scvg->status == Gsyscall))) {
		runtime·throw("all goroutines are asleep - deadlock!");
	}

	m->nextg = nil;
	m->waitnextg = 1;
	runtime·noteclear(&m->havenextg);

	// Stoptheworld is waiting for all but its cpu to go to stop.
	// Entersyscall might have decremented mcpu too, but if so
	// it will see the waitstop and take the slow path.
	// Exitsyscall never increments mcpu beyond mcpumax.
	v = runtime·atomicload(&runtime·sched.atomic);
	if(atomic_waitstop(v) && atomic_mcpu(v) <= atomic_mcpumax(v)) {
		// set waitstop = 0 (known to be 1)
		runtime·xadd(&runtime·sched.atomic, -1<<waitstopShift);
		runtime·notewakeup(&runtime·sched.stopped);
	}
	schedunlock();

	runtime·notesleep(&m->havenextg);
	if(m->helpgc) {
		runtime·gchelper();
		m->helpgc = 0;
		runtime·lock(&runtime·sched);
		goto top;
	}
	if((gp = m->nextg) == nil)
		runtime·throw("bad m->nextg in nextgoroutine");
	m->nextg = nil;
	return gp;
}
Example #20
0
Rangebar( )
{
int f[8], row, i, doends, nf, nv, ir, mlrightonly, label, xf;
double val[8];
double w, x, shade, lblpos, mlw;

gget( Buf, "Nval" );
nv = atoi( Buf );

gget( Buf, "Field" );
if( nv == 1 ) { nf = sscanf( Buf, "%d", &f[1] ); f[2] = f[3] = f[1]; f[4] = f[5] = f[1]; }
else if( nv == 2 ) { nf = sscanf( Buf, "%d %d", &f[1], &f[4] ); f[2] = f[3] = f[1]; f[5] = f[4]; }
else if( nv == 3 ) { nf = sscanf( Buf, "%d %d %d", &f[1], &f[3], &f[5] ); f[2] = f[1]; f[4] = f[5]; }
else if( nv == 4 ) { nf = sscanf( Buf, "%d %d %d %d", &f[1], &f[2], &f[4], &f[5] ); f[3] = f[4]; }
else if( nv == 5 ) { nf = sscanf( Buf, "%d %d %d %d %d", &f[1], &f[2], &f[3], &f[4], &f[5] ); }
if( nf != nv ) { fprintf( stderr, "Expecting %d Field values.\n", nv ); gdp_exit(); }


gget( Buf, "Width" ); w = atof( Buf );

gget( Buf, "Linethick" );
NTlinetype( "0", atof( Buf ), 1.0 );

label = 0;
gget( Buf, "Idfield" );
f[0] = atoi( Buf );
if( f[0] > 0 ) {
	label = 1;
	gget( Buf, "Label.size" );
	if( atoi( Buf ) > 0 ) NTptsize( atoi( Buf ) );

	gget( Buf, "Label.position" );
	lblpos = atof( Buf );

	}

doends = 0;
if( nf == 4 || nf == 5 ) {
	gget( Buf, "Ends" );
	if( Buf[0] == 'y' ) doends = 1;
	}

mlw = w;
mlrightonly = 0;
if( nf == 3 || nf == 5 ) {
	gget( Buf, "Midlinewidth" );
	if( atof( Buf ) > 0 )mlw = atof( Buf );
	gget( Buf, "Midlineright" );
	if( Buf[0] == 'y' ) mlrightonly = 1;
	}

gget( Buf, "Shade" );
shade = atof( Buf );

gget( Buf, "Xfield" );
xf = atoi( Buf );


x = DXlo;
for( ir = 0; ir < N_d_rows; ir++ ) {
	for( i = 1; i <= 5; i++ ) val[i] = atof( D[ ir ][ f[i]-1 ] );
	if( nv == 1 ) { val[1] = val[2] = 0; }
	if( xf > 0 ) x = atof( D[ir][ xf-1 ] );
	else x++;

	NTm( x-(w/2), val[2] ); /* lower edge of box */
	NTp( x+(w/2), val[2] );
	NTp( x+(w/2), val[4] );
	NTp( x-(w/2), val[4] );
	NTp( x-(w/2), val[2] ); /* upper edge */
	NTshade( shade );
	
	NTm( x, val[1] ); /* lower tail */
	NTl( x, val[2] );
	if( doends ) { NTm( x-(w/2.7), val[1] ); NTl( x+(w/2.7), val[1] ); }
	
	NTm( x-(w/2), val[2] ); /* lower edge of box */
	NTl( x+(w/2), val[2] );
	NTl( x+(w/2), val[4] );
	NTl( x-(w/2), val[4] );
	NTl( x-(w/2), val[2] ); /* upper edge */
	
	NTm( x, val[4] );
	NTl( x, val[5] ); /* upper tail */
	if( doends ) { NTm( x-(w/2.7), val[5] ); NTl( x+(w/2.7), val[5] ); }
	
	if( mlrightonly )NTm( x-(w/2), val[3] ); /* median line */
	else NTm( x-(mlw/2), val[3] ); 
	NTl( x+(mlw/2), val[3] );
	
	if( label ) {
		NTmov( da_x(x) -1, da_y(lblpos) );  /* print label */
		sprintf( Buf, "%s", D[ir][f[0]-1 ] );
		NTcentext( Buf, 2 );
		}
	
	}
NTnormline();
}
Distribution( )
{
int 	i, j, 
	dups, 
	cluster, 
	p, 
	dist, 
	justdist,
	xfield, 
	yfield, 
	idfield,
	sizefield,
	shadefield,
	markfield;
double 	adjx, adjy, 
	xdat, ydat,
	x, y, 
	prevx, prevy, 
	charh, charv, 
	size,
	sizescale,
	shade,
	shadescale,
	distlen;
char 	c[20];
FILE 	*fp, 
	*fp2;
static double xofst[38] = { 0, 0, 4, 0, -4, 4, -4, -4, 4,
	0, -8, 0, 8, 4, -8, 4, 8, -4, -8, -4, 8,
	0, 0, 12, -12, 4, 4, 12, -12, -4, -4, 12, -12,
	8, -8, -8, 8 };
static double yofst[38] = { 0, 4, 0, -4, 0, 4, -4, 4, -4,
	-8, 0, 8, 0, -8, 4, 8, 4, -8, -4, 8, -4,
	12, -12, 0, 0, 12, -12, 4, 4, 12, -12, -4, -4,
	8, -8, 8, -8 };
static double distofst[38] = { 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8, -8, 
	9, -9, 10, -10, 11, -11, 12, -12, 13, -13, 14, -14, 15, -15, 16, -16, 17, -17, 18, -18 };

gget( Buf, "Xfield" ); xfield = atoi( Buf );
gget( Buf, "Yfield" ); yfield = atoi( Buf );
if( xfield < 1 ) { fprintf( stderr, "1 or 2 fdata fields need to be given for distributions.\n" ); gdp_exit(); }
if( yfield < 1 ) {
	justdist = YES; 
	yfield = xfield;
	}
else justdist = NO;
gget( Buf, "Idfield" ); idfield = atoi( Buf );
gget( Buf, "Mark.field" ); markfield = atoi( Buf );

gget( c, "Mark" );
gget( Buf, "Mark.font" ); NTfont( Buf ); 
gget( Buf, "Mark.size" ); NTptsize( atof( Buf ) );
charv = Chh / 2.0;
charh = Chh / 4.0;
size = Chh * 0.4;
gget( Buf, "Cluster" ); if( Buf[0] == 'y' ) cluster = YES; else cluster = NO;
dist = NO;
gget( Buf, "Axdist" ); 
if( Buf[0] == 'y' ) { 
	dist = YES; 
	gget( Buf, "Axdist.length" ); distlen = atof( Buf );
	}
gget( Buf, "Sizefield" ); sizefield = atoi( Buf );
gget( Buf, "Sizescale" ); sizescale = atof( Buf );
gget( Buf, "Shadefield" ); shadefield = atoi( Buf );
gget( Buf, "Shadescale" ); shadescale = atof( Buf );

if( cluster ) {		/* sort the data numerically */
	/* write out data */
	fp = fopen( Tempfile, "w" );
	if( fp == NULL ) fprintf( stderr, "Distribution can't open: %s", Tempfile );	
	for( i = 0; i < N_d_rows; i++ ) {
		for( j = 0; j < N_d_fields; j++ ) fprintf( fp, "%s ", D[i][j] );
		fprintf( fp, "\n" );
		}
	fclose( fp );
	/* build Unix sort command */
	sprintf( Buf, "sort -n %s +%d -%d +%d -%d -o %s", Tempfile, xfield-1, xfield, yfield-1, yfield, Tempfile );
	system( Buf );
	fp2 = fopen( Tempfile, "r" );
	for( i = 0; i < N_d_rows; i++ ) {
		for( j = 0; j < N_d_fields; j++ ) { fscanf( fp2, "%s", D[i][j] ); }
		}
	fclose( fp2 );
	}

x = -99999.0;
y = -99999.0;
dups = 0;
for( i = 0; i < N_d_rows; i++ ) {
	if( (!goodnum( D[i][yfield-1], &p ) && !justdist) || !goodnum( D[i][xfield-1], &p )) {
		if( idfield ) fprintf( stderr, "%s ", D[i][idfield-1] );
		else fprintf( stderr, "row %d ", i ); 
 		fprintf( stderr, "is bad: (%s,%s) (Warning)\n", D[i][xfield-1], D[i][yfield-1] );
		continue;
		}
	x = da_x( atof( D[i][xfield-1] ) );
	y = da_y( atof( D[i][yfield-1] ) );
	if( (!justdist && (y < Ylo || y > Yhi )) || x < Xlo || x > Xhi ) { 
		if( idfield )fprintf( stderr, "%s ", D[i][idfield-1] );
		else fprintf( stderr, "Row %d ", i );
		fprintf( stderr, "is out of bounds: (%s,%s) (Warning)\n", D[i][xfield-1], D[i][yfield-1] );
		continue; 
		}  
	if( cluster && (x == prevx && y == prevy ) ) {
		dups++;
		if( dups > 36 ) dups = 1;
		if( justdist ) adjx = x + (.01*distofst[dups]);
		else adjx = x + (.01*xofst[dups]);
		adjy = y + (.01*yofst[dups]);
		}
	else	{
		dups = 0;
		prevx = x;
		prevy = y;
		adjx = x;
		adjy = y;
		}
	if( !justdist ) {
		if( sizefield > 0 && sizefield <= 24 ) {
			size = ( atof( D[i][sizefield-1] ) * sizescale) / 144.0; 
			if( size < 0.001 || size > 3 ) {
				fprintf( stderr, "warning, rec. %d, abnormal size data value\n", i  ); 
				}
			}
		if( shadefield > 0 && shadefield <= 24 ) {
			shade = atof( D[i][shadefield-1] ) * shadescale;
			if( shade < 0 || shade > 1 ) {
				fprintf( stderr, "warning, rec %d, abnormal shade data value, truncated.\n", i );
				if( shade < 0 ) shade = 0;
				if( shade > 1 ) shade = 1;
				}
			sprintf( c, "%s%4.2f", c, shade );
			}
			
			
		if( strncmp( c, "sym", 3 )==0 ) {
			point( adjx, adjy, c, size );
			c[5] = '\0'; /* get rid of shade if any */
			}
		if( markfield || strncmp( c, "sym", 3 )!=0 ) {
			NTmov( (adjx-(charh*0.20))-1, adjy-(charv*0.48) );
			if( markfield ) NTcentext( D[i][markfield-1], 2 );
			else NTcentext( c, 2 );
			}
		}
	else if( justdist ) {
		NTmov( adjx, Ylo ); NTlin( adjx, Yhi );
		}
	if( dist && !justdist ) {
		NTmov( Xhi, adjy ); NTlin( Xhi+distlen, adjy );
		NTmov( adjx, Yhi ); NTlin( adjx, Yhi+distlen );
		}
	}
}
Example #22
0
Text( )
{
int sys, ln, embedded, p, ix;
double x, y, theta, size;
char s1[40], s2[40];
FILE *fp;

gget( Buf, "System" );
if( Buf[0] == 'd' ) { 
	sys = DATA; 
	if( DXlo == 0 && DXhi == 0 ) { fprintf( stderr, "No graphics area.\n" ); gdp_exit(); }
	}
else sys = ABSOLUTE;

gget( Buf, "Font" ); NTfont( Buf );
gget( Buf, "Size" ); size = atof( Buf ); NTptsize( size );
gget( Buf, "Position" ); 
sscanf( Buf, "%lf %lf", &x, &y );
gget( Buf, "Direction" ); theta = atof( Buf );

if( theta != 0 ) NTchardir( theta );

gget( Buf, "Embeddedcoords" );
if( Buf[0] == 'y' ) embedded = 1; else embedded = 0;

gget( Buf, "File" );
if( strlen( Buf ) > 0 ) fp = fopen( Buf, "r" );		/* text in a file */
else 	{		/* text given-- put it in a file */
	gget( Buf, "Text" );
	text_tofile( Buf, Tempfile );
	fp = fopen( Tempfile, "r" );
	}
if( fp == NULL ) { fprintf( stderr, "Cant open text file" );  gdp_exit(); }

/* read the file and print the text */
if( !embedded ) {
	if( sys == DATA ) NTm( x, y );
	else NTmov( x, y );
	}

ln = 1;
while( fgets( Buf, 200, fp ) != NULL ) {
	if( embedded ) {
		ix = 0;
		strcpy( s1, getok( Buf, &ix ) );
		strcpy( s2, getok( Buf, &ix ) );
		if( goodnum( s1, &p ) && goodnum( s2, &p ) ) {
			x = atof( s1 ); y = atof( s2 );
			if( sys == DATA ) NTm( x, y );
			else NTmov( x, y );
			strcpy( Buf, &Buf[ix] );
			strip_ws( Buf );
			}
		}
	NTtext( Buf );
	if( sys == DATA ) NTmov( da_x(x), da_y(y)-(ln*Chh) ); 
	else NTmov( x, y-(ln*Chh) );
	ln++;
	}

fclose( fp );
NTchardir( 0 );
}
Example #23
0
Vbargraph( )
{
int 	k[8], 
	idf[8],
	format,
	label,
	n,
	nc,
	i, j, jj,
	p,
	start,
	yset,
	yfld,
	outline;

double s[8],
	accum,
	zer,
	cury,
	yspace,
	subspace,
	x,
	x2,
	f,
	lblpos,
	sep,
	msep = 0.03;
char str[10];


/* get the data field list */
gget( Buf, "Field" ); 
n = 0;
while( n < 1 ) { 
	n = sscanf( Buf, "%d %d %d %d %d %d %d %d", &k[0], &k[1], &k[2], &k[3], &k[4], &k[5], &k[6], &k[7] );
	if( N_d_fields == 1 ) strcpy( Buf, "1" );
	else if( n < 1 ) { fprintf( stderr, "Field bad\n" ); gdp_exit(); }
	}
for( i = 0; i < n; i++ ) 
	if( k[i] < 1 || k[i] > N_d_fields ) { fprintf( stderr, "Field out of range" ); gdp_exit(); }

/* get the label field list, if any */
gget( Buf, "Idfield" ); 
if( strlen( Buf ) > 0 ) { 
	label = YES;
	sscanf( Buf, "%d %d %d %d %d %d %d %d", &idf[0], &idf[1], &idf[2], &idf[3], &idf[4], &idf[5], &idf[6], &idf[7] );
	for( i = 0; i < n; i++ ) 
		if( idf[i] < 1 || idf[i] > N_d_fields ) { fprintf( stderr, "Idfield bad.\n" ); gdp_exit(); }
	}
else label = NO;


gget( Buf, "Format" );  
if( strcmp( Buf, "stack" )==0 ) format = STACK;
else format = CLUSTER;

DYtic = 1.0;

/* get bar shades */
gget( Buf, "Shade" ); 
sscanf( Buf, "%lf %lf %lf %lf %lf %lf %lf %lf", &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7] );

/* get zero line */
gget( Buf, "Zeroat" ); 
if( goodnum( Buf, &p )) zer = atof( Buf );
else zer = DXlo;

/* get label size */
if( label ) { gget( Buf, "Idfield.size" ); NTptsize( atof( Buf ) ) };

/* distance of label from bar top */
if( label ) { gget( Buf, "Idfield.position" ); lblpos = atof( Buf ); }

/* outline or not */
gget( Buf, "Outlinebars" ); if( Buf[0] == 'y' ) outline = YES; else outline = NO;

/* x distance between major bar spaces */
gget( Buf, "Separation" ); sep = atof( Buf );

gget( Buf, "Separation.sub" ); msep = atof( Buf );


yspace = ( (Yhi-Ylo)/((DYhi-DYlo)+1) ) - (sep*Scale_y);

if( format == CLUSTER ) nc = n; else nc = 1;

gget( Buf, "Killwild" ); /* option for aborting plot if any values out of range */
if( atof( Buf ) != 0 ) {
	for( i = 1; i <= N_d_rows; i++ ) {
		if( atof( D[i-1][ k[0] -1 ] ) > atof( Buf ) ) {
			fprintf( stderr, "Note: This Vbargraph terminated due to a value of %s.\n", D[i-1][k[0]-1] );
			return( 0 );
			}
		}
	}

gget( Buf, "Yfield" ); /* allow placement of bars by a data field */
if( strlen( Buf ) > 0 ) {
	yset = 1;
	yfld = atoi( Buf );
	if( yfld < 1 || yfld > N_d_fields ) { fprintf( stderr, "Yfield bad.\n" ); gdp_exit(); }
	}
else yset = 0;

gget( Buf, "Ystart.0or1" ); /* allow starting at 0 or 1 */
if( Buf[0] == '0' ) { start = 0; }
else { start = 1; }

for( i = 1; i <= N_d_rows; i++ ) {
	if( yset ) cury = da_y( atof( D[i-1][yfld-1] ) ) - (yspace/2);
	else cury = da_y((double)(DYlo+i+(start-1))) - (yspace/2);
	subspace = ( yspace / nc );
	for( j = 0; j < nc; j++ ) {
		if( !goodnum( D[i-1][ k[j]-1 ], &p )) {
			fprintf( stderr, "Warning: row %d, field %d, is bad (%s)\n", i, k[j], D[i-1][ k[j]-1] );
			cury += subspace;
			continue;
			}
		x = atof(D[i-1][ k[j]-1 ]);
		if( x != DXlo )
		    ab_rect( da_x(zer), cury, da_x(x), cury+(subspace-msep), s[j], (s[j]==1)?1:outline );
		if( label ) {
			if( x < zer || format == STACK ) f = (-lblpos)-Chh; else f = lblpos;
			strcpy( str, D[i-1][ idf[j]-1 ] );
			NTmov( da_x(x)+f, cury );
			NTtext( str ); /* will change */
			}
		if( format == STACK ) for( jj = 1; jj < n; jj++ ) {
			if( !goodnum( D[i-1][ k[jj] -1 ], &p ) ) {
				fprintf( stderr, "Warning: row %d, field %d, is bad (%s)\n", i, k[jj], D[i-1][k[jj]-1] );
				continue;
				}
			x2 = x + atof( D[i-1][ k[jj] -1 ] );
			if( x2 != DXlo )
			   ab_rect( da_x(x), cury,  da_x(x2), cury+(subspace-msep), s[jj], (s[jj]==1)?1:outline );
			if( label ) {
				if( x2 < zer || format == STACK ) f = (-lblpos)-Chh; else f = lblpos;
				NTmov( da_x(x2)+f, cury );
				strcpy( str, D[i-1][ idf[jj]-1 ] );
				NTtext( str );
				}
			x = x2;
			}

		cury += subspace;
		}
	}

gget( Buf, "Segment" );
if( Buf[0] == 'y' ) 
	for( f = DXlo+DXtic; f < DXhi; f += DXtic ) 
		rect(  f-(DXhi*0.003), DYlo + 0.2, f+(DXhi*0.004), DYhi - 0.2, (double)WHITE, 0 );
}
Example #24
0
File: proc.c Project: Sunmonds/gcc
// Get the next goroutine that m should run.
// Sched must be locked on entry, is unlocked on exit.
// Makes sure that at most $GOMAXPROCS g's are
// running on cpus (not in system calls) at any given time.
static G*
nextgandunlock(void)
{
	G *gp;
	uint32 v;

top:
	if(atomic_mcpu(runtime_sched.atomic) >= maxgomaxprocs)
		runtime_throw("negative mcpu");

	// If there is a g waiting as m->nextg, the mcpu++
	// happened before it was passed to mnextg.
	if(m->nextg != nil) {
		gp = m->nextg;
		m->nextg = nil;
		schedunlock();
		return gp;
	}

	if(m->lockedg != nil) {
		// We can only run one g, and it's not available.
		// Make sure some other cpu is running to handle
		// the ordinary run queue.
		if(runtime_sched.gwait != 0) {
			matchmg();
			// m->lockedg might have been on the queue.
			if(m->nextg != nil) {
				gp = m->nextg;
				m->nextg = nil;
				schedunlock();
				return gp;
			}
		}
	} else {
		// Look for work on global queue.
		while(haveg() && canaddmcpu()) {
			gp = gget();
			if(gp == nil)
				runtime_throw("gget inconsistency");

			if(gp->lockedm) {
				mnextg(gp->lockedm, gp);
				continue;
			}
			runtime_sched.grunning++;
			schedunlock();
			return gp;
		}

		// The while loop ended either because the g queue is empty
		// or because we have maxed out our m procs running go
		// code (mcpu >= mcpumax).  We need to check that
		// concurrent actions by entersyscall/exitsyscall cannot
		// invalidate the decision to end the loop.
		//
		// We hold the sched lock, so no one else is manipulating the
		// g queue or changing mcpumax.  Entersyscall can decrement
		// mcpu, but if does so when there is something on the g queue,
		// the gwait bit will be set, so entersyscall will take the slow path
		// and use the sched lock.  So it cannot invalidate our decision.
		//
		// Wait on global m queue.
		mput(m);
	}

	v = runtime_atomicload(&runtime_sched.atomic);
	if(runtime_sched.grunning == 0)
		runtime_throw("all goroutines are asleep - deadlock!");
	m->nextg = nil;
	m->waitnextg = 1;
	runtime_noteclear(&m->havenextg);

	// Stoptheworld is waiting for all but its cpu to go to stop.
	// Entersyscall might have decremented mcpu too, but if so
	// it will see the waitstop and take the slow path.
	// Exitsyscall never increments mcpu beyond mcpumax.
	if(atomic_waitstop(v) && atomic_mcpu(v) <= atomic_mcpumax(v)) {
		// set waitstop = 0 (known to be 1)
		runtime_xadd(&runtime_sched.atomic, -1<<waitstopShift);
		runtime_notewakeup(&runtime_sched.stopped);
	}
	schedunlock();

	runtime_notesleep(&m->havenextg);
	if(m->helpgc) {
		runtime_gchelper();
		m->helpgc = 0;
		runtime_lock(&runtime_sched);
		goto top;
	}
	if((gp = m->nextg) == nil)
		runtime_throw("bad m->nextg in nextgoroutine");
	m->nextg = nil;
	return gp;
}