Esempio n. 1
0
void create_children (int height, struct node *node, int maxHeight) {
    if (height < maxHeight) {
        srand(time(NULL));
        node->left = (struct node *) malloc (sizeof (struct node));
        node->left->value = 2*(node->value);
        node->right = (struct node *) malloc (sizeof (struct node));
        node->right->value = 2*(node->value) + 1;
        create_children (height+1, node->left, maxHeight);
        create_children (height+1, node->right, maxHeight);
    }
}
/**
 * gnc_general_search_new:
 *
 * Creates a new GNCGeneralSearch widget which can be used to provide
 * an easy way to choose selections.
 *
 * @param type The type of object that this widget will be used for.
 * This parameter is a QofIdTypeConst.
 * @param label The label for the GtkButton child widget.
 * @param text_editable switch to enable or disable direct text entry
 * @param search_cb The callback function to use when an object has been
 * selected in the search dialog. This dialog is created when clicking on
 * the GtkButton child widget.
 * @param user_data Generic pointer to context relevant data that can be
 * used by callback functions later on. At present, depending on the context
 * this can be a QofBook, a GncISI structure or a InvoiceWindow structure.
 * @param book Pointer to the QofBook for this search widget. This is used for
 * the autocompletion in the text entry widget.
 *
 * @return a GNCGeneralSearch widget.
 */
GtkWidget *
gnc_general_search_new (QofIdTypeConst type,
                        const char    *label,
                        gboolean       text_editable,
                        GNCSearchCB    search_cb,
                        gpointer       user_data,
                        QofBook       *book)
{
    GNCGeneralSearch *gsl;
    GNCGeneralSearchPrivate *priv;
    const QofParam *get_guid;

    g_return_val_if_fail (type && label && search_cb, NULL);

    get_guid = qof_class_get_parameter (type, QOF_PARAM_GUID);
    g_return_val_if_fail (get_guid, NULL);

    gsl = g_object_new (GNC_TYPE_GENERAL_SEARCH, NULL);

    create_children (gsl, label, text_editable, type, book);

    priv = _PRIVATE(gsl);
    priv->type = type;
    priv->search_cb = search_cb;
    priv->user_data = user_data;
    priv->get_guid = get_guid;
    priv->component_id =
        gnc_register_gui_component (GNCGENERALSEARCH_CLASS,
                                    refresh_handler, NULL, gsl);

    return GTK_WIDGET (gsl);
}
Esempio n. 3
0
void test_lock(void)
{
    SHARED_VAR = 0;
    
    pthread_t tids[NUM_THREADS];
    
    create_children(tids);
    join_children(tids);
    
    assert_int_equal(SHARED_VAR, NUM_THREADS);
}
Esempio n. 4
0
int
main(int argc, char *argv[])
{
    int i;
    signal(SIGCHLD, sig_child);
    create_children(argv);
    parent_main_loop();
    for (i = 3; i <= maxfd; i++)
	close(i);
    sleep(1);
}
Esempio n. 5
0
OutlineList *
LibraryElement::children()
{
  if (!children_valid())
    {
      create_children();
      children_valid (TRUE);
    }

  return (f_children);
}
Esempio n. 6
0
// trie construction with a depth-first traverse (dfs)
Trie* build_trie(Rule *rules, int nrules, int leaf_rules)
{
	Trie*	v;
	int		i;

	total_rules = nrules;
	LEAF_RULES = leaf_rules;
	root_node = init_trie(rules, nrules);
	create_children(root_node);

	dump_stats();
}
Esempio n. 7
0
/**
 * gnc_date_delta_new:
 * @show_polarity: whether 'from now/ago' menu should be displayed.
 *
 * Creates a new GNCDateDelta widget which can be used to provide
 * an easy to use way for entering time deltas in terms of 7 days,
 * 5 weeks, 2 months, etc.
 *
 * Returns a GNCDateDelta widget.
 */
GtkWidget *
gnc_date_delta_new (gboolean show_polarity)
{
    GNCDateDelta *gdd;

    gdd = g_object_new (gnc_date_delta_get_type (), NULL);

    gdd->show_polarity = show_polarity;

    create_children (gdd);

    return GTK_WIDGET (gdd);
}
Esempio n. 8
0
static int
create_peers(mmd_t *mdp, di_prom_handle_t ph, md_node_t *node, di_node_t dev)
{
	di_node_t	di_peer;
	int		rv;

	while ((di_peer = di_sibling_node(dev)) != DI_NODE_NIL) {
		rv = create_children(mdp, ph, node, di_peer);
		if (rv != 0)
			return (rv);
		dev = di_peer;
	}
	return (0);
}
Esempio n. 9
0
/**
 * gnc_date_edit_new_flags:
 * @the_time: The initial time for the date editor.
 * @flags: A bitmask of GNCDateEditFlags values.
 *
 * Creates a new GNCDateEdit widget with the specified flags.
 *
 * Return value: the newly-created date editor widget.
 **/
GtkWidget *
gnc_date_edit_new_flags (time64 the_time, GNCDateEditFlags flags)
{
    GNCDateEdit *gde;

    gde = g_object_new (GNC_TYPE_DATE_EDIT, NULL, NULL);

    gde->flags = flags;
    gde->initial_time = -1;
    create_children (gde);
    gnc_date_edit_set_time (gde, the_time);

    return GTK_WIDGET (gde);
}
Esempio n. 10
0
Pos uct_search(Board * board, Color color) {
    int i;
    UCTNode * root, * ptr;
    Board * clone;
    Pos best_move;

    root = uct_node_new(nil);
    create_children(root, board, color);

    LOOP(i, UCT_SIMULATIONS) {
        clone = board_clone(board);
        uct_simulate(root, clone, color);
        //board_print(clone, stdout);
        board_free(clone);
    }
Esempio n. 11
0
int uct_simulate(UCTNode * self, Board * board, Color color) {
    int result;
    Pos move;
    UCTNode * ptr;

    if(!self->child && self->visits < UCT_MIN_VISITS)
        result = uct_play_random_game(board, color);
    else {
        if(!self->child) create_children(self, board, color);
        ptr = uct_select(self, board, color);
        result = 1 - uct_simulate(ptr, board, color_other(color));
    }
    self->visits ++;
    self->wins += 1 - result;

    return result;
}
Esempio n. 12
0
/* constructs the root and it's children. 
 * once this is assured, alphabeta may continue construction */
vertex build_tree(int * game_matrix, int player,linked_list (*create_children)(int *gameMatrix, int player,int *error))
{
	linked_list new_children=NULL;
	int error=0;
	/* new root */
	vertex root = make_node(0, game_matrix,0);
	if (root == NULL){
		printf("ERROR: can't create root\n");
		return NULL;
	}
	new_children=create_children(root->game_state, player,&error);
	if (error<0){
		printf("ERROR: can't create children for root\n");
		remove_tree(root,1,0);
		return NULL;
	}
	root->edges=new_children;
	return root;
}
Esempio n. 13
0
void create_children(Trie *v)
{
	int		dim, val, max_child_nrules = 0;
	Band	*cut;
	Trie	*u;

	if (v->depth >= MAX_DEPTH-1) {
		//dump_path(v, 2);
		return;
	}

	if (v->depth > 0)	{
		for (dim = 0; dim < NFIELDS; dim++)
			dfs_uncuts[v->depth][dim] = dfs_uncuts[v->depth-1][dim];
	}

	choose_cut(v);
	cut = &dfs_cuts[v->depth];
	dfs_uncuts[v->depth][cut->dim]--;
	if (v->nrules <= REDUN_NRULES)
		calc_rule_redun(v, cut);

	for (val = 0; val < BAND_SIZE; val++) {
		cut->val = val;
		u = new_child(v, cut);
		if (u == NULL)
			continue;
		if (u->nrules > LEAF_RULES) {
			create_children(u);	
		} else if (v->depth > max_depth) {
			max_depth = v->depth;
			max_depth_leaf = u;
		}
		if (u->nrules > max_child_nrules)
			max_child_nrules = u->nrules;
	}
	calc_cut_efficiency(v->depth, v->nrules, max_child_nrules);
	depth_max_node[v->depth+1] = max_child_nrules;
	v->children = realloc(v->children, v->nchildren*sizeof(Trie));
	dfs_uncuts[v->depth][cut->dim]++;
}
void    recurse_in_directory(t_item *child_to_parent, t_args *args)
{
    DIR		*fd_file;
    
    fd_file = NULL;
    if (is_directory(child_to_parent) == 1 && is_dot(child_to_parent->name) == 0)
    {
        if ((fd_file = open_dir(child_to_parent->path)))
        {
            closedir(fd_file);
            ft_putchar('\n');
            args->options->Q ? ft_putchar('"') : 0;
            ft_putstr(child_to_parent->path);
            args->options->Q ? ft_putchar('"') : 0;
            ft_putstr(":\n");
            create_children(child_to_parent, args);
        }
        else 
            cannot_open(child_to_parent, args);
    }
}
Esempio n. 15
0
int main(int argc, char* argv [])
{
    int64_t number = 0;
    char* endptr = NULL;	
    const uint8_t base = 10;
	/*
     * Handler input data
     */
	switch (argc) {
    case 1:
        fprintf(stderr, "No arguments\n");
        exit(EXIT_FAILURE);
        break;
    case 2:
        number = strtol(argv[1], &endptr, base);
        if (argv[1] + strlen(argv[1]) != endptr) {
            fprintf (stderr, "Uncorrect input data (found symbols).\n");
            exit(EXIT_FAILURE);
        }
        if (number < 1) {
            fprintf (stderr, "Uncorrect interval(only from 1).\n");
            exit(EXIT_FAILURE);
        }
        if ((errno == ERANGE && (number == LONG_MAX || number == LONG_MIN)) || \
           (number > INT_MAX || number < INT_MIN )) {
            fprintf(stderr, "Too long for me.\n");
            exit(EXIT_FAILURE);				
        }
        break;
    default:
        fprintf (stderr, "Many arguments for me.\n");
        exit(EXIT_FAILURE);
    }	
    create_children(number);
    return (EXIT_SUCCESS);    
}
Esempio n. 16
0
static int
device_tree_to_md(mmd_t *mdp, md_node_t *top)
{
	di_node_t		node;
	di_node_t		root;
	di_prom_handle_t	ph;
	int			rv = 0;

	root = di_init("/", DINFOSUBTREE | DINFOPROP);

	if (root == DI_NODE_NIL) {
		LDMA_ERR("di_init cannot find device tree root node.");
		return (errno);
	}

	ph = di_prom_init();
	if (ph == DI_PROM_HANDLE_NIL) {
		LDMA_ERR("di_prom_init failed.");
		di_fini(root);
		return (errno);
	}

	node = di_child_node(root);
	while (node != NULL) {
		if (is_root_complex(ph, node)) {
			rv = create_children(mdp, ph, top, node);
			if (rv != 0)
				break;
		}
		node = di_sibling_node(node);
	}

	di_prom_fini(ph);
	di_fini(root);
	return (rv);
}
Esempio n. 17
0
t_item		*new_link(char const *name, t_item *parent, t_args *args)
{
	t_item	*link;

	link = NULL;
	if ((link = (t_item *)malloc(sizeof(t_item))))
	{
		set_new_link(link);
		link->name = ft_strdup(name);
		link->parent = (t_item *)parent;
		if (link->parent)
			link->path = get_path(link);
		else
			link->path = link->name;
		if (build_stats(link, args) != 0)
			return (NULL);
		if (parent == NULL && ((args->options->l == 1 &&
				is_symbolic_link(link) == 0) || (args->options->l == 0)))
			create_children(link, args);
	}
	else
		not_enough_memory(args);
	return (link);
}
Esempio n. 18
0
void Player::PlayerUCT::walk_tree(Board & board, Node * node, int depth){
	int toplay = board.toplay();

	if(!node->children.empty() && node->outcome < 0){
	//choose a child and recurse
		Node * child;
		do{
			int remain = board.movesremain();
			child = choose_move(node, toplay, remain);

			if(child->outcome < 0){
				movelist.addtree(child->move, toplay);

				if(!board.move(child->move, (player->minimax == 0), (player->locality || player->weightedrandom) )){
					logerr("move failed: " + child->move.to_s() + "\n" + board.to_s(false));
					assert(false && "move failed");
				}

				child->exp.addvloss(); //balanced out after rollouts

				walk_tree(board, child, depth+1);

				child->exp.addv(movelist.getexp(toplay));

				if(!player->do_backup(node, child, toplay) && //not solved
					player->ravefactor > min_rave &&  //using rave
					node->children.num() > 1 &&       //not a macro move
					50*remain*(player->ravefactor + player->decrrave*remain) > node->exp.num()) //rave is still significant
					update_rave(node, toplay);

				return;
			}
		}while(!player->do_backup(node, child, toplay));

		return;
	}

	if(player->profile && stage == 0){
		stage = 1;
		timestamps[1] = Time();
	}

	int won = (player->minimax ? node->outcome : board.won());

	//if it's not already decided
	if(won < 0){
		//create children if valid
		if(node->exp.num() >= player->visitexpand+1 && create_children(board, node, toplay)){
			walk_tree(board, node, depth);
			return;
		}

		if(player->profile){
			stage = 2;
			timestamps[2] = Time();
		}

		//do random game on this node
		for(int i = 0; i < player->rollouts; i++){
			Board copy = board;
			rollout(copy, node->move, depth);
		}
	}else{
		movelist.finishrollout(won); //got to a terminal state, it's worth recording
	}

	treelen.add(depth);

	movelist.subvlosses(1);

	if(player->profile){
		timestamps[3] = Time();
		if(stage == 1)
			timestamps[2] = timestamps[3];
		stage = 3;
	}

	return;
}
Esempio n. 19
0
static int dm_test_children(struct unit_test_state *uts)
{
	struct dm_test_state *dms = uts->priv;
	struct udevice *top[NODE_COUNT];
	struct udevice *child[NODE_COUNT];
	struct udevice *grandchild[NODE_COUNT];
	struct udevice *dev;
	int total;
	int ret;
	int i;

	/* We don't care about the numbering for this test */
	dms->skip_post_probe = 1;

	ut_assert(NODE_COUNT > 5);

	/* First create 10 top-level children */
	ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));

	/* Now a few have their own children */
	ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
	ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));

	/* And grandchildren */
	for (i = 0; i < NODE_COUNT; i++)
		ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
					    i == 2 ? grandchild : NULL));

	/* Check total number of devices */
	total = NODE_COUNT * (3 + NODE_COUNT);
	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);

	/* Try probing one of the grandchildren */
	ut_assertok(uclass_get_device(UCLASS_TEST,
				      NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
	ut_asserteq_ptr(grandchild[0], dev);

	/*
	 * This should have probed the child and top node also, for a total
	 * of 3 nodes.
	 */
	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);

	/* Probe the other grandchildren */
	for (i = 1; i < NODE_COUNT; i++)
		ut_assertok(device_probe(grandchild[i]));

	ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);

	/* Probe everything */
	for (ret = uclass_first_device(UCLASS_TEST, &dev);
	     dev;
	     ret = uclass_next_device(&dev))
		;
	ut_assertok(ret);

	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);

	/* Remove a top-level child and check that the children are removed */
	ut_assertok(device_remove(top[2]));
	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
	dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;

	/* Try one with grandchildren */
	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
	ut_asserteq_ptr(dev, top[5]);
	ut_assertok(device_remove(dev));
	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
		    dm_testdrv_op_count[DM_TEST_OP_REMOVE]);

	/* Try the same with unbind */
	ut_assertok(device_unbind(top[2]));
	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
	dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;

	/* Try one with grandchildren */
	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
	ut_asserteq_ptr(dev, top[6]);
	ut_assertok(device_unbind(top[5]));
	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
		    dm_testdrv_op_count[DM_TEST_OP_UNBIND]);

	return 0;
}
Esempio n. 20
0
int alphaBeta(vertex Node,int alpha, int beta,int player,int depth,int maxdepth,linked_list (*create_children)(int *gameMatrix, int player,int *error),int* error){
	int aboveAlpha=alpha,aboveBeta=beta;
	int temp,childError;
	vertex child;
	element run;
	linked_list new_children;
	
	/* end-cases, return a score for some leaf */
	if (depth>=maxdepth || Node->edges->head==NULL){
		temp= Node->score;
		if(depth>1){
			remove_tree(Node,0,0);
		}
		*error=0;
		return temp;
	}
	
	if (player==ENUM_PLAYER_1){
		/* for every node */
		for (run = Node->edges->head;run != NULL; run = run->next){
			child=run->node;
			if (depth<maxdepth-1){
				int create_childrenError=0;
				/*create a new level of children*/
				new_children=create_children(child->game_state, player*-1,&create_childrenError);
				if(create_childrenError==-1){
					printf("ERROR: can't create children\n");
					*error=-1;
					return 0;
				}
				child->edges=new_children;
			}
			/* continue this process on the next level */
			temp=alphaBeta(child,alpha,beta,player*-1,depth+1,maxdepth,create_children,&childError);
			if (childError==-1){
				*error=-1;
				return 0;
			}
			/* we now have a score from the subtreee*/
			if(aboveAlpha < temp){
				aboveAlpha=temp;
			}
			/* perfom pruning on this level */
			if (aboveBeta<=aboveAlpha){
				for ( run = run->next;run != NULL; run = run->next){
					child=run->node;
					if(depth>0){
						remove_tree(child,0,0);
					}
				}
				break;
			}
			
		}
		/* taking max */
		Node->score=aboveAlpha;
		if(depth>1){
			remove_tree(Node,0,0);
		}
		*error=0;
		/*return some score*/
		return aboveAlpha;
	}
	/* perform the same process with roles reversed */
	else {
		for (run = Node->edges->head;run != NULL; run = run->next){
			child=run->node;
			if (depth<maxdepth-1){
				int create_childrenError=0;
				new_children=create_children(child->game_state, player*-1,&create_childrenError);			// add children
				if(create_childrenError==-1){
					printf("ERROR: can't create children\n");
					*error=-1;
					return 0;
				}
				child->edges=new_children;
			}
			temp=alphaBeta(child,alpha,beta,player*-1,depth+1,maxdepth,create_children,&childError);
			if (childError==-1){
				*error=-1;
				return 0;
			}
			if(aboveBeta > temp){
				aboveBeta=temp;
			}
			if (aboveBeta<=aboveAlpha){
				for ( run = run->next;run != NULL; run = run->next){
					child=run->node;
					if(depth>0){
						remove_tree(child,0,0);
					}
				}
				break;
			}
		}
		Node->score=aboveBeta;
		if(depth>1){
			remove_tree(Node,0,0);
		}
		*error=0;
		return aboveBeta;
	}
}