Example #1
0
/* _zzt_board_relink(brd, offset, start, end movefrom, moveto)
 * Add offset to all links in a board between start and end, except
 * links to "movefrom" which become "moveto" instead
 */
int _zzt_board_relink(ZZTboard *brd, int offset, int start, int end, int movefrom, int moveto)
{
	int j, board_length;

	/* Cool macro to save major space */
#define relink(link) if ((link) == movefrom) (link) = moveto; else if ((link) >= start && (link) <= end) (link) += offset;

	/* Relink board connections */
	relink(brd->info.board_n);
	relink(brd->info.board_s);
	relink(brd->info.board_e);
	relink(brd->info.board_w);

	/* Do the same for passages */
	if (!zztBoardDecompress(brd)) {
		fprintf(stderr, "Error decompressing board\n");
		return 0;
	}
	board_length = brd->bigboard->width * brd->bigboard->height;
	for (j = 0; j < board_length; j++) {
		if (brd->bigboard->tiles[j].type == ZZT_PASSAGE && brd->bigboard->tiles[j].param != NULL) {
			relink(brd->bigboard->tiles[j].param->data[2]);
		}
	}

	return 1;
}
Example #2
0
static void linearize(Node p, Node next) {
	int i;

	for (i = 0; i < NELEMS(p->x.kids) && p->x.kids[i]; i++)
		linearize(p->x.kids[i], next);
	relink(next->x.prev, p);
	relink(p, next);
	debug(fprint(stderr, "(listing %x)\n", p));
}
Example #3
0
fp_item *fp_directory::add(fp_node *node){
	fp_item *item = find(node);
	if (item == NULL) {
		// no node representing this item was inserted

		map_dir::iterator it =  m_map_dir.find(node->get_attr_id());

		min_max_item *mmi;

		if (it != m_map_dir.end()){
			mmi = it->second;
		} else {
			mmi = new min_max_item();
		}

		item = new fp_item(node->get_attr_id(),
				node->get_min(),
				node->get_max(),
				node);

		mmi->insert(std::pair<double, fp_item *>(node->get_min(), item));
		m_map_dir.insert(make_pair(node->get_attr_id(), mmi));
	} else {
		// set a new first node ?
		relink(node);
	}

	return item;
}
Example #4
0
/**
 * Link a written file to the allocation table and unlink any free space used.
 *
 * File is linked to the allocation table before unlinking free space so if the
 * system fails before the free space is unlinked, there will be no orphaned blocks.
 *
 * \param fh File handle
 */
void close(file_handle_t* fh)
{
	_fs_debug1("Finalising file %d.\n", fh->filename);

	if (fh->type == FH_APPEND)
	{
		// Sum the filesize of the existing file
		fh->filesize += alloc_table[fh->filename].filesize;

		if (alloc_table[fh->filename].filesize > EEPROM_FS_BLOCK_DATA_SIZE)
		{
			// If the first block hasn't been rewritten, link the appended data to the last block
			lba_t last = last_block_in_chain(
					alloc_table[fh->filename].data_block);

			_fs_debug2("Appending block %d to block %d...\n", fh->first_block,
					last);

			// Point the last block of the current file to the first block in the new chain
			relink(last, fh->first_block);

			_fs_debug2("Done.\n");
		}
		else
		{
			// Otherwise, just link the file to the new stuff and discard the old block
			if (alloc_table[fh->filename].filesize > 0)
			{
				unlink(alloc_table[fh->filename].data_block);
			}
			link(fh);
		}
	}
	else
	{
		link(fh);
	}

	_fs_debug2("Marking end of file %d.\n", fh->filename);

	// Mark end of file
	relink(fh->last_block, NULL_PTR);

	_fs_debug1("File %d successfully finalised.\n", fh->filename);
}
Example #5
0
void fpp_directory_inqp::add(fpp_stream_node *node){

//	std::cout << "ItemDir::add " << node << std::endl;
	fpp_item_inqp *item = dynamic_cast<fpp_item_inqp *>(find(node));
	if ( item == NULL ) {
		// no node representing this item was inserted

		map_dir::iterator it =  m_map_dir.find(node->get_attr_id());

		min_max_item *mmi;

		if (it != m_map_dir.end()){
			mmi = it->second;
		} else {
			mmi = new min_max_item();
		}

		item = new fpp_item_inqp(node->get_attr_id(),
								node->get_min(),
								node->get_max(),
								node,
								m_histo_size,
								m_histo_type);

		mmi->insert(std::pair<double, fpp_item *>(node->get_min(), item));
		//! \todo We always insert the attr id list into the map? Can't this lead to some failures? Is this std conformant?
		m_map_dir.insert(make_pair(node->get_attr_id(), mmi));

		{
			int attr_id = node->get_attr_id();
			id_factory_ptr p = m_id_factory_factory.get_factory(attr_id);
			if (p.get() == NULL)
			{
				// interval ids will only use 1 byte (unsigned char) and the 0
				// is currently used for invalid ids...
				//! \todo Roh replace those magic numbers?
				p = m_id_factory_factory.create_factory(attr_id, 1, (unsigned char)-1);
			}
			int id = dynamic_cast<fpp_stream_node_inqp *>(node)->get_interval_id();
			if (id != 0)
			{
				p->occupy_id(id);
			} else {
				id = p->get_id();
				dynamic_cast<fpp_stream_node_inqp *>(node)->set_interval_id(id);
			}
			p->set_id_pointer(id, item);
			item->set_interval_id(id);
		}
	} else {
		// to make sure that the node has the same interval id as the item
		dynamic_cast<fpp_stream_node_inqp *>(node)->set_interval_id(item->get_interval_id());
		// setting a new first node ?
		relink(node);
	}
}
Example #6
0
int main(void)
{
    struct node *nodes = NULL;
    struct node *tour = NULL;
    double distance, total_distance, min_distance;
    distance = 0;
    min_distance = 0;
    total_distance = 0;

    nodes = get_input();

    struct node *curr;
    struct node *prev;
    struct node *closest;
    struct node *prev_closest;
    curr = nodes;
    prev = NULL;
    closest = NULL;
    prev_closest = NULL;

    relink(&tour, &curr, &prev);

    while (nodes != NULL)
    {
        for (curr = nodes, prev = NULL; curr != NULL; prev = curr, curr = curr->next)
        {
            distance = distance_between(tour, curr);
            if (min_distance == 0)
            {
                min_distance = distance;
            }
            if (distance < min_distance)
            {
                min_distance = distance;
                closest = curr;
                prev_closest = prev;
            }

        }
        total_distance += min_distance;
        relink(&tour, &closest, &prev_closest);
    }
}
void sortNodes( Node* in, Node** out, Node** oldCopy )
{
    // Sort by frequency.
    //Converts the list into array.
    int len = sizeOfList( in );
    Node* arr = convertListToArray( in, len);
    *oldCopy = arr;
    qsort( arr, len, sizeof(Node), Comparator );
	relink( arr, len );
    *out = arr;

}
Example #8
0
Node gen(Node forest) {
	int i;
	struct node sentinel;
	Node dummy, p;

	head = forest;
	for (p = forest; p; p = p->link) {
		assert(p->count == 0);
		if (generic(p->op) == CALL)
			docall(p);
		else if (   generic(p->op) == ASGN
		&& generic(p->kids[1]->op) == CALL)
			docall(p->kids[1]);
		else if (generic(p->op) == ARG)
			(*IR->x.doarg)(p);
		rewrite(p);
		p->x.listed = 1;
	}
	for (p = forest; p; p = p->link)
		prune(p, &dummy);
	relink(&sentinel, &sentinel);
	for (p = forest; p; p = p->link)
		linearize(p, &sentinel);
	forest = sentinel.x.next;
	assert(forest);
	sentinel.x.next->x.prev = NULL;
	sentinel.x.prev->x.next = NULL;
	for (p = forest; p; p = p->x.next)
		for (i = 0; i < NELEMS(p->x.kids) && p->x.kids[i]; i++) {
			assert(p->x.kids[i]->syms[RX]);
			if (p->x.kids[i]->syms[RX]->temporary) {
				p->x.kids[i]->x.prevuse =
					p->x.kids[i]->syms[RX]->x.lastuse;
				p->x.kids[i]->syms[RX]->x.lastuse = p->x.kids[i];
			}
		}
	for (p = forest; p; p = p->x.next) {
		ralloc(p);
		if (p->x.listed && NeedsReg[opindex(p->op)]
		&& (*IR->x.rmap)(opkind(p->op))) {
			assert(generic(p->op) == CALL || generic(p->op) == LOAD);
			putreg(p->syms[RX]);
		}
	}
	return forest;
}
/**
 * Perform the work of removing a node equal to the given one.
 * @param node		Currently processed node.
 * @param rem_node	Removed node.
 * @return		Tree after removal.
 */
SortedBinTree::Node *SortedBinTree::remove(Node *node, Node *rem_node) {
	if(!node)
		return node;
	else {
		int result = compare(rem_node, node);
		if(!result) {
			Node *result = relink(node->left(), node->right());
			delete node;
			return result;
		}
		else if(result < 0)
			node->insertLeft(remove(node->left(), rem_node));
		else
			node->insertRight(remove(node->right(), rem_node));
		return node;
	}
}
/**
 * Relink the tree just after the removal of a node.
 * @param left		Sub-tree to put left.
 * @param right		Sub-tree to put right.
 * @return		Tree after removal.
 */
SortedBinTree::Node *SortedBinTree::relink(Node *left, Node *right) {
	if(!left)
		return right;
	else if(!right)
		return left;
	else if(!left->right()) {
		left->insertRight(right);
		return left;
	}
	else if(!right->left()) {
		right->insertLeft(left);
		return right;
	}
	else {
		left->insertLeft(relink(left->left(), left->right()));
		left->insertRight(right);
		return left;
	}
}
Example #11
0
int add_device(int n, SPICEdev **devs, int flag){
  int i;
  DEVices = TREALLOC(SPICEdev *, DEVices, DEVNUM + n);
  DEVicesfl = TREALLOC(int, DEVicesfl, DEVNUM + n);
  for(i = 0; i < n;i++){
#ifdef TRACE
      printf("Added device: %s\n",devs[i]->DEVpublic.name);
#endif
    DEVices[DEVNUM+i] = devs[i];

    /* added by SDB on 6.20.2003 */
    DEVices[DEVNUM+i]->DEVinstSize = &MIFiSize;

    DEVicesfl[DEVNUM+i] = flag;
  }
  DEVNUM += n;
  relink();
  return 0;
}
Example #12
0
/*==============================================================================
 * gen_refgrid: 
 *--------------------
 * 1. test current grid for refinement, and generate adaptive grid (if required) 
 * 2. re-link particles 
 * 3. ajdust densities as neccessary.
 *==============================================================================*/
boolean gen_refgrid(gridls **grid_list, int *no_grids)
{
  gridls *coa_grid, *fin_grid;     /* old/new grid pointer                    */
  boolean refined;                 /* refined YES/NO flag                     */
  boolean mg;                      /* does refinement hold enough particles   */
  int     idim;
  
#ifdef REF_TEST
  fprintf(stderr,"\ngen_refgrid:         current coarse grid         = %ld\n",
          (*grid_list + (*no_grids-1))->l1dim);
#endif
  
  /* set coarse grid pointer */
  coa_grid = *grid_list + *no_grids - 1;
  fin_grid = *grid_list + *no_grids;
  
  /* timing... */
  coa_grid->time.grid -= time(NULL);
  
  if(coa_grid->next == FALSE)  /* fin_grid does not exist ? */
    {
      /* reallocate the grid list array (including space for one additional grid) */
      (*grid_list) = (gridls *) realloc(*grid_list, (*no_grids + 1) * sizeof(gridls));
      
      if((*grid_list) == NULL)
        {
          fprintf(io.logfile,"gen_refgrid: error reallocating grid list\n");
          fflush(io.logfile);
          fclose(io.logfile);
          exit(1);
        }
      
      /* grid_list block might have moved in memory */
      global.dom_grid = *grid_list + global.domgrid_no;
      coa_grid        = *grid_list + *no_grids - 1;
      fin_grid        = *grid_list + *no_grids;
      
      /* fill in new grid's entries */
      fin_grid->masstodens     = coa_grid->masstodens     * CRITMULTI;
      fin_grid->masstopartdens = coa_grid->masstopartdens * CRITMULTI;
#ifdef MULTIMASS
      fin_grid->critdens       = simu.Nth_ref*fin_grid->masstopartdens;
#else
      fin_grid->critdens       = simu.Nth_ref*fin_grid->masstodens;
#endif
      fin_grid->timecounter    = coa_grid->timecounter;
      fin_grid->timestep       = coa_grid->timestep/2;
      fin_grid->l1dim          = coa_grid->l1dim * 2;
      fin_grid->spacing        = (double)1.0 / (double)fin_grid->l1dim;
      fin_grid->spacing2       = fin_grid->spacing * fin_grid->spacing;
      
      fin_grid->no_pquad       = 0;
      fin_grid->pquad_array    = NULL;

      /* we measure the time throughout the two DKD cycles of this fin_grid */
      fin_grid->time.potential = 0;
      fin_grid->time.density   = 0;
      fin_grid->time.DK        = 0;
      fin_grid->time.grid      = 0;
      fin_grid->time.hydro     = 0;
       
      fin_grid->no_sweeps      = 0;
      fin_grid->cur_resid      = 0.;
      
      /* remember that fin_grid exists from now on */
      fin_grid->next = FALSE;
      coa_grid->next = TRUE;
    }
  
  
  /* (re-)set values that are changing within the two DKD cycles the grid lives through */
  fin_grid->old_resid             = 0.0;
  fin_grid->cur_resid             = 0.0;
  
  fin_grid->multistep             = 0;
  
  
  /*------------------------------
   * now call refinement rountine 
   *------------------------------*/
  refined = refine_grid(fin_grid, coa_grid);
  
  if(refined)
    {
#ifdef REF_TEST
      fprintf(stderr,"                     placed new refinement       = %ld\n", fin_grid->l1dim);
#endif
      
      /* update number of grids */
      *no_grids += 1;
      
      /* now relink particles to new grid */
      mg = relink(coa_grid, fin_grid);        /* performs: unassign_part */
      
      /* now assign mass to new grid: THIS IS NEEDED AS IT RETURNS "mg" ! */
      zero_dens        (fin_grid);
      mg = assign_npart(fin_grid);
      
#ifdef REF_TEST
      fprintf(stderr,"             grid %6ld:  nnodes=%ld npart=%ld => %g (<! %g)\n",
              fin_grid->l1dim,fin_grid->size.no_nodes,fin_grid->size.no_part,
              (double)fin_grid->size.no_nodes/(double)fin_grid->size.no_part, CRITMULTI);
#endif
      
      /* only use grid if it holds enough particles */
      if(mg == FALSE)
        {
          refined = FALSE;
          relink_back(coa_grid, fin_grid);
          fin_grid->size.no_nodes         = 0;
          fin_grid->size.no_part          = 0;
          
          free_grid(fin_grid, no_grids);
          
#ifdef REF_TEST
          fprintf(stderr,"                     destroyed new refinement    = %ld (%g)\n",
                  fin_grid->l1dim, fin_grid->critdens);
#endif
        }
      else
        {
          /* get potential onto fine grid (important for boundaries !) */
          go_down(coa_grid);          
        }
    }
  
  else
    {
#ifdef REF_TEST
      fprintf(stderr,"                     NO new refinement grid !\n\n");
#endif
      /*
       * no need to call free_grid() 
       * -> temporary pquads, cquads, nquads already destroyed within ref_grid() 
       *
       * no need to realloc grid_list
       * -> grid will be kept in memory from now on...
       */
    }
  
  /* grid_list block might have changed position in memory */
  global.dom_grid = *grid_list + global.domgrid_no;
  
  /* ...timing */
  coa_grid->time.grid += time(NULL);
  
  return(refined);
}
Example #13
0
void ShaderProgram::add_and_compile(ShaderType type, const std::string& source) {
    check_and_log_error(__FILE__, __LINE__);

    if(program_id_ == 0) {
        program_id_ = glCreateProgram();
        check_and_log_error(__FILE__, __LINE__);
    }

    if(shader_ids_[type] != 0) {
        glDeleteShader(shader_ids_[type]);
        shader_ids_[type] = 0;
        check_and_log_error(__FILE__, __LINE__);
    }

    GLuint shader_type;
    switch(type) {
        case ShaderType::SHADER_TYPE_VERTEX: {
            L_DEBUG("Adding vertex shader");
            shader_type = GL_VERTEX_SHADER;
        } break;
        case ShaderType::SHADER_TYPE_FRAGMENT: {
            L_DEBUG("Adding fragment shader");
            shader_type = GL_FRAGMENT_SHADER;
        } break;
        default:
            throw std::logic_error("Invalid shader type");
    }
    check_and_log_error(__FILE__, __LINE__);
    GLuint shader = glCreateShader(shader_type);
    check_and_log_error(__FILE__, __LINE__);
    shader_ids_[type] = shader;

    const char* c_str = source.c_str();
    glShaderSource(shader, 1, &c_str, nullptr);
    check_and_log_error(__FILE__, __LINE__);

    glCompileShader(shader);
    check_and_log_error(__FILE__, __LINE__);

    GLint compiled = 0;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
    if(!compiled) {
        GLint length;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);

        std::vector<char> log;
        log.resize(length);

        glGetShaderInfoLog(shader, length, NULL, &log[0]);
        L_ERROR(std::string(log.begin(), log.end()));
    }
    assert(compiled);
    assert(program_id_);
    assert(shader);

    check_and_log_error(__FILE__, __LINE__);
    glAttachShader(program_id_, shader);
    check_and_log_error(__FILE__, __LINE__);

    relink();
}
Example #14
0
/**
 * Perform a format of the EEPROM
 *
 * \param f FORMAT_FULL clears all the data in each block.
 *          FORMAT_QUICK resets the allocation table and
 *          	free block chain pointers only.
 *          FORMAT_WIPE performs a full wipe of the EEPROM
 *          	before performing a quick format
 */
void format_eepromfs(format_type_t f)
{
	_fs_debug1("Formatting filesystem.\n");

	if (f == FORMAT_WIPE)
	{
		wipe_eeprom();
	}

	/*
	 * Mark all blocks as free
	 */
	block_t block;
	if (f == FORMAT_FULL)
	{
		// Clear block data
		for (size_t i = 0; i < EEPROM_FS_BLOCK_DATA_SIZE; i++)
		{
			block.data[i] = 0;
		}
	}

	for (lba_t i = 0; i < EEPROM_FS_NUM_BLOCKS; i++)
	{
		block.next_block = i - 1;
		if (f == FORMAT_FULL)
		{
			// Overwrite entire block, clearing data
			_fs_debug3("Relinking block %d -> %d...", i, block.next_block);

			eeprom_update_block((void*) &block, get_block_pointer(i),
			EEPROM_FS_BLOCK_SIZE);

			_fs_debug3("Done.\n");
		}
		else
		{
			// Relink address only
			relink(i, block.next_block);
		}
	}

	/*
	 * Allocation table
	 */
	_fs_debug2("Writing file allocation table...");

	// Set all allocations to 'null'
	file_alloc_t null;
	null.data_block = NULL_PTR;
	null.filesize = 0;
	for (uint16_t i = 0; i < EEPROM_FS_MAX_FILES; i++)
	{
		alloc_table[i] = null;
	}

	// Set free block at end of allocation table
	file_alloc_t free;
	free.data_block = EEPROM_FS_NUM_BLOCKS - 1;
	free.filesize = 0;
	alloc_table[EEPROM_FS_MAX_FILES] = free;

	eeprom_update_block((void*) alloc_table,
			(void*) (EEPROM_FS_START + EEPROM_FS_ALLOC_TABLE_OFFSET),
			sizeof(alloc_table));

	_fs_debug2("Done.\n");

	// Write meta
	_fs_debug2("Writing metadata...");
	fs_meta_t this_meta;
	this_meta.block_size = EEPROM_FS_BLOCK_SIZE;
	this_meta.start_address = EEPROM_FS_START;
	this_meta.fs_size = EEPROM_FS_SIZE;
	this_meta.max_files = EEPROM_FS_MAX_FILES;
	this_meta.max_blocks_per_file = EEPROM_FS_MAX_BLOCKS_PER_FILE;
	eeprom_write_block((void*) &this_meta,
			(void*) (EEPROM_FS_START + EEPROM_FS_META_OFFSET),
			sizeof(fs_meta_t));
	_fs_debug2("Done.\n");

	_fs_debug1("Successfully formatted.\n");
}