Esempio n. 1
0
static int countnodes(NODE *node)
{
  if (node)
  {
    nodecount++;
    if (nodecount > maxnodes)
    {
      printf("Too many nodes found - exceeds total nodes %d\n", maxnodes);
      return 0;
    }
    if (node->member)
    {
      if (parent_of(member_of(node)) != node)
	printf("Bad node linkage\n");
      countnodes(member_of(node));
    }
    if (node->child)
    {
      if (parent_of(child_of(node)) != node)
	printf("Bad node linkage\n");
      countnodes(child_of(node));
    }
    if (node->brother)
    {
      if (parent_of(brother_of(node)) != parent_of(node))
	printf("Bad node linkage\n");
      countnodes(brother_of(node));
    }
  }
  return 1;
}
struct node *parent_of(struct node *root, int data){

	if (root == NULL)
		return NULL;

	if (root->left != NULL){
		if (root->left->data == data)
			return root;
	}

	if (root->right != NULL){
		if (root->right->data == data)
			return root;
	}

	struct node *l = NULL;
	struct node *r = NULL;
	
	if (root->left != NULL)
		l = parent_of(root->left, data);
	
	if (root->right != NULL)
		r = parent_of(root->right, data);

	if (l == NULL && r == NULL)
		return NULL;

	if (l != NULL)
		return l;
	if (r != NULL)
		return r;
}
Esempio n. 3
0
File: heap.c Progetto: vdt/libcore
/* Complexity: O(log n) */
static void heapify_up(Heap *heap, unsigned long index)
{
    while(index > 0 &&
            heap->comparefn(darray_index(heap->h, index),
                            darray_index(heap->h, parent_of(index))) > 0) {
        darray_swap(heap->h, index, parent_of(index));
        index = parent_of(index);
    }
}
Esempio n. 4
0
	rbtree_node* find_impl(Key const& key) const
	{
		rbtree_node* next = _root;

		while (next) {
			if (key < *parent_of(next, NodeMember))
				next = next->left;
			else if (key > *parent_of(next, NodeMember))
				next = next->right;
			else
				return next;
		}

		return nullptr;
	}
Esempio n. 5
0
/*
 * test mvderwin().
 */
static bool
move_subwin(WINDOW *win)
{
    WINDOW *parent = parent_of(win);
    bool result = FALSE;

    if (parent != 0) {
	bool top = (parent == stdscr);
	if (!top) {
	    int min_col = top ? COL_MIN : 0;
	    int max_col = top ? COL_MAX : getmaxx(parent);
	    int min_line = top ? LINE_MIN : 0;
	    int max_line = top ? LINE_MAX : getmaxy(parent);
	    PAIR *tmp;

	    head_line("Select new position for subwindow");

	    if ((tmp = selectcell(parent,
				  min_line, min_col,
				  max_line, max_col)) != 0) {
		int y0, x0;
		getbegyx(parent, y0, x0);
		if (mvderwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
		    refresh_all(win);
		    doupdate();
		    result = TRUE;
		}
	    }
	}
    }
    return result;
}
Esempio n. 6
0
static int countfree(NODE *node)
{
  NODE     *lnode;
  for (lnode = node; lnode; lnode = parent_of(lnode))
  {
    nodecount++;
    if (nodecount > maxnodes)
    {
      printf("Too many nodes found - exceeds total nodes %d\n", maxnodes);
      return 0;
    }
    if (lnode->parent)
      if (child_of(parent_of(lnode)) != lnode)
	printf("Bad node linkage\n");
  }
  return 1;
}
Esempio n. 7
0
	std::pair<iterator, bool> insert_unique(reference elem)
	{
		rbtree_node*  node   = member_of(&elem, NodeMember);
		rbtree_node*  parent = nullptr;
		rbtree_node** next   = &_root;
		Compare       cmp;

		while (*next) {
			parent = *next;
			if (cmp(elem, *parent_of(*next, NodeMember)))
				next = &(*next)->left;
			else if (cmp(*parent_of(*next, NodeMember), elem))
				next = &(*next)->right;
			else
				return std::pair<iterator, bool>(iterator(*next), false);
		}
		*next = node;
		node->insert(&_root, parent);

		return std::pair<iterator, bool>(iterator(node), true);
	}
Esempio n. 8
0
    void operator()(core::events::join const& event) {
        Segment parent_segment = parent_of(event);
        Segment child_segment = child_of(event);
        Segment new_parent_segment = new_parent_of(event);

        // Segments:      parent    child       new_parent
        // Parent thread:   o______________________o
        // Child thread:              o___________/
        add_vertex(new_parent_segment, graph);
        add_edge(parent_segment, new_parent_segment, graph);
        add_edge(child_segment, new_parent_segment, graph);
    }
Esempio n. 9
0
/*
 * test mvwin().
 */
static bool
move_window(WINDOW *win, bool recur)
{
    WINDOW *parent = parent_of(win);
    bool result = FALSE;

    if (parent != 0) {
	bool top = (parent == stdscr);
	int min_col = top ? COL_MIN : 0;
	int max_col = top ? COL_MAX : getmaxx(parent);
	int min_line = top ? LINE_MIN : 0;
	int max_line = top ? LINE_MAX : getmaxy(parent);
	PAIR *tmp;
	bool more;

	head_line("Select new position for %swindow", top ? "" : "sub");

	while ((tmp = selectcell(parent,
				 win,
				 min_line, min_col,
				 max_line, max_col,
				 FALSE,
				 &more)) != 0) {
	    int y0, x0;
	    getbegyx(parent, y0, x0);
	    /*
	     * Moving a subwindow has the effect of moving a viewport around
	     * the screen.  The parent window retains the contents of the
	     * subwindow in the original location, but the viewport will show
	     * the contents (again) at the new location.  So it will look odd
	     * when testing.
	     */
	    if (mvwin(win, y0 + tmp->y, x0 + tmp->x) != ERR) {
		if (recur) {
		    recur_move_window(win, tmp->y, tmp->x);
		}
		refresh_all(win);
		doupdate();
		result = TRUE;
	    } else {
		result = FALSE;
	    }
	    if (!more)
		break;
	}
    }
    head_line("done");
    return result;
}
Esempio n. 10
0
inline typename adobe::forest<T>::iterator find_index_parent(typename forest_map<T>::type& map,
                                                             const adobe::bitpath_t&       path)
{
    adobe::bitpath_t parent(parent_of(path));

    if (parent.empty())
        return typename adobe::forest<T>::iterator();

    typename forest_map<T>::type::const_iterator found(map.find(parent));

    if (found == map.end())
        throw std::runtime_error("Index not found.");

    return found->second;
}
Esempio n. 11
0
	iterator insert_equal(reference elem)
	{
		rbtree_node*  node   = member_of(&elem, NodeMember);
		rbtree_node*  parent = nullptr;
		rbtree_node** next   = &_root;
		Compare       cmp;

		while (*next) {
			parent = *next;
			if (cmp(elem, *parent_of(*next, NodeMember)))
				next = &(*next)->left;
			else
				next = &(*next)->right;
		}
		*next = node;
		node->insert(&_root, parent);

		return iterator(node);
	}
int closest_distance(struct node *root, struct node *temp){

	int dist = distance(temp);

	if (dist == 0 || dist == 1)
		return dist;
	int p,min;
	struct node *parent = parent_of(root, temp->data);

	if (parent != NULL){

		p = closest_distance(root, parent);
		min = minimum(p, dist);

		if (p == min)
			return p+1;
	}
	return dist;
}
Esempio n. 13
0
File: heap.c Progetto: vdt/libcore
/* Complexity: O(n)
 *
 * Checks that the heap relational property is valid
 * throughout the heap. Looks at every node.
 */
int heap_is_valid(Heap *heap)
{
    unsigned long i;

    assert(heap != NULL);

    if(heap_size(heap) < 2) {
        return 1;
    }

    for(i = 1; i < heap_size(heap); i++) {
        if(heap->comparefn(darray_index(heap->h, parent_of(i)),
                           darray_index(heap->h, i)) < 0) {
            return 0;
        }
    }

    return 1;

    /* return check_heap(heap, 0); */
}
Esempio n. 14
0
void build_segmentation_graph(Iterator first, Iterator last, Graph& graph) {
    // We need to be able to add new vertices/edges to build the
    // segmentation graph.
    // Note: The VertexMutableGraph concept includes a check for
    //       remove_vertex(). However, since the SegmentationGraph is a
    //       named_graph using vecS as its out edge list, we can't use
    //       remove_vertex because of iterator invalidation as seen in
    //       https://svn.boost.org/trac/boost/ticket/7863. Therefore, we
    //       do not check the concept because we don't need remove_vertex
    //       anyway.
#if 0
    BOOST_CONCEPT_ASSERT((boost::MutableGraphConcept<Graph>));
    BOOST_CONCEPT_ASSERT((boost::VertexMutablePropertyGraphConcept<Graph>));
#endif

    BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>));

    typedef typename boost::vertex_property_type<Graph>::type Segment;

    if (first == last)
        return;

    // The first event should be a `start`. We deduce the initial
    // segment from it. If the first event is not a `start`, we can't
    // continue because we really need to know the initial segment.
    typedef typename boost::iterator_reference<Iterator>::type Event;
    Event first_event = *first;
    core::events::start const* initial_event =
                            boost::get<core::events::start>(&first_event);
    if (initial_event == NULL)
        D2_THROW(EventTypeException()
                    << ExpectedType("start")
                    << ActualType(typeid(first_event).name()));
    add_vertex(parent_of(*initial_event), graph);

    EventVisitor<Graph, Segment, SilentlyIgnoreOtherEvents> visitor(graph);
    do {
        boost::apply_visitor(visitor, *first);
    } while (++first != last);
}
Esempio n. 15
0
/*
 * test mvderwin().
 */
static bool
move_derwin(WINDOW *win)
{
    WINDOW *parent = parent_of(win);
    bool result = FALSE;

    if (parent != 0) {
	bool top = (parent == stdscr);
	int min_col = top ? COL_MIN : 0;
	int max_col = top ? COL_MAX : getmaxx(parent);
	int min_line = top ? LINE_MIN : 0;
	int max_line = top ? LINE_MAX : getmaxy(parent);
	PAIR *tmp;
	bool more;

	show_derwin(win);
	while ((tmp = selectcell(parent,
				 win,
				 min_line, min_col,
				 max_line, max_col,
				 TRUE,
				 &more)) != 0) {
	    if (mvderwin(win, tmp->y, tmp->x) != ERR) {
		refresh_all(win);
		doupdate();
		repaint_one(win);
		doupdate();
		result = TRUE;
		show_derwin(win);
	    } else {
		flash();
	    }
	    if (!more)
		break;
	}
    }
    head_line("done");
    return result;
}
Esempio n. 16
0
extern void       _TreeDeleteNodeExecute(void *dbid)
{
  PINO_DATABASE *dblist = (PINO_DATABASE *)dbid;
  static NID       nid;
  NODE     *node;
  NODE     *prevnode = 0;
  NODE     *parent;
  static NCI empty_nci;
  NODE     *firstempty = (dblist->tree_info->header->free == -1) ? (NODE *) 0 :
	  (NODE *) ((char *) dblist->tree_info->node + dblist->tree_info->header->free);

  TREE_EDIT *edit = dblist->tree_info->edit;
  static int zero = 0;
/*------------------------------------------------------------------------------

 Executable:                                                                  */

  nid.tree = 0;
  nid.node = 0;
  while (_TreeDeleteNodeGetNid(dbid, (int*)&nid) & 1)
  {
    int       found = 0;
    _TreeRemoveNodesTags(dbid, *(int *)&nid);
    _TreeSetNoSubtree(dbid, *(int *)&nid);
    nid_to_node(dblist, (&nid), node);
    parent = parent_of(node);
    if (child_of(parent) == node)
    {
      found = 1;
      if (node->INFO.TREE_INFO.brother)
      {
        link_it(parent->INFO.TREE_INFO.child,brother_of(node),parent);
      }
      else
	parent->INFO.TREE_INFO.child = 0;
    }
    else if (parent->INFO.TREE_INFO.child)
    {
      NODE     *bro;
      for (bro = child_of(parent); bro->INFO.TREE_INFO.brother && (brother_of(bro) != node); bro = brother_of(bro));
      if (brother_of(bro) == node)
      {
	found = 1;
	if (node->INFO.TREE_INFO.brother)
	{
          link_it(bro->INFO.TREE_INFO.brother,brother_of(node),bro);
        }
	else
	  bro->INFO.TREE_INFO.brother = 0;
      }
    }
    if (!found)
    {
      if (member_of(parent) == node)
      {
	if (node->INFO.TREE_INFO.brother)
	{
	  link_it(parent->INFO.TREE_INFO.member,brother_of(node), parent);
        }
	else
	  parent->INFO.TREE_INFO.member = 0;
      }
      else if (parent->INFO.TREE_INFO.member)
      {
	NODE     *bro;
	for (bro = member_of(parent); bro->INFO.TREE_INFO.brother && (brother_of(bro) != node); bro = brother_of(bro));
	if (brother_of(bro) == node)
	{
	  found = 1;
	  if (node->INFO.TREE_INFO.brother)
	  {
	    link_it(bro->INFO.TREE_INFO.brother,brother_of(node), bro);
          }
	  else
	    bro->INFO.TREE_INFO.brother = 0;
	}
      }
    }
    if ((int)nid.node < edit->first_in_mem)
    {
      NCI       old_nci;
      int       nidx = nid.node;
      TreeGetNciLw(dblist->tree_info, nidx, &old_nci);
      TreePutNci(dblist->tree_info, nidx, &empty_nci, 1);
    }
    else
      memcpy(edit->nci + nid.node - edit->first_in_mem, &empty_nci, sizeof(struct nci));
    memcpy(node->name,"deleted node",sizeof(node->name));
    LoadShort(zero,&node->conglomerate_elt);
    node->INFO.TREE_INFO.member = 0;
    node->INFO.TREE_INFO.brother = 0;
    node->usage = 0;
    if (prevnode)
    {
      int tmp;
      link_it(prevnode->parent, node, prevnode);
      tmp = -swapint((char *)&prevnode->parent);
      node->INFO.TREE_INFO.child = swapint((char *)&tmp);
    }
    else
    {
      int tmp;
      link_it(tmp,node, dblist->tree_info->node);
      dblist->tree_info->header->free = swapint((char *)&tmp);
      node->INFO.TREE_INFO.child = 0;
    }
    if (firstempty)
    {
      int tmp;
      link_it(node->parent, firstempty, node);
      tmp = -swapint((char *)&node->parent);
      firstempty->INFO.TREE_INFO.child = swapint((char *)&tmp);
    }
    else
      node->parent = 0;
    prevnode = node;
  }
  dblist->modified = 1;
  _TreeDeleteNodeInitialize(dbid, 0, 0, 1);
}
Esempio n. 17
0
	T& dereference() const { return *parent_of(_node, NodeMember); }
Esempio n. 18
0
int ccn_merkle_root_hash(const unsigned char *msg, size_t size,
                         const struct ccn_parsed_ContentObject *co,
                         const EVP_MD *digest_type,
                         MP_info *merkle_path_info,
                         unsigned char *result, int result_size)
{
    int node = ASN1_INTEGER_get(merkle_path_info->node);
    EVP_MD_CTX digest_context;
    EVP_MD_CTX *digest_contextp = &digest_context;
    size_t data_size;
    unsigned char *input_hash[2];
    //int hash_count = merkle_path_info->hashes->num;
    int hash_index = merkle_path_info->hashes->num - 1;
    //ASN1_OCTET_STRING *sibling_hash;
    int res;
    
    if (result_size != EVP_MD_size(digest_type))
        return -1;

    /*
     * This is the calculation for the node we're starting from
     *
     * The digest type for the leaf node we'll take from the MHT OID
     * We can assume that, since we're using the same digest function, the
     * result size will always be the same.
     */

    EVP_MD_CTX_init(digest_contextp);
    EVP_DigestInit_ex(digest_contextp, digest_type, NULL);
    data_size = co->offset[CCN_PCO_E_Content] - co->offset[CCN_PCO_B_Name];
    res = EVP_DigestUpdate(digest_contextp, msg + co->offset[CCN_PCO_B_Name], data_size);
    res = EVP_DigestFinal_ex(digest_contextp, result, NULL);

    /* input_hash[0, 1] = address of hash for (left,right) node of parent
     */
    while (node != 1) {
        input_hash[node & 1] = result;
        input_hash[(node & 1) ^ 1] = ((ASN1_OCTET_STRING *)merkle_path_info->hashes->data[hash_index])->data;
        if (((ASN1_OCTET_STRING *)merkle_path_info->hashes->data[hash_index])->length != result_size)
            return (-1);
        hash_index -= 1;
#ifdef DEBUG
        fprintf(stderr, "node[%d].lefthash = ", parent_of(node));
        for (int x = 0; x < result_size; x++) {
            fprintf(stderr, "%02x", input_hash[0][x]);
        }
        fprintf(stderr, "\n");
   
        fprintf(stderr, "node[%d].righthash = ", parent_of(node));
        for (int x = 0; x < result_size; x++) {
            fprintf(stderr, "%02x", input_hash[1][x]);
        }
        fprintf(stderr, "\n");
#endif
        EVP_MD_CTX_init(digest_contextp);
        EVP_DigestInit_ex(digest_contextp, digest_type, NULL);
        res = EVP_DigestUpdate(digest_contextp, input_hash[0], result_size);
        res = EVP_DigestUpdate(digest_contextp, input_hash[1], result_size);
        res = EVP_DigestFinal_ex(digest_contextp, result, NULL);
        node = parent_of(node);
   
#ifdef DEBUG
        fprintf(stderr, "yielding node[%d] hash = ", node);
        for (int x = 0; x < result_size; x++) {
            fprintf(stderr, "%02x", result[x]);
        }
        fprintf(stderr, "\n");
#endif
    }
    return (0);
}