Example #1
0
template<class KEY, class VALUE> Nde* Nde::merge_left (int parent_index_this) {
// copy all elements from this node into the left sibling, along with the
// element in the parent node vector that has this node as its subtree.
// this node and its parent element are then deleted.
 
    Elem parent_elem = (*mp_parent)[parent_index_this];
    parent_elem.mp_subtree = (*this)[0].mp_subtree;
    Nde* left_sib = (*mp_parent)[parent_index_this-1].mp_subtree;
    left_sib->vector_insert (parent_elem);
    for (unsigned int i=1; i<m_count; i++)
        left_sib->vector_insert (m_vector[i]);
    mp_parent->vector_delete (parent_index_this);
    Nde* parent_node = mp_parent;  // copy before deleting this node
    if (mp_parent==find_root() && !mp_parent->key_count()) {
        m_root.set_root(m_root.get_root(), left_sib);
        delete mp_parent;
        left_sib->mp_parent = null_ptr;
        delete this;
        return null_ptr;
    }
    else if (mp_parent==find_root() && mp_parent->key_count()) {
        delete this;
        return null_ptr;
    }
    delete this;
    if (parent_node->key_count() >= parent_node->minimum_keys())
        return null_ptr; // no need for parent to import an element
    return parent_node; // parent must import an element
 
} //_______________________________________________________________________
Example #2
0
int merge_left (PBTREE btree, int node_idx, int parent_index_this) {
// copy all elements from this node into the left sibling, along with the
// element in the parent node vector that has this node as its subtree.
// this node and its parent element are then deleted.
    int i;
    int parent_node_idx = btree->nodes[node_idx].parent_node_idx;
    BTREE_ELEMENT parent_elem;
    memcpy(&parent_elem, &btree->nodes[parent_node_idx].elements[parent_index_this], sizeof(BTREE_ELEMENT));
    int left_sib = btree->nodes[parent_node_idx].elements[parent_index_this-1].subtree_node_idx;
    parent_elem.subtree_node_idx = btree->nodes[node_idx].elements[0].subtree_node_idx;
    vector_insert (btree, left_sib, &parent_elem);
    for (i=1; i<btree->nodes[node_idx].element_count; i++)
        vector_insert (btree, left_sib, &btree->nodes[node_idx].elements[i]);
    vector_delete_pos (btree, parent_node_idx, parent_index_this);
    if (parent_node_idx==find_root(btree) && !key_count(btree, parent_node_idx)) {
        set_root(btree, left_sib);
        delete_node(btree, parent_node_idx);
        btree->nodes[left_sib].parent_node_idx = BTREE_INVALID_NODE_IDX;
        delete_node(btree, node_idx);
        return BTREE_INVALID_NODE_IDX;
    }
    else if (parent_node_idx==find_root(btree) && key_count(btree, parent_node_idx)) {
        delete_node(btree, node_idx);
        return BTREE_INVALID_NODE_IDX;
    }
    delete_node(btree, node_idx);
    if (key_count(btree, parent_node_idx) >= btree_minimum_keys())
        return BTREE_INVALID_NODE_IDX; // no need for parent to import an element
    return parent_node_idx; // parent must import an element
}
Example #3
0
int JoinSet::join(int i, int j)
{
    if (i < 0 || j < 0 || i >= m_set_size || j >= m_set_size) {
        return -1;
    }

    int i_root = find_root(i);
    int j_root = find_root(j);

    if (i_root == j_root) {
        return 0;
    }

    LOG("i:%d j:%d m_join_set[i]:%d m_join_set[j]:%d\n", i, j, m_join_set[i], m_join_set[j]);

    if (m_rank[i_root] < m_rank[j_root]) {
        m_join_set[i_root] = j_root;
    } else {
        m_join_set[j_root] = i_root;

        if (m_rank[i_root] == m_rank[j_root]) {
            ++m_rank[i_root];
        }
    }

    LOG("i:%d j:%d m_join_set[i]:%d m_join_set[j]:%d\n", i, j, m_join_set[i], m_join_set[j]);
    return 0;
}
Example #4
0
template<class KEY, class VALUE> Nde* Nde::merge_right (int parent_index_this) {
// copy elements from the right sibling into this node, along with the
// element in the parent node vector that has the right sibling as it subtree.
// the right sibling and that parent element are then deleted
 
    Elem parent_elem = (*mp_parent)[parent_index_this+1];
    Nde* right_sib = (*mp_parent)[parent_index_this+1].mp_subtree;
    parent_elem.mp_subtree = (*right_sib)[0].mp_subtree;
    vector_insert (parent_elem);
    for (unsigned int i=1; i<right_sib->m_count; i++)
        vector_insert ((*right_sib)[i]);
    mp_parent->vector_delete (parent_index_this+1);
    delete right_sib;
    if (mp_parent==find_root() && !mp_parent->key_count()) {
        m_root.set_root(m_root.get_root(), this);
        delete mp_parent;
        mp_parent = 0;
        return null_ptr;
    }
    else if (mp_parent==find_root() && mp_parent->key_count())
        return null_ptr;
    if (mp_parent&& mp_parent->key_count() >= mp_parent->minimum_keys())
        return null_ptr; // no need for parent to import an element
    return mp_parent; // parent must import an element
}
Example #5
0
int btree_delete_ex (PBTREE btree, int node_idx, long target_key) {
// target is just a package for the key value.  the reference does not
// provide the address of the Elem instance to be deleted.

    // first find the node contain the Elem instance with the given key
    int parent_index_this = BTREE_INVALID_ELEMENT_IDX;
    PBTREE_ELEMENT found;
    int last_visted_node_idx;
    if (node_idx == BTREE_INVALID_NODE_IDX)
        node_idx = btree->root_node_idx;
    last_visted_node_idx = node_idx;
    found = btree_search_ex (btree, &last_visted_node_idx, target_key);
    if (!found)
        return 0;

    if (is_leaf(btree, last_visted_node_idx) && key_count(btree, last_visted_node_idx) > btree_minimum_keys())
        return vector_delete (btree, last_visted_node_idx, target_key);
    else if (is_leaf(btree, last_visted_node_idx)) {
        vector_delete (btree, last_visted_node_idx, target_key);
        // loop invariant: if _node_ is not null_ptr, it points to a node
        // that has lost an element and needs to import one from a sibling
        // or merge with a sibling and import one from its parent.
        // after an iteration of the loop, _node_ may become null or
        // it may point to its parent if an element was imported from the
        // parent and this caused the parent to fall below the minimum
        // element count.
        while (BTREE_IS_VALID_NODE_IDX(last_visted_node_idx)) {
            int right, left;
            // NOTE: the "this" pointer may no longer be valid after the first
            // iteration of this loop!!!
            if (last_visted_node_idx == find_root(btree) && is_leaf(btree, last_visted_node_idx))
                break;
            if (last_visted_node_idx == find_root(btree) && !is_leaf(btree, last_visted_node_idx)) // sanity check
                return 0;
            // is an extra element available from the right sibling (if any)
            right = right_sibling(btree, last_visted_node_idx, &parent_index_this);
            if (BTREE_IS_VALID_NODE_IDX(right) && key_count(btree, right) > btree_minimum_keys())
                last_visted_node_idx = rotate_from_right(btree, last_visted_node_idx, parent_index_this);
            else {
                // is an extra element available from the left sibling (if any)
                left = left_sibling(btree, last_visted_node_idx, &parent_index_this);
                if (BTREE_IS_VALID_NODE_IDX(left) && key_count(btree, left) > btree_minimum_keys())
                    last_visted_node_idx = rotate_from_left(btree, last_visted_node_idx, parent_index_this);
                else if (BTREE_IS_VALID_NODE_IDX(right))
                    last_visted_node_idx = merge_right(btree, last_visted_node_idx, parent_index_this);
                else if (BTREE_IS_VALID_NODE_IDX(left))
                    last_visted_node_idx = merge_left(btree, last_visted_node_idx, parent_index_this);
            }
        }
    }
    else {
        PBTREE_ELEMENT smallest_in_subtree = smallest_key_in_subtree(btree, found->subtree_node_idx);
        found->key = smallest_in_subtree->key;
        found->data_entry_idx = smallest_in_subtree->data_entry_idx;
        btree_delete_ex (btree, found->subtree_node_idx, smallest_in_subtree->key);
    }
    return 1;
}
Example #6
0
int main() {
    n = read(); m = read();
    for (int i = 1; i <= n; i++) root[i] = i;

    for (int i = 1; i <= m; i++) {
        if (num_edges + 1 > n) break;
        a = read(); b = read();
        const int ar = find_root(a), br = find_root(b);
        if (ar != br) root[ar] = br, edges[num_edges++] = i;
    }
    
    if (num_edges + 1 < n) printf("Disconnected Graph\n");
    else for (int i = 0; i < num_edges; i++) printf("%d\n", edges[i]);
}
Example #7
0
 double find_root(
   double f(double x), double x_guess, double x_second_guess
 ) {
   set_first_root_estimate(x_guess);
   set_second_root_estimate(x_second_guess);
   return find_root(f);
 }
GtFeatureNode* gt_feature_info_find_root(const GtFeatureInfo *fi,
                                         const char *id)
{
  gt_assert(fi && id);
  gt_assert(gt_feature_info_get(fi, id));
  return find_root(fi, id);
}
Example #9
0
int find_root(int n)
{
    if(parent[n] != n)
        parent[n] = find_root(parent[n]);
    
    return parent[n];
}
/**
 * Trims the tree, killing some of the parents
 *@param board the current board
 *@param score my score
 *@param opponentScore other score
 *@param pointsLeft how many points left in the game
 *@return the root of it all
 */
AlphaBetaNode* AlphaBetaAI::trim_tree(int* board, int score, int opponentScore, int pointsLeft) {
    pthread_mutex_lock(&mutex);
    AlphaBetaNode* root = find_root(board, NULL, pointsLeft);
    //printf("found root");
    int zeroD = root->getDepth();
    // surely there is a better way of doing this...

    // kill parents and reshape tree
    for (unsigned int i = 0; i < nodes.size(); i++) {
        if (nodes[i] != root && nodes[i]->getDepth() <= zeroD) {
            //printf("going to kill self\n");
            nodes[i]->killSelfAndKids();
            //printf("managed to kill self\n");
        } else {
            nodes[i]->raise(zeroD);
        }
    }
    // dispose of bodies
    for (int i = 0; i < (signed)nodes.size(); i++) {
        if (nodes[i] != root && nodes[i]->isDead()) {
            //printf("killed one\n");
            delete nodes[i];
            nodes.erase(nodes.begin()+i);
            i--;
        }
    }

    myScore = score;
    otherScore = opponentScore;
    reset = true;

    pthread_mutex_unlock(&mutex);
    return root;
}
Example #11
0
ea_t
auto_find_root_addr()
{
  int i;
  segment_t *s;
  ea_t start, end, found;

  for ( i = 0; i < segs.get_area_qty(); i++ )
  {
    s = getnseg(i);
    if ( NULL == s )
     continue;
    start = s->startEA;
    end = s->endEA;
    while(start < end)
    {
     found = bin_search(start, end, root_name, NULL, ROOT_LEN + 1,
      BIN_SEARCH_FORWARD, BIN_SEARCH_CASE);
     if ( BADADDR == found )
      break;
     start = found + ROOT_LEN; /* for next iteration */
     xrefblk_t xb;
     int ok;
     for ( ok = xb.first_to(found,XREF_ALL); ok; ok=xb.next_to() )
     {
       if ( xb.iscode ) /* we want data ref */
         continue;
       if ( NULL != ( found = find_root(found, true) ) )
         return found;
     }
    }
  }
  return NULL;
}
Example #12
0
int find_pol_roots_homog(unsigned int p, int deg, unsigned int *polmod, unsigned int *roots)
{
  unsigned int r;
  int n, d, i;

  if (assess_mod_len<deg+1) {
    assess_mod_len=deg+1;
    assess_mod=(unsigned int *)xrealloc(assess_mod,assess_mod_len*sizeof(unsigned int));
  }
  for (i=0; i<=deg; i++) assess_mod[i]=polmod[i]%p;
  d=deg; n=0; 
  while ((d) && (assess_mod[d]==0)) { d--; roots[n++]=p; }
  if (d==0) {
    if (assess_mod[0]==0) complain("find_pol_roots_homog: zero pol\n");
    return n;
  }
  while (d) {
    r=find_root(p,d,assess_mod);
    if (r==p) break;
    if (pol_eval(p,d,assess_mod,r)) complain("find_pol_roots_homog\n");
    do {
      pol_div_lin(p,&d,assess_mod,r); roots[n++]=r;
    } while (pol_eval(p,d,assess_mod,r)==0);
  }
  return n;
}
static GtFeatureNode* find_root(const GtFeatureInfo *fi, const char *id)
{
  const char *delim, *parents;
  GtFeatureNode *this_feature, *parent_pseudo_feature;
  gt_assert(fi && id);
  /* get feature */
  delim = strchr(id, ';');
  if (delim) {
    char *first_parent = gt_cstr_dup_nt(id, delim - id);
    this_feature = gt_hashmap_get(fi->id_to_genome_node, first_parent);
    parent_pseudo_feature = gt_hashmap_get(fi->id_to_pseudo_parent,
                                           first_parent);
    gt_free(first_parent);
  }
  else {
    this_feature = gt_hashmap_get(fi->id_to_genome_node, id);
    parent_pseudo_feature = gt_hashmap_get(fi->id_to_pseudo_parent, id);
  }
  gt_assert(this_feature);
  /* recursion */
  parents = gt_feature_node_get_attribute(this_feature, GT_GFF_PARENT);
  if (parents)
    return find_root(fi, parents);
  else if (parent_pseudo_feature)
    return parent_pseudo_feature;
  return this_feature;
}
Example #14
0
/*template <class T_t, class G_t>*/
static int tree_dec_lospre (tree_dec_lospre_t/*T_t*/ &T, cfg_lospre_t/*G_t*/ &G, const iCode *ic)
{
  if(tree_dec_lospre_nodes(T, find_root(T), G))
    return(-1);

  wassert(T[find_root(T)].assignments.begin() != T[find_root(T)].assignments.end());
  const assignment_lospre &winner = *(T[find_root(T)].assignments.begin());

  //std::cout << "Winner (lospre): ";
  //print_assignment(winner, G);

  int change;
  if (change = implement_lospre_assignment(winner, T, G, ic))
    nicify (T);
  T[find_root(T)].assignments.clear();
  return(change);
}
Example #15
0
File: grpdata.c Project: d4g33z/lie
local boolean simp_isroot(entry* alpha, simpgrp* g)
{ _index i,r=g->lierank; entry level=0; boolean neg,result=false;
  for(i=0; i<r; ++i) level+=alpha[i]; /* compute level of |alpha| */
  neg=level<0; /* if |neg| holds, |alpha| can only be a negative root */
  if (neg) { level= -level; for(i=0; i<r; ++i) alpha[i]= -alpha[i]; }
  result=find_root(alpha,level,g)>=0;
  if (neg)  for(i=0; i<r; ++i) alpha[i]= -alpha[i]; /* restore |alpha| */
  return result;
}
Example #16
0
/*template <class T_t, class G_t>*/
static int tree_dec_safety (tree_dec_lospre_t/*T_t*/ &T, cfg_lospre_t/*G_t*/ &G, const iCode *ic)
{
  if(tree_dec_safety_nodes(T, find_root(T), G))
    return(-1);

  wassert(T[find_root(T)].assignments.begin() != T[find_root(T)].assignments.end());
  const assignment_lospre &winner = *(T[find_root(T)].assignments.begin());

  implement_safety(winner, G);

#ifdef DEBUG_LOSPRE
  std::cout << "Winner (safety): ";
  print_assignment(winner, G);
#endif

  T[find_root(T)].assignments.clear();
  return (0);
}
Example #17
0
    joint* joint::find_joint_name(const string & _name)
    {
        joint* search_down = _find_joint_name(_name);
        if (search_down != NULL)
            return search_down;

        joint* root = find_root();
        return root->_find_joint_name(_name);
    }
int find_root(int x, int n)
{
  if((x*x) == n)
    {
      return x;
    }
  else if (x == 0)
    return -1;
  return (find_root((x-1),n));
}
Example #19
0
File: mtree.c Project: taysom/tau
int next_mtree(struct iterator_mtree *imt)
{
	int rc;
	u64 blknum = find_root(imt->mt);

	if (!blknum)
		return DONE;
	rc = next_key(imt, blknum);
	return rc ? DONE : 0;
}
Example #20
0
File: ac_1211.c Project: shaung/oj
int main(int argc, char* argv[])
{
	short t = 0, n = 0, i = 0, x = 0, y = 0, gx = 0, gy = 0;
	char failed = 0, nconfess = 0;

#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
#endif

	scanf("%d\n", &t);
	for(i = 0; i < t; i++){
		scanf("%d\n", &n);

		memset(parent, 0, sizeof(parent));
		failed = 0;
		nconfess = 0;
		for(x = 1; x <= n; x++){
			scanf("%d ", &y);
			if(failed == 1) continue;
			if(y == 0){
				if(++nconfess > 1) failed = 1;
				continue;
			}
			gx = find_root(x);
			gy = find_root(y);
			if(gx == 0 && gy == 0){
				parent[x] = x;
				parent[y] = x;
			}
			else if(gx != 0 && gy != 0){
				if(gx == gy) failed = 1;
				else parent[gy] = gx;
			}
			else if(gx == 0) parent[x] = gy;
			else if(gy == 0) parent[y] = gx;
		}
		if(failed == 0 && nconfess == 1) printf("YES\n");
		else printf("NO\n");
	}

	return 0;
}
Example #21
0
void top_sort(int graph[][10], int sort[])
{
	int curr_root, sort_ind = 0;
	
	while ((curr_root = find_root(graph)) != -1)
	{
		sort[sort_ind] = curr_root;
		sort_ind++;
	}
}
Example #22
0
File: mtree.c Project: taysom/tau
void pr_mtree(struct mtree *mt)
{
	u64 root = find_root(mt);

	if (!root) {
		printf("<empty>\n");
		return;
	}
	printf("Root: %lld\n", root);
	pr_block(mt, root, 0);
}
Example #23
0
void build(int x, int c) {
    c += x <= n;
    cut_root[x] = c, vis[x] = true;
    for (int i = 0; i < int(pass[x].size()); ++i)
        if (vis[pass[x][i].to])
            lca[pass[x][i].index] = find_root(pass[x][i].to);
    for (int i = 0; i < int(chd[x].size()); ++i) {
        build(chd[x][i], c);
        d_set_merge(chd[x][i], x);
    }
}
Example #24
0
int main(int argc, char **argv)
{
	struct btrfs_root *root;
	int dev_fd;
	int opt;
	int ret;

	while ((opt = getopt(argc, argv, "l:o:g:")) != -1) {
		switch(opt) {
			case 'o':
				search_objectid = arg_strtou64(optarg);
				break;
			case 'g':
				search_generation = arg_strtou64(optarg);
				break;
			case 'l':
				search_level = arg_strtou64(optarg);
				break;
			default:
				usage();
				exit(1);
		}
	}

	set_argv0(argv);
	argc = argc - optind;
	if (check_argc_min(argc, 1)) {
		usage();
		exit(1);
	}

	dev_fd = open(argv[optind], O_RDONLY);
	if (dev_fd < 0) {
		fprintf(stderr, "Failed to open device %s\n", argv[optind]);
		exit(1);
	}

	root = open_ctree_broken(dev_fd, argv[optind]);
	close(dev_fd);

	if (!root) {
		fprintf(stderr, "Open ctree failed\n");
		exit(1);
	}

	if (search_generation == 0)
		search_generation = btrfs_super_generation(root->fs_info->super_copy);

	csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
	ret = find_root(root);
	close_ctree(root);
	return ret;
}
int square_root(int n)
{
  int x;
  /*int sqr_root;*/
  x = n - 1;
  if ((n == 0) || (n == 1))
    return n;
  else if(n < 0)
    return -1;
  /*sqr_root = find_root(x,n);*/
  return find_root(x,n);
}
Example #26
0
int main()
{
    int i, j, m, n, x, y, ncase, min_cap;

    scanf("%d", &ncase);
    
    for(i = 1; i <= ncase; i++)
    {
        scanf("%d%d", &n, &m);
        
        for(j = 0; j < m; j++)
        {
            scanf("%d%d%d", &roads[j][0], &roads[j][1], &roads[j][2]);
        }
        
        qsort(roads, m, sizeof(roads[0]), cmp);
        
        for(j = 0; j < n; j++)
        {
            parent[j] = j;
        }
        
        min_cap = roads[0][2];
        
        for(j = 0; j < m; j++)
        {
            x = find_root(roads[j][0]);
            y = find_root(roads[j][1]);

            if ( x != y )
            {
                parent[x] = y;
                min_cap = roads[j][2];
            }
        }
        
        printf("Case #%d: %d\n", i, min_cap);
    }
    return 0;
}
Example #27
0
int main()
{
    int i, n, m, a, b, x, y, max, ncase;
    
    scanf("%d", &ncase);
    
    while (ncase-- > 0)
    {
        scanf("%d%d", &n, &m);
        
        for(i = 1; i <= n; i++)
        {
            parent[i] = i;
            size[i] = 1;
        }
        
        while (m-- > 0)
        {
            scanf("%d%d", &a, &b);
    
            x = find_root(a);
            y = find_root(b);
    
            if ( x != y )
                link(x, y);
        }
        
        max = 0;
        for(i = 1; i <= n; i++)
        {
            if ( parent[i] == i )
            {
                if ( size[i] > max )
                    max = size[i];
            }
        }
        printf("%d\n", max);
    }
    return 0;
}
Example #28
0
//---------------------------------------------------------------------------------------
void XmlParser::parse_file(const std::string& filename, bool UNUSED(fErrorMsg))
{
    m_fOffsetDataReady = false;
    m_filename = filename;
    pugi::xml_parse_result result = m_doc.load_file(filename.c_str());

    if (!result)
    {
        m_errorMsg = string(result.description());
        m_errorOffset = result.offset;
    }
    find_root();
}
Example #29
0
//---------------------------------------------------------------------------------------
void XmlParser::parse_char_string(char* str)
{
    m_fOffsetDataReady = false;
    m_filename.clear();
    pugi::xml_parse_result result = m_doc.load_string(str, pugi::parse_default | pugi::parse_declaration);

    if (!result)
    {
        m_errorMsg = string(result.description());
        m_errorOffset = result.offset;
    }
    find_root();
}
Example #30
0
off_t file_bstore::find_root()
{
    off_t best_root = 0;
    slabs_t::reverse_iterator it = m_slabs.rbegin();
    while(it != m_slabs.rend())
    {
        best_root = find_root(it->first, it->second.io);
        if (best_root != 0)
            break;
        it++;
    }
    return best_root;
}