sexpr lx_make_environment (sexpr env) { static struct memory_pool pool = MEMORY_POOL_INITIALISER(sizeof (struct environment)); struct environment *rv; struct tree_node *n; int_pointer hash = hash_murmur2_pt ((void *)&env, sizeof(env), 0); /* note: the hashing may seem pointless, but it does help performance by * effectively randomising the input data, thus helping the tree performance * by making it far less likely for the tree to degenerate into a linked * list. */ if ((n = tree_get_node (&environment_tree, hash))) { return (sexpr)node_get_value (n); } rv = get_pool_mem (&pool); if (rv == (struct environment *)0) { return sx_nonexistent; } rv->type = environment_type_identifier; rv->environment = env; tree_add_node_value (&environment_tree, hash, (void *)rv); return (sexpr)rv; }
gchar* tree_get_string(const gchar *node, const gchar *def) { xmlNodePtr n; n = tree_get_node(node, def); return obt_xml_node_string(n); }
gboolean tree_get_bool(const gchar *node, gboolean def) { xmlNodePtr n; n = tree_get_node(node, (def ? "yes" : "no")); return obt_xml_node_bool(n); }
void tree_delete_node(const gchar *path) { xmlNodePtr n; n = tree_get_node(path, NULL); xmlUnlinkNode(n); xmlFreeNode(n); }
void tree_set_bool(const gchar *node, const gboolean value) { xmlNodePtr n; n = tree_get_node(node, NULL); xmlNodeSetContent(n, (const xmlChar*) (value ? "yes" : "no")); tree_apply(); }
void tree_set_string(const gchar *node, const gchar *value) { xmlNodePtr n; n = tree_get_node(node, NULL); xmlNodeSetContent(n, (const xmlChar*) value); tree_apply(); }
gint tree_get_int(const gchar *node, gint def) { xmlNodePtr n; gchar *d; d = g_strdup_printf("%d", def); n = tree_get_node(node, d); g_free(d); return obt_xml_node_int(n); }
void tree_set_int(const gchar *node, const gint value) { xmlNodePtr n; gchar *s; n = tree_get_node(node, NULL); s = g_strdup_printf("%d", value); xmlNodeSetContent(n, (const xmlChar*) s); g_free(s); tree_apply(); }
session *sess_get(int key) { rb_node *node; session *ret; node = tree_get_node(sess_head, key); if (node != NULL) { ret = (session *)(node->data); return ret; } return NULL; }
static unsigned int test_tree_value(unsigned int keys) { struct tree *t = tree_create (); unsigned int i; struct tree_node *n; for (i = 0; i < keys; i++) { tree_add_node_value (t, i, sentinelvalue(i)); } for (i = 0; i < keys; i++) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 7; } if (n->key != i) { tree_destroy(t); return 8; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 9; } } n = tree_get_node (t, keys + 1); if (n != (struct tree_node *)0) { tree_destroy(t); return 10; } /* we do this twice to stress the optimising algo once it's in */ for (i = 0; i < keys; i++) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 11; } if (n->key != i) { tree_destroy(t); return 12; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 13; } } n = tree_get_node (t, keys + 1); if (n != (struct tree_node *)0) { tree_destroy(t); return 14; } tree_destroy(t); return 0; }
static void record_local_sequence(struct uct *u, struct tree *t, struct board *endb, struct uct_descent *descent, int dlen, int di, enum stone seq_color) { #define LTREE_DEBUG if (UDEBUGL(6)) /* Ignore pass sequences. */ if (is_pass(node_coord(descent[di].node))) return; LTREE_DEBUG board_print(endb, stderr); LTREE_DEBUG fprintf(stderr, "recording local %s sequence: ", stone2str(seq_color)); /* Sequences starting deeper are less relevant in general. */ int pval = LTREE_PLAYOUTS_MULTIPLIER; if (u->local_tree && u->local_tree_depth_decay > 0) pval = ((floating_t) pval) / pow(u->local_tree_depth_decay, di - 1); if (!pval) { LTREE_DEBUG fprintf(stderr, "too deep @%d\n", di); return; } /* Pick the right local tree root... */ struct tree_node *lnode = seq_color == S_BLACK ? t->ltree_black : t->ltree_white; lnode->u.playouts++; double sval = 0.5; if (u->local_tree_rootgoal) { sval = local_value(u, endb, node_coord(descent[di].node), seq_color); LTREE_DEBUG fprintf(stderr, "(goal %s[%s %1.3f][%d]) ", coord2sstr(node_coord(descent[di].node), t->board), stone2str(seq_color), sval, descent[di].node->d); } /* ...and record the sequence. */ int di0 = di; while (di < dlen && (di == di0 || descent[di].node->d < u->tenuki_d)) { enum stone color = (di - di0) % 2 ? stone_other(seq_color) : seq_color; double rval; if (u->local_tree_rootgoal) rval = sval; else rval = local_value(u, endb, node_coord(descent[di].node), color); LTREE_DEBUG fprintf(stderr, "%s[%s %1.3f][%d] ", coord2sstr(node_coord(descent[di].node), t->board), stone2str(color), rval, descent[di].node->d); lnode = tree_get_node(t, lnode, node_coord(descent[di++].node), true); assert(lnode); stats_add_result(&lnode->u, rval, pval); } /* Add lnode for tenuki (pass) if we descended further. */ if (di < dlen) { double rval = u->local_tree_rootgoal ? sval : 0.5; LTREE_DEBUG fprintf(stderr, "pass "); lnode = tree_get_node(t, lnode, pass, true); assert(lnode); stats_add_result(&lnode->u, rval, pval); } LTREE_DEBUG fprintf(stderr, "\n"); }
const void *immutable (const void * data, unsigned long length) { const char *rv; const char *data_char = (const char *)data; struct tree_node *n; int_pointer hash = hash_murmur2_pt (&data, sizeof (const void *), 0); if (tree_get_node (&immutable_data_tree, hash) != (struct tree_node *)0) { return data; } hash = hash_murmur2_pt (data, length, 0); if ((n = tree_get_node (&immutable_hashes, hash)) != (struct tree_node *)0) { return (const void *)node_get_value (n); } if ((length + 1) > immutable_data_space_left) { unsigned long new_size = IMMUTABLE_CHUNKSIZE; lock_immutable_pages(); if (length > IMMUTABLE_CHUNKSIZE) { new_size = ((length / IMMUTABLE_CHUNKSIZE) + (((length % IMMUTABLE_CHUNKSIZE) != 0) ? 1 : 0)) * IMMUTABLE_CHUNKSIZE; } if (immutable_data != (void *)0) { lock_immutable_pages(); } immutable_data = get_mem(new_size); immutable_data_space_left = new_size; immutable_cursor = immutable_data; immutable_data_size = new_size; } for (rv = immutable_cursor; length != 0; immutable_cursor++, data_char++, length--, immutable_data_space_left--) { *immutable_cursor = *data_char; } *immutable_cursor = 0; /* write an extra 0 after whatever we just wrote */ immutable_cursor++; immutable_data_space_left--; while ((((unsigned long)immutable_cursor) % sizeof(void *)) != 0) { *immutable_cursor = 0; immutable_cursor++; immutable_data_space_left--; } tree_add_node_value (&immutable_hashes, hash, (void *)rv); hash = hash_murmur2_pt (&rv, sizeof (const void *), 0); tree_add_node (&immutable_data_tree, hash); return rv; }
static unsigned int test_tree_node_removal(unsigned int keys) { struct tree *t = tree_create (); unsigned int i; struct tree_node *n; for (i = 0; i < keys; i++) { tree_add_node_value (t, i, sentinelvalue(i)); } /* remove half the nodes */ for (i = 0; i < keys; i+=2) { tree_remove_node (t, i); } /* search for the nodes that should still be present */ for (i = 1; i < keys; i+=2) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 15; } if (n->key != i) { tree_destroy(t); return 16; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 17; } } /* search for the nodes that should be missing */ for (i = 0; i < keys; i+=2) { n = tree_get_node (t, i); if (n != (struct tree_node *)0) { tree_destroy(t); return 18; } } /* search for an arbitrary node that can't be present */ n = tree_get_node (t, keys + 1); if (n != (struct tree_node *)0) { tree_destroy(t); return 19; } /* we do the searches twice to stress the optimising algo once it's in */ /* search for the nodes that should still be present */ for (i = 1; i < keys; i+=2) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 20; } if (n->key != i) { tree_destroy(t); return 21; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 22; } } /* search for the nodes that should be missing */ for (i = 0; i < keys; i+=2) { n = tree_get_node (t, i); if (n != (struct tree_node *)0) { tree_destroy(t); return 23; } } /* remove the remaining nodes */ for (i = 1; i < keys; i+=2) { tree_remove_node (t, i); } if (t->root != (struct tree_node *)0) { tree_destroy(t); return 24; } tree_destroy(t); return 0; }
/* * Query a child node from the given binary tree. */ struct pdb_node_t* pdb_query_tree_node_cb(struct pdb_node_t* nptr, char* id) { struct binaryTree* tptr = nptr->data; return (struct pdb_node_t*)tree_get_node(tptr, id); }