Ejemplo n.º 1
0
/**
 * Try to make the new node into a parent of the tree-node n. The problem 
 * here is that we must include any siblings of n in r if they fit.
 * @param n the node above which to add the parent
 * @param r the new unattached node 
 */
static void dom_make_parent( dom *d, node *n, node *r )
{
    node *parent = node_parent(n);
    node *prev = node_prec_sibling( n );
    if ( parent==NULL )
        printf("parent is NULL\n");
    //fprintf( stderr,"n: %s %d:%d; r %s %d:%d\n",node_name(n),node_offset(n),
    //    node_end(n),node_name(r),node_offset(r),node_end(r));
    //node_debug_check_siblings( node_first_child(parent) );
    while ( n != NULL && !node_follows(r,n) )
    {
        node *next = node_next_sibling(n);
        if ( dom_nests(d,node_name(n),node_name(r)) )
        {
            if ( range_encloses_node(n,r) || range_equals_node(n,r) )
            {
                node_detach_sibling( n, prev );
                node_add_child( r, n );
                if ( node_overlaps_on_right(parent,r) )
                {
                    node_split( r, node_end(parent) );
                    node *r2 = node_next_sibling( r );
                    node_detach_sibling( r, NULL );
                    dom_store_range( d, node_to_range(r2) );
                    node_dispose( r2 );
                }
            }
            else if ( node_overlaps_on_left(n,r) )
            {
                node_split( n, node_end(r) );
                node_detach_sibling( n, prev );
                node_add_child( r, n );
                break;
            }
            else
                break;
        }
        else 
        {
            // split off the rest of r and and push it back
            // Q: what happens to r??
            node *r2;
            node_split( r, node_offset(n) );
            r2 = node_next_sibling( r );
            node_detach_sibling( r, NULL );
            dom_store_range( d, node_to_range(r2) );
            //queue_push( d->q, node_to_range(r2) );
            node_dispose( r2 );
            break;
        }
        n = next;
        if ( n != NULL )
            prev = node_prec_sibling( n );
    }
    // make n's original parent the parent of r
    node_add_child( parent, r );
   // node_debug_check_siblings( node_first_child(parent) );
}
Ejemplo n.º 2
0
Node* node_new_expr_index1(Node* left, Node* index)
{
  Node* expr_list = node_new(EXPR_LIST);
  node_add_child(expr_list, index);
  Node* rv = node_new(EXPR_INDEX);
  node_add_child(rv, left);
  node_add_child(rv, expr_list);
  return rv;
}
Ejemplo n.º 3
0
static struct ast_node *parse_pipe(struct parser_context *context)
{
    struct ast_node *left = parse_val(context);
    if(token_match(context, TOK_PIPE)) {
        token_read(context);
        struct ast_node *rdir = node_create(NODE_PIPE, sizeof(struct ast_node));
        node_add_child(rdir, left);
        node_add_child(rdir, parse_pipe(context));
        return rdir;
    }
    return left;
}
Ejemplo n.º 4
0
Archivo: eval.c Proyecto: merolle/ripe
// Returns code for accessing index (if assign = NULL), or setting index
// when assign is of type expr.
const char* eval_index(Node* self, Node* idx, Node* assign)
{
  if (assign == NULL) {
    return eval_obj_call(self, "index", idx);
  } else {
    Node* arg_list = node_new(EXPR_LIST);
    for (int i = 0; i < node_num_children(idx); i++){
      node_add_child(arg_list, node_get_child(idx, i));
    }
    node_add_child(arg_list, assign);
    return eval_obj_call(self, "index_set", arg_list);
  }
}
Ejemplo n.º 5
0
void node_extend_children(Node* new_parent, Node* old_parent)
{
  for (int i = 0; i < node_num_children(old_parent); i++){
    Node* child = node_get_child(old_parent, i);
    node_add_child(new_parent, child);
  }
}
Ejemplo n.º 6
0
/**
 * Try to add r as a child to n, already in the tree
 * @param n the tree-node
 * @param r the new r-node
 */
static void dom_make_child( dom *d, node *n, node *r )
{
    if ( node_has_children(n) )
        dom_add_node(d,node_first_child(n),r );
    else
        node_add_child( n, r );
}
Ejemplo n.º 7
0
/**
 * Build a tree using a given string
 * @param txt the text to build it from
 * @return the finished tree's root
 */
node *build_tree( char *txt )
{
    // init globals
    e = 0;
    root=NULL;
    f=NULL;
    current=NULL;
    memset( &last, 0, sizeof(pos) );
    memset( &old_beta, 0, sizeof(pos) );
    old_j = 0;
    str = txt;
    slen = strlen(txt);
    // actually build the tree
    root = node_create( 0, 0 );
    if ( root != NULL )
    {
        f = node_create_leaf( 0 );
        if ( f != NULL )
        {
            int i;
            node_add_child( root, f );
            for ( i=1; i<=slen; i++ )
                phase(i);
            set_e( root );
        }
    }
    return root;
}
Ejemplo n.º 8
0
/**
 * Extend the implicit suffix tree by adding one suffix of the current prefix
 * @param j the offset into str of the suffix's start
 * @param i the offset into str at the end of the current prefix
 * @return 1 if the phase continues else 0
 */
static int extension( int j, int i )
{
    int res = 1;
    pos *p = find_beta( j, i-1 );
    // rule 1 (once a leaf always a leaf)
    if ( node_is_leaf(p->v) && pos_at_edge_end(p) )
        res = 1;
    // rule 2
    else if ( !continues(p,str[i]) )
    {
        //printf("applying rule 2 at j=%d for phase %d\n",j,i);
        node *leaf = node_create_leaf( i );
        if ( p->v==root || pos_at_edge_end(p) )
        {
            node_add_child( p->v, leaf );
            update_current_link( p->v );
        }
        else
        {
            node *u = node_split( p->v, p->loc );
            update_current_link( u );
            if ( i-j==1 )
            {
                node_set_link( u, root );
#ifdef DEBUG
                verify_link( current );
#endif
            }
            else
                current = u;
            node_add_child( u, leaf );
        }
        update_old_beta( p, i );
    }
    // rule 3
    else
    {
        //printf("applying rule 3 at j=%d for phase %d\n",j,i);
        update_current_link( p->v );
        update_old_beta( p, i );
        res = 0;
    }
    free( p );
    return res;
}
Ejemplo n.º 9
0
/**
 * Extend the implicit suffix tree by adding one suffix of the current prefix
 * @param st the current suffixtree
 * @param j the offset into str of the suffix's start
 * @param i the offset into str at the end of the current prefix
 * @param log the log to record errors in
 * @return 1 if the phase continues else 0
 */
static int extension( suffixtree *st, int j, int i, plugin_log *log )
{
    int res = 1;
    pos *p = find_beta( st, j, i-1, log );
    // rule 1 (once a leaf always a leaf)
    if ( node_is_leaf(p->v) && pos_at_edge_end(st,p) )
        res = 1;
    // rule 2
    else if ( !continues(st,p,st->str[i]) )
    {
        //printf("applying rule 2 at j=%d for phase %d\n",j,i);
        node *leaf = node_create_leaf( i, log );
        if ( p->v==st->root || pos_at_edge_end(st,p) )
        {
            node_add_child( p->v, leaf, st->str, log );
            update_current_link( st, p->v );
        }
        else
        {
            node *u = node_split( p->v, p->loc, st->str, log );
            update_current_link( st, u );
            if ( i-j==1 )
            {
                node_set_link( u, st->root );
#ifdef DEBUG
                verify_link( current );
#endif
            }
            else 
                st->current = u;
            node_add_child( u, leaf, st->str, log );
        }
        update_old_beta( st, p, i );
    }
    // rule 3
    else
    {
        //printf("applying rule 3 at j=%d for phase %d\n",j,i);
        update_current_link( st, p->v );
        update_old_beta( st, p, i );
        res = 0;
    }
    free( p );
    return res;
}
Ejemplo n.º 10
0
void* bintree_add(bintree* b, void* data)
{
	bintree_node* n = new_node(data);
	if (b->root)
		node_add_child(b->root,n,b->comp);
	else
		b->root = n;
	b->size++;
	return data;
}
Ejemplo n.º 11
0
/**
 * Add an initially single-char leaf to the tree
 * @param parent the node to hang it off
 * @param i start-index in str of the leaf
 */
int node_add_leaf( node *parent, int i )
{
    int res = 0;
    node *leaf = node_create_leaf( i );
    if ( leaf != NULL )
    {
        node_add_child( parent, leaf );
        res = 1;
    }
    return res;
}
Ejemplo n.º 12
0
Archivo: btree.c Proyecto: shiva/fun
void btree_node_insert(node_t node, node_t newnode)
{
	node_t child = NULL;
	node_orientation_t orientation;

	orientation = (newnode->data < node->data) ? NODE_LEFT : NODE_RIGHT;
	child = node_get_child(node, orientation);
	if (child) {
		return btree_node_insert(child, newnode);
	} else {  // reached leaf, add newnode
		return node_add_child(node, orientation, newnode);
	}
}
Ejemplo n.º 13
0
/* Inserts S into TRIE. Returns the new node marking the end of S, or NULL on
 * memory failure. */
Node *trie_insert(Trie *trie, char *s) {
        Node *np;
        char *si;

        np = trie_lookup(trie, s, &si);
        for (s = si; *s != '\0'; s++) {
                np = node_add_child(np, *s);
                if (np == NULL)
                        return NULL;
                trie->size++;
        }
        np->end_of_word = 1;
        return np;
}
Ejemplo n.º 14
0
Node* node_new_field_call(Node* callee, const char* field_name, int64 num, ...)
{
  Node* field = node_new(EXPR_FIELD);
  node_add_child(field, callee);
  node_set_string(field, "name", field_name);

  Node* rv = node_new(EXPR_CALL);
  node_set_node(rv, "callee", field);

  Node* expr_list = node_new(EXPR_LIST);
  va_list ap;
  va_start(ap, num);
  for (int64 i = 0; i < num; i++){
    node_add_child(expr_list, va_arg(ap, Node*));
  }
  va_end(ap);
  node_set_node(rv, "args", expr_list);
  return rv;
}
Ejemplo n.º 15
0
int main( int argc, char **argv )
{
    node *root = node_create(0,0);
    int i,slen = strlen(str)-1;
    for ( i=0;i<slen;i++ )
    {
        node *v = node_create(i,1);
        if ( find_child(root,str[i])==NULL )
            node_add_child( root, v );
        else
            node_dispose( v );
    }
    for ( i=0;i<slen;i++ )
    {
        node *v = find_child(root,str[i]);
        if ( v == NULL )
            printf("couldn't find %c\n",str[i]);
    }
    node_dispose( root );
}
Ejemplo n.º 16
0
bintree_node* node_add_child(bintree_node* n, bintree_node* c, compfunc comp)
{
	int r = comp(c->data,n->data);
	
	// (assumes every entry is unique... should I do that?)
	if (!r) return 0;
	
	// (why did I do it like this?  Obfuscation for fun?  Obfuscate...  Obfuscate...)
	bintree_node** cp = (bintree_node**)(r < 0 ? &n->left : &n->right);
	if (*cp)
		node_add_child(*cp,c,comp);
	else
	{
		*cp = c;
		c->parent = n;
		// (we're going to assume a weight of 1, for now)
		node_propagate_weight(c,1);
	}
	
	return c;
}
Ejemplo n.º 17
0
/**
 * Build a tree using a given string
 * @param txt the text to build it from
 * @param tlen its length
 * @param log the log to record errors in
 * @return the finished tree
 */
suffixtree *suffixtree_create( UChar *txt, size_t tlen, plugin_log *log )
{
    if ( txt[tlen] != 0 )
    {
        plugin_log_add(log,"suffixtree: text not null-terminated!");
        return NULL;
    }
    else
    {
        suffixtree *st = calloc( 1, sizeof(suffixtree) );
        if ( st != NULL )
        {
            st->e = 0;
            memset( &st->last, 0, sizeof(pos) );
            memset( &st->old_beta, 0, sizeof(pos) );
            st->str = txt;
            st->slen = tlen;
            // actually build the tree
            st->root = node_create( 0, 0, log );
            if ( st->root != NULL )
            {
                st->f = node_create_leaf( 0, log );
                if ( st->f != NULL )
                {
                    int i;
                    node_add_child( st->root, st->f, st->str, log );
                    for ( i=1; i<=tlen; i++ )
                        phase(st,i,log);
                    set_e( st, st->root, log );
                }
            }
        }
        else
            fprintf(stderr,"suffixtree: failed to allocate tree\n");
        return st;
    }
}
Ejemplo n.º 18
0
int main(int argc, char** argv)
{
	printf("testing bintree\n");
	
	node na,nb,nc,nd;
	init_node(&na,"Anchor");
	init_node(&nb,"Bunk");
	init_node(&nc,"Corgi");
	init_node(&nd,"Dapper");
	
	node_add_child(&nb,&nd,comp_string);
	node_add_child(&nb,&nc,comp_string);
	node_add_child(&nb,&na,comp_string);
	
	printf("heythere\n");
	printf("%x: %x, %x\n",nb.data,nb.left,nb.right);
	printf("%s: %s, %s\n",nb.data,((bintree_node*)nb.left)->data,((bintree_node*)nb.right)->data);
	printf("%s: %s, %s\n",nd.data,((bintree_node*)nd.left)->data,(nd.right ? ((bintree_node*)nd.right)->data : 0));
	
	bintree_node* ns[] = {&na,&nb,&nc,&nd};
	int i;
	for (i=0; i<4; ++i)
	{
#define ndata(a,b) ((bintree_node*)a->b)->data
		printf("<%s: (%s) %s, %s, %i>\n",
			(ns[i]->data ? ns[i]->data : 0),
			(ns[i]->parent ? ndata(ns[i],parent) : 0),
			(ns[i]->left ? ndata(ns[i],left) : 0),
			(ns[i]->right ? ndata(ns[i],right) : 0),
			ns[i]->weight);
	}
	
	bintree_node* n = &na;
	printf("testing node_next\n");
	do {
		printf("- %s\n",n->data);
	} while ((n = node_next(n)));
	
	n = &nd;
	printf("testing node_prev\n");
	do {
		printf("- %s\n",n->data);
	} while ((n = node_prev(n)));
	
#undef ndata
	
#ifdef mtrace_active
	mtrace();	
#endif

	bintree* b = new_bintree(comp_string);	
	del_bintree(b,DEL_STRUCT);
	
	b = new_bintree(comp_string);
	bintree_add(b,"Brawn");
	bintree_add(b,"Dour");
	bintree_add(b,"Court");
	bintree_add(b,"Acclamate");
	
	char* s;
	void* h = iter_bintree(b);
	while (s = iter_bintree_next(&h))
		printf("%s\n",s);
	
	h = iter_bintree(b);
	while (s = iter_bintree_next(&h))
		printf("%s\n",bintree_contains(b,s));
	
	del_bintree(b,DEL_STRUCT);
	
#ifdef mtrace_active
	muntrace();
#endif
	
	return 0;
}
Ejemplo n.º 19
0
static int trie_insert(struct trie *trie, struct trie_node *node, const char *search,
                       const char *key, const char *value) {
        size_t i = 0;
        int err = 0;

        for (;;) {
                size_t p;
                uint8_t c;
                struct trie_node *child;

                for (p = 0; (c = trie->strings->buf[node->prefix_off + p]); p++) {
                        _cleanup_free_ char *s = NULL;
                        ssize_t off;
                        _cleanup_free_ struct trie_node *new_child = NULL;

                        if (c == search[i + p])
                                continue;

                        /* split node */
                        new_child = calloc(sizeof(struct trie_node), 1);
                        if (!new_child)
                                return -ENOMEM;

                        /* move values from parent to child */
                        new_child->prefix_off = node->prefix_off + p+1;
                        new_child->children = node->children;
                        new_child->children_count = node->children_count;
                        new_child->values = node->values;
                        new_child->values_count = node->values_count;

                        /* update parent; use strdup() because the source gets realloc()d */
                        s = strndup(trie->strings->buf + node->prefix_off, p);
                        if (!s)
                                return -ENOMEM;

                        off = strbuf_add_string(trie->strings, s, p);
                        if (off < 0)
                                return off;

                        node->prefix_off = off;
                        node->children = NULL;
                        node->children_count = 0;
                        node->values = NULL;
                        node->values_count = 0;
                        err = node_add_child(trie, node, new_child, c);
                        if (err)
                                return err;

                        new_child = NULL; /* avoid cleanup */
                        break;
                }
                i += p;

                c = search[i];
                if (c == '\0')
                        return trie_node_add_value(trie, node, key, value);

                child = node_lookup(node, c);
                if (!child) {
                        ssize_t off;

                        /* new child */
                        child = calloc(sizeof(struct trie_node), 1);
                        if (!child)
                                return -ENOMEM;

                        off = strbuf_add_string(trie->strings, search + i+1, strlen(search + i+1));
                        if (off < 0) {
                                free(child);
                                return off;
                        }

                        child->prefix_off = off;
                        err = node_add_child(trie, node, child, c);
                        if (err) {
                                free(child);
                                return err;
                        }

                        return trie_node_add_value(trie, child, key, value);
                }

                node = child;
                i++;
        }
}
Ejemplo n.º 20
0
int doc_parse(Doc *doc) {
	char txt[BUFSIZ];
	char buf[BUFSIZ];
	char *cptr = NULL;
	char *stateptr = NULL;
	int r;
	int s;
	Node *tmp = NULL;

	doc->numroots = 0;
	doc->roots = (Node**)calloc(1, sizeof(Node*) * (doc->numroots + 1));
	
	while (1) {
		if (!stateptr) {
			if (!pushtxt(doc->file, txt, stateptr))
				return 0;
		}
		if (!tmp) {
			stateptr = strstr(stateptr? stateptr: txt, "<");
			if (!stateptr)
				continue;
			stateptr += 1;
			cptr = strstr(stateptr, ">");
			if (!cptr) {
				if (!pushtxt(doc->file, txt, stateptr))
					return 0;
				cptr = strstr(stateptr, ">");
				if (!cptr) {
					return -1;
				}
			}
			r = cptr - stateptr;
			strncpy(buf, stateptr, r);
			buf[r] = '\0';
			buf[strcspn(buf, "\n\t ")] = '\0';
		
			s = (*(stateptr-1+r) == '/');
			if (s) {
				s = strlen(buf);
				if (s && (buf[s-1] == '/')) 
					buf[s-1] = '\0';
				s = 1;
	 		}

			if (!stralpha(buf)) {
				s = 2;
				r = cptr - stateptr;
				strncpy(buf, stateptr, r);
				buf[r] = '\0';
			} else {
				cptr++;
			}
			tmp = new_node(buf);
			tmp->single = s;

			stateptr += r;

			cptr = buf + strlen(buf);
			attrib_parse(tmp, cptr);

			doc_new_root(doc, tmp);
			if (s)
				tmp = NULL;
			continue;
		}
		
		if (stateptr) {
			stateptr++;
			r = strcspn(stateptr, "<");
			strncpy(buf, stateptr, r);
			buf[r] = '\0';

			tmp = node_add_text(tmp, buf);
			stateptr = strstr(stateptr, "<");
		} else {
			r = strcspn(txt, "<");
			strncpy(buf, txt, r);
			buf[r] = '\0';

			tmp = node_add_text(tmp, buf);
			stateptr = strstr(txt, "<");
		}
		if (!stateptr)
			continue;
		stateptr += 1;
		if (stateptr[0] == '/') {
			cptr = strstr(stateptr, ">");
			if (!cptr) {
				if (!pushtxt(doc->file, txt, stateptr))
					return 0;
				cptr = strstr(stateptr, ">");
				if (!cptr) {
					return -1;
				}
			}
			stateptr += 1;
			r = cptr - stateptr;
			strncpy(buf, stateptr, r);
			buf[r] = '\0';
			if (strcasecmp(buf, tmp->name)) {
				fprintf(stderr, "malformed tag: <%s></%s>\n", tmp->name, buf);
			}
			tmp = tmp->parent;
			stateptr += r;
			continue;
		}
		cptr = strstr(stateptr, ">");
		if (!cptr) {
			if (!pushtxt(doc->file, txt, stateptr))
				return 0;
			cptr = strstr(stateptr, ">");
			if (!cptr) {
				return -1;
			}
		}
		r = cptr - stateptr;
		strncpy(buf, stateptr, r);
		buf[r] = '\0';
		buf[strcspn(buf, "\n\t ")] = '\0';
		stateptr += r;

		s = (*(stateptr - 1) == '/');
		if (s) {
			r = strlen(buf);
			if (r && (buf[r-1] == '/')) 
				buf[r-1] = '\0';
	 	}
		tmp = node_add_child(tmp, new_node(buf), s);

		cptr = buf + strlen(buf) + 1;
		attrib_parse(tmp, cptr);

		if (s) {
			tmp = tmp->parent;
		}
	}
	return 0;
}