Ejemplo n.º 1
0
int main(int argc, char ** argv)
{
  Vector * vector_a = Vector_create(1024);

  for (int i = 0; i < vector_a->length; ++i) {
    vector_a->data[i][0] = i;
    vector_a->data[i][1] = i;
  }

  Vector * vector_b = Vector_create(1024);
  Vector * vector_c = Vector_create(1024);

  for (int i = 0; i < vector_b->length; ++i) {
    vector_b->data[i][0] = i + 1;
  }


  struct timeval stop, start;
  gettimeofday(&start, NULL);
  for (int i = 0; i < 100000; ++i) {
    Vector_multiply_real(vector_c, vector_a, vector_b);
  }
  gettimeofday(&stop, NULL);
  printf("took %lu\n", stop.tv_usec - start.tv_usec);

  Vector * vector_d = Vector_create(2);
  Vector * vector_e = Vector_create(2);
  float d_data[] = {1, 1, 2, 2};
  float e_data[] = {3, -1, 1, 1};

  for (int i = 0; i < 4; ++i) {
      vector_d->data[i / 2][i % 2] = d_data[i];
      vector_e->data[i / 2][i % 2] = e_data[i];
  }

  Vector_print(vector_d);
  Vector_print(vector_e);

  Vector_multiply(vector_d, vector_d, vector_e);

  Vector_print(vector_d);


  Vector_destroy(vector_a);
  Vector_destroy(vector_b);
  Vector_destroy(vector_c);
  Vector_destroy(vector_d);
  Vector_destroy(vector_e);

}
Ejemplo n.º 2
0
void part1(){
  HashMap h = HM_create(Node_hash, Node_equals, NULL, free);
  Vector stack = Vector_create(sizeof(struct Node*));
  struct Node *n;
  while(readline(h, stack));
  solve(stack);
  
  HM_foreach(h, part1_finder, &n);
  
  printf("%d\n", n->id);
  HM_destroy(&h);
  Vector_free(stack);
}
Ejemplo n.º 3
0
void part2(){
  HashMap h = HM_create(Node_hash, Node_equals, NULL, free);
  Vector stack = Vector_create(sizeof(struct Node*));
  struct Node *n;
  int ans = 1;
  while(readline(h, stack));
  solve(stack);

  for(int i = 0; i < 3; ++i){
    n = get_node(h, Output, i);
    ans *= n->inputs[0];
  }
  
  HM_destroy(&h);
  Vector_free(stack);
  printf("%d\n", ans);
}
Ejemplo n.º 4
0
// Test your vector here
int main() { 
	Vector* temp = Vector_create(); //create a vector
	
	Vector_append( temp, "a");//1
	Vector_append( temp, "b");//2
	Vector_append( temp, "c");//3
	Vector_append( temp, "d");//4
	Vector_append( temp, NULL);//5
	Vector_append( temp, "f");//6
	Vector_append( temp, "g");//7
	Vector_append( temp, NULL);//8
	Vector_append( temp, "");//9
	Vector_append( temp, "h");//10
	Vector_append( temp, "i");//11
	Vector_append( temp, NULL);//12
	
	Vector_resize( temp, 20 );
	
	if( Vector_size(temp) != 20 || strcmp( Vector_get( temp, 10 ), "i" ) || Vector_get( temp, 15 ) != NULL )
		perror( "something wrong.\n");
	
	//done for append, resize, get, size
	
	Vector_set( temp, 19, "caibi" );
	Vector_insert( temp, 20, "niubi" );
	Vector_insert( temp, 30, "wori" );
	
	if( Vector_size(temp) != 31 || strcmp( Vector_get( temp, 19 ), "caibi" ) || strcmp( Vector_get( temp, 20 ), "niubi" ) ||  Vector_get( temp, 15 ) != NULL || strcmp( Vector_get( temp, 30 ), "wori" ) )
		perror( "something wrong.\n");
	
	Vector_delete( temp, 11 );
	Vector_delete( temp, 27 );
	Vector_delete( temp, 1 );
	Vector_delete( temp, 18 );
	
	if( Vector_size(temp) != 27 || strcmp( Vector_get( temp, 4 ), "f" ) || strcmp( Vector_get( temp, 26 ), "wori") || Vector_get( temp, 18 ) !=NULL || strcmp( Vector_get( temp, 17 ), "caibi") )
		perror( "something wrong.\n");
	
	
	Vector_destroy( temp );

	return 0; 
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
  Table formt;
  Vector stats;
  gdImagePtr im;
  int black,white,blue;
  char *user, *formdata;
  int size,pos,x;
  float searchscore,searchincrement;
  double thisscore,score,lineheight;
  char label[5];
  int labellen;
  float variance;
  int height,width,labelheight,horizpad;
  int scoremark;
  char *votelabel;
  int lightblue;

  /**********************************
    HTTP header
    ********************************/
  /* use image/png if possible */
  printf("content-type: image/gif\n\n");
  
  /**********************************
    get info about request
    ********************************/
  formt=cgiparse();
  
  formdata=Table_gets(formt,"WIDTH");
  if (formdata!=NULL)
    width=atoi(formdata);
  else
    width=DEFAULTWIDTH;
  
  formdata=Table_gets(formt,"HEIGHT");
  if (formdata!=NULL)
    height=atoi(formdata);
  else
    height=DEFAULTHEIGHT;
 
  formdata=Table_gets(formt,"LABELHEIGHT");
  if (formdata!=NULL)
    labelheight=atoi(formdata);
  else
    labelheight=DEFAULTLABELHEIGHT;

  formdata=Table_gets(formt,"HORIZPADDING");
  if (formdata!=NULL)
    horizpad=atoi(formdata);
  else
    horizpad=DEFAULTHORIZPADDING;
 
  formdata=Table_gets(formt,"VARIANCE");
  if (formdata!=NULL)
    variance=atof(formdata);
  else
    variance=DEFAULTVARIANCE;
 
  stats=Vector_create(0);
  user=Table_gets(formt,"USER");

  if (user!=NULL)
    {
      readdatafile(stats,user);
    }

  size=Vector_getSize(stats);
  
  /**********************************
    create the image object and set it up
    ********************************/
  
  im=gdImageCreate(width+2*horizpad,height+labelheight);

  black=gdImageColorAllocate(im,0,0,0);
  white=gdImageColorAllocate(im,255,255,255);
  blue=gdImageColorAllocate(im,0,0,200);
  lightblue=gdImageColorAllocate(im,80,80,255);

  /**********************************
    perform the search. awfully inefficient.
    ********************************/
  searchincrement=(HIGHESTSCORE-LOWESTSCORE)/width;
  
  searchscore=LOWESTSCORE;
  x=0;

  while (searchscore<=HIGHESTSCORE)
    {
      score=0;

      for (pos=0;pos<size;pos++)
	{
	  thisscore= *((double*) Vector_elementAt(stats,pos));

	  score+=exp(-fabs(pow(thisscore-searchscore,2)/variance));
	}

      if (size>0)
	lineheight=score*height/size;
      else
	lineheight=0;

      gdImageLine(im,x+horizpad,height,x+horizpad,height-lineheight,white);

      searchscore+=searchincrement;
      x++;
    }

  /**********************************
    Label the x axis
    ********************************/

  for (scoremark=LOWESTSCORE;scoremark<=HIGHESTSCORE;scoremark++)
    {
      x=(scoremark-LOWESTSCORE)*(width/(HIGHESTSCORE-LOWESTSCORE));

      snprintf(label,5,"%i",scoremark);
      labellen=strlen(label);

      gdImageLine(im,x+horizpad,0,x+horizpad,height,blue);
      gdImageString(im,gdFontLarge, x+horizpad-(gdFontLarge->w/2)*labellen,height+2,label,white);
    }

  /**********************************
    Outline the actual data
    ********************************/
  gdImageRectangle(im,horizpad,1,width+horizpad,height,white);
  
  votelabel=sprintfalloc("%i samples",Vector_getSize(stats));
  gdImageString(im,gdFontLarge,width+horizpad-(gdFontLarge->w)*strlen(votelabel)-2,2,votelabel,lightblue);
  free(votelabel);

  /**********************************
    Output the data 
    ********************************/

  /* use gdImagePng if possible */
  gdImageGif(im,stdout);

  gdImageDestroy(im);

  return 0;
}
Ejemplo n.º 6
0
Document *Document_create() {
  Document *document = (Document *)malloc(sizeof(Document));
  document->vector = Vector_create();
  assert(document);
  return document;
}
void ConvectionMPISendRecv2d(structMesh *mesh, double **c){
    double *CTopIn  = Vector_create(mesh->ndim2);
    double *CBotIn  = Vector_create(mesh->ndim2);
    double *CTopOut = Vector_create(mesh->ndim2);
    double *CBotOut = Vector_create(mesh->ndim2);
    
    int dim1, dim2;
    for (dim2=0; dim2<mesh->ndim2; dim2++) {
        dim1=1;
        CTopOut[dim2] = c[dim1][dim2];
        dim1=mesh->ndim1-2;
        CBotOut[dim2] = c[dim1][dim2];
    }
    
    int procid, nprocs;
    MPI_Comm_rank(MPI_COMM_WORLD, &procid);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
    
    MPI_Request sendRequest[2], recvRequest[2];
    
    int topProcid=((procid-1)<0         )?0:(procid-1);
    int BotProcid=((procid+1)>(nprocs-1))?(nprocs-1):(procid+1);
    
//    printf("procid=%d, top procid=%d, bot procid=%d\n", procid, topProcid, BotProcid);
    
    // use destination process id as tags.
    MPI_Isend(CTopOut, mesh->ndim2, MPI_DOUBLE, topProcid, topProcid+665, MPI_COMM_WORLD, sendRequest);
    MPI_Isend(CBotOut, mesh->ndim2, MPI_DOUBLE, BotProcid, BotProcid, MPI_COMM_WORLD, sendRequest+1);
    
    // receive the message by own process id.
    int topTags=procid;
    int botTags=procid+665;
    if (procid==0) {
        topTags=procid+665; // for top process
    }else if(procid==nprocs-1){
        botTags=procid; // for bottom process
    }
    
    MPI_Irecv(CTopIn, mesh->ndim2, MPI_DOUBLE, topProcid, topTags, MPI_COMM_WORLD, recvRequest);
    MPI_Irecv(CBotIn, mesh->ndim2, MPI_DOUBLE, BotProcid, botTags, MPI_COMM_WORLD, recvRequest+1);
    
    MPI_Status instatus[2];
    MPI_Status outstatus[2];
    
    MPI_Waitall(2, recvRequest, instatus);
    MPI_Waitall(2, sendRequest, outstatus);
    
    for (dim2=0; dim2<mesh->ndim2; dim2++) {
        dim1=0;
        c[dim1][dim2] = CTopIn[dim2];
        dim1=mesh->ndim1-1;
        c[dim1][dim2] = CBotIn[dim2];
    }
    
    Vector_free(CTopIn);
    Vector_free(CTopOut);
    Vector_free(CBotIn);
    Vector_free(CBotOut);
    
    return;
}
Ejemplo n.º 8
0
Vector read_input(){
  Vector v = Vector_create(sizeof(struct Inst));
  while(read_line(v));
  return v;
}