Esempio n. 1
0
File: c.c Progetto: cmin764/cmiN
int nalloc(Node* node, int length, int size)
{
    /**
     * Aloca `length` memorie si intoarce adresa
     * de inceput. Daca nu s-a putut aloca memorie
     * intoarce -1.
     */
    // putin cod redundant
    if ((*node)->offset == -1) {
        /* daca suntem in nodul santinela
           inseamna ca lista este vida */
        if (length > size)
            return -1;
        // cream noul nod
        Node other = node_create(*node, 0, length);
        // totul ok
        *node = other;
        return 0;
    }
    // de fapt, mai mult cod redundant
    Node first, second;
    first = *node;
    second = first->next;
    // verificam daca exista spatiu inaintea capului
    if (length <= first->offset) {
        // alocam inainte si actualizam noul cap
        Node other = node_create(first, 0, length);
        *node = other;
        return 0;
    }
    /* daca am ajuns aici inseamna ca trebuie sa gasim
       o gaura printre zonele ocupate sau de la ultima
       zona pana la finalul campului */
    for (; second->offset != -1; first = second,
                                 second = first->next) {
        int newOffset;
        if (length > second->offset -
            (newOffset = first->offset + first->length)) {
            continue; // nu ne ajunge spatiul
        }
        // ne ajunge spatiul :]
        Node other = node_create(second, newOffset, length);
        first->next = other;
        return newOffset;
    }
    // poate mai e o sansa sa gasim dupa ultimul nod
    int newOffset;
    if (length <= size -
        (newOffset = first->offset + first->length)) {
        // Neo, nu uita ca acum `second` e santinela
        Node other = node_create(second, newOffset, length);
        first->next = other;
        return newOffset;
    }
    // daca s-a ajuns aici inseamna ca nu exista vreo zona buna
    return -1;
}
int main()
{
    struct list *l;
    struct node *n;
    struct node *n1;

    printf("\ntesting node_create...");
    n = node_create('e');
    n1 = node_create('i');
    if( n == NULL || n->c != 'e' || n->next != NULL )
        goto fail;
    if( n1 == NULL || n1->c != 'i' || n1->next != NULL )
        goto fail;
    printf("[ok]\n");

    printf("\ntesting list_create...");
    l = list_create();
    if(l)
        printf("[ok]\n");
    else
        goto fail;

    printf("testing list_add_head...");
    list_add_head(l,n);
    if (l->head != n)
        goto fail;
    list_add_head(l,n1);
    if(l->head != n1)
        goto fail;
    printf("[ok]\n");

    printf("testing list_find...");
    if ( list_find( l, 'e' ) != n || list_find( l, 'i' ) != n1 )
        goto fail;
    printf("[ok]\n");

    printf("testing list_del...");
    list_del(l, n);
    if(l->head == n)
        goto fail;
    else
        printf("[ok]\n");

    printf("testing list_find...");
    if ( list_find( l, 'e' ) != NULL || list_find( l, 'i' ) != n1 )
        goto fail;
    printf("[ok]\n");

    return 0;
fail:
    printf("[failed]\n");
    return -1;
}
static void test_dsync_mailbox_tree_sync_renames15(void)
{
	struct dsync_mailbox_tree *tree1, *tree2;

	test_begin("dsync mailbox tree sync renames 15");
	tree1 = dsync_mailbox_tree_init('/', '_');
	tree2 = dsync_mailbox_tree_init('/', '_');

	node_create(tree1, 1, "1", 0);
	node_create(tree2, 2, "1", 1);

	test_trees(tree1, tree2);
	test_end();
}
Esempio n. 4
0
/*初始化路由器列表,包括每个路由器的编号、距离之类的(搭拓扑的时候可能要用)*/
void init_router_list(struct sr_instance* sr){

	assert(sr);
	router_state *rs = get_router_state(sr);

	/* build an entry for our router */
	pwospf_router *our_router = (pwospf_router *)calloc(1, sizeof(pwospf_router));
	our_router->router_id = rs->router_id;
	our_router->area_id = rs->area_id;
	our_router->seq = 0;
	our_router->distance = 0;
	our_router->shortest_path_found = 0;
	time(&our_router->last_update);


	/* insert our_router in the pwospf router list */
	assert(rs->pwospf_router_list == NULL);
	node *n = node_create();
	n->data = (void *)our_router;
	rs->pwospf_router_list = n;


	/* advertise what's directly connected to us */
	node *il_walker = rs->if_list;
	while(il_walker) {
		iface_entry *ie = (iface_entry *)il_walker->data;

		pwospf_interface *pi = (pwospf_interface *)calloc(1, sizeof(pwospf_interface));
		pi->subnet.s_addr = (ie->ip & ie->mask);
		pi->mask.s_addr = ie->mask;
		pi->router_id = 0;
		pi->is_active = 0;

		node *rl_entry = node_create();
		rl_entry->data = (void *)pi;
		if(our_router->interface_list == NULL) {
			our_router->interface_list = rl_entry;
		}
		else {
			node_push_back(our_router->interface_list, rl_entry);
		}

		il_walker = il_walker->next;
	}


	//char *str; int len;
	//sprint_pwospf_router_list(rs, &str, &len);
	//printf("\nINITIAL ROUTER LIST:\n\n%s\n\n", str);
}
int main(int argc, char *argv[]) {
  Node *head, *tmp;
  node_create(&head, 3);
  node_append(head, 1);
  node_append(head, 2);
  node_append(head, 4);
  node_append(head, 1);
  node_append(head, 1);
  node_append(head, 2);

  printf("BEFORE\n");
  tmp = head;
  while (tmp != NULL) {
    printf("elem=%d\n", tmp->data);
    tmp = tmp->next;
  }

  node_rm_dup(head);

  printf("AFTER\n");
  tmp = head;
  while (tmp != NULL) {
    printf("elem=%d\n", tmp->data);
    tmp = tmp->next;
  }

  node_delete_all(&head);
  if (head == NULL) {
    printf("delete succeeded\n");
  }

  return 0;
}
Esempio n. 6
0
/*插入新的节点*/
void list_insert(LIST_NODE *head, int data)
{
	LIST_NODE *node = node_create(data);
	
	node->next = head->next;
	head->next = node;
}
Esempio 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;
}
Esempio n. 8
0
/**
 * Split this node's edge by creating a new node in the middle. Remember
 * to preserve the "once a leaf always a leaf" property or f will be wrong.
 * @param v the node in question
 * @param loc the place on the edge after which to split it
 * @return the new internal node
 */
node *node_split( node *v, int loc )
{
    // create front edge leading to internal node u
    int u_len = loc-v->start+1;
    node *u = node_create( v->start, u_len );
    // now shorten the following node v
    if ( !node_is_leaf(v) )
        v->len -= u_len;
    v->start = loc+1;
    // replace v with u in the children of v->parent
    node *child = v->parent->children;
    node *prev = child;
    while ( child != NULL && child != v )
    {
        prev = child;
        child = child->next;
    }
    if ( child == prev )
        v->parent->children = u;
    else
        prev->next = u;
    u->next = child->next;
    v->next = NULL;
    // reset parents
    u->parent = v->parent;
    v->parent = u;
    u->children = v;
    return u;
}
Esempio n. 9
0
File: dep_cpp.c Progetto: malind/bam
static void cachehit_callback(struct NODE *node, struct CACHENODE *cachenode, void *user)
{
	struct CACHERUNINFO *info = (struct CACHERUNINFO *)user;
	
	/* check if the file has been removed */
	struct NODE *existing_node = node_find_byhash(node->graph, cachenode->hashid);
	if(existing_node)
	{
		struct NODE *newnode = node_add_dependency (node, existing_node);
		dependency_cpp_run(info->context, newnode, info->callback, info->userdata);
	}
	else
	{
		time_t timestamp = file_timestamp(cachenode->filename);
		if(timestamp)
		{
			/* this shouldn't be able to fail */
			struct NODE *newnode;
			node_create(&newnode, info->context->graph, cachenode->filename, NULL, timestamp);
			node_add_dependency (node, newnode);

			/* recurse the dependency checking */
			dependency_cpp_run(info->context, newnode, info->callback, info->userdata);
		}
		else
		{
			node->dirty = NODEDIRTY_MISSING;
		}
	}
}
Esempio n. 10
0
File: node.c Progetto: scolobb/nsmux
/*Creates the root node and the corresponding lnode*/
error_t node_create_root (node_t ** root_node)
{
  /*Try to create a new lnode */
  lnode_t *lnode;
  error_t err = lnode_create (NULL, &lnode);

  /*Stop, if the creation failed */
  if (err)
    return err;

  /*Try to derive the node corresponding to `lnode` */
  node_t *node;
  err = node_create (lnode, &node);

  /*If the operation failed */
  if (err)
    {
      /*destroy the created lnode */
      lnode_destroy (lnode);

      /*stop */
      return err;
    }

  /*Release the lock on the lnode */
  mutex_unlock (&lnode->lock);

  /*Store the result in the parameter */
  *root_node = node;

  /*Return the result */
  return err;
}				/*node_create_root */
Esempio n. 11
0
struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name,
                         struct fuse_attr *attr)
{
    int res;
    struct stat s;
    char *path, buffer[PATH_BUFFER_SIZE];
    struct node *node;

    path = node_get_path(parent, buffer, name);
        /* XXX error? */

    res = lstat(path, &s);
    if (res < 0)
        return 0;
    
    node = lookup_child_by_name(parent, name);
    if (!node) {
        node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++);
        if (!node)
            return 0;
        node->nid = ptr_to_id(node);
        node->all = fuse->all;
        fuse->all = node;
    }

    attr_from_stat(attr, &s);
    attr->ino = node->nid;

    return node;
}
Esempio n. 12
0
Node* node_append(Node* n, char* key, char* value) {
    Node* new_n = node_create(key, value);

    n->next = new_n;

    return new_n;
}
Esempio n. 13
0
File: luafuncs.c Progetto: MAP94/bam
/* add_output(string output, string other_output) */
int lf_add_output(lua_State *L)
{
	struct NODE *output;
	struct NODE *other_output;
	struct CONTEXT *context;
	int i;
	const char *filename;
	
	if(lua_gettop(L) != 2)
		luaL_error(L, "add_output: incorrect number of arguments");

	luaL_checktype(L, 1, LUA_TSTRING);
	luaL_checktype(L, 2, LUA_TSTRING);
	
	context = context_get_pointer(L);

	output = node_find(context->graph, lua_tostring(L,1));
	if(!output)
		luaL_error(L, "add_output: couldn't find node with name '%s'", lua_tostring(L,1));

	if(!output->job->real)
		luaL_error(L, "add_output: '%s' does not have a job", lua_tostring(L,1));


	filename = lua_tostring(L, -1);
	i = node_create(&other_output, context->graph, filename, output->job);
	if(i == NODECREATE_NOTNICE)
		luaL_error(L, "add_output: node '%s' is not nice", filename);
	else if(i == NODECREATE_EXISTS)
		luaL_error(L, "add_output: node '%s' already exists", filename);
	else if(i != NODECREATE_OK)
		luaL_error(L, "add_output: unknown error creating node '%s'", filename);

	return 0;
}
Esempio n. 14
0
File: luafuncs.c Progetto: MAP94/bam
/* add_pseudo(string node) */
int lf_add_pseudo(lua_State *L)
{
	struct NODE *node;
	struct CONTEXT *context;
	int i;
	
	if(lua_gettop(L) != 1)
		luaL_error(L, "add_pseudo: incorrect number of arguments");

	luaL_checktype(L, 1, LUA_TSTRING);
	
	/* fetch contexst from lua */
	context = context_get_pointer(L);

	/* create the node */
	i = node_create(&node, context->graph, lua_tostring(L,1), NULL);
	if(i == NODECREATE_NOTNICE)
		luaL_error(L, "add_pseudo: node '%s' is not nice", lua_tostring(L,1));
	else if(i == NODECREATE_EXISTS)
		luaL_error(L, "add_pseudo: node '%s' already exists", lua_tostring(L,1));
	else if(i != NODECREATE_OK)
		luaL_error(L, "add_pseudo: unknown error creating node '%s'", lua_tostring(L,1));
		
	node_set_pseudo(node);
	return 0;
}
Esempio n. 15
0
/* Insert a node with name and value into the proper place in the DB rooted at
 * head. */
int add(char *name, char *value) {
	node_t *parent;	    /* The new node will be the child of this node */
	node_t *target;	    /* The existing node with key name if any */
	node_t *newnode;    /* The new node to add */

	//Target and parent will be locked after this
	if ((target = searchAR(name, &head, &parent))) 
	{
	    /* There is already a node with this key in the tree */
	    pthread_rwlock_unlock(&(target->mutex_node_lock));
	    pthread_rwlock_unlock(&(parent->mutex_node_lock));
	    return 0;
	}
	/* No idea how this could happen, but... */
	if (!parent) return 0;

	/* make the new node and attach it to parent */
	newnode = node_create(name, value, 0, 0);

	if (strcmp(name, parent->name) < 0) 
	{
		parent->lchild = newnode;
	}
	else 
	{
		parent->rchild = newnode;
	}
	//Unlock the lock
	pthread_rwlock_unlock(&(parent->mutex_node_lock));

	return 1;
}
Esempio n. 16
0
/*
 * IS THREADSAFE
 */
int add_local_ip_filter(router_state* rs, struct in_addr* ip, char* name) {
	if (get_local_ip_filter_by_ip(rs, ip) || get_local_ip_filter_by_name(rs, name)) {
		return 1;
	}

	node* cur = node_create();
	local_ip_filter_entry* entry = (local_ip_filter_entry*)calloc(1, sizeof(local_ip_filter_entry));
	strncpy(entry->name, name, (LOCAL_IP_FILTER_ENTRY_NAME_LEN - 1));
	entry->ip.s_addr = ip->s_addr;
	cur->data = entry;

	lock_local_ip_filters(rs);

	if (!rs->local_ip_filter_list) {
		rs->local_ip_filter_list = cur;
	} else {
		node_push_back(rs->local_ip_filter_list, cur);
	}

	unlock_local_ip_filters(rs);

	trigger_local_ip_filters_change(rs);

	return 0;
}
Esempio n. 17
0
TreeNode *tree_node_insert (Tree *tree, TreeNode *root, Object *key, void *value)
{
	if (root == tree->nil) {
		if (!(root = node_create (tree, key, 1))) {
			error (FunctionCall);
			return NULL;
		}
        	root->value = value;
    	}
    	else {
		TreeNode *node = NULL;
		int direction;

		if (root->key->id == key->id) {
			error (InvalidOperation);
			return NULL;
		}
		direction = root->key->id < key->id;
		if (!(node = tree_node_insert (tree, root->link[direction], key, value))) {
			return NULL;
		}
		root->link[direction] = node;
        	root = skew (root);
        	root = split (root);
    	}
    	return root;
}
Esempio n. 18
0
int
main(int argc, char **argv)
{
    opt_t *options;
    int    rc = EXIT_FAILURE;

    options = opt_new(argc, argv);
    if (options->input) {
        if (!(yyin = fopen(options->input, "r"))) {
            int n = errno;
            log_error("cannot open input file '%s': %s\n", options->input, strerror(n));
            goto end;
        }
    }
    if (yyparse() != 0) {
        log_error("could not parse DDL");
        goto end;
    }
    if (node_create(file, NULL, options) != 0) {
        log_error("error creating HDF5 file");
    }
    node_free(file);
    rc = EXIT_SUCCESS;

end:
    if (options->input) {
        fclose(yyin);
    }
    opt_free(options);
    return rc;
}
Esempio n. 19
0
//
// AUTOTEST: ann node
//
void xautotest_ann_node()
{
    float w[4] = {1,2,3,4}; // weights vector
    float x[3] = {5,1,3};   // input vector
    float y[1];             // output vector

    // create node
    node q = node_create(w,     // weights
                         x,     // input
                         y,     // output
                         3,     // num_inputs
                         0,     // activation function ID
                         1.0f   // activation function gain
                        );

    // evaluate node
    node_evaluate(q);

    if (liquid_autotest_verbose) {
        node_print(q);
        printf("y = %12.8f\n", y[0]);
    }

    // contend equality of output
    CONTEND_EQUALITY(y[0], 20.0f);

    // destroy node
    node_destroy(q);
}
Esempio n. 20
0
node_t* node_create_unary_oper(nodetype_t nodetype, node_t* expr) {
    node_t* self = node_create(nodetype);

    self->n1 = expr;

    return self;
}
Esempio n. 21
0
node_t* node_create_vardecl(ijavatype_t type, node_t* vars) {
    DEBUG_PRINT("[node_create_vardecl] type='%d' ('%s'), vars='%p'\n", type, node_type_names[type], vars);
    assert(vars != NULL);
    node_t* self = node_create(NODE_VARDECL);

    /* n1 points to a node such as IntArray, Int, Bool, etc */
    self->n1 = node_create_type(type);

    /* n2 points to the first node belonging to this vardecl */
    self->n2 = vars;


    DEBUG_PRINT("[node_create_vardecl]-->%s ", node_type_names[type]);
    node_t* iter = vars;
    while ( iter != NULL ) {
        assert(iter->type == TYPE_UNKNOWN );
        iter->type = TYPE_ID;
        DEBUG_PRINT("%s, ", iter->id);
        iter = iter->next;        
    }
    DEBUG_PRINT("\n");

    DEBUG_PRINT("[node_create_vardecl]-->self = %p\n", self);
    return self;
}
Esempio n. 22
0
void skiplist_insert(struct skiplist* s, int key) {
    struct node *p,*q,*cur[32];
    int i;
    volatile int level;

    q = s->head;
    for(i=s->level; i>=0; i--) {
        p = q->next[i];
        while(p && p->key < key) {
            q = p;
            p = p->next[i];
        }
        cur[i] = q;
    }
    level = random_level();
    if(level > s->level) {
        s->level++;
        level = s->level;
        cur[level] = s->head;
    }
    p = node_create(key,level);
    for(i=level; i>=0; i--) {
        p->next[i] = cur[i]->next[i];
        cur[i]->next[i] = p;
    }
    s->length++;
}
Esempio n. 23
0
/* Insert a node with name and value into the proper place in the DB rooted at
 * head. */
int add(char *name, char *value) {
  node_t *parent;	    /* The new node will be the child of this node */
  node_t *target;	    /* The existing node with key name if any */
  node_t *newnode;    /* The new node to add */

  // Lock the mutex for adding
  pthread_mutex_lock(&g_coarse_mutex);

  if ((target = search(name, &head, &parent))) {
    /* There is already a node with this key in the tree */
    pthread_mutex_unlock(&g_coarse_mutex);
    return 0;
  }

  /* No idea how this could happen, but... */
  if (!parent) { 
    pthread_mutex_unlock(&g_coarse_mutex);
    return 0;
  }

  /* make the new node and attach it to parent */
  newnode = node_create(name, value, 0, 0);

  if (strcmp(name, parent->name) < 0) parent->lchild = newnode;
  else parent->rchild = newnode;

  // Unlock the mutex
  pthread_mutex_unlock(&g_coarse_mutex);

  return 1;
}
Esempio n. 24
0
/*
 * Add outlet to breaker, given outlet model id and breaker id.
 *
 * result will store the outlet_id and 2 receptacle id's.
 *
 * Returns:
 *  Success: SUCCESS
 *  Failure: ERR_E_MODEL_NOT_EXISTS, ERR_INVALID_BREAKER_ID, ERR_INVALID_MODEL_ID, ERR_OUTLET_AMPS_EXCEED_BREAKER_AMPS
 */
int8_t add_outlet_to_breaker(CIRCUIT_MODELS_T outlet_model_id, uint32_t breaker_id, assemble_result_t *result) {

	if (FALSE == load_center_is_created()) {
		return ERR_E_MODEL_NOT_EXISTS;
	}

	breaker_t *breaker = get_breaker_by_id(breaker_id);
	if (NULL == breaker) {
		return ERR_INVALID_BREAKER_ID;
	}
	outlet_t *o = get_new_outlet_by_model_id(outlet_model_id);
	if (NULL == o) {
		return ERR_INVALID_MODEL_ID;
	}

	// don't want a 20amp outlet on a 15amp breaker
	if (o->amp_rating > breaker->amp_rating) {
		deallocate((void *)o, sizeof(outlet_t));
		return ERR_OUTLET_AMPS_EXCEED_BREAKER_AMPS;
	}

	result->object_id = o->id;
	result->receptacle_id[0] = o->r1.id;
	result->receptacle_id[1] = o->r2.id;
	result->receptacle_ids_assigned_cnt = 2;
	return list_append(breaker->outlets, node_create((void *)o));
}
Esempio n. 25
0
static struct node_t* new_uint_node(uint64_t value)
{
    plist_data_t data = plist_new_plist_data();
    data->type = PLIST_UINT;
    data->intval = value;
    data->length = sizeof(uint64_t);
    return node_create(NULL, data);
}
Esempio n. 26
0
node_t* node_create_statement_print(node_t* expr) {
    node_t* self = node_create(NODE_STATEMENT_PRINT);

    assert(expr);
    self->n1 = expr;

    return self; 
}
Esempio n. 27
0
node_t* node_create_statement_return(node_t* expr) {
    node_t* self = node_create(NODE_STATEMENT_RETURN);

    /* expr might be NULL, in which case it is okay to still assign */
    self->n1 = expr;

    return self;
}
Esempio n. 28
0
node_t* node_create_binary_oper(nodetype_t nodetype, node_t* lhs, node_t* rhs) {
    node_t* self = node_create(nodetype);

    self->n1 = lhs;
    self->n2 = rhs;

    return self;
}
Esempio n. 29
0
// リストの末尾に追加する
void node_append(node_t *nd, int dt) {
    node_t *n = node_create();
    n->data = dt;
    while (nd->next != NULL) {
        nd = nd->next;
    }
    nd->next = n;
}
Esempio n. 30
0
/**
 * Creates a new node in the graph and returns the node pointer.
 */
node *graph_add_node(graph *g) {
    node *n = node_create();

    if (!n || NULL == node_array_add(g->nodes, n))
	return NULL;

    return n;
}