Exemple #1
0
/*
   Adds a flight to src's schedule, stating a flight will leave to dst at departure time and arrive at arrival time.
 */
void addFlight(airport_t* src, airport_t* dst, timeHM_t* departure, timeHM_t* arrival, int cost) {
    // Replace this line with your code
    if (dst&&src&&dst->name&&src->name) {
      flight_t * temp=(flight_t*)malloc(sizeof(flight_t));
       if(!temp) 
         allocation_failed();
      temp->depart=(char*)malloc(strlen(dst->name)+1); 
      temp->leave=(timeHM_t*)malloc(sizeof(timeHM_t));
      temp->arrive=(timeHM_t*)malloc(sizeof(timeHM_t));
      if (!temp->depart||!temp->leave||!temp->arrive)
        allocation_failed();

      strcpy(temp->depart,dst->name);
      *temp->leave=*departure;
      *temp->arrive=*arrival;
      temp->cost=cost;
      temp->next=temp;
      temp->prev=temp;
      temp->first=0;
      if (src->flights==NULL){
        temp->first=1;
        src->flights=temp;
        src->CountFlights++;
      }
      else {
        temp->next=src->flights->next;
        temp->prev=src->flights;
        src->flights->next->prev=temp;
        src->flights->next=temp;
        src->flights=temp;
        src->CountFlights++;
      }
    }else fprintf(stderr, "pass in addFlight invalide inputs!\n");
    
}
Exemple #2
0
/* Create a new vector */
vector_t *vector_new() {
	vector_t *retval;  

	/* First, we need to allocate the memory for the struct */
	retval = malloc(1 * sizeof(vector_t));

	/* Check our return value to make sure we got memory */
	if(retval == NULL)
                allocation_failed();
	 
	/* Now we need to initialize our data */
	retval->size = 1;
	retval->data = malloc(retval->size * sizeof(int));

	/* Check our return value to make sure we got memory */
	if(retval->data == NULL) {
		free(retval);
                allocation_failed();
	}

	retval->data[0] = 0;
	
	/* and return... */
	return retval;
}
Exemple #3
0
/*
   Adds a airport with the given name to the system. You must copy the string and store it.
   Do not store "name" (the pointer) as the contents it point to may change.
 */
void addAirport(flightSys_t* s, char* name) {
    // Replace this line with your code
    if (name!=NULL){
        airport_t* NewAirp=(airport_t*)malloc(sizeof(airport_t));
      if (!NewAirp){
        allocation_failed();
      }
        NewAirp->name=(char*) malloc(strlen(name)+1);
        if (!NewAirp->name){
          allocation_failed();
        }
        strcpy(NewAirp->name,name);
        NewAirp->flights=NULL;
        NewAirp->first=0;
        NewAirp->CountFlights=0;
        NewAirp->next=NewAirp;
        NewAirp->prev=NewAirp;
      if(s->airports){
        NewAirp->next=s->airports->next;
        NewAirp->prev=s->airports;
        s->airports->next->prev=NewAirp;
        s->airports->next=NewAirp;
        s->airports=NewAirp;
      }else{
        s->airports=NewAirp;
        s->airports->first=1;
      }
    }else
      fprintf(stderr,"the char string passed in is NULL\n");
}
Exemple #4
0
/* Loads the bitmap file in FILENAME. */
Image load_bmp(char *filename) {
    unsigned char info[54], *data, *color_table;
    int width, height, size, color_table_size, check, i;

    FILE* f = fopen(filename, "rb");
    if(!f) {
        printf("Error opening file: %s\n", filename);
        exit(1);
    }
    check = fread(info, sizeof(unsigned char), 54, f); // Read the 54-byte info header
    if (check != 54) {
        printf("Bad file format..\n");
        exit(1);
    }

    color_table_size = readInt(info + 0x0a) - 54;

    if (readInt(info + 0x0e) != 40 || readInt(info + 0x1c) != 8 ) {
        printf( "Unsupported image format. Please specify an 8-bit (grayscale) BMP image.\n" );
        exit(1);
    }

    color_table = (unsigned char *) malloc(sizeof(unsigned char) * color_table_size);
    check = fread(color_table, sizeof(unsigned char), color_table_size, f);

    width = readInt(info + 0x12);
    height = readInt(info + 0x16);
    width = (width > 0) ? width : -1 * width;
    height = (height > 0) ? height : -1 * height;
    size = width * height;

    data = (unsigned char*) malloc(sizeof(unsigned char) * size);
    if(!data) allocation_failed();
    //load data from bottom to top
    for(i = height-1; i >= 0; i--) {
        check = fread(data+i*width, sizeof(unsigned char), width, f);
    }
    fclose(f);

    float* floatData = (float*) malloc(sizeof(float) * size);
    if(!data) allocation_failed();
    for(i = 0;i < size; i++) {
        floatData[i] = data[i] / 255.0;
    }
    Image img = { .data = floatData, .width = width, .height = height };
    free(color_table);
    return img;
}
qNode *qtree_helper(unsigned char *depth_map, int map_width, int section_width, int x, int y) {
    
    qNode *root = (qNode *)malloc(sizeof(qNode));
    if(root==NULL)
    {
        allocation_failed();
    }

    root->x = x;
    root->y = y;
    root->size = section_width;
    int val = homogenous(depth_map, map_width, x, y, section_width);
    (*root).gray_value = val;

    if(val!=256)
    {
        (*root).leaf = 1;
        return root;
    }
    else
    {
        root->leaf = 0;
        root->child_NW = qtree_helper(depth_map, map_width, section_width/2, x, y);
        root->child_NE = qtree_helper(depth_map, map_width, section_width/2, x + (section_width/2), y);
        root->child_SW = qtree_helper(depth_map, map_width, section_width/2, x, y + (section_width/2));
        root->child_SE = qtree_helper(depth_map, map_width, section_width/2, x + (section_width/2), y + (section_width/2));
        return root;
    }

}
Exemple #6
0
/* Adds a new symbol and its address to the SymbolTable pointed to by TABLE. 
   ADDR is given as the byte offset from the first instruction. The SymbolTable
   must be able to resize itself as more elements are added. 

   Note that NAME may point to a temporary array, so it is not safe to simply
   store the NAME pointer. You must store a copy of the given string.

   If ADDR is not word-aligned, you should call addr_alignment_incorrect() and
   return -1. If the table's mode is SYMTBL_UNIQUE_NAME and NAME already exists 
   in the table, you should call name_already_exists() and return -1. If memory
   allocation fails, you should call allocation_failed(). 

   Otherwise, you should store the symbol name and address and return 0.
 */
int add_to_table(SymbolTable* table, const char* name, uint32_t addr) {
    /* YOUR CODE HERE */
     
     if((addr % 4) !=  0)  {
	  addr_alignment_incorrect();
	  return -1;
     }
     
     if(table->mode == SYMTBL_UNIQUE_NAME &&  get_addr_for_symbol(table, name) != -1) {
	       name_already_exists(name);
	       return -1;
     }
     
     
     if(table->len >= table->cap) {

	  int new_cap = table->cap * 2;
          
	  table->tbl = realloc(table->tbl,  new_cap * sizeof(Symbol));

	  if(table->tbl  == NULL)
	       allocation_failed();
	  
	  table->cap =  new_cap;
     }
     
     table->tbl[table->len].name = strdup(name);
     table->tbl[table->len].addr = addr;

     table->len++;
     
     return 0;
}
qNode* depth_helper(unsigned char *depth_map, int map_width, int section_width, int x, int y) {
    
    qNode *node = (qNode*) malloc(sizeof(qNode));
    if (!(node)){
        allocation_failed();
    }

    int gray_value = homogenous(depth_map, map_width, x, y, section_width);
    node->gray_value = gray_value;
    node->size = section_width;
    node->x = x;
    node->y = y;

    if(gray_value == 256) {
        node->leaf = 0;

        node->child_NW = depth_helper(depth_map, map_width, .5 * section_width, x, y);
        node->child_NE = depth_helper(depth_map, map_width, .5 * section_width, x + (.5 * section_width), y);
        node->child_SE = depth_helper(depth_map, map_width, .5 * section_width, x + (.5 * section_width), y + (.5 * section_width));
        node->child_SW = depth_helper(depth_map, map_width, .5 * section_width, x, y + (.5 * section_width));
                        
    
    } else {
        node -> leaf = 1;
    }
    return node;
}
/* Adds a new symbol and its address to the SymbolTable pointed to by TABLE. 
   ADDR is given as the byte offset from the first instruction. The SymbolTable
   must be able to resize itself as more elements are added. 

   Note that NAME may point to a temporary array, so it is not safe to simply
   store the NAME pointer. You must store a copy of the given string.

   If ADDR is not word-aligned, you should call addr_alignment_incorrect() and
   return -1. If the table's mode is SYMTBL_UNIQUE_NAME and NAME already exists 
   in the table, you should call name_already_exists() and return -1. If memory
   allocation fails, you should call allocation_failed(). 

   Otherwise, you should store the symbol name and address and return 0.
 */
int add_to_table(SymbolTable* table, const char* name, uint32_t addr) {
    if ((addr % 4) != 0) {
        addr_alignment_incorrect();
        return -1;
    }
    if ((*table).len == (*table).cap) {
        int r = (4 * (*table).cap) * (sizeof(Symbol));
        table->tbl = realloc(table->tbl, r);
        if (table->tbl == NULL) {
            allocation_failed();
            return -1;
        }
        table->cap = table->cap * 4;
    }
    int i;
    Symbol* t = table->tbl;
    for (i = 0; i < table->len; i++) {
      if (strcmp(t[i].name, name) == 0 && table->mode) {
          name_already_exists(name);
          return -1;
      }
    }
    Symbol *temp = malloc(sizeof(Symbol));
    temp->name = malloc(sizeof(name)*4 + 1);
    strcpy(temp->name, name);
    temp->addr = addr;
    (*table).len += 1;
    t[i] = *temp;
    free(temp);
    return 0;
  }
Exemple #9
0
qNode* quad_helper_NW(unsigned char *depth_map, int map_width, int x, int y){

    qNode *node = malloc(sizeof(qNode));
    if (node == NULL){
        allocation_failed();
    }
    
    
    unsigned char *pointer_NW = (unsigned char *) malloc(sizeof(depth_map)/4 * sizeof(unsigned char));
    unsigned char *pointer_C = pointer_NW;

    for(int i = 0; i < map_width * map_width; i++){
        unsigned char pixels = depth_map[i];
        int x, y = 0;
        x = i % map_width;
        y = i / map_width;
        
        if (x < map_width/2 && y < map_width/2){
            *pointer_NW = pixels;
            pointer_NW = &pointer_NW[1];
        }
    }

    node = depth_to_quad_helper(pointer_C, map_width/2, x, y);
    return node;
}
Exemple #10
0
/* Adds a new symbol and its address to the SymbolTable pointed to by TABLE. 
   ADDR is given as the byte offset from the first instruction. The SymbolTable
   must be able to resize itself as more elements are added. 

   Note that NAME may point to a temporary array, so it is not safe to simply
   store the NAME pointer. You must store a copy of the given string.

   If ADDR is not word-aligned, you should call addr_alignment_incorrect() and
   return -1. If the table's mode is SYMTBL_UNIQUE_NAME and NAME already exists 
   in the table, you should call name_already_exists() and return -1. If memory
   allocation fails, you should call allocation_failed(). 

   Otherwise, you should store the symbol name and address and return 0.
 */
int add_to_table(SymbolTable* table, const char* name, uint32_t addr) {
    /* YOUR CODE HERE */
    if (addr % 4 != 0) {
      addr_alignment_incorrect();
      return -1;
    }

    if (table->mode == SYMTBL_UNIQUE_NAME && get_addr_for_symbol(table, name) != -1) {
      name_already_exists(name);
      return -1;
    }

    if (!table) return -1;

    Symbol* to_add = (Symbol*) malloc(sizeof(Symbol));

    if (!to_add) allocation_failed();

    Symbol* sym = table->tbl;
    char* new_name = create_copy_of_str(name);
    to_add->name = new_name;
    to_add->addr = addr;

    if (table->len == table->cap) {
      sym = realloc(sym, table->len * SCALING_FACTOR * sizeof(Symbol));
      table->len += 1;
      table->cap *= SCALING_FACTOR;
      sym[table->len - 1] = *to_add;
      table->tbl = sym;
    } else {
      sym[table->len] = *to_add;
      table->len++;
    }
    return 0;
}
Exemple #11
0
/*
   Creates and initializes a flight system, which stores the flight schedules of several airports.
   Returns a pointer to the system created.
 */
flightSys_t* createSystem() {
    // Replace this line with your code
    flightSys_t * s=(flightSys_t*)malloc(sizeof(flightSys_t));
    if (!s)
      allocation_failed();
    s->airports=NULL;
    return s;
}
Exemple #12
0
/* A suggested helper function for copying the contents of a string. */
static char* create_copy_of_str(const char* str) {
    size_t len = strlen(str) + 1;
    char *buf = (char *) malloc(len);
    if (!buf) {
       allocation_failed();
    }
    strncpy(buf, str, len); 
    return buf;
}
Exemple #13
0
/* Creates a new SymbolTable containg 0 elements and returns a pointer to that
   table. Multiple SymbolTables may exist at the same time. 
   If memory allocation fails, you should call allocation_failed(). 
   Mode will be either SYMTBL_NON_UNIQUE or SYMTBL_UNIQUE_NAME. You will need
   to store this value for use during add_to_table().
 */
SymbolTable* create_table(int mode) {
    /* YOUR CODE HERE */
     
     SymbolTable* table = malloc(sizeof(SymbolTable));
     
     if(table == NULL)  allocation_failed();
        
     // inital capacity is 2
     Symbol* sym = malloc(sizeof(Symbol) * 2);
     if(sym == NULL) allocation_failed();

     table->tbl = sym;
     table->cap = 2;
     table->len = 0;
     table->mode = mode;
     
     return table;
}
/* Creates a new SymbolTable containg 0 elements and returns a pointer to that
   table. Multiple SymbolTables may exist at the same time. 
   If memory allocation fails, you should call allocation_failed(). 
   Mode will be either SYMTBL_NON_UNIQUE or SYMTBL_UNIQUE_NAME. You will need
   to store this value for use during add_to_table().
 */
SymbolTable* create_table(int mode) {
    SymbolTable *t = malloc(sizeof(SymbolTable));
    if (t == NULL) {
      allocation_failed();
    }
    (*t).tbl = malloc(sizeof(Symbol) * 10);
    (*t).len = 0;
    (*t).cap = 10;
    (*t).mode = mode;
    return t;
}
Exemple #15
0
/* Creates a new SymbolTable containing 0 elements and returns a pointer to that
   table. Multiple SymbolTables may exist at the same time. 
   If memory allocation fails, you should call allocation_failed(). 
   Mode will be either SYMTBL_NON_UNIQUE or SYMTBL_UNIQUE_NAME. You will need
   to store this value for use during add_to_table().
 */
SymbolTable* create_table(int mode) {
    /* YOUR CODE HERE */
    SymbolTable* st = malloc(sizeof(SymbolTable));
    if (!st) allocation_failed();

    st->len = 0;
    st->cap = INITIAL_SIZE;
    st->mode = mode;

    Symbol* sym = (Symbol*) malloc(INITIAL_SIZE * sizeof(Symbol));
    st->tbl = sym;
    return st;
}
Exemple #16
0
/* Set a value in the vector. If the extra memory allocation fails, call
   allocation_failed(). */
void vector_set(vector_t *v, size_t loc, int value) {
	/* What do you need to do if the location is greater than the size we have
	 * allocated?  Remember that unset locations should contain a value of 0.
	 */
	int *vec; size_t i;
	if (!v) {
		return;
	}
	if (v->size <= loc) {
		if (!(vec = malloc((loc + 1) * sizeof(int)))) {
			allocation_failed();
		}
		bzero(vec, (loc + 1) * sizeof(int));
		for (i = 0; i < v->size; ++i) {
			vec[i] = v->data[i];
		}
		free(v->data);
		v->data = vec;
		v->size = (loc + 1);
	}
	v->data[loc] = value;
}
Exemple #17
0
qNode *depth_to_quad_helper(unsigned char *depth_map, int map_width, int x, int y) {
    qNode *node = malloc(sizeof(qNode));
    if (node == NULL){
        allocation_failed();
    }
    int same = 1;
    unsigned char gray = *depth_map;
    for(int i = 0; i < map_width * map_width; i++){
        if (depth_map[i] != gray){
            same = 0;
        }
    }
    
    if (same){
        node->gray_value = gray;
        node->x = x;
        node->y = y;
        node->size = map_width;
        node->child_NW = NULL;
        node->child_NE = NULL;
        node->child_SE = NULL;
        node->child_SW = NULL;
        node->leaf = 1;
    }
    else{
        node->gray_value = 256;
        node->x = x;
        node->y = y;
        node->size = map_width;
        node->leaf = 0;
        node->child_NW = quad_helper_NW(depth_map, map_width, node->x, node->y);
        node->child_NE = quad_helper_NE(depth_map, map_width, node->x + map_width/2, node->y);
        node->child_SE = quad_helper_SE(depth_map, map_width, node->x + map_width/2, node->y + map_width/2);
        node->child_SW = quad_helper_SW(depth_map, map_width, node->x, node->y + map_width/2);
    }
    return node;
}
Exemple #18
0
int main(int argc, char **argv) {
    char *left_file = NULL;
    char *right_file = NULL;
    char *out_file = NULL;

    int feature_width = -1;
    int feature_height = -1;
    int maximum_displacement = -1;

    int verbosity = 0;

    int i;

    for (i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
                case 'l':
                    if (argc > i + 1) left_file = argv[++i];
                    break;
                case 'r':
                    if (argc > i + 1) right_file = argv[++i];
                    break;
                case 'o':
                    if (argc > i + 1) out_file = argv[++i];
                    break;
                case 'w':
                    if (argc > i + 1) feature_width = atoi(argv[++i]);
                    break;
                case 'h':
                    if (argc > i + 1) feature_height = atoi(argv[++i]);
                    break;
                case 't':
                    if (argc > i + 1) maximum_displacement = atoi(argv[++i]);
                    break;
                case 'v':
                    verbosity = 1;
                    break;
                default:
                    printf("Unknown option: %s\n", argv[i]);
                    exit(EINVAL);
            }
        }
    }

    if (left_file == NULL ||
        right_file == NULL ||
        feature_width == -1 ||
        feature_height == -1 ||
        maximum_displacement == -1) {

        printf(USAGE, argv[0]);
        exit(EINVAL);

    }

    Image left_image = load_image(left_file);
    Image right_image = load_image(right_file);
    if (left_image.height != right_image.height ||
        left_image.width != right_image.width) {
        printf("The two images do not have the same dimensions.\n");
        exit(EINVAL);
    }

    Image depth;

    depth.width = left_image.width;
    depth.height = right_image.height;
    depth.data = (unsigned char*) malloc(depth.width * depth.height);
    if (!depth.data) allocation_failed();

    srand((unsigned int) time(NULL));
    for (i = 0; i < depth.width * depth.height; i++) {
        depth.data[i] = rand();
    }

    calc_depth(depth.data, left_image.data, right_image.data,
            left_image.width, left_image.height, feature_width,
            feature_height, maximum_displacement);

    if (out_file != NULL) {
        save_image_with_depth(out_file, left_image.data, depth.data,
                left_image.width, left_image.height, feature_width,
                feature_height);
    }

    if (verbosity >= 1) {
        print_image(depth.data, depth.width, depth.height);
    }

    return 0;
}